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.

#[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 the SacDeployer contract in the test environment and returns its contract ID.
  • SacDeployerClient::new(&env, &contract_id): creates a client used to invoke the deployed SacDeployer contract's functions.
  • serialized_asset: the Stellar Asset XDR serialized to bytes. This example serializes the native XLM asset via soroban_sdk::xdr::Asset; for an issued asset, use Asset::CreditAlphanum4/Asset::CreditAlphanum12 instead.
  • 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.