Local Development Guide
This guide will get you started deploying cw20 smart contracts on a local Injective network running on your computer.
We'll use the cw20-base contract from CosmWasm's collection of specifications and contracts designed for production use on real networks. cw20-base is a basic implementation of a cw20 compatible contract that can be imported in any custom contract you want to build on. It contains a straightforward but complete implementation of the cw20 spec along with all extensions. cw20-base can be deployed as-is or imported by other contracts.
Prerequisites
Install Go, Rust, and other Cosmwasm dependencies by following the instructions:
Before starting, make sure you have rustup along with recent versions of rustc and cargo installed. Currently, we are testing on Rust v1.58.1+.
You also need to have the wasm32-unknown-unknown target installed as well as the cargo-generate Rust crate.
You can check versions via the following commands:
rustc --version
cargo --version
rustup target list --installed
# if wasm32 is not listed above, run this
rustup target add wasm32-unknown-unknown
# to install cargo-generate, run this
cargo install cargo-generateinjectived
Make sure you have injectived installed locally. You can follow the Install injectivedguide to get injectived and other prerequisites running locally.
Once you have injectived installed, you should also start a local chain instance.
Compile CosmWasm Contracts
In this step, we will get all CW production template contracts and compile them using the CosmWasm Rust Optimizer Docker image for compiling multiple contracts (called workspace-optimizer)—see here (x86) or here (ARM) for latest versions. This process may take a bit of time and CPU power.
Non ARM (Non-Apple silicon) devices:
Alternatively for Apple Silicon devices (M1, M2, etc.) please use:
The docker script builds and optimizes all the CW contracts in the repo, with the compiled contracts located under the artifacts directory. Now we can deploy the cw20_base.wasm contract (cw20_base-aarch64.wasm if compiled on an ARM device).
Upload the CosmWasm Contract to the Chain
Output:
Then query the transaction by the txhash to verify the contract was indeed deployed.
Output:
Inspecting the output more closely, we can see the code_id of 1 for the contract
We’ve uploaded the contract code, but we still need to instantiate the contract.
Instantiate the Contract
Before instantiating the contract, let's take a look at the CW-20 contract function signature for instantiate.
Notably, it contains the InstantiateMsg parameter which contains the token name, symbol, decimals, and other details.
The first step of instantiating the contract is to select an address to supply with our initial CW20 token allocation. In our case, we can just use the genesis address since we have the keys already set up, but feel free to generate new addresses and keys.
Make sure you have the private keys for the address you choose—you won't be able to test token transfers from the address otherwise. In addition, the chosen address must be a valid address on the chain (the address must have received funds at some point in the past) and must have balances to pay for gas when executing the contract.
To find the genesis address, run:
Output:
Run the CLI command with code_id 1 along with the JSON encoded initialization arguments (with your selected address) and a label (a human-readable name for this contract in lists) to instantiate the contract:
Now the address of the instantiated contract can be obtained on http://localhost:10337/swagger/#/Query/ContractsByCode
And the contract info metadata can be obtained on http://localhost:10337/swagger/#/Query/ContractInfo or by CLI query
Output:
Querying the contract
The entire contract state can be queried with:
Output:
The individual user’s token balance can also be queried with:
Output:
Transferring Tokens
Then confirm the balance transfer occurred successfully with:
Output:
And confirm the recipient received the funds:
Output:
Testnet Development
Here are the main differences between a local and testnet development/deployment
You can use our Injective Testnet Faucet to get testnet funds to your address,
You can use the Injective Testnet Explorer to query your transactions and get more details,
When you are using
injectivedyou have to specify thetestnetrpc using thenodeflag--node=https://testnet.sentry.tm.injective.network:443Instead of using
injective-1as achainIdyou should useinjective-888i.e thechain-idflag should now be--chain-id="injective-888"You can use the Injective Testnet Explorer to find information about the
codeIdof the uploaded smart contracts OR find your instantiated smart contract
You can read more on the injectived and how to use it to query/send transactions against testnet Using injectived.
Last updated
