コンテンツにスキップ

通貨(トークン)の発行 | JavaScriptでのXRPL開発 三段編

このガイドはクロスカレンシー送金について、発行者AのA.USD、発行者BのB.EURを取り扱う形で解説しています。

クロスカレンシー送金の学習を進めるために、これまで作成・トラストラインの設定を行ったアカウントに対し、発行者から通貨(トークン)の送金をしておきます。

前提

  • アリスにA.USDB.EURを発行します。
  • ボブにB.EURを発行します。
  • チャーリーにA.USDを発行します。
  • ダニエルにB.EURを発行します。

使用するメソッド

この章では、先ほどの章で作成した以下のメソッドを利用します。

utils/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;
}
}

スクリプトの作成

  1. プロジェクトディレクトリにissueCurrency.jsという新しいファイルを作成します。

  2. 以下のコードをissueCurrency.jsに貼り付けます。

    import { Client } from 'xrpl';
    import { wallets } from './wallets.js';
    import { issueCurrency } from './utils/issueCurrency.js';
    const client = new Client('wss://s.altnet.rippletest.net:51233'); // テストネットを使用
    async function main() {
    try {
    await client.connect();
    const { issuerA, issuerB, alice, bob, charlie, daniel } = wallets;
    const limitAmount = '1000'; // 送金量
    // Alice
    await issueCurrency(client, issuerA, alice.address, 'USD', limitAmount);
    await issueCurrency(client, issuerB, alice.address, 'EUR', limitAmount);
    // Bob
    await issueCurrency(client, issuerB, bob.address, 'EUR', limitAmount);
    // Charlie
    await issueCurrency(client, issuerA, charlie.address, 'USD', limitAmount);
    // Daniel
    await issueCurrency(client, issuerB, daniel.address, 'EUR', limitAmount);
    } catch (error) {
    console.error(`Error in setup: ${error}`);
    } finally {
    await client.disconnect();
    }
    }
    main();

スクリプトの実行

  1. コマンドラインで以下のコマンドを実行して、スクリプトを実行します。

    Terminal window
    node issueCurrency.js
  2. 以下のようなコンソールログが表示されれば、成功です。

    Terminal window
    Currency issued to r3KQdHtUHUouGkLBFLZRxRYiugbW8cNwyJ: {
    id: 12,
    result: {
    Account: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    Amount: {
    currency: 'USD',
    issuer: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    value: '1000'
    },
    DeliverMax: {
    currency: 'USD',
    issuer: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    value: '1000'
    },
    Destination: 'r3KQdHtUHUouGkLBFLZRxRYiugbW8cNwyJ',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1049258,
    Sequence: 1046419,
    SigningPubKey: 'EDEBAB4C81CF854860D393191A001822A9B58242302710A2D3EFE5BE8620A1F8A9',
    TransactionType: 'Payment',
    TxnSignature: '74CC6F81AA8B0F080A57FBB12587FDD6CD8654317950D380DEA47EC090581CF369BBFB05BFA567BC9323E7933E951BC510CCCFDED518A90599CE5BB166423105',
    ctid: 'C010029800000001',
    date: 770217711,
    hash: '32C9AF7855BC4CB4978A4B9F96F8D05A7ED5E9AEC90A8523AAA4D37921FC4102',
    inLedger: 1049240,
    ledger_index: 1049240,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 0,
    TransactionResult: 'tesSUCCESS',
    delivered_amount: [Object]
    },
    validated: true
    },
    type: 'response'
    }
    Currency issued to r3KQdHtUHUouGkLBFLZRxRYiugbW8cNwyJ: {
    id: 22,
    result: {
    Account: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    Amount: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    DeliverMax: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    Destination: 'r3KQdHtUHUouGkLBFLZRxRYiugbW8cNwyJ',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1049260,
    Sequence: 1046421,
    SigningPubKey: 'EDBE91E21E32E1848B805B22597D87667FBC6B045B9ABADC67807DB88CD1767A97',
    TransactionType: 'Payment',
    TxnSignature: '64E95A4A04CBD2726B16729564A9E7EF716ADDEF7817087C8C92B52B4F3550D13C3DF9E83C59FCC0C29EB5A8CB69AC6EE54CF49F261829F78ED9153EA6D25B0F',
    ctid: 'C010029A00000001',
    date: 770217720,
    hash: '263B9E60D187DEC3473DC2154100F6490CBE7ABE734244A36429401F2BB27E0E',
    inLedger: 1049242,
    ledger_index: 1049242,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 0,
    TransactionResult: 'tesSUCCESS',
    delivered_amount: [Object]
    },
    validated: true
    },
    type: 'response'
    }
    Currency issued to rn9GFEPHW5tX2CaKVc7mA4L2GyXh88JiTy: {
    id: 36,
    result: {
    Account: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    Amount: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    DeliverMax: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    Destination: 'rn9GFEPHW5tX2CaKVc7mA4L2GyXh88JiTy',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1049262,
    Sequence: 1046422,
    SigningPubKey: 'EDBE91E21E32E1848B805B22597D87667FBC6B045B9ABADC67807DB88CD1767A97',
    TransactionType: 'Payment',
    TxnSignature: '6B22A203EC9C350A91A3908D69047AEDA8BC860C5845173C0E442C0C3ADF68D3838571FF330DF165D6A50D894AA8629B94E4A2CAE7D357FB7F1E08F009BC810A',
    ctid: 'C010029C00020001',
    date: 770217722,
    hash: '87F37DDB448ED9EEE2AB91997C2EE83C3DF786BF592E7BFDC498610D84AF2BAF',
    inLedger: 1049244,
    ledger_index: 1049244,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 2,
    TransactionResult: 'tesSUCCESS',
    delivered_amount: [Object]
    },
    validated: true
    },
    type: 'response'
    }
    Currency issued to rnP7Q6LaejYi7oaN4qJHkKhnESZYAyVQwh: {
    id: 51,
    result: {
    Account: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    Amount: {
    currency: 'USD',
    issuer: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    value: '1000'
    },
    DeliverMax: {
    currency: 'USD',
    issuer: 'rfkJ7Uz6NrNV1FdbmKB5wepoim51KFpYQp',
    value: '1000'
    },
    Destination: 'rnP7Q6LaejYi7oaN4qJHkKhnESZYAyVQwh',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1049264,
    Sequence: 1046420,
    SigningPubKey: 'EDEBAB4C81CF854860D393191A001822A9B58242302710A2D3EFE5BE8620A1F8A9',
    TransactionType: 'Payment',
    TxnSignature: 'C83C3A41C1D5EEEACD20E4E3DBF6F98A6ADA4E2C3327BF04488E67576ABD2ADA7F8E3CA07DF96B34D396C9A1AE82058DECEC4B3B3633ED3E2C36A72F3A16EF02',
    ctid: 'C010029E00000001',
    date: 770217731,
    hash: '4C9FDB7F3E5E27677E0218E157389F6700D2D0B90A9AAE4A526BA707526B53C6',
    inLedger: 1049246,
    ledger_index: 1049246,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 0,
    TransactionResult: 'tesSUCCESS',
    delivered_amount: [Object]
    },
    validated: true
    },
    type: 'response'
    }
    Currency issued to rf6R7pFgytT9JKjMVjKqeTv6MeDTrbNw3: {
    id: 61,
    result: {
    Account: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    Amount: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    DeliverMax: {
    currency: 'EUR',
    issuer: 'rstM9rxhKo4N4WEaQgQmQSPQRgZqkhHkaU',
    value: '1000'
    },
    Destination: 'rf6R7pFgytT9JKjMVjKqeTv6MeDTrbNw3',
    Fee: '12',
    Flags: 0,
    LastLedgerSequence: 1049266,
    Sequence: 1046423,
    SigningPubKey: 'EDBE91E21E32E1848B805B22597D87667FBC6B045B9ABADC67807DB88CD1767A97',
    TransactionType: 'Payment',
    TxnSignature: '0A217D0EC6731761596AE6DA7E131010601DE05A7B925E287AAB0DA1F653BCBEC0EB91E4746C45F9CE35D18DDE802EEC92EE18D065B311FCABB1359944BD200E',
    ctid: 'C01002A000000001',
    date: 770217740,
    hash: 'D6FDDB8E55F62A55D13A358C76FAA8D399159F471A10CC0F1820A31D37233ECC',
    inLedger: 1049248,
    ledger_index: 1049248,
    meta: {
    AffectedNodes: [Array],
    TransactionIndex: 0,
    TransactionResult: 'tesSUCCESS',
    delivered_amount: [Object]
    },
    validated: true
    },
    type: 'response'
    }