npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@horizonx/aptos-codegen

v0.14.0

Published

Generate interface for aptos modules from ABI

Downloads

5

Readme

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) | Description | Examples | | ------------------ | ------------------------------------------------------------------------ | ------------------------------------------------------ | | -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 | | -f | ABI file path pattern(s) (glob). (*1) | abi/**/*.json | | -a | Directory name alias(es) of an address for duplicate name modules. (*2) | 0x1=framework | | -t | Generation target(s) of code. entryFunctions/getters/utilities | entryFunctions, entryFunctions utilities | | --minify-abi | Minify ABI output to factory. | - | | -c | Read 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

| Function | Description | Arguments | | -------- | ---------------------------------- | -------------------------------------------- | | connect | creates the module client instance | signerOrClient: 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

| Function | Description | Arguments | | -------- | ---------------------------- | ------------------------ | | isXXX | a type guard of the resource | resource: MoveResource |

Common Utils

| Function | Description | Arguments | | --------------------- | --------------------------------------------------- | ------------------------- | | extractTypeParameters | extract type parameters string of the resource type | type: MoveResource.type | | typeToString | convert parsed type to string | type: ParsedType | | parseTypeStr | parse type str | type: 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>>"