Skip to content

SurgeCode/near2ts

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

near2ts

Effortlessly create TypeScript types for your NEAR smart contracts.

Within the NEAR ecosystem, the ABI is depicted as a JSON schema outlining the contract's interface. Utilizing this library enables you to quickly generate TypeScript types by simply providing the contract name or the path to the ABI, streamlining your development process.

Note: This only works for contracts that have generated ABI's

Generate an ABI by following these instructions or read below

Example

Generate the types for the devhub.near contract and add them to your root

npx near2ts devhub.near

or

npx near2ts ./devhub.near_abi.json

Use the type ContractCallArgs to be able to accept arguments for any of the methods or reference the specific type for each call (in this case AddLike)

import { AddLike, ContractCallArgs } from "./contract_types";

interface AddLikeCall {
    contractId: 'devhub.near',
    methodName: 'add_like',
    args: AddLike
}

const contractCall: AddLikeCall = {
    contractId: 'devhub.near',
    methodName: 'add_like',
    args: {post_id: 20}
}

const receipt = await account.functionCall(contractCall)
interface DevHubContractCall {
    contractId: 'devhub.near',
    methodName: string,
    args: ContractCallArgs
}

const contractCall: DevHubContractCall = {
    contractId: 'devhub.near',
    methodName: 'add_member',
    args: {
        ....
    }
}

You can also install it as a dev dependency for you project


npm i near2ts --save-dev

Then add the script to your package.json and run it!

package.json

  "scripts": {
    "near2ts": "near2ts"
  },

npm run near2ts ./abi.json

Generating ABI

(taken from https://github.com/near/abi)

Rust

Ensure you have cargo-near installed

To generate an ABI for a contract within the directory containing contract's Cargo.toml

$ cargo near abi

Ensure that the project you are generating an ABI for depends on a version of near-sdk of 4.1.0 or later and has the abi feature enabled.

near-sdk = { version = "4.1.0", features = ["abi"] }

TypeScript

Ensure you have a near-sdk-js (version 0.7.0 or later) contract

To generate an ABI for a contract within the directory containing contract's package.json

$ npx near-sdk-js build --generateABI path/to/contract.ts

The ABI will be put in build/contract-abi.json

TODO

  • generate types in a way that are more compatible with near-api-js (whole function call object?)
  • dev dependency uses local version before node modules so if you have near cli js installed its using that one instead of rs, making it fail

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published