๐ฆ 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:
- Rust :-) (https://doc.rust-lang.org/cargo/getting-started/installation.html)
- wasm-pack (on Apple's M1s you may need Rosetta
softwareupdate --install-rosetta
for wasm-pack to run) - Node.js version 16.5 or above
- yarn installed
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:
- Firstly JSON schemas are generated from Rust contract definitions using schemars.
- Then, JSON schemas are compiled to Typescript using json-schema-to-typescript.
- 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.
read
- reads contract state, check out the code in deploy/scripts/read-contract-state.js
npm run read:[network]
balance
- get balance for a wallet address, check out the code in deploy/scripts/interact-balance.js
npm run balance:[network]
transfer
- transfer specific amount of tokens to the indicated wallet, check out the code in deploy/scripts/interact-transfer.js