Skip to main content

Deploy a SAC for a Stellar asset using code

Overview

In this guide, you'll learn how to deploy a Stellar Asset Contract (SAC) for a Stellar asset using the Stellar SDK. The Stellar SDK is a set of tools and library designed to help developers build applications that interact with the Stellar blockchain network.

Prerequisites:

Code overview

deployassetcontract.js
import * as StellarSdk from "@stellar/stellar-sdk";
import { Server } from "@stellar/stellar-sdk/rpc";

const networkRPC = "https://soroban-testnet.stellar.org";
const server = new 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

import * as StellarSdk from "@stellar/stellar-sdk";

const networkRPC = "https://soroban-testnet.stellar.org";
const server = new StellarSdk.SorobanRpc.Server(networkRPC);
const network_passphrase = StellarSdk.Networks.TESTNET;
  • networkRPC: The URL for the Soroban testnet.
  • server: A new instance of SorobanRpc.Server is created, which will be used to interact with the Soroban testnet.
  • network_passphrase: sets the network passphrase to the TESTNET

DeployStellarAssetContract function

const deployStellarAssetContract = async () => {
const sourceSecrets =
"SASI6PA4K52GQJF6BC263GLYOADVKFJ4SZ7TFX4QQF2U76T3EJ54DT7Y"; // Replace with your Secret Key
const networkPassphrase = StellarSdk.Networks.TESTNET;
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();

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. extracts the contract buffer from the feedback and converts it to a contract ID using StellarSdk.Address. finally, it logs the contract ID for the deployed asset.