> ## 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 SAC for a Stellar asset using code

> Deploy a SAC for a Stellar asset using Javascript 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) for a Stellar asset using the [Stellar SDK](https://developers.stellar.org/docs/tools/sdks/client-sdks.md#javascript-sdk). The Stellar SDK is a set of tools and libraries designed to help developers build applications that interact with the Stellar blockchain network.

### Prerequisites:

* [Node.js ](https://nodejs.org/en) and npm installed.
* Stellar SDK for [JavaScript](https://www.npmjs.com/package/@stellar/stellar-sdk) installed
* [Knowledge about Issuing an Asset on Stellar](https://developers.stellar.org/docs/tokens/how-to-issue-an-asset.md)
* An understanding of the rudimentary, retry-enabled transaction polling function `submitTx` which we outlined in [another guide](https://developers.stellar.org/docs/build/guides/transactions/submit-transaction-wait-js.md)

## Code overview

```javascript title="deployassetcontract.js" theme={"theme":{"light":"github-light","dark":"github-dark"}}
import * as StellarSdk from "@stellar/stellar-sdk";

const networkRPC = "https://soroban-testnet.stellar.org";
const server = new StellarSdk.rpc.Server(networkRPC);
const networkPassphrase = StellarSdk.Networks.TESTNET;

const deployStellarAssetContract = async () => {
  const sourceSecrets =
    "SASI6PA4K52GQJF6BC263GLYOADVKFJ4SZ7TFX4QQF2U76T3EJ54DT7Y"; // Replace with your Secret Key
  const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecrets);
  const sourceAccount = await server.getAccount(sourceKeypair.publicKey());

  try {
    const assetCode = "JOEBOY";
    const issuerPublicKey = sourceKeypair.publicKey();
    const customAsset = new StellarSdk.Asset(assetCode, issuerPublicKey);

    const transaction = new StellarSdk.TransactionBuilder(sourceAccount, {
      fee: StellarSdk.BASE_FEE,
      networkPassphrase,
    })
      .addOperation(
        StellarSdk.Operation.createStellarAssetContract({
          asset: customAsset,
        }),
      )
      .setTimeout(30)
      .build();

    const uploadTx = await server.prepareTransaction(transaction);
    uploadTx.sign(sourceKeypair);

    const feedback = await submitTx(uploadTx);
    const contract = StellarSdk.Address.fromScAddress(
      feedback.returnValue.address(),
    );
    console.log(
      `ContractID of Our ${customAsset.code} Asset`,
      contract.toString(),
    );
  } catch (e) {
    console.error("An error occurred while Deploying assets:", e);
  }
};

await deployStellarAssetContract();
```

## Code explanation

**Server Setup**

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
import * as StellarSdk from "@stellar/stellar-sdk";

const networkRPC = "https://soroban-testnet.stellar.org";
const server = new StellarSdk.rpc.Server(networkRPC);
const networkPassphrase = StellarSdk.Networks.TESTNET;
```

* `networkRPC`: The URL for the Soroban testnet.
* `server`: A new instance of `rpc.Server` is created, which will be used to interact with the Soroban testnet.
* `networkPassphrase`: sets the network passphrase to the TESTNET

**`DeployStellarAssetContract` function**

```javascript theme={"theme":{"light":"github-light","dark":"github-dark"}}
const deployStellarAssetContract = async () => {
  const sourceSecrets =
    "SASI6PA4K52GQJF6BC263GLYOADVKFJ4SZ7TFX4QQF2U76T3EJ54DT7Y"; // Replace with your Secret Key
  const sourceKeypair = StellarSdk.Keypair.fromSecret(sourceSecrets);
  const sourceAccount = await server.getAccount(sourceKeypair.publicKey());

  try {
    const assetCode = "JOEBOY";
    const issuerPublicKey = sourceKeypair.publicKey();
    const customAsset = new StellarSdk.Asset(assetCode, issuerPublicKey);

    const transaction = new StellarSdk.TransactionBuilder(sourceAccount, {
      fee: StellarSdk.BASE_FEE,
      networkPassphrase,
    })
      .addOperation(
        StellarSdk.Operation.createStellarAssetContract({
          asset: customAsset,
        }),
      )
      .setTimeout(30)
      .build();

    const uploadTx = await server.prepareTransaction(transaction);
    uploadTx.sign(sourceKeypair);

    const feedback = await submitTx(uploadTx);
    const contract = StellarSdk.Address.fromScAddress(
      feedback.returnValue.address(),
    );
    console.log(
      `ContractID of Our ${customAsset.code} Asset`,
      contract.toString(),
    );
  } catch (e) {
    console.error("An error occurred while Deploying assets:", e);
  }
};
```

This function is designed to deploy a Stellar Asset Contract (SAC) on the Soroban testnet.

* **Secret Key**: It starts by defining the secret key for the source account (`sourceSecrets`), which you must replace with your own.
* **Keypair and Account**: Generates the keypair from the secret key and fetches the account details from the Soroban server.
* **Custom Asset**: Defines a custom asset with the code `JOEBOY` and the issuer's public key.
* **Transaction Building**: A transaction is built using the `TransactionBuilder`, which includes the `createStellarAssetContract` operation for the custom asset. The transaction is then prepared and signed.
* **Send Transaction**: The signed transaction is sent to the network using `server.sendTransaction`.
* **Feedback Handling**: It waits for the transaction feedback using the `submitTx` function to ensure it has succeeded. Then, it extracts the return value and converts it to a contract ID using `StellarSdk.Address`. Finally, it logs the contract ID for the deployed asset.
