Python/JS code sample for Memo (#1685)

This commit is contained in:
Wo Jake
2023-02-07 22:38:03 +00:00
committed by GitHub
parent 20a6b9ff4c
commit 627b5e2730
2 changed files with 125 additions and 0 deletions

View File

@@ -0,0 +1,62 @@
if (typeof module !== "undefined") {
// Use var here because const/let are block-scoped to the if statement.
var xrpl = require('xrpl')
}
// Encode and send a Memo.
// In general, Memo's can be added to any transaction, and are sometimes used to
// share extra information needed for a 3rd party integration.
// https://xrpl.org/transaction-common-fields.html#memos-field
async function main() {
// Connect to a testnet node
console.log("Connecting to Testnet...")
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233')
await client.connect()
// Get account credentials from the Testnet Faucet
console.log("Requesting an account from the Testnet faucet...")
const { wallet, balance } = await client.fundWallet()
console.log("Account: ", wallet.address)
console.log(" Seed: ", wallet.seed)
// Enter memo data to insert into a transaction
const MemoData = xrpl.convertStringToHex(string="Example Memo - 123 -=+");
const MemoType = xrpl.convertStringToHex(string="Text");
// MemoFormat values: # MemoFormat values: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
const MemoFormat = xrpl.convertStringToHex(string="text/plain");
// Send AccountSet transaction
const prepared = await client.autofill({
"TransactionType": "Payment",
"Account": wallet.address,
"Destination": "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe",
"Amount": "1000000", // Amount in drops, 1 XRP = 1,000,000 drops
"Memos": [{
"Memo":{
"MemoType": MemoType,
"MemoData": MemoData,
"MemoFormat": MemoFormat
}
}]
})
const signed = wallet.sign(prepared)
print("Submitting a payment transaction with our memo field...")
const submit_result = await client.submitAndWait(signed.tx_blob)
xrpl.convertHexToString
const tx_MemoData = xrpl.convertHexToString(string=submit_result.result.Memos[0].Memo.MemoData);
const tx_MemoFormat = xrpl.convertHexToString(string=submit_result.result.Memos[0].Memo.MemoFormat);
const tx_MemoType = xrpl.convertHexToString(string=submit_result.result.Memos[0].Memo.MemoType);
console.log(`\n Encoded Transaction MEMO: ${JSON.stringify({"MemoType": MemoType, "MemoData": MemoData, "MemoFormat": MemoFormat})}`)
console.log(` Decoded Transaction MEMO: ${JSON.stringify({"MemoType": tx_MemoType, "MemoData": tx_MemoData, "MemoFormat": tx_MemoFormat})}`);
console.log("\n Transaction hash:", signed.hash)
console.log(" Submit result:", submit_result.result.meta.TransactionResult)
client.disconnect()
// End main()
}
main()

View File

@@ -0,0 +1,63 @@
from xrpl.clients import JsonRpcClient
from xrpl.models.transactions import Payment, Memo
from xrpl.transaction import safe_sign_and_autofill_transaction, send_reliable_submission
from xrpl.wallet import generate_faucet_wallet
# This code sample validates and sends a transaction with a Memo attached
# A funded account on the testnet is provided for testing purposes
# https://xrpl.org/transaction-common-fields.html#memos-field
# Connect to a testnet node
JSON_RPC_URL = "https://s.altnet.rippletest.net:51234/"
client = JsonRpcClient(JSON_RPC_URL)
# Get account credentials from the Testnet Faucet
print("Requesting an account from the Testnet faucet...")
test_wallet = generate_faucet_wallet(client=client)
myAddr = test_wallet.classic_address
memo_data = "Example Memo - 123 -=+"
memo_type = "Text"
memo_format = "text/plain"
memo_data = memo_data.encode('utf-8').hex()
memo_type = memo_type.encode('utf-8').hex()
# MemoFormat values: https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
memo_format = memo_format.encode('utf-8').hex()
# Construct Payment transaction w/ memo field filled
payment_tx = Payment(
account=myAddr,
destination="rjhpRPyL5FfCxkq2LXoWxEEyuz4koPeP5",
amount="1000000",
memos=[
Memo(
memo_type=memo_type,
memo_data=memo_data,
memo_format=memo_format
),
],
)
# Sign the transaction
print("Submitting a payment transaction with our memo...")
payment_tx_signed = safe_sign_and_autofill_transaction(payment_tx, wallet=test_wallet, client=client)
print(f"\n Encoded Transaction MEMO: {payment_tx_signed.memos}")
# Send the transaction to the node
submit_tx_regular = send_reliable_submission(client=client, transaction=payment_tx_signed)
submit_tx_regular = submit_tx_regular.result
tx_MemoData = bytes.fromhex(submit_tx_regular['Memos'][0]['Memo']['MemoData']).decode('utf-8')
tx_MemoFormat = bytes.fromhex(submit_tx_regular['Memos'][0]['Memo']['MemoFormat']).decode('utf-8')
tx_MemoType = bytes.fromhex(submit_tx_regular['Memos'][0]['Memo']['MemoType']).decode('utf-8')
print(f" Decoded Transaction MEMO:")
print(f" MemoData: {tx_MemoData}")
print(f" MemoFormat: {tx_MemoFormat}")
print(f" MemoType: {tx_MemoType}")
print(f"\n Payment tx w/ memo result: {submit_tx_regular['meta']['TransactionResult']}")
print(f" Tx Hash: {submit_tx_regular['hash']}")