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:
lib.rs
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
SacDeployer
contract: this contract defines thedeploy_sac
function to handle the deployment of the SAC.deploy_sac
function: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.