Files
xrpl-dev-portal/_code-samples/send-an-mpt/js/sendMPTSetup.js
2026-06-12 18:57:27 +01:00

100 lines
3.0 KiB
JavaScript

import xrpl from 'xrpl'
import fs from 'fs'
import { fileURLToPath } from 'url'
const TOTAL_STEPS = 4
export async function setup() {
let step = 1
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
await client.connect()
// Fund issuer and sender in parallel
process.stdout.write(`Setting up tutorial: ${step++}/${TOTAL_STEPS}\r`)
const [
{ wallet: issuer },
{ wallet: sender }
] = await Promise.all([
client.fundWallet(),
client.fundWallet()
])
// Issue MPT with Can Transfer flag
process.stdout.write(`Setting up tutorial: ${step++}/${TOTAL_STEPS}\r`)
// XLS-89 metadata, encoded to hex with the SDK utility.
const metadata = {
ticker: 'TUTO',
name: 'Tutorial Token',
desc: 'Example MPT for the Send an MPT tutorial.',
icon: 'https://example.org/tutorial-icon.png',
asset_class: 'other',
issuer_name: 'XRPL Tutorial Issuer'
}
const issueResp = await client.submitAndWait({
TransactionType: 'MPTokenIssuanceCreate',
Account: issuer.address,
AssetScale: 2,
MaximumAmount: '1000000',
TransferFee: 0,
Flags: xrpl.MPTokenIssuanceCreateFlags.tfMPTCanTransfer,
MPTokenMetadata: xrpl.encodeMPTokenMetadata(metadata)
}, { wallet: issuer, autofill: true })
if (issueResp.result.meta.TransactionResult !== 'tesSUCCESS') {
throw new Error(`MPTokenIssuanceCreate failed: ${issueResp.result.meta.TransactionResult}`)
}
const mptIssuanceID = issueResp.result.meta.mpt_issuance_id
// Sender authorizes the MPT
process.stdout.write(`Setting up tutorial: ${step++}/${TOTAL_STEPS}\r`)
const authResp = await client.submitAndWait({
TransactionType: 'MPTokenAuthorize',
Account: sender.address,
MPTokenIssuanceID: mptIssuanceID
}, { wallet: sender, autofill: true })
if (authResp.result.meta.TransactionResult !== 'tesSUCCESS') {
throw new Error(`MPTokenAuthorize failed: ${authResp.result.meta.TransactionResult}`)
}
// Issuer sends sender MPTs
process.stdout.write(`Setting up tutorial: ${step++}/${TOTAL_STEPS}\r`)
const seedResp = await client.submitAndWait({
TransactionType: 'Payment',
Account: issuer.address,
Destination: sender.address,
Amount: {
mpt_issuance_id: mptIssuanceID,
value: '500'
}
}, { wallet: issuer, autofill: true })
if (seedResp.result.meta.TransactionResult !== 'tesSUCCESS') {
throw new Error(`Issuer seed payment failed: ${seedResp.result.meta.TransactionResult}`)
}
// Write setup data
const setupData = {
description: 'This file is auto-generated by sendMPTSetup.js. It stores XRPL account info and the MPT issuance ID for the send-an-mpt tutorial.',
issuer: {
address: issuer.address,
seed: issuer.seed
},
sender: {
address: sender.address,
seed: sender.seed
},
mptIssuanceID
}
fs.writeFileSync('sendMPTSetup.json', JSON.stringify(setupData, null, 2))
await client.disconnect()
}
// Allow running this file directly: `node sendMPTSetup.js`
if (process.argv[1] === fileURLToPath(import.meta.url)) {
await setup()
}