Building the Soroban Token Smart Contract
In dapps like the Example Payment Dapp, the Soroban Token smart contracts are used to represent the tokenized asset that users can send and receive. Here is an example of how to build and deploy the Soroban Token smart contracts: Start by cloning the Soroban examples repository:token directory:
token/target/wasm32v1-none/release directory. After building, you’re ready to deploy the smart contracts to Testnet.
Deploying and Initializing the Soroban Token Smart Contract
The smart contract can be initialized at deploy-time by utilizing the__constructor function. This function will be called at deploy-time and can be declared with parameters you can pass to the function with the deploy command.
The __constructor function is added to the contract entry point, in this token smart contract it is found in the contract.rs file.
contract.rs
__constructor function in this example project takes the following parameters:
- Administrator Account: This is the public key of the administrator account. The administrator has control and authority over the token contract, enabling management of various contract functionalities. Learn more about the administrator’s role from the Soroban Token Interface.
- Decimal Precision: The decimal precision specifies the maximum number of decimal places the token can support transactions up to. This precision level enables flexibility when transferring token amounts.
- Token Name: The token’s name.
- Token Symbol: This is the token’s symbol.
soroban-examples/token directory and execute the following:
stellar contract deploy function. The smart contract will be deployed with the mandatory parameters, and the smart contract will be initialized with these values.
Minting Tokens
Lastly, you need to mint some tokens to the sender’s account:bob.
By following these steps, you ensure that the Soroban token smart contracts are correctly deployed and initialized, setting the stage for the Dapp to effectively interact with the token.
For a deeper dive into Stellar CLI commands, check out the Stellar CLI repo.
Automating Initialization with Scripts
To streamline the initialization process, you can use a script. This script should automate various tasks such as setting up the network, generating token-admin identities, funding the token-admin account, building and deploying the contracts, and initializing them with necessary parameters. Here’s an example initializer script:initialize.sh
initialize.sh script does:
- Identifies the network (standalone or futurenet) based on user input
- Determines the Stellar RPC host URL depending on its execution environment (either inside the soroban-preview Docker container or locally)
- Sets the Stellar RPC URL based on the previously determined host URL
- Sets the Soroban network passphrase and Friendbot URL depending on the chosen network
- Adds the network configuration using
stellar network add - Generates a token-admin identity using
stellar keys generate - Fetches the TOKEN_ADMIN_SECRET and TOKEN_ADMIN_ADDRESS from the newly generated identity
- Saves the TOKEN_ADMIN_SECRET and TOKEN_ADMIN_ADDRESS in the .soroban directory
- Funds the token-admin account using Friendbot
- Builds the contract with
make buildand deploys it usingstellar contract deploy, storing the returned CROWDFUND_ID - Prints “Done” to signify the end of the initialization process