Deploy a Stellar Asset Contract (SAC) from within a contract
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:
use soroban_sdk::{contract, contractimpl, Env, Address, Bytes};
#[contract]
pub struct SacDeployer;
#[contractimpl]
impl SacDeployer {
pub fn deploy_sac(env: Env, serialized_asset: Bytes) -> Address {
// Create the Deployer with Asset
let deployer = env.deployer().with_stellar_asset(serialized_asset);
let _ = deployer.deployed_address();
// Deploy the Stellar Asset Contract
let sac_address = deployer.deploy();
sac_address
}
}
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.
#[test]
fn test() {
use soroban_sdk::{
xdr::{Asset, Limits, WriteXdr},
Bytes, Env,
};
let env = Env::default();
let contract_id = env.register(SacDeployer, ());
let client = SacDeployerClient::new(&env, &contract_id);
let serialized_asset = Bytes::from_slice(&env, &Asset::Native.to_xdr(Limits::none()).unwrap());
let sac_address = client.deploy_sac(&serialized_asset);
assert_eq!(sac_address, env.deployer().with_stellar_asset(serialized_asset).deployed_address());
}
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.
Conclusion
By following this guide, you’ve successfully deployed a Stellar Asset Contract from within another contract using the Soroban Rust SDK. This approach enables smart contracts to handle SAC deployments dynamically, providing flexibility for various use cases in the Stellar ecosystem.
For further details, refer to the Soroban SDK documentation and explore more advanced features and configurations.
Guides in this category:
Set a custom admin account for a Stellar Asset Contract (SAC)
Set a custom administrator account on a deployed SAC
Deploy a Stellar Asset Contract (SAC) from within a contract
Deploy a SAC from another smart contract using the Rust SDK
Integrate Stellar Assets Contracts
Test and use Stellar assets in a Stellar smart contract