Creating Generic Methods | XRPL Development in JavaScript - Level 3
In the previous section, we discussed ES6. In this guide, we will get accustomed to practical implementation by defining generic methods as modules.
First, let’s set up a utils directory to organize our learning process efficiently.
1. Creating trustSet.js
Section titled “1. Creating trustSet.js”Define a generic method for setting a trust line.
// Function to set a trust lineexport async function setTrustLine(client, wallet, currency, issuer, limit) {  try {    const trustSet = {      TransactionType: 'TrustSet',      Account: wallet.classicAddress,      LimitAmount: {        currency: currency,        issuer: issuer,        value: limit,      },    };    const response = await client.submitAndWait(trustSet, { wallet });    return response;  } catch (error) {    console.error(`Error setting trust line: ${error}`);    throw error;  }}import { Client, Wallet } from 'xrpl';import { setTrustLine } from './utils/trustSet.js';
// Initialize client and walletconst client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const wallet = Wallet.fromSeed('s████████████████████████████');
const currency = 'USD';const issuer = 'r████████████████████████████';const limit = '1000';
// Set the trust lineawait setTrustLine(client, wallet, currency, issuer, limit);
client.disconnect();2. Creating createOffer.js
Section titled “2. Creating createOffer.js”Define a generic method to send an OfferCreate transaction.
// Function to create an offerexport async function createOffer(client, wallet, takerGets, takerPays) {  try {    const offerCreate = {      TransactionType: 'OfferCreate',      Account: wallet.classicAddress,      TakerGets: takerGets,      TakerPays: takerPays,    };    const response = await client.submitAndWait(offerCreate, { wallet });    console.log(`Offer created for ${wallet.classicAddress}:`, response);    return response;  } catch (error) {    console.error(      `Error creating offer for ${wallet.classicAddress}: ${error}`    );    throw error;  }}import { Client, Wallet } from 'xrpl';import { createOffer } from './utils/createOffer.js';
...
// Initialize client and walletconst client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const wallet = Wallet.fromSeed('s████████████████████████████');
const takerGets = {  currency: 'USD',  issuer: 'r████████████████████████████',  value: '100',};
const takerPays = {  currency: 'XRP',  value: '100000000',};
// Create the offerawait createOffer(client, wallet, takerGets, takerPays);
client.disconnect();3. Creating payment.js
Section titled “3. Creating payment.js”Define a generic method to send payments.
// Initialize client and walletexport async function sendPayment(client, wallet, amount, destination, options = {}) {  try {    const payment = {      TransactionType: 'Payment',      Account: wallet.classicAddress,      Amount: typeof amount === 'object' ? amount : amount.toString(),      Destination: destination,    };
    if (options.sendMax !== undefined && options.sendMax !== null) {      payment.SendMax = typeof options.sendMax === 'object' ? options.sendMax : options.sendMax.toString();    }
    if (options.paths !== undefined && options.paths !== null) {      payment.Paths = options.paths;    }
    const response = await client.submitAndWait(payment, { wallet });    console.log(`Payment sent from ${wallet.classicAddress} to ${destination}:`, response);    return response;  } catch (error) {    console.error(`Error sending payment from ${wallet.classicAddress} to ${destination}: ${error}`);    throw error;  }}import { Client, Wallet } from 'xrpl';import { sendPayment } from './utils/payment.js';
...
// Initialize client and walletconst client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const wallet = Wallet.fromSeed('s████████████████████████████');
const amount = {  currency: 'USD',  issuer: 'r████████████████████████████',  value: '10',};
const destination = 'r████████████████████████████';const sendMaxAmount = '100000000';
// Send the paymentawait sendPayment(client, wallet, amount, destination, {  sendMax: sendMaxAmount})
client.disconnect();4. Creating setAccountFlags.js
Section titled “4. Creating setAccountFlags.js”Define a generic method to change account settings (such as enabling rippling).
// Function to set account flagsexport async function setAccountFlags(  client,  wallet,  setFlag) {  try {    const accountSet = {      TransactionType: 'AccountSet',      Account: wallet.classicAddress,      SetFlag: setFlag,    };
    const response = await client.submitAndWait(accountSet, { wallet });    console.log(`Account flags set for ${wallet.classicAddress}:`, response);    return response;  } catch (error) {    console.error(      `Error setting account flags for ${wallet.classicAddress}: ${error}`    );    throw error;  }}import { Client, Wallet, AccountSetAsfFlags } from 'xrpl';import { setAccountFlags } from './utils/setAccountFlags.js';
...
// Initialize client and walletconst client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const wallet = Wallet.fromSeed('s████████████████████████████');
const setFlag = AccountSetAsfFlags.asfDefaultRipple; // asfDefaultRipple 8
// Set account flagsawait setAccountFlags(client, wallet, setFlag);
client.disconnect();5. Creating issueCurrency.js
Section titled “5. Creating issueCurrency.js”Define a generic method to issue tokens.
// Function to issue tokensexport async function issueCurrency(  client,  issuerWallet,  recipientAddress,  currency,  amount) {  try {    const payment = {      TransactionType: 'Payment',      Account: issuerWallet.classicAddress,      Amount: {        currency: currency,        issuer: issuerWallet.classicAddress,        value: amount,      },      Destination: recipientAddress,    };    const response = await client.submitAndWait(payment, {      wallet: issuerWallet,    });    console.log(`Currency issued to ${recipientAddress}:`, response);    return response;  } catch (error) {    console.error(`Error issuing currency to ${recipientAddress}: ${error}`);    throw error;  }}import { Client, Wallet } from 'xrpl';import { issueCurrency } from './utils/issueCurrency.js';
...
// Initialize client and walletconst client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const issuerWallet = Wallet.fromSeed('s████████████████████████████');const recipientAddress = 'r████████████████████████████';
const currency = 'USD';const amount = '1000';
// Issue the tokenawait issueCurrency(client, issuerWallet, recipientAddress, currency, amount);
client.disconnect();