Skip to main content

๐Ÿฆ€ Rust template

This repository contains an example SmartWeave contracts in Rust and building them into WASM binaries which can be then processed by Warp SDK.

It contains an example implementation of a PST contract - which you can use as a base for implementing your own contract. If you are not familiar with the concept of Profit Sharing Tokens, check out a tutorial for writing your first PST contract in our Warp Academy.

๐Ÿ“ฆ Installationโ€‹

You will need:

To install all Node.js dependencies run the following command:

yarn install

๐Ÿ‘ท Buildโ€‹

Compile your contract to WASM binary by running following command:

yarn build

Typescript bindingsโ€‹

Rust contract definitions can be compiled to Typescript:

  1. Firstly JSON schemas are generated from Rust contract definitions using schemars.
  2. Then, JSON schemas are compiled to Typescript using json-schema-to-typescript.
  3. Lastly, a helper class is generated from typescript bindings which allows to easily interact with the contract. Instead of using writeInteraction method each time, specific functions can be called within the contract, e.g.:
  async transfer(transfer: Transfer, options?: WriteInteractionOptions): Promise<WriteInteractionResponse | null> {
return await this.contract.writeInteraction<BaseInput & Transfer>({ function: 'transfer', ...transfer }, options);
}

Generate JSON:

yarn gen-json

Compile JSON to Typescript:

yarn gen-ts

Gnerate JSON and compile to Typescript:

yarn gen-bindings

Files will be generated in contract/definition/bindings.

๐Ÿงช Testsโ€‹

Write tests for your contract (we will use Jest library for testing) - you can find a template in the tests/ folder. Run tests with

yarn test

๐Ÿ“œ Deployโ€‹

Deploy your contract to one of the networks (mainnet/Warp public testnet/localhost) by running following command (network: mainnet | testnet | local)

Please note that in case of local deployment, you need to have ArLocal instance running - npx arlocal.

yarn deploy:[network]

๐Ÿ’กNOTE: If you want to deploy your contract locally you need to run Arlocal by typing following command:

npx arlocal

๐Ÿ’กNOTE: When using mainnet please put your wallet key in deploy/mainnet/.secrets/wallet-mainnet.json. .secrets folder has been added to .gitignore so your key is kept securely.

You can view deploy script code here.

๐ŸŸฅ Using SDKโ€‹

Optionally - you can run one of the scripts which uses Warp SDK to interact with the contract. Using SDKs' methods works exactly the same as in case of a regular JS contract.

๐Ÿ’กNOTE You will need to have a file with the wallet key and a file with the contract id to run these scripts. If you do not have them please run a deploy script.

  1. read - reads contract state, check out the code in deploy/scripts/read-contract-state.js
    npm run read:[network]
  1. balance - get balance for a wallet address, check out the code in deploy/scripts/interact-balance.js
    npm run balance:[network]
  1. transfer - transfer specific amount of tokens to the indicated wallet, check out the code in deploy/scripts/interact-transfer.js