0.14.0 • Published 2 years ago

@horizonx/aptos-codegen v0.14.0

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

aptos-codegen

workflow workflow workflow workflow

import { CoinModuleFactory } from './__generated__/CoinModuleFactory'

const coin = CoinModuleFactory.connect(signerOrClient)
coin.transfer({
  type_arguments: ['0x1::aptos_coin::AptosCoin'],
  arguments: ['0x2', 99999999],
})

coin.getCoinInfo('0x1', '0x1::aptos_coin::AptosCoin')
// => {
//  type: '0x1::coin::CoinInfo<0x1::aptos_coin::AptosCoin>',
//  data: { ... }
// }

coin.getDepositEvents('0x1', { typeParameter: '0x1::aptos_coin::AptosCoin' })
// => [
//   { sequence_number: 1, data: { ... }, ... },
//   { sequence_number: 2, data: { ... }, ... },
// ]

Installation

yarn add -D @horizonx/aptos-codegen
yarn add @horizonx/aptos-module-client

or

npm install --save-dev @horizonx/aptos-codegen
npm install @horizonx/aptos-module-client

Usage

CLI

aptos-codegen -m {module(s)} -o {output-dir} -u {node-url}

or

aptos-codegen -c {configuration-file}

Options

Option(*Required)DescriptionExamples
-m*Module identifier(s).0x1::coin, 0x1::coin 0x1::account
-o*Output generated code to this directory.__generated__
-u*Aptos node URL.https://fullnode.devnet.aptoslabs.com/v1
-fABI file path pattern(s) (glob). (*1)abi/**/*.json
-aDirectory name alias(es) of an address for duplicate name modules. (*2)0x1=framework
-tGeneration target(s) of code. entryFunctions/getters/utilitiesentryFunctions, entryFunctions utilities
--minify-abiMinify ABI output to factory.-
-cRead options from this configuration file. (*3)./aptos-codegen.json (example)

*1: ABIs loaded from files are referenced in preference to those loaded from the chain.

*2: By default, files are output to address dir if a duplicate name module is included.

*3: Configuration can be overwritten by arguments.

Factory of Module Interface

FunctionDescriptionArguments
connectcreates the module client instancesignerOrClient: AptosClient or Signer(*1)

*1 Signer is a implementation of signAndSubmitTransaction: (payload, options) => Promise<string | { hash: string }>, \ such as aptos-wallet-connector or aptos-wallet-adapter.

import { CoinModuleFactory } from './__generated__/CoinModuleFactory'

const coin = CoinModuleFactory.connect(signerOrClient)

// You can overwrite address of module
const coin = CoinModuleFactory.connect(signerOrClient, "0xAnotherAddress")

Utils of Module

FunctionDescriptionArguments
isXXXa type guard of the resourceresource: MoveResource

Common Utils

FunctionDescriptionArguments
extractTypeParametersextract type parameters string of the resource typetype: MoveResource.type
typeToStringconvert parsed type to stringtype: ParsedType
parseTypeStrparse type strtype: MoveResource.type
import { Types } from 'aptos'
import { CoinUtils } from './__generated__/CoinUtils'
import { extractTypeParameters, parseTypeStr, typeToString } from 'src/libs/modules/__generated__/utils'

const coinUtils = new CoinUtils() // or new CoinUtils("0xAnotherAddress")

const resources: Types.MoveResource[] = [
  { type: "0x1::coin::CoinInfo<0x123456::coin::CoinA>", data: { symbol: "CoinA", ...} },
  { type: "0x1::coin::CoinInfo<0x123456::coin::CoinB>", data: { symbol: "CoinB", ...} },
  { type: "0x123456::type::SomeType", data: {...} },
  ...
]

if (coinUtils.isCoinInfo(resources[0])) {
  console.log(resources[0].data.symbol) // You can access as CoinInfo
}

resources.filter(coinUtils.isCoinInfo)
  .forEach((resource) => console.log(resource.data.symbol))  // You can access as CoinInfo

const coins = resources.filter(coinUtils.isCoinInfo).map(({ type }) => {
  const parsedTypes = extractTypeParameters(type)
  return parsedTypes.type
})
console.log(coins) // ["0x123456::coin::CoinA", "0x123456::coin::CoinB"]

const exampleStruct = { type: "0x1::exmaple::Example<0x123456::coin::CoinA, 0x1::coin::CoinInfo<0x123456::coin::CoinB>>", data: {...} }
const parsedType = parseTypeStr(coinA.type)
console.log(parsedType) 
  // { 
  //   type: "0x1::coin::Example",
  //   genericTypes: [
  //     { type: "0x123456::coin::CoinA" },
  //     { type: "0x1::coin::CoinInfo", genericTypes: [{ type: "0x123456::coin::CoinB" }] }
  //   ]
  // }
console.log(typeToString(parsedType)) // "0x1::exmaple::Example<0x123456::coin::CoinA, 0x1::coin::CoinInfo<0x123456::coin::CoinB>>"
0.10.0

2 years ago

0.11.0

2 years ago

0.9.0

2 years ago

0.12.0

2 years ago

0.8.0

2 years ago

0.13.0

2 years ago

0.9.2

2 years ago

0.14.0

2 years ago

0.9.1

2 years ago

0.7.0

2 years ago

0.6.1

2 years ago

0.6.0

2 years ago

0.5.2

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.1

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.1

2 years ago