Tutorial:- Setting Account Options and Flags

Introduction

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.

Prerequisites

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).
Step 1: Set Up the Project

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
Step 2: Load the Stellar SDK and Configure Accounts

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.

Step 3: Set Account Options

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.

Step 4: Set Account Flags

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.

Step 5: Run the Script

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.

Conclusion

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.