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:
- Node.js and npm installed.
- Stellar SDK for JavaScript installed
- Knowledge about Issuing an Asset on Stellar
- An understanding of the rudimentary, retry-enabled transaction polling function
yeetTx
which we outlined in another guide
Code overview
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 network_passphrase = 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: network_passphrase,
})
.addOperation(
StellarSdk.Operation.createStellarAssetContract({
asset: customAsset,
}),
)
.setTimeout(30)
.build();
const uploadTx = await server.prepareTransaction(transaction);
uploadTx.sign(sourceKeypair);
const result = await server.sendTransaction(uploadTx);
const feedback = await yeetTx(result);
const contractBuffer = feedback.returnValue._value._value;
const contractID = StellarSdk.StrKey.encodeContract(contractBuffer);
console.log(`ContractID of Our ${customAsset.code} Asset`, contractID);
} 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 ofSorobanRpc.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 network_passphrase = 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: network_passphrase,
})
.addOperation(
StellarSdk.Operation.createStellarAssetContract({
asset: customAsset,
}),
)
.setTimeout(30)
.build();
const uploadTx = await server.prepareTransaction(transaction);
uploadTx.sign(sourceKeypair);
const result = await server.sendTransaction(uploadTx);
const feedback = await yeetTx(result);
const contractBuffer = feedback.returnValue._value._value;
const contractID = StellarSdk.StrKey.encodeContract(contractBuffer);
console.log(`ContractID of Our ${customAsset.code} Asset`, contractID);
} 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 thecreateStellarAssetContract
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
yeetTx
function to ensure it has succeeded. extracts the contract buffer from the feedback and converts it to a contract ID usingStellarSdk.StrKey.encodeContract
. finally, it logs the contract ID for the deployed asset.
Guides in this category:
📄️ Using __check_auth in interesting ways
Two guides that walk through using __check_auth
📄️ Making cross-contract calls
Call a smart contract from within another smart contract
📄️ Deploy a contract from installed Wasm bytecode using a deployer contract
Deploy a contract from installed Wasm bytecode using a deployer contract
📄️ Deploy a SAC for a Stellar asset using code
Deploy a SAC for a Stellar asset using Javascript SDK
📄️ Organize contract errors with an error enum type
Manage and communicate contract errors using an enum struct stored as Status values
📄️ Extend a deployed contract’s TTL with code
How to extend the TTL of a deployed contract's Wasm code using JavaScript SDK
📄️ Upgrading Wasm bytecode for a deployed contract
Upgrade Wasm bytecode for a deployed contract
📄️ Write metadata for your contract
Use the contractmeta! macro in Rust SDK to write metadata in Wasm contracts