Skip to content

A working example of generating Typescript types process from Near Protocol smart contract written on Rust

License

Notifications You must be signed in to change notification settings

PiVortex/near-rust-contract-types-generator

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation


Near Protocol | Contract Types Generator

A working example of generating Typescript types from Near Protocol smart contract written in Rust

Explore other examples · Report Bug

Developed by

Near Ukrainians Guild cover

Near Ukrainians Guild is a fast-growing guild aimed at providing high-quality educational content and assistance to grow a strong community of Ukrainian developers/entrepreneurs in the Near Protocol ecosystem

Twitter Youtube Telegram Chat Telegram Channel Medium Github


About The Project

This example demonstrates the process of generating TypeScript typed files based on Near Protocol smart contract

  • main branch contains plain contract with an example of generator usage

  • only_generator branch contains script for generating types & example of output in /type_generator/example/typescript

Pros

  • Contract public functions are automatically generated
  • Output type file contains Contract class with all functions

Cons

  • Custom struct and enum must be decorated with proc macro #[witgen]
  • Type &str is not supported (but you can use String)

Built With


Getting Started

💡 Before you begin, make sure you have the following installed

Installation

Follow these simple instructions to setup the required dependencies before usage

  1. Clone the repo
    git clone https://github.com/nearuaguild/near-rust-contract-types-generator.git
  2. Checkout to branch only_generator
    git switch only_generator
  3. Copy the folder to your project
    cp /type_generator/ /PATH_TO_YOUR_PROJECT_ROOT/
  4. Add witgen lib to your dependencies in Cargo.toml
    [dependencies]
    witgen = "0.15.0"
  5. Install witme package
    cargo install witme --version 0.2.6 --force

Usage

Follow these simple instructions to generate Typescript interface files

  1. Import witgen crate to lib.rs

    use witgen::witgen;
  2. Decorate with #[witgen] macro your contract types (example in /src/lib.rs)

    #[witgen]
    #[derive(BorshSerialize, BorshDeserialize, Clone)]
    pub enum Kind {
       OK { code: u8 },
       FAIL { code: u8, text: String },
    }
    #[derive(BorshSerialize, BorshDeserialize, PanicOnDefault, Clone)]
    #[witgen]
    pub struct Config {
       number: u16,
       kind: Kind,
    }
  3. Execute script from the root smart contract folder

    ./type_generator/generate.sh
  4. Copy generated types to your TypeScript project

    cp ./type_generator/typescript/ /PATH_TO_TYPESCRIPT_PROJECT/

Creating an ABI

To create an ABI for a rust smart contract, follow these instructions:

  1. Build the contract. In this example, create a new folder named /res, compile the smart contract into a wasm file by running

    ./build.sh
  2. Deploy the contract

    near dev-deploy res/contract.wasm
  3. Create the ABI

    cargo near abi

This will have created an ABI in the location target/near/near_rust_contract_types_generator_abi.json If you want to use this ABI to interact with the contract quickly, you can use Pagoda. To do this, link the account that you have deployed the contract to then attach the ABI

License

Distributed under the MIT License. See LICENSE.txt for more information.

About

A working example of generating Typescript types process from Near Protocol smart contract written on Rust

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 69.8%
  • Rust 18.1%
  • Shell 12.1%