Giter VIP home page Giter VIP logo

near2ts's Introduction

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

near2ts's People

Contributors

surgecode avatar

Stargazers

Mohamed ElKhamisy avatar Carina.Akaia.io avatar Elliot Braem avatar Arun  avatar Elijah Spina, PhD avatar Ruben Marcus avatar Vlad Frolov avatar

Watchers

 avatar

Forkers

elliotbraem

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.