Stellar accounts have various options and flags that can be set to customize their behavior. This tutorial will guide you through setting account options and flags using the Stellar SDK for JavaScript.
Stellar Dev Guide
Tutorial:- Setting Account Options and Flags
Ensure you have:
- Node.js installed.
- A text editor or IDE (like VSCode).
- Basic knowledge of JavaScript.
- A funded Stellar account (refer to the first tutorial).
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 setOptions.js and add the following code:
const StellarSdk = require('stellar-sdk');
const server = new StellarSdk.Server('https://horizon-testnet.stellar.org');
// Account keys
const accountKeys = StellarSdk.Keypair.fromSecret('ACCOUNT_SECRET_KEY');
// Options to set
const homeDomain = 'example.com';
const inflationDest = 'INFLATION_DEST_PUBLIC_KEY';
Replace ACCOUNT_SECRET_KEY with the secret key of your account and INFLATION_DEST_PUBLIC_KEY with the public key of the inflation destination account.
Add the following code to set account options:
async function setAccountOptions() {
try {
const account = await server.loadAccount(accountKeys.publicKey());
const transaction = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(StellarSdk.Operation.setOptions({
homeDomain: homeDomain,
inflationDest: inflationDest,
}))
.setTimeout(30)
.build();
transaction.sign(accountKeys);
const transactionResult = await server.submitTransaction(transaction);
console.log('Account options set successfully!', transactionResult);
}
catch (error) {
console.error('Error!', error);
}
}
setAccountOptions();
This script sets the home domain and inflation destination for your account.
Add the following code to set account flags:
async function setAccountFlags() {
try {
const account = await server.loadAccount(accountKeys.publicKey());
const transaction = new StellarSdk.TransactionBuilder(account, {
fee: StellarSdk.BASE_FEE,
networkPassphrase: StellarSdk.Networks.TESTNET,
})
.addOperation(StellarSdk.Operation.setOptions({
setFlags: StellarSdk.AuthRequiredFlag | StellarSdk.AuthRevocableFlag,
}))
.setTimeout(30)
.build();
transaction.sign(accountKeys);
const transactionResult = await server.submitTransaction(transaction);
console.log('Account flags set successfully!', transactionResult);
}
catch (error) {
console.error('Error!', error);
}
}
setAccountFlags();
This script sets the AUTH_REQUIRED and AUTH_REVOCABLE flags for your account.
Run the script to set account options:
node setOptions.js
Run the script to set account flags:
node setFlags.js
If successful, you'll see messages indicating the account options and flags were set successfully, along with the transaction details.
You've successfully set account options and flags on the Stellar network using the Stellar SDK for JavaScript. You can use these techniques to customize your account behavior and manage your assets more effectively. For more details on account options and flags, check out the Stellar Set Options documentation.