Build a CW20 token factory
In this tutorial, you'll build a CW20 token factory. A token factory allows any Cosmos SDK address (including a contract) to mint a new fungible token.
Prerequisites
Use the following guides to set up your environment:
You'll also need:
- An IDE or text editor of your choice. This tutorial will use Visual Studio Code.
- A command line interface.
Instantiate a new app using Terrain
- Instantiate a new app using Terrain.
- When the app is generated, the following displays.
- Instantiate a new app using Terrain.
- When the app is generated, the following displays.
Generate the cw20_factory_token contract
- Navigate to the
token_factorydirectory.
- Create the
cw20_factory_tokencontract.
- When the contract is generated, the following displays:
- Navigate to the
token_factorydirectory.
- Create the
cw20_factory_tokencontract.
- When the contract is generated, the following displays:
Modify the mnemonic passphrase
Before editing the smart contracts you created in step 2, modify the mnemonic you'll use to deploy the contract to LocalTerra:
- Open
/keys.terrain.jsand set themnemonicto the following.
_1notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius
Because the wallet contains funds, it is recommended that you also import the passphrase listed below into the Station Extension. You can view other example mnemonics on Github:
_1notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius
The module.exports section of your keys.terrain.js file should now look similar to the following.
_6module.exports = {_6 test: {_6 mnemonic:_6 'notice oak worry limit wrap speak medal online prefer cluster roof addict wrist behave treat actual wasp year salad speed social layer crew genius',_6 },_6};
Deploy the smart contracts
The token_factory contract mints new cw20_factory_token tokens, increases the supply of minted tokens, burns tokens to return LUNA, and tracks all tokens created.
Deploy each smart contract to ensure that the development environment is configured correctly:
- Deploy the
token_factorycontract.
- Deploy the
cw20_factory_tokencontract.
- Deploy the
token_factorycontract.
- Deploy the
cw20_factory_tokencontract.
Modify the CW20 Factory Token smart contract
In this section, you will modify the cw20_factory_token contract that you instantiated. This contract implements the CW20 Base, along with several custom files.
To modify the cw20_factory_token contract, follow the procedure below.
1. Add the the CW20 base
First, add the CW20 Base, which implements the base CW20 token functionalities. This allows for:
- the smart contract to be easily deployed to LocalTerra.
- extended functionality using the migration implementation.
To add the CW20 Base to the cw20_factory_token contract, do the following.
- Navigate to the
/cw20_factory_token/directory.
- Open
cargo.tomland add this to the dependencies:
- Navigate to the
/cw20_factory_token/directory.
- Open
cargo.tomland add this to the dependencies:
2. Modify the contract files
Now that you've added the CW20 Base to implement the base CW20 token logic, modify the following files:
msg.rslib.rscontract.rsschemas.rs
To modify the contract files, follow the procedure below.
msg.rs
- Navigate to the
/cw20_factory_token/srcdirectory.
- Open
msg.rsin your code editor and paste the following.
- Save and close
msg.rs.
- Navigate to the
/cw20_factory_token/srcdirectory.
- Open
msg.rsin your code editor and paste the following.
- Save and close
msg.rs.
lib.rs
- Open
lib.rsand paste the following.
- Save and close
lib.rs.
- Open
lib.rsand paste the following.
- Save and close
lib.rs.
contract.rs
- Open
contract.rsand paste the following.
- Save and close
contract.rs.
- Open
contract.rsand paste the following.
- Save and close
contract.rs.
schema.rs
- Open
schemas.rsand paste the following.
- Save and close
schemas.rs.
- Open
schemas.rsand paste the following.
- Save and close
schemas.rs.
3. Generate and test the schema
- Navigate to
/token_factory/contracts/cw20_factory_token.
- Generate the new schema.
- Test the schema.
- Navigate to
/token_factory/contracts/cw20_factory_token.
- Generate the new schema.
- Test the schema.
4. Modify config.terrain.json
-
Open
config.terrain.json. -
Modify the
InstantiateMsgproperty in theconfig.terrain.jsonso that it contains thename,symbol,decimals, andinitial_balancesshown in the example. This allows you to send the correct data to the smart contract upon instantiation.
-
Open
config.terrain.json. -
Modify the
InstantiateMsgproperty in theconfig.terrain.jsonso that it contains thename,symbol,decimals, andinitial_balancesshown in the example. This allows you to send the correct data to the smart contract upon instantiation.
5. Test the smart contract deployment
Deploy the contract again to confirm that the workplace still compiles.
Deploy the contract again to confirm that the workplace still compiles.
If your code is not working as expected, you can clone the repo with all the changes described above so that you can continue with the tutorial. To clone the repo, do the following.
_3git clone -n https://github.com/emidev98/token-factory_3cd token-factory_3git checkout 8da7892486704c54e33442b156d63178f5137527
6. Use crate.io to implement the CW20 Token Factory as a dependency
For the purpose of this tutorial, crates.io is used to implement the CW20 Token Factory as a dependency. This ensures that the CW20 Token Factory is platform agnostic, so you can use Linux, Windows or Mac.
As the deployment to crates.io is out of scope for this tutorial, you can find the CW20 Token Factory package deployed to crates.io. You can use this deployment when you add the CW20 Token Factory contract as a dependency of the Token Factory contract in the the next section.
Create the Token Factory smart contract
To set up the contract, follow the procedure below:
1. Add the dependencies
In this section, you will add the following dependencies to cargo.toml:
cw2cw20cw20-basecw20_factory_token
To add the dependencies, do the following.
-
Navigate to
/token_factory/contracts/token_factory. -
Open
cargo.tomland add the dependencies inside the header.
-
Navigate to
/token_factory/contracts/token_factory. -
Open
cargo.tomland add the dependencies inside the header.
2. Modify the contract files
Now that you've added the dependencies, you will need to modify the following files:
error.rsmsg.rslib.rsstate.rscontract.rstest.rs
To modify the contract files, follow the procedure below:
-
Navigate to
/token_factory/contracts/token_factory/src. -
Open
error.rsand add the following.
- Save and close
error.rs.
- Open
msg.rsand add the following.
- Save and close
msg.rs.
- Open
state.rsand add the following.
- Save and close
state.rs.
- Open
lib.rsand add the following.
- Save and close
lib.rs.
- Open
contract.rsand add the following.
- Open
test.rsand add the following.
- Save and Close
test.rs.
- Navigate to
token_factory/contracts/token_factory/examples.
- Open
schema.rsand paste the following.
- Save and close
schema.rs.
-
Navigate to
/token_factory/contracts/token_factory/src. -
Open
error.rsand add the following.
- Save and close
error.rs.
- Open
msg.rsand add the following.
- Save and close
msg.rs.
- Open
state.rsand add the following.
- Save and close
state.rs.
- Open
lib.rsand add the following.
- Save and close
lib.rs.
- Open
contract.rsand add the following.
- Open
test.rsand add the following.
- Save and Close
test.rs.
- Navigate to
token_factory/contracts/token_factory/examples.
- Open
schema.rsand paste the following.
- Save and close
schema.rs.
3. Generate and test the schema
Now that you have modified the token_factory contract, generate the schema and run the tests to validate that the code works as expected:
- Navigate to
/token_factory/contracts/token_factory.
- Generate the schema.
- You should see output similar to the following.
- Run the tests.
- You will see output similar to the following.
- Navigate to
/token_factory/contracts/token_factory.
- Generate the schema.
- You should see output similar to the following.
- Run the tests.
- You will see output similar to the following.
4. Modify config.terrain.json
-
Open
config.terrain.json. -
Modify the property
InstantiateMsg, using your<token_contract_code_id>. The<token_contract_code_id>should not be surrounded by quotes.
To determine which <token_contract_code_id>, check the file refs.terrain.json from the workspace's root under the cw20-token_factory object.
_12{_12 "_global": {_12 "_base": {_12 "instantiation": {_12 "instantiateMsg": {_12 "stable_denom": "uluna",_12 "token_contract_code_id": <token_contract_id>_12 }_12 }_12 }_12 }, // ..._12}
Deploy the smart contract to LocalTerra
Now that you've created, modified and tested each smart contract, deploy the token_factory to your LocalTerra instance using Terrain:
_1terrain deploy token_factory --signer test
If your code is not working as expected, you can clone the repo with all changes done until now.
A hosted website for the token factory can be found here.