diff --git a/src/commands/contract/interfaces.ts b/src/commands/contract/interfaces.ts index dede835..f01fe7c 100644 --- a/src/commands/contract/interfaces.ts +++ b/src/commands/contract/interfaces.ts @@ -46,30 +46,28 @@ export function generateActionInterface( types = abiVariant.types const variantTypeNodes = types.map((type) => - ts.factory.createTypeReferenceNode(parseType(findExternalType(type, 'Types.', abi))) + ts.factory.createTypeReferenceNode(parseType(findExternalType(type, 'Base.', abi))) ) - const variantInterface = ts.factory.createInterfaceDeclaration( - [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], - removeCommas(abiVariant.name), + const allTypeNodes = [ + ...variantTypeNodes, + ts.factory.createTypeReferenceNode(`Types.${abiVariant.name}`), + ] + + const variantTypeAlias = ts.factory.createTypeAliasDeclaration( undefined, + [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], + abiVariant.name, undefined, - [ - ts.factory.createPropertySignature( - undefined, - 'value', - undefined, - ts.factory.createUnionTypeNode(variantTypeNodes) - ), - ] + ts.factory.createUnionTypeNode(allTypeNodes) ) - typeInterfaces.push(variantInterface) + typeInterfaces.push(variantTypeAlias) } else { types = [field.type] } - const variantName = variantType && `Types.${variantType}` + const variantName = variantType && `Base.${variantType}` types.forEach((type) => { const typeStruct = findAbiStruct(type, abi) @@ -82,7 +80,7 @@ export function generateActionInterface( }) const typeReferenceNode = ts.factory.createTypeReferenceNode( - variantName || findParamTypeString(aliasType || field.type, 'Types.', abi) + variantName || findParamTypeString(aliasType || field.type, 'Base.', abi) ) return ts.factory.createPropertySignature( @@ -125,7 +123,7 @@ export function generateActionsNamespace(abi: ABI.Def): ts.ModuleDeclaration { const actionParamsTypes = ts.factory.createModuleDeclaration( [ts.factory.createModifier(ts.SyntaxKind.ExportKeyword)], - ts.factory.createIdentifier('Types'), + ts.factory.createIdentifier('Base'), ts.factory.createModuleBlock(removeDuplicateInterfaces(typeInterfaces)), ts.NodeFlags.Namespace ) diff --git a/test/data/contracts/mock-atomicassets.ts b/test/data/contracts/mock-atomicassets.ts index 0ad3aa0..fd5e655 100644 --- a/test/data/contracts/mock-atomicassets.ts +++ b/test/data/contracts/mock-atomicassets.ts @@ -96,17 +96,17 @@ export interface ActionNameParams { withdraw: ActionParams.withdraw } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface FORMAT { name: string type: string } export interface pair_string_ATOMIC_ATTRIBUTE { key: string - value: Types.variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC + value: Base.variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC } - export interface variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC { - value: + export type variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC = + | Int8Type | Int16Type | Int32Type @@ -129,7 +129,7 @@ export namespace ActionParams { | Float32Type[] | Float64Type[] | string[] - } + | Types.variant_int8_int16_int32_int64_uint8_uint16_uint32_uint64_float32_float64_string_INT8_VEC_INT16_VEC_INT32_VEC_INT64_VEC_UINT8_VEC_UINT16_VEC_UINT32_VEC_UINT64_VEC_FLOAT_VEC_DOUBLE_VEC_STRING_VEC } export interface acceptoffer { offer_id: UInt64Type @@ -147,7 +147,7 @@ export namespace ActionParams { account_to_add: NameType } export interface admincoledit { - collection_format_extension: Types.FORMAT[] + collection_format_extension: Base.FORMAT[] } export interface announcedepo { owner: NameType @@ -173,7 +173,7 @@ export namespace ActionParams { authorized_accounts: NameType[] notify_accounts: NameType[] market_fee: Float64Type - data: Types.pair_string_ATOMIC_ATTRIBUTE[] + data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface createoffer { sender: NameType @@ -186,7 +186,7 @@ export namespace ActionParams { authorized_creator: NameType collection_name: NameType schema_name: NameType - schema_format: Types.FORMAT[] + schema_format: Base.FORMAT[] } export interface createtempl { authorized_creator: NameType @@ -195,7 +195,7 @@ export namespace ActionParams { transferable: boolean burnable: boolean max_supply: UInt32Type - immutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + immutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface declineoffer { offer_id: UInt64Type @@ -204,7 +204,7 @@ export namespace ActionParams { authorized_editor: NameType collection_name: NameType schema_name: NameType - schema_format_extension: Types.FORMAT[] + schema_format_extension: Base.FORMAT[] } export interface forbidnotify { collection_name: NameType @@ -227,8 +227,8 @@ export namespace ActionParams { schema_name: NameType template_id: Int32Type backed_tokens: AssetType[] - old_immutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] - old_mutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + old_immutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] + old_mutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] asset_ram_payer: NameType } export interface logmint { @@ -238,10 +238,10 @@ export namespace ActionParams { schema_name: NameType template_id: Int32Type new_asset_owner: NameType - immutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] - mutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + immutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] + mutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] backed_tokens: AssetType[] - immutable_template_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + immutable_template_data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface lognewoffer { offer_id: UInt64Type @@ -259,13 +259,13 @@ export namespace ActionParams { transferable: boolean burnable: boolean max_supply: UInt32Type - immutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + immutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface logsetdata { asset_owner: NameType asset_id: UInt64Type - old_data: Types.pair_string_ATOMIC_ATTRIBUTE[] - new_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + old_data: Base.pair_string_ATOMIC_ATTRIBUTE[] + new_data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface logtransfer { collection_name: NameType @@ -280,8 +280,8 @@ export namespace ActionParams { schema_name: NameType template_id: Int32Type new_asset_owner: NameType - immutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] - mutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + immutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] + mutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] tokens_to_back: AssetType[] } export interface payofferram { @@ -300,11 +300,11 @@ export namespace ActionParams { authorized_editor: NameType asset_owner: NameType asset_id: UInt64Type - new_mutable_data: Types.pair_string_ATOMIC_ATTRIBUTE[] + new_mutable_data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface setcoldata { collection_name: NameType - data: Types.pair_string_ATOMIC_ATTRIBUTE[] + data: Base.pair_string_ATOMIC_ATTRIBUTE[] } export interface setmarketfee { collection_name: NameType diff --git a/test/data/contracts/mock-boid.ts b/test/data/contracts/mock-boid.ts index dd5ecfc..f451eb7 100644 --- a/test/data/contracts/mock-boid.ts +++ b/test/data/contracts/mock-boid.ts @@ -123,7 +123,7 @@ export interface ActionNameParams { withdraw: ActionParams.withdraw } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface AccountCreate { boid_id: NameType keys: PublicKeyType[] @@ -132,7 +132,7 @@ export namespace ActionParams { export interface Action { account: NameType name: NameType - authorization: Types.PermissionLevel[] + authorization: Base.PermissionLevel[] data: BytesType } export interface PermissionLevel { @@ -147,14 +147,14 @@ export namespace ActionParams { aggregate_pwr_capacity: UInt32Type } export interface Config { - account: Types.ConfigAccount - power: Types.ConfigPower - mint: Types.ConfigMint - team: Types.ConfigTeam - stake: Types.ConfigStake - time: Types.ConfigTime - auth: Types.ConfigAuth - nft: Types.ConfigNft + account: Base.ConfigAccount + power: Base.ConfigPower + mint: Base.ConfigMint + team: Base.ConfigTeam + stake: Base.ConfigStake + time: Base.ConfigTime + auth: Base.ConfigAuth + nft: Base.ConfigNft paused: boolean allow_deposits: boolean allow_withdrawals: boolean @@ -239,49 +239,48 @@ export namespace ActionParams { export interface OfferAction { delegated_stake: UInt16Type stake_locked_additional_rounds: UInt16Type - nft_actions: Types.NftAction[] + nft_actions: Base.NftAction[] balance_payment: UInt32Type } export interface NftAction { collection_name: NameType schema_name: NameType template_id: Int32Type - match_immutable_attributes: Types.AtomicAttribute[] - match_mutable_attributes: Types.AtomicAttribute[] + match_immutable_attributes: Base.AtomicAttribute[] + match_mutable_attributes: Base.AtomicAttribute[] burn: boolean lock_rounds: UInt16Type } export interface AtomicAttribute { key: string - value: Types.AtomicValue - } - export interface AtomicValue { - value: - | Int8Type - | Int16Type - | Int32Type - | Int64Type - | UInt8Type - | UInt16Type - | UInt32Type - | UInt64Type - | Float32Type - | Float64Type - | string - | Int8Type[] - | Int16Type[] - | Int32Type[] - | Int64Type[] - | BytesType - | UInt16Type[] - | UInt32Type[] - | UInt64Type[] - | Float32Type[] - | Float64Type[] - | string[] + value: Base.AtomicValue } + export type AtomicValue = + | Int8Type + | Int16Type + | Int32Type + | Int64Type + | UInt8Type + | UInt16Type + | UInt32Type + | UInt64Type + | Float32Type + | Float64Type + | string + | Int8Type[] + | Int16Type[] + | Int32Type[] + | Int64Type[] + | BytesType + | UInt16Type[] + | UInt32Type[] + | UInt64Type[] + | Float32Type[] + | Float64Type[] + | string[] + | Types.AtomicValue export interface OfferRewards { - nft_mints: Types.NftMint[] + nft_mints: Base.NftMint[] balance_deposit: UInt32Type delegated_stake: UInt16Type stake_locked_additional_rounds: UInt16Type @@ -291,8 +290,8 @@ export namespace ActionParams { mint_template_id: Int32Type mint_schema_name: NameType mint_collection_name: NameType - immutable_data: Types.AtomicAttribute[] - mutable_data: Types.AtomicAttribute[] + immutable_data: Base.AtomicAttribute[] + mutable_data: Base.AtomicAttribute[] quantity: UInt8Type } export interface OfferLimits { @@ -316,7 +315,7 @@ export namespace ActionParams { } export interface accountbuy { payer_boid_id: NameType - new_account: Types.AccountCreate + new_account: Base.AccountCreate } export interface accountedit { boid_id: NameType @@ -335,7 +334,7 @@ export namespace ActionParams { export interface accountsclr {} export interface auth { boid_id: NameType - actions: Types.Action[] + actions: Base.Action[] sig: SignatureType keyIndex: Int32Type expires_utc_sec: UInt32Type @@ -355,7 +354,7 @@ export namespace ActionParams { mod_id: UInt8Type } export interface boosternew { - mod: Types.Booster + mod: Base.Booster } export interface boosterrm { boid_id: NameType @@ -363,14 +362,14 @@ export namespace ActionParams { } export interface configclear {} export interface configset { - config: Types.Config + config: Base.Config } export interface globalchain { chain_name: NameType } export interface globalclear {} export interface globalset { - globalData: Types.Global + globalData: Base.Global } export interface internalxfer { from_boid_id: NameType @@ -391,7 +390,7 @@ export namespace ActionParams { sponsor_boid_id: NameType invite_code: UInt64Type sig: SignatureType - new_account: Types.AccountCreate + new_account: Base.AccountCreate } export interface inviterm { sponsor_boid_id: NameType @@ -407,8 +406,8 @@ export namespace ActionParams { } export interface logpwrclaim { boid_id: NameType - power: Types.PowerClaimLog - mint: Types.MintLog + power: Base.PowerClaimLog + mint: Base.MintLog } export interface metaclean {} export interface mint { @@ -435,10 +434,10 @@ export namespace ActionParams { asset_ids: UInt64Type[] } export interface offeradd { - requirements: Types.OfferRequirements - actions: Types.OfferAction - rewards: Types.OfferRewards - limits: Types.OfferLimits + requirements: Base.OfferRequirements + actions: Base.OfferAction + rewards: Base.OfferRewards + limits: Base.OfferLimits } export interface offerclaim { boid_id: NameType @@ -471,7 +470,7 @@ export namespace ActionParams { sponsor_boid_id: NameType } export interface sponsorset { - row: Types.Sponsor + row: Base.Sponsor } export interface stake { boid_id: NameType diff --git a/test/data/contracts/mock-eosio.msig.ts b/test/data/contracts/mock-eosio.msig.ts index ca95b5e..c3a64e4 100644 --- a/test/data/contracts/mock-eosio.msig.ts +++ b/test/data/contracts/mock-eosio.msig.ts @@ -47,20 +47,20 @@ export interface ActionNameParams { unapprove: ActionParams.unapprove } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface permission_level { actor: NameType permission: NameType } export interface transaction { - context_free_actions: Types.action[] - actions: Types.action[] - transaction_extensions: Types.extension[] + context_free_actions: Base.action[] + actions: Base.action[] + transaction_extensions: Base.extension[] } export interface action { account: NameType name: NameType - authorization: Types.permission_level[] + authorization: Base.permission_level[] data: BytesType } export interface extension { @@ -71,7 +71,7 @@ export namespace ActionParams { export interface approve { proposer: NameType proposal_name: NameType - level: Types.permission_level + level: Base.permission_level proposal_hash?: Checksum256Type } export interface cancel { @@ -90,13 +90,13 @@ export namespace ActionParams { export interface propose { proposer: NameType proposal_name: NameType - requested: Types.permission_level[] - trx: Types.transaction + requested: Base.permission_level[] + trx: Base.transaction } export interface unapprove { proposer: NameType proposal_name: NameType - level: Types.permission_level + level: Base.permission_level } } export namespace Types { diff --git a/test/data/contracts/mock-eosio.token.ts b/test/data/contracts/mock-eosio.token.ts index ca3562d..f231033 100644 --- a/test/data/contracts/mock-eosio.token.ts +++ b/test/data/contracts/mock-eosio.token.ts @@ -34,7 +34,7 @@ export interface ActionNameParams { transfer: ActionParams.transfer } export namespace ActionParams { - export namespace Types {} + export namespace Base {} export interface close { owner: NameType symbol: Asset.SymbolType diff --git a/test/data/contracts/mock-eosio.ts b/test/data/contracts/mock-eosio.ts index 9f482b6..76e539a 100644 --- a/test/data/contracts/mock-eosio.ts +++ b/test/data/contracts/mock-eosio.ts @@ -126,14 +126,14 @@ export interface ActionNameParams { withdraw: ActionParams.withdraw } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface permission_level { actor: NameType permission: NameType } export interface powerup_config { - net: Types.powerup_config_resource - cpu: Types.powerup_config_resource + net: Base.powerup_config_resource + cpu: Base.powerup_config_resource powerup_days: UInt32Type min_powerup_fee: AssetType } @@ -149,16 +149,16 @@ export namespace ActionParams { } export interface authority { threshold: UInt32Type - keys: Types.key_weight[] - accounts: Types.permission_level_weight[] - waits: Types.wait_weight[] + keys: Base.key_weight[] + accounts: Base.permission_level_weight[] + waits: Base.wait_weight[] } export interface key_weight { key: PublicKeyType weight: UInt16Type } export interface permission_level_weight { - permission: Types.permission_level + permission: Base.permission_level weight: UInt16Type } export interface wait_weight { @@ -173,22 +173,22 @@ export namespace ActionParams { transaction_mroot: Checksum256Type action_mroot: Checksum256Type schedule_version: UInt32Type - new_producers: Types.producer_schedule + new_producers: Base.producer_schedule } export interface producer_schedule { version: UInt32Type - producers: Types.producer_key[] + producers: Base.producer_key[] } export interface producer_key { producer_name: NameType block_signing_key: PublicKeyType } - export interface variant_block_signing_authority_v0 { - value: Types.block_signing_authority_v0 - } + export type variant_block_signing_authority_v0 = + | Base.block_signing_authority_v0 + | Types.variant_block_signing_authority_v0 export interface block_signing_authority_v0 { threshold: UInt32Type - keys: Types.key_weight[] + keys: Base.key_weight[] } export interface blockchain_parameters_v1 { max_action_return_value_size: UInt32Type @@ -221,11 +221,11 @@ export namespace ActionParams { amount: AssetType } export interface canceldelay { - canceling_auth: Types.permission_level + canceling_auth: Base.permission_level trx_id: Checksum256Type } export interface cfgpowerup { - args: Types.powerup_config + args: Base.powerup_config } export interface claimrewards { owner: NameType @@ -302,11 +302,11 @@ export namespace ActionParams { export interface newaccount { creator: NameType name: NameType - owner: Types.authority - active: Types.authority + owner: Base.authority + active: Base.authority } export interface onblock { - header: Types.block_header + header: Base.block_header } export interface onerror { sender_id: UInt128Type @@ -335,7 +335,7 @@ export namespace ActionParams { } export interface regproducer2 { producer: NameType - producer_authority: Types.variant_block_signing_authority_v0 + producer_authority: Base.variant_block_signing_authority_v0 url: string location: UInt16Type } @@ -406,7 +406,7 @@ export namespace ActionParams { votepay_factor: Int64Type } export interface setparams { - params: Types.blockchain_parameters_v1 + params: Base.blockchain_parameters_v1 } export interface setpriv { account: NameType @@ -446,7 +446,7 @@ export namespace ActionParams { account: NameType permission: NameType parent: NameType - auth: Types.authority + auth: Base.authority authorized_by?: NameType } export interface updaterex { diff --git a/test/data/contracts/mock-hegemon.hgm.ts b/test/data/contracts/mock-hegemon.hgm.ts index 1fe220b..6e76a2c 100644 --- a/test/data/contracts/mock-hegemon.hgm.ts +++ b/test/data/contracts/mock-hegemon.hgm.ts @@ -136,7 +136,7 @@ export interface ActionNameParams { upgradechar: ActionParams.upgradechar } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface pair_uint32_uint64 { first: UInt32Type second: UInt64Type @@ -180,12 +180,12 @@ export namespace ActionParams { reputation_level: UInt8Type experience_level: UInt32Type experience_points: UInt32Type - cooldowns: Types.cooldown[] + cooldowns: Base.cooldown[] active_project: UInt64Type faction: UInt64Type base_faction_voting_power: Float64Type max_inventory_size: UInt32Type - inventory: Types.pair_uint32_uint64 + inventory: Base.pair_uint32_uint64 currency: AssetType last_respawn: TimePointType location_tile_id: UInt64Type @@ -215,7 +215,7 @@ export namespace ActionParams { } export interface addinventory { player: NameType - ingredients: Types.pair_uint32_uint64 + ingredients: Base.pair_uint32_uint64 } export interface addmap { area_map: NameType @@ -307,7 +307,7 @@ export namespace ActionParams { export interface doaddmats { character_id: UInt64Type project_id: UInt64Type - materials: Types.pair_uint32_uint64 + materials: Base.pair_uint32_uint64 } export interface doaddproj { character_id: UInt64Type @@ -356,7 +356,7 @@ export namespace ActionParams { } export interface dodropoff { player: NameType - items: Types.pair_uint32_uint64 + items: Base.pair_uint32_uint64 } export interface doeat { character_id: UInt64Type @@ -376,7 +376,7 @@ export namespace ActionParams { export interface dogive { owner: NameType recipient: NameType - items: Types.pair_uint32_uint64 + items: Base.pair_uint32_uint64 } export interface doheal { character_id: UInt64Type @@ -400,7 +400,7 @@ export namespace ActionParams { export interface doload { player: NameType vehicle_id: UInt64Type - items: Types.pair_uint32_uint64 + items: Base.pair_uint32_uint64 } export interface domobilize { player: NameType @@ -421,7 +421,7 @@ export namespace ActionParams { } export interface dopickup { player: NameType - items: Types.pair_uint32_uint64 + items: Base.pair_uint32_uint64 } export interface dorepair { character_id: UInt64Type @@ -453,7 +453,7 @@ export namespace ActionParams { export interface dounload { player: NameType vehicle_id: UInt64Type - items: Types.pair_uint32_uint64 + items: Base.pair_uint32_uint64 } export interface dowork { character_id: UInt64Type @@ -502,11 +502,11 @@ export namespace ActionParams { payer: NameType } export interface setcharacter { - c: Types.character + c: Base.character } export interface setcrew { spaceship_id: UInt64Type - crew: Types.pair_uint64_CrewRole + crew: Base.pair_uint64_CrewRole } export interface setgm { player: NameType @@ -516,7 +516,7 @@ export namespace ActionParams { ship_operator: NameType } export interface setplayer { - p: Types.player + p: Base.player } export interface settilecw { tile_id: UInt64Type diff --git a/test/data/contracts/mock-rewards.gm.ts b/test/data/contracts/mock-rewards.gm.ts index 03d2788..bb5a128 100644 --- a/test/data/contracts/mock-rewards.gm.ts +++ b/test/data/contracts/mock-rewards.gm.ts @@ -41,7 +41,7 @@ export interface ActionNameParams { updateuser: ActionParams.updateuser } export namespace ActionParams { - export namespace Types { + export namespace Base { export interface oracle_pair { name: NameType precision: UInt16Type @@ -63,7 +63,7 @@ export namespace ActionParams { export interface configure { token_symbol: Asset.SymbolType oracle_account: NameType - oracle_pairs: Types.oracle_pair[] + oracle_pairs: Base.oracle_pair[] } export interface deluser { account: NameType @@ -71,7 +71,7 @@ export namespace ActionParams { export interface receipt { account: NameType amount: AssetType - ticker: Types.price_info[] + ticker: Base.price_info[] } export interface updateuser { account: NameType diff --git a/test/tests/contract-functionality.ts b/test/tests/contract-functionality.ts index 4f31c4d..9aceaea 100644 --- a/test/tests/contract-functionality.ts +++ b/test/tests/contract-functionality.ts @@ -3,6 +3,7 @@ import {makeClient} from '@wharfkit/mock-data' import {assert} from 'chai' import * as RewardsGm from '$test/data/contracts/mock-rewards.gm' +import * as Boid from '$test/data/contracts/mock-boid' import {PlaceholderName, PlaceholderPermission} from '@wharfkit/signing-request' import {Table, TableRowCursor} from '@wharfkit/contract' @@ -78,4 +79,87 @@ suite('functionality', function () { }) }) }) + test('boid-test', async function () { + const contract = new Boid.Contract({client}) + // Create a sample action and serialize it + // This is to simulate what would be returned from an API call + const nftmint: Boid.ActionParams.Base.NftMint = { + mint_template_id: 0, + mint_schema_name: 'wharf', + mint_collection_name: 'test', + immutable_data: [ + { + key: 'foo', + value: ['string', 'bar'], + }, + ], + mutable_data: [], + quantity: 1, + } + + const rewards: Boid.ActionParams.Base.OfferRewards = { + activate_powermod_ids: Bytes.fromString('FFFF', 'hex'), + balance_deposit: 0, + delegated_stake: 0, + nft_mints: [nftmint], + stake_locked_additional_rounds: 0, + } + + const requirements: Boid.ActionParams.Base.OfferRequirements = { + min_balance: 0, + min_cumulative_team_contribution: 0, + min_power: 0, + min_stake: 0, + team_id: Bytes.fromString('FFFF', 'hex'), + } + + const actions: Boid.ActionParams.Base.OfferAction = { + balance_payment: 0, + delegated_stake: 0, + nft_actions: [], + stake_locked_additional_rounds: 0, + } + + const limits: Boid.ActionParams.Base.OfferLimits = { + available_until_round: 0, + offer_quantity_remaining: 0, + } + + const data: Boid.ActionParams.offeradd = { + actions, + limits, + requirements, + rewards, + } + + console.log(JSON.stringify(data, null, 2)) + + const serializedAction = contract.action('offer.add', data) + console.log(JSON.stringify(serializedAction, null, 2)) + + // Now decode that action data using the ABI + // This is what the client would do with the response from the API + const retrievedActionData = Serializer.decode({ + data: serializedAction.data, + abi: contract.abi, + type: Boid.Types.offeradd, + }) + console.log(retrievedActionData.rewards.nft_mints[0].immutable_data[0]) + + // { key: 'foo', value: [ 'string', 'bar' ] } + + // If the developer wanted to perform that action again + // The following commented out approach should work, but does not + // const action = contract.action('createcol', retrievedActionData) + + // Instead the developer must recast the data + const recreatedAction = contract.action( + 'offer.add', + Serializer.objectify(retrievedActionData) + ) + console.log(JSON.stringify(recreatedAction, null, 2)) + + assert.isTrue(serializedAction.equals(recreatedAction)) + // const action2 = contract.action('createcol', test) + }) })