|
| 1 | +import json |
| 2 | +from xrpl.utils import str_to_hex, hex_to_str |
| 3 | +from xrpl.clients import JsonRpcClient |
| 4 | +from xrpl.wallet import generate_faucet_wallet |
| 5 | +from xrpl.transaction import submit_and_wait |
| 6 | +from xrpl.models import LedgerEntry, MPTokenIssuanceCreate, MPTokenIssuanceCreateFlag |
| 7 | + |
| 8 | +# Set up client and get a wallet |
| 9 | +client = JsonRpcClient("https://s.devnet.rippletest.net:51234") |
| 10 | +print("Funding new wallet from faucet...") |
| 11 | +wallet = generate_faucet_wallet(client, debug=True) |
| 12 | + |
| 13 | +# Define metadata as JSON |
| 14 | +mpt_metadata = { |
| 15 | + "ticker": "TBILL", |
| 16 | + "name": "T-Bill Yield Token", |
| 17 | + "desc": "A yield-bearing stablecoin backed by short-term U.S. Treasuries and money market instruments.", |
| 18 | + "icon": "https://example.org/tbill-icon.png", |
| 19 | + "asset_class": "rwa", |
| 20 | + "asset_subclass": "treasury", |
| 21 | + "issuer_name": "Example Yield Co.", |
| 22 | + "urls": [ |
| 23 | + { |
| 24 | + "url": "https://exampleyield.co/tbill", |
| 25 | + "type": "website", |
| 26 | + "title": "Product Page" |
| 27 | + }, |
| 28 | + { |
| 29 | + "url": "https://exampleyield.co/docs", |
| 30 | + "type": "docs", |
| 31 | + "title": "Yield Token Docs" |
| 32 | + } |
| 33 | + ], |
| 34 | + "additional_info": { |
| 35 | + "interest_rate": "5.00%", |
| 36 | + "interest_type": "variable", |
| 37 | + "yield_source": "U.S. Treasury Bills", |
| 38 | + "maturity_date": "2045-06-30", |
| 39 | + "cusip": "912796RX0" |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +# Convert JSON to a string (without excess whitespace), then string to hex |
| 44 | +mpt_metadata_string = json.dumps(mpt_metadata, separators=(',', ':')) |
| 45 | +mpt_metadata_hex = str_to_hex(mpt_metadata_string) |
| 46 | + |
| 47 | +# Define the transaction, including other MPT parameters |
| 48 | +mpt_issuance_create = MPTokenIssuanceCreate( |
| 49 | + account=wallet.address, |
| 50 | + asset_scale=4, |
| 51 | + maximum_amount="50000000", |
| 52 | + transfer_fee=0, |
| 53 | + flags=MPTokenIssuanceCreateFlag.TF_MPT_CAN_TRANSFER | |
| 54 | + MPTokenIssuanceCreateFlag.TF_MPT_CAN_TRADE, |
| 55 | + mptoken_metadata=mpt_metadata_hex |
| 56 | +) |
| 57 | + |
| 58 | +# Prepare, sign, and submit the transaction |
| 59 | +print("Sending MPTokenIssuanceCreate transaction...") |
| 60 | +response = submit_and_wait(mpt_issuance_create, client, wallet, autofill=True) |
| 61 | +print(json.dumps(response.result, indent=2)) |
| 62 | + |
| 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