Integrating a Token Factory Token in a Smart Contract
This guide will walk you through integrating a Token Factory token into a smart contract. The contract will allow users to:
Retrieve their Token Factory token balance.
Send Token Factory tokens to another user.
Prerequisites
Before deploying your smart contract on-chain, ensure you have completed the following setup steps:
Set up your local environment: Follow the installation and setup guide to configure your development environment.
Install the XION daemon: Set up the XION CLI by following the installation instructions to interact with the blockchain.
Make sure you have Docker installed and running, as it is required to compile your contract.
Creating the Smart Contract
Initialize a New Contract
We are going to use the cw-counter contract as a base for our contract. Run the following command to create a new contract:
Implementing Core Logic
Remove some files
We need to remove the src/helpers.rs and src/integration_test.rs files. Then update the lib.rs file with the following:
Store the Token Address in Contract State
Modify state.rs
to store the token denomination in the contract’s persistent storage.
Define Contract Messages
Delete the contents of src/msg.rs
and replace with the following:
Contract Logic
Modify src/contract.rs
to implement the core logic of the contract:
Optimize Contract
Next, compile and optimize the smart contract using the CosmWasm Optimizing Compiler. You need to have Docker running to execute the command below. Make sure you are in the root folder of your project and then executes the following:
Upload Optimized Contract On-chain
First, set your wallet address or key name by executing the following in your terminal:
Now, upload the contract to the blockchain:
After running the command, extract the transaction hash by executing:
Example output:
Copy the txhash value for the next step.
Retrieve the Code ID
The Code ID is required for creating an instance of your contract.
Set your transaction hash you retrieved above by executing:
Query the blockchain to get the Code ID:
Now, display the retrieved Code ID:
Example output:
Instantiate the Contract
To initialize the contract, set up the initialization message:
Replace "factory/xion1xyz.../denom-name"
with the actual token factory token you created.
Now, instantiate the contract using the CODE_ID
obtained from the previous step:
Example output:
Copy the new transaction hash for the next step.
Retrieve the Contract Address
Once the contract has been instantiated, it is assigned a unique contract address. Follow these steps to retrieve it:
Set the transaction hash (TXHASH
) from the contract instantiation step:
Replace "your-txhash-here"
with the actual transaction hash from the previous step.
Query the blockchain to get the contract address:
Display the contract address:
Example output:
Querying the Contract
After deploying the contract, you can interact with it using queries.
The contract exposes a method to fetch the balance of a specific address. Define the query message in the following format:
Replace "add-address-here"
with the actual address you want to check.
Run the following command to query the contract for the specified address's token balance:
Where:
$CONTRACT
is the smart contract address retrieved in the previous step.$QUERY
contains the request payload.
If the query is successful, you will receive a JSON response containing the balance of the specified address. Example output:
This indicates that the queried address holds 1,000,000
units of the token.
Execute Transactions
Now that you have an instance of the contract, you can interact with it by executing transactions. The contract accepts a message that allows token transfers from the sender to a specified recipient.
Define the message as follows:
Replace "recipient-address-here"
with the actual wallet address you want to send tokens to.
Run the following command to execute the token transfer:
Where:
$CONTRACT
: The deployed contract address.$SEND_TOKEN
: The execution message to send tokens.--amount "10factory/xion1ka5gdcv4m7kfzxkllapqdflenwe0fv8ftm357r/emp"
: Specifies the token amount and denom being sent.
After executing this transaction, you can run the get_balance query again to verify that the amount was deducted from your account and also check the recipient's address to see if they received the tokens.
Last updated
Was this helpful?