mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-11-04 11:55:50 +00:00
Merge pull request #1492 from ObiajuluM/master
Added Python code samples for escrows
This commit is contained in:
49
content/_code-samples/escrow/py/account_escrows.py
Normal file
49
content/_code-samples/escrow/py/account_escrows.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import AccountObjects
|
||||
from xrpl.utils import drops_to_xrp, ripple_time_to_datetime
|
||||
|
||||
# Retreive all escrows created or received by an account, formatted
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
|
||||
|
||||
account_address = "r9CEVt4Cmcjt68ME6GKyhf2DyEGo2rG8AW"
|
||||
|
||||
all_escrows_dict = {}
|
||||
sent_escrows = []
|
||||
received_escrows = []
|
||||
|
||||
# Build and make request
|
||||
req = AccountObjects(account=account_address, ledger_index="validated", type="escrow")
|
||||
response = client.request(req)
|
||||
|
||||
# Return account escrows
|
||||
escrows = response.result["account_objects"]
|
||||
|
||||
# Loop through result and parse account escrows
|
||||
for escrow in escrows:
|
||||
escrow_data = {}
|
||||
if isinstance(escrow["Amount"], str):
|
||||
escrow_data["escrow_id"] = escrow["index"]
|
||||
escrow_data["sender"] = escrow["Account"]
|
||||
escrow_data["receiver"] = escrow["Destination"]
|
||||
escrow_data["amount"] = str(drops_to_xrp(escrow["Amount"]))
|
||||
if "PreviousTxnID" in escrow:
|
||||
escrow_data["prex_txn_id"] = escrow["PreviousTxnID"]
|
||||
if "FinishAfter" in escrow:
|
||||
escrow_data["redeem_date"] = str(ripple_time_to_datetime(escrow["FinishAfter"]))
|
||||
if "CancelAfter" in escrow:
|
||||
escrow_data["expiry_date"] = str(ripple_time_to_datetime(escrow["CancelAfter"]))
|
||||
if "Condition" in escrow:
|
||||
escrow_data["condition"] = escrow["Condition"]
|
||||
|
||||
# Sort escrows
|
||||
if escrow_data["sender"] == account_address:
|
||||
sent_escrows.append(escrow_data)
|
||||
else:
|
||||
received_escrows.append(escrow_data)
|
||||
|
||||
# Add lists to escrow dict
|
||||
all_escrows_dict["sent"] = sent_escrows
|
||||
all_escrows_dict["received"] = received_escrows
|
||||
|
||||
print(all_escrows_dict)
|
||||
29
content/_code-samples/escrow/py/cancel_escrow.py
Normal file
29
content/_code-samples/escrow/py/cancel_escrow.py
Normal file
@@ -0,0 +1,29 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowCancel
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to the testnetwork
|
||||
|
||||
# Cancel an escrow
|
||||
# An Escrow can only be canceled if it was created with a CancelAfter time
|
||||
|
||||
escrow_sequence = 30215126
|
||||
|
||||
# Sender wallet object
|
||||
sender_wallet = generate_faucet_wallet(client=client)
|
||||
|
||||
# Build escrow cancel transaction
|
||||
cancel_txn = EscrowCancel(account=sender_wallet.classic_address, owner=sender_wallet.classic_address, offer_sequence=escrow_sequence)
|
||||
|
||||
# Sign and submit transaction
|
||||
stxn = safe_sign_and_autofill_transaction(cancel_txn, sender_wallet, client)
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# 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"])
|
||||
54
content/_code-samples/escrow/py/create_escrow.py
Normal file
54
content/_code-samples/escrow/py/create_escrow.py
Normal file
@@ -0,0 +1,54 @@
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowCreate
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.utils import datetime_to_ripple_time, xrp_to_drops
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
# Create Escrow
|
||||
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234") # Connect to client
|
||||
|
||||
amount_to_escrow = 10.000
|
||||
|
||||
receiver_addr = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Example: send back to Testnet Faucet
|
||||
|
||||
# Escrow will be available to claim after 3 days
|
||||
claim_date = datetime_to_ripple_time(datetime.now() + timedelta(days=3))
|
||||
|
||||
# Escrow will expire after 5 days
|
||||
expiry_date = datetime_to_ripple_time(datetime.now() + timedelta(days=5))
|
||||
|
||||
# Optional field
|
||||
# You can optionally use a Crypto Condition to allow for dynamic release of funds. For example:
|
||||
condition = "A02580205A0E9E4018BE1A6E0F51D39B483122EFDF1DDEF3A4BE83BE71522F9E8CDAB179810120" # do not use in production
|
||||
|
||||
# sender wallet object
|
||||
sender_wallet = generate_faucet_wallet(client=client)
|
||||
|
||||
# Build escrow create transaction
|
||||
create_txn = EscrowCreate(
|
||||
account=sender_wallet.classic_address,
|
||||
amount=xrp_to_drops(amount_to_escrow),
|
||||
destination=receiver_addr,
|
||||
finish_after=claim_date,
|
||||
cancel_after=expiry_date,
|
||||
condition=condition)
|
||||
|
||||
# Sign and send transaction
|
||||
stxn = safe_sign_and_autofill_transaction(create_txn, sender_wallet, client)
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# Return result of transaction
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
|
||||
# Parse result and print out the neccesary info
|
||||
print(stxn_result["Account"])
|
||||
print(stxn_result["Sequence"])
|
||||
|
||||
print(stxn_result["meta"]["TransactionResult"])
|
||||
print(stxn_result["hash"])
|
||||
42
content/_code-samples/escrow/py/finish_escrow.py
Normal file
42
content/_code-samples/escrow/py/finish_escrow.py
Normal file
@@ -0,0 +1,42 @@
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowFinish
|
||||
from xrpl.transaction import (safe_sign_and_autofill_transaction,
|
||||
send_reliable_submission)
|
||||
from xrpl.wallet import Wallet, 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).classic_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.classic_address, owner=escrow_creator, offer_sequence=escrow_sequence, condition=condition, fulfillment=fulfillment)
|
||||
|
||||
# Sign transaction with wallet
|
||||
stxn = safe_sign_and_autofill_transaction(finish_txn, sender_wallet, client)
|
||||
|
||||
# Send transaction and wait for response
|
||||
stxn_response = send_reliable_submission(stxn, client)
|
||||
|
||||
# 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"])
|
||||
19
content/_code-samples/escrow/py/generate_condition.py
Normal file
19
content/_code-samples/escrow/py/generate_condition.py
Normal file
@@ -0,0 +1,19 @@
|
||||
import random
|
||||
from os import urandom
|
||||
|
||||
from cryptoconditions import PreimageSha256
|
||||
|
||||
# """Generate a condition and fulfillment for escrows"""
|
||||
|
||||
# Generate a random preimage with at least 32 bytes of cryptographically-secure randomness.
|
||||
secret = urandom(32)
|
||||
|
||||
# Generate cryptic image from secret
|
||||
fufill = PreimageSha256(preimage=secret)
|
||||
|
||||
# Parse image and return the condition and fulfillment
|
||||
condition = str.upper(fufill.condition_binary.hex()) # conditon
|
||||
fulfillment = str.upper(fufill.serialize_binary().hex()) # fulfillment
|
||||
|
||||
# Print condition and fulfillment
|
||||
print(f"condition: {condition} \n fulfillment {fulfillment}")
|
||||
2
content/_code-samples/escrow/py/requirements.txt
Normal file
2
content/_code-samples/escrow/py/requirements.txt
Normal file
@@ -0,0 +1,2 @@
|
||||
xrpl-py
|
||||
cryptoconditions
|
||||
24
content/_code-samples/escrow/py/return_escrow_sequence.py
Normal file
24
content/_code-samples/escrow/py/return_escrow_sequence.py
Normal file
@@ -0,0 +1,24 @@
|
||||
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"]}')
|
||||
Reference in New Issue
Block a user