Update cancel escrow tutorial w/ rewritten (Python) sample code

This commit is contained in:
mDuo13
2025-12-05 16:52:40 -08:00
parent fbf3668e57
commit 22c883fd67
19 changed files with 81 additions and 565 deletions

View File

@@ -1,60 +0,0 @@
'use strict'
const xrpl = require('xrpl')
// Preqrequisites:
// 1. Create an escrow using the create-escrow.js snippet
// 2. Replace the OfferSequence with the sequence number of the escrow you created
// 3. Replace the Condition and Fulfillment with the values from the escrow you created
// 4. Paste the seed of the account that created the escrow
// 5. Run the snippet
const seed = "sEd7jfWyNG6J71dEojB3W9YdHp2KCjy"; // Test seed. Don't use
const offerSequence = null;
const condition = "";
const fulfillment = "";
const main = async () => {
try {
// Connect ----------------------------------------------------------------
const client = new xrpl.Client('wss://s.altnet.rippletest.net:51233');
await client.connect();
// Prepare wallet to sign the transaction ---------------------------------
const wallet = await xrpl.Wallet.fromSeed(seed);
console.log("Wallet Address: ", wallet.address);
console.log("Seed: ", seed);
if((!offerSequence)|| (condition === "" || fulfillment === "")){
throw new Error("Please specify the sequence number, condition and fulfillment of the escrow you created");
};
// Prepare EscrowFinish transaction ---------------------------------
const escrowFinishTransaction = {
"Account": wallet.address,
"TransactionType": "EscrowFinish",
"Owner": wallet.address,
// This should equal the sequence number of the escrow transaction
"OfferSequence": offerSequence,
// Crypto condition that must be met before escrow can be completed, passed on escrow creation.
// Omit this for time-held escrows.
"Condition": condition,
// Fulfillment of the condition, passed on escrow creation.
// Omit this for time-held escrows.
"Fulfillment": fulfillment,
};
xrpl.validate(escrowFinishTransaction);
// Sign and submit the transaction ----------------------------------------
console.log('Signing and submitting the transaction:', JSON.stringify(escrowFinishTransaction, null, "\t"));
const response = await client.submitAndWait(escrowFinishTransaction, { wallet });
console.log(`Finished submitting! ${JSON.stringify(response.result, null, "\t")}`);
await client.disconnect();
} catch (error) {
console.log(error);
}
}
main()

View File

@@ -1,9 +0,0 @@
const cc = require('five-bells-condition')
const crypto = require('crypto')
const preimageData = crypto.randomBytes(32)
const myFulfillment = new cc.PreimageSha256()
myFulfillment.setPreimage(preimageData)
console.log('Condition:', myFulfillment.getConditionBinary().toString('hex').toUpperCase())
console.log('Fulfillment:', myFulfillment.serializeBinary().toString('hex').toUpperCase())

View File

@@ -98,7 +98,10 @@ const response2 = await client.submitAndWait(escrowFinish, {
})
console.log(JSON.stringify(response2.result, null, 2))
if (response2.result.meta.TransactionResult === 'tesSUCCESS') {
console.log('Escrow finished successfully.')
console.log('Escrow finished successfully. Balance changes:')
console.log(
JSON.stringify(xrpl.getBalanceChanges(response2.result.meta), null, 2)
)
}
client.disconnect()

View File

@@ -41,7 +41,7 @@ if result_code != "tesSUCCESS":
print(f"EscrowCreate failed with result code {result_code}")
exit(1)
# Wait for the escrow to be finishable --------------------------------------
# Wait for the escrow to expire ---------------------------------------------
# Since ledger close times can be rounded by up to 10 seconds, wait an extra
# 10 seconds to make sure the escrow has officially expired.
sleep(cancel_delay + 10)
@@ -100,13 +100,16 @@ if not response.is_successful():
if response.result["tx_json"]["TransactionType"] == "EscrowCreate":
# Save this sequence number for canceling the escrow
escrow_seq = response.result["tx_json"]["Sequence"]
if escrow_seq == 0:
# This transaction used a Ticket, so use the TicketSequence instead.
escrow_seq = response.result["tx_json"]["TicketSequence"]
else:
# Currently, this is impossible since no current transaction can update
# an escrow without finishing or canceling it. But in the future, if
# that becomes possible, you would have to look at the transaction
# metadata to find the previous transaction and repeat until you found
# the transaction that created the escrow.
print("The escrow's previous transaction wasn't Create?!")
print("The escrow's previous transaction wasn't EscrowCreate!")
exit(1)
# Send EscrowCancel transaction ---------------------------------------------

View File

@@ -1,44 +0,0 @@
from xrpl.clients import JsonRpcClient
from xrpl.models import EscrowFinish
from xrpl.transaction import submit_and_wait
from xrpl.wallet import generate_faucet_wallet
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
# Complete an escrow
# Cannot be called until the finish time is reached
# Required fields (modify to match an escrow you create)
escrow_creator = generate_faucet_wallet(client=client).address
escrow_sequence = 27641268
# Optional fields
# Crypto condition that must be met before escrow can be completed, passed on escrow creation
condition = "A02580203882E2EB9B44130530541C4CC360D079F265792C4A7ED3840968897CB7DF2DA1810120"
# Crypto fulfillment of the condtion
fulfillment = "A0228020AED2C5FE4D147D310D3CFEBD9BFA81AD0F63CE1ADD92E00379DDDAF8E090E24C"
# Sender wallet object
sender_wallet = generate_faucet_wallet(client=client)
# Build escrow finish transaction
finish_txn = EscrowFinish(
account=sender_wallet.address,
owner=escrow_creator,
offer_sequence=escrow_sequence, # The sequence number of the escrow transaction
condition=condition, # Omit this for time-held escrows
fulfillment=fulfillment # Omit this for time-held escrows
)
# Autofill, sign, then submit transaction and wait for result
stxn_response = submit_and_wait(finish_txn, client, sender_wallet)
# Parse response and return result
stxn_result = stxn_response.result
# Parse result and print out the transaction result and transaction hash
print(stxn_result["meta"]["TransactionResult"])
print(stxn_result["hash"])

View File

@@ -1,24 +0,0 @@
from xrpl.clients import JsonRpcClient
from xrpl.models import Tx
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
prev_txn_id = "" # should look like this '84503EA84ADC4A65530C6CC91C904FCEE64CFE2BB973C023476184288698991F'
# Return escrow seq from `PreviousTxnID` for finishing or cancelling escrows
if prev_txn_id == "":
print("No transaction id provided. Use create_escrow.py to generate an escrow transaction, then you can look it up by modifying prev_txn_id to use that transaction's id.")
# Build and send query for PreviousTxnID
req = Tx(transaction=prev_txn_id)
response = client.request(req)
# Return the result
result = response.result
# Print escrow sequence if available
if "Sequence" in result:
print(f'escrow sequence: {result["Sequence"]}')
# Use escrow ticket sequence if escrow sequence is not available
if "TicketSequence" in result:
print(f'escrow ticket sequence: {result["TicketSequence"]}')

View File

@@ -54,7 +54,7 @@ if result_code != "tesSUCCESS":
# Save the sequence number so you can identify the escrow later
escrow_seq = response.result["tx_json"]["Sequence"]
# Send the EscrowFinish transaction -----------------------------------------
# Send EscrowFinish transaction ---------------------------------------------
escrow_finish = EscrowFinish(
account=wallet.address,
owner=wallet.address,