Skip to main content

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:

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 the deploy_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.

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.

#[cfg(feature = "testutils")]
fn main() {
let env = TestEnv::default();
let serialized_asset = Bytes::from("actual serialized asset"); // Replace with actual serialized asset info
let sac_deployer = SacDeployer::default();
let sac_address = sac_deployer.deploy_sac(env.clone(), serialized_asset);
println!("SAC Address: {}", sac_address.to_string());
}

Explanation

  • TestEnv::default(): creates a default testing environment for the deployment.
  • SacDeployer::default(): initializes the SacDeployer contract.
  • deploy_sac(env.clone(), serialized_asset): calls the deploy_sac function to deploy the SAC and retrieves the address.
  • println!("SAC Address: {}", sac_address.to_string()): prints the address of the deployed SAC.
  • assert!(sac_address.to_string().len() > 0, "SAC address should be non-empty"): asserts that the SAC address is non-empty, ensuring a successful deployment.
  • serialized_assetis the Stellar Asset XDR serialized to bytes. Refer to [soroban_sdk::xdr::Asset] XDR

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.