Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 130 additions & 3 deletions _code-samples/issue-mpt-with-metadata/js/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,135 @@ npm i
node issue-mpt-with-metadata.js
```

The script should output a validated transaction and end with a line such as the following:
The script should output a validated transaction and decoded metadata, similar to the following:

```text
MPToken created successfully with issuance ID 005073C721E14A7613BAAF5E0B1A253459832FF8D0D81278.
```sh
=== Funding new wallet from faucet...===
Issuer address: r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM

=== Encoding metadata...===
Encoded mpt_metadata_hex: 7B226163223A22727761222C226169223A7B226375736970223A22393132373936525830222C22696E7465726573745F72617465223A22352E303025222C22696E7465726573745F74797065223A227661726961626C65222C226D617475726974795F64617465223A22323034352D30362D3330222C227969656C645F736F75726365223A22552E532E2054726561737572792042696C6C73227D2C226173223A227472656173757279222C2264223A2241207969656C642D62656172696E6720737461626C65636F696E206261636B65642062792073686F72742D7465726D20552E532E205472656173757269657320616E64206D6F6E6579206D61726B657420696E737472756D656E74732E222C2269223A2268747470733A2F2F6578616D706C652E6F72672F7462696C6C2D69636F6E2E706E67222C22696E223A224578616D706C65205969656C6420436F2E222C226E223A22542D42696C6C205969656C6420546F6B656E222C2274223A225442494C4C222C227573223A5B7B2263223A2277656273697465222C2274223A2250726F647563742050616765222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F7462696C6C227D2C7B2263223A22646F6373222C2274223A225969656C6420546F6B656E20446F6373222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F646F6373227D5D7D

=== Sending MPTokenIssuanceCreate transaction...===
{
"TransactionType": "MPTokenIssuanceCreate",
"Account": "r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM",
"AssetScale": 4,
"MaximumAmount": "50000000",
"TransferFee": 0,
"Flags": 48,
"MPTokenMetadata": "7B226163223A22727761222C226169223A7B226375736970223A22393132373936525830222C22696E7465726573745F72617465223A22352E303025222C22696E7465726573745F74797065223A227661726961626C65222C226D617475726974795F64617465223A22323034352D30362D3330222C227969656C645F736F75726365223A22552E532E2054726561737572792042696C6C73227D2C226173223A227472656173757279222C2264223A2241207969656C642D62656172696E6720737461626C65636F696E206261636B65642062792073686F72742D7465726D20552E532E205472656173757269657320616E64206D6F6E6579206D61726B657420696E737472756D656E74732E222C2269223A2268747470733A2F2F6578616D706C652E6F72672F7462696C6C2D69636F6E2E706E67222C22696E223A224578616D706C65205969656C6420436F2E222C226E223A22542D42696C6C205969656C6420546F6B656E222C2274223A225442494C4C222C227573223A5B7B2263223A2277656273697465222C2274223A2250726F647563742050616765222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F7462696C6C227D2C7B2263223A22646F6373222C2274223A225969656C6420546F6B656E20446F6373222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F646F6373227D5D7D"
}

=== Checking MPTokenIssuanceCreate results... ===
{
"close_time_iso": "2025-11-20T18:13:30Z",
"ctid": "C0148E8700000002",
"hash": "555FAFDB99B239567FDF30DDF22BA3B30F8E70D8D06833B1270AC600E1575948",
"ledger_hash": "A7010A2025989778420280F7F96B10F5D3C879E049BE5DA12500FFBB90D162C5",
"ledger_index": 1347207,
"meta": {
"AffectedNodes": [
{
"CreatedNode": {
"LedgerEntryType": "DirectoryNode",
"LedgerIndex": "33468621DEF32177E84C1EBC2C457C908567E245622CBDE03185C4ABC83B7F9D",
"NewFields": {
"Owner": "r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM",
"RootIndex": "33468621DEF32177E84C1EBC2C457C908567E245622CBDE03185C4ABC83B7F9D"
}
}
},
{
"CreatedNode": {
"LedgerEntryType": "MPTokenIssuance",
"LedgerIndex": "6567EE49937AADAB4FC4D5DDBD6A4A6E179E0E5A9DF2FC7ED8B41B807F0DDBF2",
"NewFields": {
"AssetScale": 4,
"Flags": 48,
"Issuer": "r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM",
"MPTokenMetadata": "7B226163223A22727761222C226169223A7B226375736970223A22393132373936525830222C22696E7465726573745F72617465223A22352E303025222C22696E7465726573745F74797065223A227661726961626C65222C226D617475726974795F64617465223A22323034352D30362D3330222C227969656C645F736F75726365223A22552E532E2054726561737572792042696C6C73227D2C226173223A227472656173757279222C2264223A2241207969656C642D62656172696E6720737461626C65636F696E206261636B65642062792073686F72742D7465726D20552E532E205472656173757269657320616E64206D6F6E6579206D61726B657420696E737472756D656E74732E222C2269223A2268747470733A2F2F6578616D706C652E6F72672F7462696C6C2D69636F6E2E706E67222C22696E223A224578616D706C65205969656C6420436F2E222C226E223A22542D42696C6C205969656C6420546F6B656E222C2274223A225442494C4C222C227573223A5B7B2263223A2277656273697465222C2274223A2250726F647563742050616765222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F7462696C6C227D2C7B2263223A22646F6373222C2274223A225969656C6420546F6B656E20446F6373222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F646F6373227D5D7D",
"MaximumAmount": "50000000",
"Sequence": 1347205
}
}
},
{
"ModifiedNode": {
"FinalFields": {
"Account": "r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM",
"Balance": "99999999",
"Flags": 0,
"OwnerCount": 1,
"Sequence": 1347206
},
"LedgerEntryType": "AccountRoot",
"LedgerIndex": "AB5FC35110CED5BFD2CEA3E37B41E43CC4BBAF89AE66BA85942E04CBC38550FB",
"PreviousFields": {
"Balance": "100000000",
"OwnerCount": 0,
"Sequence": 1347205
},
"PreviousTxnID": "1CDF420134492607EC54838F91FA06A655E07DD296ED69CC7172C1AC356BF22B",
"PreviousTxnLgrSeq": 1347205
}
}
],
"TransactionIndex": 0,
"TransactionResult": "tesSUCCESS",
"mpt_issuance_id": "00148E8558E6AEAA301085FBFD01D615F059A7CCE6E38296"
},
"tx_json": {
"Account": "r9fhoyac7uUM9XZFDJV9wXQ4pcJb6UDpJM",
"AssetScale": 4,
"Fee": "1",
"Flags": 48,
"LastLedgerSequence": 1347225,
"MPTokenMetadata": "7B226163223A22727761222C226169223A7B226375736970223A22393132373936525830222C22696E7465726573745F72617465223A22352E303025222C22696E7465726573745F74797065223A227661726961626C65222C226D617475726974795F64617465223A22323034352D30362D3330222C227969656C645F736F75726365223A22552E532E2054726561737572792042696C6C73227D2C226173223A227472656173757279222C2264223A2241207969656C642D62656172696E6720737461626C65636F696E206261636B65642062792073686F72742D7465726D20552E532E205472656173757269657320616E64206D6F6E6579206D61726B657420696E737472756D656E74732E222C2269223A2268747470733A2F2F6578616D706C652E6F72672F7462696C6C2D69636F6E2E706E67222C22696E223A224578616D706C65205969656C6420436F2E222C226E223A22542D42696C6C205969656C6420546F6B656E222C2274223A225442494C4C222C227573223A5B7B2263223A2277656273697465222C2274223A2250726F647563742050616765222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F7462696C6C227D2C7B2263223A22646F6373222C2274223A225969656C6420546F6B656E20446F6373222C2275223A2268747470733A2F2F6578616D706C657969656C642E636F2F646F6373227D5D7D",
"MaximumAmount": "50000000",
"Sequence": 1347205,
"SigningPubKey": "ED1EC65DB85E686A55F8FD9BC6E405E8F2F8EA5E1712AED64E28C97350EB4EF6E7",
"TransactionType": "MPTokenIssuanceCreate",
"TransferFee": 0,
"TxnSignature": "3A671905D57342F051E3BF057CCF65B0D94114C04D255D4AE3CEE01C2D0B368118E94011CEB27EC9BB447D3498B24B750F2691B4D7AB71F82626BC6F49465806",
"ctid": "C0148E8700000002",
"date": 816977610,
"ledger_index": 1347207
},
"validated": true
}

- MPToken created successfully with issuance ID: 00148E8558E6AEAA301085FBFD01D615F059A7CCE6E38296
- Explorer URL: https://devnet.xrpl.org/mpt/00148E8558E6AEAA301085FBFD01D615F059A7CCE6E38296

=== Confirming MPT Issuance metadata in the validated ledger... ===
Decoded MPT metadata:
{
asset_class: 'rwa',
additional_info: {
cusip: '912796RX0',
interest_rate: '5.00%',
interest_type: 'variable',
maturity_date: '2045-06-30',
yield_source: 'U.S. Treasury Bills'
},
asset_subclass: 'treasury',
desc: 'A yield-bearing stablecoin backed by short-term U.S. Treasuries and money market instruments.',
icon: 'https://example.org/tbill-icon.png',
issuer_name: 'Example Yield Co.',
name: 'T-Bill Yield Token',
ticker: 'TBILL',
uris: [
{
category: 'website',
title: 'Product Page',
uri: 'https://exampleyield.co/tbill'
},
{
category: 'docs',
title: 'Yield Token Docs',
uri: 'https://exampleyield.co/docs'
}
]
}
```
143 changes: 83 additions & 60 deletions _code-samples/issue-mpt-with-metadata/js/issue-mpt-with-metadata.js
Original file line number Diff line number Diff line change
@@ -1,85 +1,108 @@
import { stringToHex, hexToString } from '@xrplf/isomorphic/dist/utils/index.js'
import { MPTokenIssuanceCreateFlags, Client } from 'xrpl'
import {
MPTokenIssuanceCreateFlags,
Client,
encodeMPTokenMetadata,
decodeMPTokenMetadata,
} from "xrpl";

// Connect to network and get a wallet
const client = new Client('wss://s.devnet.rippletest.net:51233')
await client.connect()
const client = new Client("wss://s.devnet.rippletest.net:51233");
await client.connect();

console.log('Funding new wallet from faucet...')
const { wallet } = await client.fundWallet()
console.log("=== Funding new wallet from faucet...===");
const { wallet: issuer } = await client.fundWallet();
console.log(`Issuer address: ${issuer.address}`);

// Define metadata as JSON
const mpt_metadata = {
t: 'TBILL',
n: 'T-Bill Yield Token',
d: 'A yield-bearing stablecoin backed by short-term U.S. Treasuries and money market instruments.',
i: 'https://example.org/tbill-icon.png',
ac: 'rwa',
as: 'treasury',
in: 'Example Yield Co.',
us: [
ticker: "TBILL",
name: "T-Bill Yield Token",
desc: "A yield-bearing stablecoin backed by short-term U.S. Treasuries and money market instruments.",
icon: "https://example.org/tbill-icon.png",
asset_class: "rwa",
asset_subclass: "treasury",
issuer_name: "Example Yield Co.",
uris: [
{
u: 'https://exampleyield.co/tbill',
c: 'website',
t: 'Product Page'
uri: "https://exampleyield.co/tbill",
category: "website",
title: "Product Page",
},
{
u: 'https://exampleyield.co/docs',
c: 'docs',
t: 'Yield Token Docs'
}
uri: "https://exampleyield.co/docs",
category: "docs",
title: "Yield Token Docs",
},
],
ai: {
interest_rate: '5.00%',
interest_type: 'variable',
yield_source: 'U.S. Treasury Bills',
maturity_date: '2045-06-30',
cusip: '912796RX0'
}
}
additional_info: {
interest_rate: "5.00%",
interest_type: "variable",
yield_source: "U.S. Treasury Bills",
maturity_date: "2045-06-30",
cusip: "912796RX0",
},
};

// Convert JSON to a string (without excess whitespace), then string to hex
const mpt_metadata_hex = stringToHex(JSON.stringify(mpt_metadata))
// Encode the metadata.
// The encodeMPTokenMetadata function converts the JSON metadata object into
// a compact, hex-encoded string, following the XLS-89 standard.
// https://xls.xrpl.org/xls/XLS-0089-multi-purpose-token-metadata-schema.html
console.log("\n=== Encoding metadata...===");
const mpt_metadata_hex = encodeMPTokenMetadata(mpt_metadata);
console.log("Encoded mpt_metadata_hex: ", mpt_metadata_hex);

// Define the transaction, including other MPT parameters
const mpt_issuance_create = {
TransactionType: 'MPTokenIssuanceCreate',
Account: wallet.address,
TransactionType: "MPTokenIssuanceCreate",
Account: issuer.address,
AssetScale: 4,
MaximumAmount: '50000000',
MaximumAmount: "50000000",
TransferFee: 0,
Flags: MPTokenIssuanceCreateFlags.tfMPTCanTransfer |
MPTokenIssuanceCreateFlags.tfMPTCanTrade,
MPTokenMetadata: mpt_metadata_hex
}
Flags:
MPTokenIssuanceCreateFlags.tfMPTCanTransfer |
MPTokenIssuanceCreateFlags.tfMPTCanTrade,
MPTokenMetadata: mpt_metadata_hex,
};

// Prepare, sign, and submit the transaction
console.log('Sending MPTokenIssuanceCreate transaction...')
const submit_response = await client.submitAndWait(mpt_issuance_create, { wallet, autofill: true })
// Sign and submit the transaction
console.log("\n=== Sending MPTokenIssuanceCreate transaction...===");
console.log(JSON.stringify(mpt_issuance_create, null, 2));
const submit_response = await client.submitAndWait(mpt_issuance_create, {
wallet: issuer,
autofill: true,
});

// Check transaction results and disconnect
console.log(JSON.stringify(submit_response, null, 2))
if (submit_response.result.meta.TransactionResult !== 'tesSUCCESS') {
const result_code = response.result.meta.TransactionResult
console.warn(`Transaction failed with result code ${result_code}.`)
process.exit(1)
// Check transaction results
console.log("\n=== Checking MPTokenIssuanceCreate results... ===");
console.log(JSON.stringify(submit_response.result, null, 2));
if (submit_response.result.meta.TransactionResult !== "tesSUCCESS") {
const result_code = submit_response.result.meta.TransactionResult;
console.warn(`Transaction failed with result code ${result_code}.`);
await client.disconnect();
process.exit(1);
}

const issuance_id = submit_response.result.meta.mpt_issuance_id
console.log(`MPToken created successfully with issuance ID ${issuance_id}.`)
const issuance_id = submit_response.result.meta.mpt_issuance_id;
console.log(
`\n- MPToken created successfully with issuance ID: ${issuance_id}`
);
// View the MPT issuance on the XRPL Explorer
console.log(`- Explorer URL: https://devnet.xrpl.org/mpt/${issuance_id}`);

// Look up MPT Issuance entry in the validated ledger
console.log('Confirming MPT Issuance metadata in the validated ledger.')
console.log("\n=== Confirming MPT Issuance metadata in the validated ledger... ===");
const ledger_entry_response = await client.request({
"command": "ledger_entry",
"mpt_issuance": issuance_id,
"ledger_index": "validated"
})

// Decode the metadata
const metadata_blob = ledger_entry_response.result.node.MPTokenMetadata
const decoded_metadata = JSON.parse(hexToString(metadata_blob))
console.log('Decoded metadata:', decoded_metadata)
command: "ledger_entry",
mpt_issuance: issuance_id,
ledger_index: "validated",
});

// Decode the metadata.
// The decodeMPTokenMetadata function takes a hex-encoded string representing MPT metadata,
// decodes it to a JSON object, and expands any compact field names to their full forms.
const metadata_blob = ledger_entry_response.result.node.MPTokenMetadata;
const decoded_metadata = decodeMPTokenMetadata(metadata_blob);
console.log("Decoded MPT metadata:\n", decoded_metadata);

client.disconnect()
// Disconnect from the client
await client.disconnect();
2 changes: 1 addition & 1 deletion _code-samples/issue-mpt-with-metadata/js/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"dependencies": {
"xrpl": "^4.4.0"
"xrpl": "^4.4.3"
},
"type": "module"
}
Loading