> ## Documentation Index
> Fetch the complete documentation index at: https://developers.stellar.org/llms.txt
> Use this file to discover all available pages before exploring further.

# Deploy a Stellar Asset Contract (SAC) from within a contract

> Deploy a SAC from another smart contract using the Rust SDK

## Overview

In this guide, you'll learn how to deploy a [Stellar Asset Contract (SAC)](https://developers.stellar.org/docs/tokens/stellar-asset-contract.md) 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](https://www.rust-lang.org). To brush up on Rust, check out [Rustlings](https://github.com/rust-lang/rustlings) or [The Rust book](https://doc.rust-lang.org/book).
* [Soroban Rust SDK](https://developers.stellar.org/docs/tools/sdks/contract-sdks.md#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:

```rust title="lib.rs" theme={"theme":{"light":"github-light","dark":"github-dark"}}
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.

```rust theme={"theme":{"light":"github-light","dark":"github-dark"}}
#[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`](https://docs.rs/soroban-sdk/latest/soroban_sdk/xdr/enum.Asset.html); 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.
