Overview
In this guide, you’ll learn how to deploy a Stellar Asset Contract (SAC) from within another smart contract using the Soroban Rust SDK. The Soroban Rust SDK provides tools and utilities for working with Stellar smart contracts, allowing you to deploy and interact with SACs directly from your contract logic.Prerequisites:
Before you begin, make sure you have the following:- Basic understanding of Rust programming language. To brush up on Rust, check out Rustlings or The Rust book.
- Soroban Rust SDK installed and configured in your development environment.
- Basic understanding of the Soroban Rust SDK and familiarity with Soroban’s core concepts and Rust programming.
1. Define the SacDeployer contract
The SacDeployer contract will be responsible for deploying the Stellar Asset Contract. Here is the code for the SacDeployer contract:lib.rs
Explanation
SacDeployercontract: this contract defines thedeploy_sacfunction to handle the deployment of the SAC.deploy_sacfunction:env.deployer().with_stellar_asset(serialized_asset): creates a deployer configured to deploy a Stellar Asset Contract using the provided serialized asset.deployer.deploy(): Deploys the SAC and returns the address of the deployed contract.
2. Testing the deployment
You need to test the deployment to ensure everything works as expected. The following code demonstrates how to test the SacDeployer contract using the Soroban Rust SDK’s test utilities.Explanation
env.register(SacDeployer, ()): registers theSacDeployercontract in the test environment and returns its contract ID.SacDeployerClient::new(&env, &contract_id): creates a client used to invoke the deployedSacDeployercontract’s functions.serialized_asset: the StellarAssetXDR serialized to bytes. This example serializes the native XLM asset viasoroban_sdk::xdr::Asset; for an issued asset, useAsset::CreditAlphanum4/Asset::CreditAlphanum12instead.client.deploy_sac(&serialized_asset): invokes the contract, which deploys the SAC and returns its address.- The assertion confirms the returned address matches the deterministic address the SDK computes for that same serialized asset.