Building the Soroban Token Smart Contract
In a dapp, the Soroban Token smart contract can represent the tokenized asset that users send and receive. Here is an example of how to build and deploy the Soroban Token smart contract: 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.
alice deploys the contract and is its admin, and bob receives the minted tokens. Create and fund both on Testnet first. Skip this step if you already have them:
soroban-examples/token directory and execute the following:
stellar contract deploy command. Everything after -- goes to the __constructor function, so the contract is initialized with these values. The --alias token option saves the new contract ID under the name token, so later commands can use --id token.
Minting Tokens
Lastly, you need to mint some tokens to the sender’s account:bob. The token has 7 decimals, so an --amount of 1000000000 is 100 tokens.
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 automates tasks such as generating and funding a deployer identity, building and deploying the contract, and initializing it with the necessary parameters. Here’s an example initializer script. Save it in thesoroban-examples/token directory and run it from there, for example bash initialize.sh testnet alice 7 'Demo Token' DT:
initialize.sh
initialize.sh script does:
- Checks the arguments and picks one of the networks the Stellar CLI knows by default:
testnet,futurenetorlocal - Generates a
deployeridentity withstellar keys generateand funds it with Friendbot, only if the identity does not exist yet - Builds the contract with
make build - Deploys the contract with
stellar contract deploy, passes the constructor arguments after--, and saves the contract ID under the aliastoken - Prints the contract ID and “Done”
deployer identity only pays for the deployment. The <admin> argument sets the token’s admin, so pass the identity or address that should manage the token. The deployer’s secret key stays in the Stellar CLI’s config, and the script never prints it or writes it to a file. You can run the script again: it reuses the deployer identity, deploys a new contract, and points the token alias at it.
By leveraging automated initialization, you can streamline the setup process for your Soroban Dapp, ensuring it is correctly deployed and initialized.