Stellar allows you to create and issue your own custom assets. This is useful for creating tokens that represent real-world assets or other digital assets. In this tutorial, we'll walk through the process of creating and issuing a custom asset using the Stellar SDK for JavaScript.
Stellar Dev Guide
Tutorial:- Creating and Issuing Custom Assets
Ensure you have:
- Node.js installed.
- A text editor or IDE (like VSCode).
- Basic knowledge of JavaScript.
- Two Stellar accounts (one for issuing the asset and one for receiving it). You can create accounts and get testnet funds using the Stellar Laboratory.
First, create a new project directory and install the Stellar SDK:
mkdir stellar-custom-asset
cd stellar-custom-asset
npm init -y
npm install stellar-sdk
Create a file named issueAsset.js and add the following code:
const StellarSdk = require('stellar-sdk');
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
// Issuing account
const issuingKeys = StellarSdk.Keypair.fromSecret('ISSUING_ACCOUNT_SECRET_KEY');
// Receiving account
const receivingKeys = StellarSdk.Keypair.fromSecret('RECEIVING_ACCOUNT_SECRET_KEY');
// Define the asset
const customAsset = new StellarSdk.Asset('MYTOKEN', issuingKeys.publicKey());
console.log('Issuing Public Key:', issuingKeys.publicKey());
console.log('Receiving Public Key:', receivingKeys.publicKey());
Replace ISSUING_ACCOUNT_SECRET_KEY and RECEIVING_ACCOUNT_SECRET_KEY with the secret keys of your issuing and receiving accounts, respectively.
The receiving account needs to trust the custom asset. We'll create a trustline to allow this.
Add the following code to create a trustline:
async function createTrustline() {
try {
const account = await server.loadAccount(receivingKeys.publicKey());
const transaction = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(StellarSdk.Operation.changeTrust({
asset: customAsset,
}))
.setTimeout(30)
.build();
transaction.sign(receivingKeys);
const transactionResult = await server.submitTransaction(transaction);
console.log('Trustline created successfully!', transactionResult);
}
catch (error) {
console.error('Error creating trustline!', error);
}
}
createTrustline();
This script loads the receiving account, creates a trustline for the custom asset, and submits the transaction to the Stellar test network.
Now that the trustline is established, we can issue the asset from the issuing account to the receiving account.
Add the following code to issue the asset:
async function issueAsset() {
try {
const account = await server.loadAccount(issuingKeys.publicKey());
const transaction = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(StellarSdk.Operation.payment({
destination: receivingKeys.publicKey(),
asset: customAsset,
amount: '1000', // Amount of the custom asset to issue
}))
.setTimeout(30)
.build();
transaction.sign(issuingKeys);
const transactionResult = await server.submitTransaction(transaction);
console.log('Asset issued successfully!', transactionResult);
}
catch (error) {
console.error('Error issuing asset!', error);
}
}
issueAsset();
This script loads the issuing account, builds a payment transaction to issue the custom asset, signs it, and submits it to the Stellar test network.
Run the script using Node.js:
node issueAsset.js
If successful, you'll see messages indicating the trustline was created and the asset was issued, along with the transaction details.
You've successfully created and issued a custom asset on the Stellar network using the Stellar SDK for JavaScript. You can use this technique to create various types of tokens and distribute them to different accounts. For more advanced asset management options, check out the Stellar Assets documentation.