Skip to content

Commit 01f835c

Browse files
committed
MPT metadata sample code: add lookup/decoding to example
1 parent 6c16fa1 commit 01f835c

File tree

2 files changed

+46
-12
lines changed

2 files changed

+46
-12
lines changed

_code-samples/issue-mpt-with-metadata/js/issue-mpt-with-metadata.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { stringToHex } from '@xrplf/isomorphic/dist/utils/index.js'
1+
import { stringToHex, hexToString } from '@xrplf/isomorphic/dist/utils/index.js'
22
import { MPTokenIssuanceCreateFlags, Client } from 'xrpl'
33

44
// Connect to network and get a wallet
@@ -55,13 +55,31 @@ const mpt_issuance_create = {
5555

5656
// Prepare, sign, and submit the transaction
5757
console.log('Sending MPTokenIssuanceCreate transaction...')
58-
const result = await client.submitAndWait(mpt_issuance_create, { wallet, autofill: true })
58+
const submit_response = await client.submitAndWait(mpt_issuance_create, { wallet, autofill: true })
5959

6060
// Check transaction results and disconnect
61-
console.log(JSON.stringify(result, null, 2))
62-
if (result.result.meta.TransactionResult === 'tesSUCCESS') {
63-
const issuance_id = result.result.meta.mpt_issuance_id
64-
console.log(`MPToken created successfully with issuance ID ${issuance_id}.`)
61+
console.log(JSON.stringify(submit_response, null, 2))
62+
if (submit_response.result.meta.TransactionResult !== 'tesSUCCESS') {
63+
const result_code = response.result.meta.TransactionResult
64+
console.warn(`Transaction failed with result code ${result_code}.`)
65+
process.exit(1)
6566
}
6667

68+
const issuance_id = submit_response.result.meta.mpt_issuance_id
69+
console.log(`MPToken created successfully with issuance ID ${issuance_id}.`)
70+
71+
// Look up MPT Issuance entry in the validated ledger
72+
console.log('Confirming MPT Issuance metadata in the validated ledger.')
73+
const ledger_entry_response = await client.request({
74+
"command": "ledger_entry",
75+
"mpt_issuance": issuance_id,
76+
"ledger_index": "validated"
77+
})
78+
79+
// Decode the metadata
80+
const metadata_blob = ledger_entry_response.result.node.MPTokenMetadata
81+
const decoded_metadata = JSON.parse(hexToString(metadata_blob))
82+
console.log('Decoded metadata:', decoded_metadata)
83+
84+
6785
client.disconnect()

_code-samples/issue-mpt-with-metadata/py/issue-mpt-with-metadata.py

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import json
2-
from xrpl.utils import str_to_hex
2+
from xrpl.utils import str_to_hex, hex_to_str
33
from xrpl.clients import JsonRpcClient
44
from xrpl.wallet import generate_faucet_wallet
55
from xrpl.transaction import submit_and_wait
6-
from xrpl.models.transactions import MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag
6+
from xrpl.models import LedgerEntry, MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag
77

88
# Set up client and get a wallet
99
client = JsonRpcClient("https://s.devnet.rippletest.net:51234")
@@ -60,7 +60,23 @@
6060
response = submit_and_wait(mpt_issuance_create, client, wallet, autofill=True)
6161
print(json.dumps(response.result, indent=2))
6262

63-
# Check transaction results and disconnect
64-
if response.result["meta"]["TransactionResult"] == "tesSUCCESS":
65-
issuance_id = response.result["meta"]["mpt_issuance_id"]
66-
print(f"MPToken successfully created with issuance ID {issuance_id}")
63+
# Check transaction results
64+
result_code = response.result["meta"]["TransactionResult"]
65+
if result_code != "tesSUCCESS":
66+
print(f"Transaction failed with result code {result_code}")
67+
exit(1)
68+
69+
issuance_id = response.result["meta"]["mpt_issuance_id"]
70+
print(f"MPToken successfully created with issuance ID {issuance_id}")
71+
72+
# Look up MPT Issuance entry in the validated ledger
73+
print("Confirming MPT Issuance metadata in the validated ledger.")
74+
ledger_entry_response = client.request(LedgerEntry(
75+
mpt_issuance=issuance_id,
76+
ledger_index="validated"
77+
))
78+
79+
# Decode the metadata
80+
metadata_blob = ledger_entry_response.result["node"]["MPTokenMetadata"]
81+
decoded_metadata = json.loads(hex_to_str(metadata_blob))
82+
print("Decoded metadata:", decoded_metadata)

0 commit comments

Comments
 (0)