diff --git a/Makefile b/Makefile index 5af30df..0241259 100644 --- a/Makefile +++ b/Makefile @@ -26,6 +26,7 @@ test_generate: node_modules clean lib update_mock_contracts: node_modules clean lib node lib/cli.js generate atomicassets -u https://jungle4.greymass.com -j test/data/abis/atomicassets.json -f test/data/contracts/mock-atomicassets.ts -e .eslintrc node lib/cli.js generate boid -u https://jungle4.greymass.com -j test/data/abis/boid.json -f test/data/contracts/mock-boid.ts -e .eslintrc + node lib/cli.js generate payroll.boid -u https://telos.api.animus.is -j test/data/abis/payroll.boid.json -f test/data/contracts/mock-payroll.boid.ts -e .eslintrc node lib/cli.js generate eosio -u https://jungle4.greymass.com -j test/data/abis/eosio.json -f test/data/contracts/mock-eosio.ts -e .eslintrc node lib/cli.js generate eosio.msig -u https://jungle4.greymass.com -j test/data/abis/eosio.msig.json -f test/data/contracts/mock-eosio.msig.ts -e .eslintrc node lib/cli.js generate eosio.token -u https://jungle4.greymass.com -j test/data/abis/eosio.token.json -f test/data/contracts/mock-eosio.token.ts -e .eslintrc diff --git a/src/commands/contract/helpers.ts b/src/commands/contract/helpers.ts index f2fd9f5..7e02a6d 100644 --- a/src/commands/contract/helpers.ts +++ b/src/commands/contract/helpers.ts @@ -116,7 +116,7 @@ function structIsUsedInActionParams(struct: ABI.Struct, abi: ABI.Def) { } export function findCoreClassImport(type: string) { - if (type === 'symbol') { + if (type === 'symbol' || type === 'symbol_code') { return 'Asset' } diff --git a/src/commands/contract/interfaces.ts b/src/commands/contract/interfaces.ts index 84b3728..64bfda2 100644 --- a/src/commands/contract/interfaces.ts +++ b/src/commands/contract/interfaces.ts @@ -139,6 +139,14 @@ export function generateActionsNamespace(abi: ABI.Def): ts.ModuleDeclaration { function findParamTypeString(typeString: string, namespace = '', abi: ABI.Def): string { const fieldType = findExternalType(typeString, namespace, abi) + if (fieldType === 'Symbol_code') { + return 'Asset.SymbolCodeType' + } + + if (fieldType === 'Symbol_code[]') { + return 'Asset.SymbolCodeType[]' + } + if (fieldType === 'Symbol') { return 'Asset.SymbolType' } diff --git a/src/commands/contract/structs.ts b/src/commands/contract/structs.ts index d009c0c..f3d2540 100644 --- a/src/commands/contract/structs.ts +++ b/src/commands/contract/structs.ts @@ -363,6 +363,10 @@ export function findFieldTypeString( return 'Asset.Symbol' } + if (fieldType === 'Symbol_code') { + return 'Asset.SymbolCode' + } + return parseType(fieldType) } diff --git a/test/data/abis/payroll.boid.json b/test/data/abis/payroll.boid.json new file mode 100644 index 0000000..d341ec6 --- /dev/null +++ b/test/data/abis/payroll.boid.json @@ -0,0 +1,245 @@ +{ + "version": "eosio::abi/1.2", + "types": [], + "structs": [ + { + "name": "Payroll", + "base": "", + "fields": [ + { + "name": "id", + "type": "uint64" + }, + { + "name": "total", + "type": "asset" + }, + { + "name": "paid", + "type": "asset" + }, + { + "name": "startTime", + "type": "time_point_sec" + }, + { + "name": "finishTime", + "type": "time_point_sec" + }, + { + "name": "lastPayout", + "type": "time_point_sec" + }, + { + "name": "minClaimFrequencySec", + "type": "uint32" + }, + { + "name": "receiverAccount", + "type": "name" + }, + { + "name": "treasuryAccount", + "type": "name" + }, + { + "name": "paused", + "type": "bool" + }, + { + "name": "meta", + "type": "bytes" + } + ] + }, + { + "name": "PayrollConfig", + "base": "", + "fields": [ + { + "name": "total", + "type": "asset" + }, + { + "name": "startTime", + "type": "time_point_sec" + }, + { + "name": "finishTime", + "type": "time_point_sec" + }, + { + "name": "minClaimFrequencySec", + "type": "uint32" + }, + { + "name": "receiverAccount", + "type": "name" + }, + { + "name": "treasuryAccount", + "type": "name" + }, + { + "name": "meta", + "type": "bytes" + }, + { + "name": "paused", + "type": "bool" + } + ] + }, + { + "name": "TokensWhitelist", + "base": "", + "fields": [ + { + "name": "sym", + "type": "symbol" + }, + { + "name": "contract", + "type": "name" + } + ] + }, + { + "name": "payroll.add", + "base": "", + "fields": [ + { + "name": "payrollConfig", + "type": "PayrollConfig" + } + ] + }, + { + "name": "payroll.edit", + "base": "", + "fields": [ + { + "name": "payrollId", + "type": "uint64" + }, + { + "name": "pause", + "type": "bool" + }, + { + "name": "minClaimFrequencySec", + "type": "uint32" + }, + { + "name": "receiverAccount", + "type": "name" + }, + { + "name": "treasuryAccount", + "type": "name" + }, + { + "name": "meta", + "type": "bytes" + } + ] + }, + { + "name": "payroll.pay", + "base": "", + "fields": [ + { + "name": "payrollId", + "type": "uint64" + } + ] + }, + { + "name": "payroll.rm", + "base": "", + "fields": [ + { + "name": "payrollId", + "type": "uint64" + } + ] + }, + { + "name": "tokenwl.add", + "base": "", + "fields": [ + { + "name": "sym", + "type": "symbol" + }, + { + "name": "contract", + "type": "name" + } + ] + }, + { + "name": "tokenwl.rm", + "base": "", + "fields": [ + { + "name": "sym", + "type": "symbol_code" + } + ] + } + ], + "actions": [ + { + "name": "payroll.add", + "type": "payroll.add", + "ricardian_contract": "" + }, + { + "name": "payroll.edit", + "type": "payroll.edit", + "ricardian_contract": "" + }, + { + "name": "payroll.pay", + "type": "payroll.pay", + "ricardian_contract": "" + }, + { + "name": "payroll.rm", + "type": "payroll.rm", + "ricardian_contract": "" + }, + { + "name": "tokenwl.add", + "type": "tokenwl.add", + "ricardian_contract": "" + }, + { + "name": "tokenwl.rm", + "type": "tokenwl.rm", + "ricardian_contract": "" + } + ], + "tables": [ + { + "name": "payrolls", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "Payroll" + }, + { + "name": "tokenwl", + "index_type": "i64", + "key_names": [], + "key_types": [], + "type": "TokensWhitelist" + } + ], + "ricardian_clauses": [], + "error_messages": [], + "abi_extensions": [], + "variants": [], + "action_results": [] +} diff --git a/test/data/contracts/mock-payroll.boid.ts b/test/data/contracts/mock-payroll.boid.ts new file mode 100644 index 0000000..8dc92a3 --- /dev/null +++ b/test/data/contracts/mock-payroll.boid.ts @@ -0,0 +1,196 @@ +import type { + Action, + AssetType, + BytesType, + NameType, + UInt32Type, + UInt64Type, +} from '@wharfkit/antelope' +import { + ABI, + Asset, + Blob, + Bytes, + Name, + Struct, + TimePointSec, + UInt32, + UInt64, +} from '@wharfkit/antelope' +import type {ActionOptions, ContractArgs, PartialBy, Table} from '@wharfkit/contract' +import {Contract as BaseContract} from '@wharfkit/contract' +export const abiBlob = Blob.from( + 'DmVvc2lvOjphYmkvMS4yAAkHUGF5cm9sbAALAmlkBnVpbnQ2NAV0b3RhbAVhc3NldARwYWlkBWFzc2V0CXN0YXJ0VGltZQ50aW1lX3BvaW50X3NlYwpmaW5pc2hUaW1lDnRpbWVfcG9pbnRfc2VjCmxhc3RQYXlvdXQOdGltZV9wb2ludF9zZWMUbWluQ2xhaW1GcmVxdWVuY3lTZWMGdWludDMyD3JlY2VpdmVyQWNjb3VudARuYW1lD3RyZWFzdXJ5QWNjb3VudARuYW1lBnBhdXNlZARib29sBG1ldGEFYnl0ZXMNUGF5cm9sbENvbmZpZwAIBXRvdGFsBWFzc2V0CXN0YXJ0VGltZQ50aW1lX3BvaW50X3NlYwpmaW5pc2hUaW1lDnRpbWVfcG9pbnRfc2VjFG1pbkNsYWltRnJlcXVlbmN5U2VjBnVpbnQzMg9yZWNlaXZlckFjY291bnQEbmFtZQ90cmVhc3VyeUFjY291bnQEbmFtZQRtZXRhBWJ5dGVzBnBhdXNlZARib29sD1Rva2Vuc1doaXRlbGlzdAACA3N5bQZzeW1ib2wIY29udHJhY3QEbmFtZQtwYXlyb2xsLmFkZAABDXBheXJvbGxDb25maWcNUGF5cm9sbENvbmZpZwxwYXlyb2xsLmVkaXQABglwYXlyb2xsSWQGdWludDY0BXBhdXNlBGJvb2wUbWluQ2xhaW1GcmVxdWVuY3lTZWMGdWludDMyD3JlY2VpdmVyQWNjb3VudARuYW1lD3RyZWFzdXJ5QWNjb3VudARuYW1lBG1ldGEFYnl0ZXMLcGF5cm9sbC5wYXkAAQlwYXlyb2xsSWQGdWludDY0CnBheXJvbGwucm0AAQlwYXlyb2xsSWQGdWludDY0C3Rva2Vud2wuYWRkAAIDc3ltBnN5bWJvbAhjb250cmFjdARuYW1lCnRva2Vud2wucm0AAQNzeW0Lc3ltYm9sX2NvZGUGAFIyIEZ6vakLcGF5cm9sbC5hZGQAkF1SIEZ6vakMcGF5cm9sbC5lZGl0AAC8qSBGer2pC3BheXJvbGwucGF5AACAvCBGer2pCnBheXJvbGwucm0AAFIyIPKpIM0LdG9rZW53bC5hZGQAAIC8IPKpIM0KdG9rZW53bC5ybQACAAAAOEZ6vakDaTY0AAAHUGF5cm9sbAAAACDyqSDNA2k2NAAAD1Rva2Vuc1doaXRlbGlzdAAAAAAA' +) +export const abi = ABI.from(abiBlob) +export namespace Types { + @Struct.type('Payroll') + export class Payroll extends Struct { + @Struct.field(UInt64) + id!: UInt64 + @Struct.field(Asset) + total!: Asset + @Struct.field(Asset) + paid!: Asset + @Struct.field(TimePointSec) + startTime!: TimePointSec + @Struct.field(TimePointSec) + finishTime!: TimePointSec + @Struct.field(TimePointSec) + lastPayout!: TimePointSec + @Struct.field(UInt32) + minClaimFrequencySec!: UInt32 + @Struct.field(Name) + receiverAccount!: Name + @Struct.field(Name) + treasuryAccount!: Name + @Struct.field('bool') + paused!: boolean + @Struct.field(Bytes) + meta!: Bytes + } + @Struct.type('PayrollConfig') + export class PayrollConfig extends Struct { + @Struct.field(Asset) + total!: Asset + @Struct.field(TimePointSec) + startTime!: TimePointSec + @Struct.field(TimePointSec) + finishTime!: TimePointSec + @Struct.field(UInt32) + minClaimFrequencySec!: UInt32 + @Struct.field(Name) + receiverAccount!: Name + @Struct.field(Name) + treasuryAccount!: Name + @Struct.field(Bytes) + meta!: Bytes + @Struct.field('bool') + paused!: boolean + } + @Struct.type('TokensWhitelist') + export class TokensWhitelist extends Struct { + @Struct.field(Asset.Symbol) + sym!: Asset.Symbol + @Struct.field(Name) + contract!: Name + } + @Struct.type('payroll.add') + export class payrolladd extends Struct { + @Struct.field(PayrollConfig) + payrollConfig!: PayrollConfig + } + @Struct.type('payroll.edit') + export class payrolledit extends Struct { + @Struct.field(UInt64) + payrollId!: UInt64 + @Struct.field('bool') + pause!: boolean + @Struct.field(UInt32) + minClaimFrequencySec!: UInt32 + @Struct.field(Name) + receiverAccount!: Name + @Struct.field(Name) + treasuryAccount!: Name + @Struct.field(Bytes) + meta!: Bytes + } + @Struct.type('payroll.pay') + export class payrollpay extends Struct { + @Struct.field(UInt64) + payrollId!: UInt64 + } + @Struct.type('payroll.rm') + export class payrollrm extends Struct { + @Struct.field(UInt64) + payrollId!: UInt64 + } + @Struct.type('tokenwl.add') + export class tokenwladd extends Struct { + @Struct.field(Asset.Symbol) + sym!: Asset.Symbol + @Struct.field(Name) + contract!: Name + } + @Struct.type('tokenwl.rm') + export class tokenwlrm extends Struct { + @Struct.field(Asset.SymbolCode) + sym!: Asset.SymbolCode + } +} +export const TableMap = { + payrolls: Types.Payroll, + tokenwl: Types.TokensWhitelist, +} +export interface TableTypes { + payrolls: Types.Payroll + tokenwl: Types.TokensWhitelist +} +export type RowType = T extends keyof TableTypes ? TableTypes[T] : any +export type TableNames = keyof TableTypes +export namespace ActionParams { + export namespace Type { + export interface PayrollConfig { + total: AssetType + startTime: TimePointSec + finishTime: TimePointSec + minClaimFrequencySec: UInt32Type + receiverAccount: NameType + treasuryAccount: NameType + meta: BytesType + paused: boolean + } + } + export interface payrolladd { + payrollConfig: Type.PayrollConfig + } + export interface payrolledit { + payrollId: UInt64Type + pause: boolean + minClaimFrequencySec: UInt32Type + receiverAccount: NameType + treasuryAccount: NameType + meta: BytesType + } + export interface payrollpay { + payrollId: UInt64Type + } + export interface payrollrm { + payrollId: UInt64Type + } + export interface tokenwladd { + sym: Asset.SymbolType + contract: NameType + } + export interface tokenwlrm { + sym: Asset.SymbolCodeType + } +} +export interface ActionNameParams { + 'payroll.add': ActionParams.payrolladd + 'payroll.edit': ActionParams.payrolledit + 'payroll.pay': ActionParams.payrollpay + 'payroll.rm': ActionParams.payrollrm + 'tokenwl.add': ActionParams.tokenwladd + 'tokenwl.rm': ActionParams.tokenwlrm +} +export type ActionNames = keyof ActionNameParams +export class Contract extends BaseContract { + constructor(args: PartialBy) { + super({ + client: args.client, + abi: abi, + account: args.account || Name.from('payroll.boid'), + }) + } + action( + name: T, + data: ActionNameParams[T], + options?: ActionOptions + ): Action { + return super.action(name, data, options) + } + table(name: T, scope?: NameType): Table> { + return super.table(name, scope, TableMap[name]) + } +} diff --git a/test/tests/contract-codegen.ts b/test/tests/contract-codegen.ts index 8febee8..ac0e23e 100644 --- a/test/tests/contract-codegen.ts +++ b/test/tests/contract-codegen.ts @@ -13,6 +13,7 @@ import * as RewardsGm from '$test/data/contracts/mock-rewards.gm' import * as AtomicAssets from '$test/data/contracts/mock-atomicassets' import * as Hegemon from '$test/data/contracts/mock-hegemon.hgm' import * as Boid from '$test/data/contracts/mock-boid' +import * as PayrollBoid from '$test/data/contracts/mock-payroll.boid' import * as GreymassTesting from '$test/data/contracts/mock-testing.gm' import {generateCodegenContract, removeCodegenContracts} from '$test/utils/codegen' @@ -56,6 +57,10 @@ suite('codegen', async function () { mock: Boid, generated: null, }, + 'payroll.boid': { + mock: PayrollBoid, + generated: null, + }, 'testing.gm': { mock: GreymassTesting, generated: null, diff --git a/test/tests/helpers/generic.ts b/test/tests/helpers/generic.ts index 9d0d6d6..08fc92a 100644 --- a/test/tests/helpers/generic.ts +++ b/test/tests/helpers/generic.ts @@ -1,4 +1,4 @@ -import {Action, Name, UInt64} from '@wharfkit/antelope' +import {Action, Bytes, Name, TimePointSec, UInt64} from '@wharfkit/antelope' import {assert} from 'chai' import type {ActionDataType, Contract} from '@wharfkit/contract' @@ -56,6 +56,20 @@ export function getMockParams(contract: Contract): ActionDataType { keys: ['PUB_K1_6RrvujLQN1x5Tacbep1KAk8zzKpSThAQXBCKYFfGUYeACcSRFs'], } } + case 'payroll.boid': { + return { + payrollConfig: { + minClaimFrequencySec: 1, + receiverAccount: 'foo', + treasuryAccount: 'bar', + meta: Bytes.from('meta', 'utf8').toString('hex'), + paused: false, + startTime: new Date('2021-01-01T00:00:00Z'), + finishTime: new Date('2021-01-01T00:00:00Z'), + total: '1.0000 BOID', + }, + } + } case 'testing.gm': { return { oracle: 'foo',