汎用的なメソッドの作成 | JavaScriptでのXRPL開発 三段編
先ほどES6について解説しましたが、このガイドでは実用的な実装方法に慣れていくことを目的に、汎用的なメソッドをモジュールとして定義しながら解説していきます。
ますは下準備として、学習を効率化するために、utilsディレクトリにファイルを作成していきましょう。
1. trustSet.jsの作成
Section titled “1. trustSet.jsの作成”トラストラインを設定する汎用的なメソッドを定義します。
// トラストラインを設定する関数export 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';
// クライアントとウォレットの初期化const 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';
// トラストラインの設定await setTrustLine(client, wallet, currency, issuer, limit);
client.disconnect();2. createOffer.jsの作成
Section titled “2. createOffer.jsの作成”offerCreateトランザクションを送信する汎用的なメソッドを定義します。
// オファーを作成する関数export 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';
...
// クライアントとウォレットの初期化const 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',};
// オファーの作成await createOffer(client, wallet, takerGets, takerPays);
client.disconnect();3. payment.jsの作成
Section titled “3. payment.jsの作成”送金を行う汎用的なメソッドを定義します。
// 送金を行う汎用的な関数export 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';
...
// クライアントとウォレットの初期化const 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';
// 送金の実行await sendPayment(client, wallet, amount, destination, {  sendMax: sendMaxAmount})
client.disconnect();4. setAccountFlags.jsの作成
Section titled “4. setAccountFlags.jsの作成”アカウント設定の変更を行う(リップリング許可など)汎用的なメソッドを定義します。
// アカウントのフラグを設定する関数export 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';
...
// クライアントとウォレットの初期化const client = new Client('wss://s.altnet.rippletest.net:51233');await client.connect();
const wallet = Wallet.fromSeed('s████████████████████████████');
const setFlag = AccountSetAsfFlags.asfDefaultRipple; // リップリング許可の設定 8
// アカウントフラグの設定await setAccountFlags(client, wallet, setFlag);
client.disconnect();5. issueCurrency.jsの作成
Section titled “5. issueCurrency.jsの作成”トークンを発行する汎用的なメソッドを定義します。
// トークンを発行する関数export 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';
...
// クライアントとウォレットの初期化const 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';
// トークンの発行await issueCurrency(client, issuerWallet, recipientAddress, currency, amount);
client.disconnect();