mirror of
https://github.com/XRPLF/xrpl-dev-portal.git
synced 2025-12-06 17:27:57 +00:00
Rewrite Python escrow tutorials in new format
This commit is contained in:
@@ -1,52 +0,0 @@
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowCreate
|
||||
from xrpl.transaction import submit_and_wait
|
||||
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.address,
|
||||
amount=xrp_to_drops(amount_to_escrow),
|
||||
destination=receiver_addr,
|
||||
finish_after=claim_date,
|
||||
cancel_after=expiry_date,
|
||||
condition=condition # Omit this for time-held escrows
|
||||
)
|
||||
|
||||
# Autofill, sign, then submit transaction and wait for result
|
||||
stxn_response = submit_and_wait(create_txn, client, sender_wallet)
|
||||
|
||||
# Return result of transaction
|
||||
stxn_result = stxn_response.result
|
||||
|
||||
|
||||
# Parse result and print out the neccesary info
|
||||
print(stxn_result["tx_json"]["Account"])
|
||||
print(stxn_result["tx_json"]["Sequence"])
|
||||
|
||||
print(stxn_result["meta"]["TransactionResult"])
|
||||
print(stxn_result["hash"])
|
||||
@@ -1,2 +1,2 @@
|
||||
xrpl-py>=3.0.0
|
||||
cryptoconditions
|
||||
cryptoconditions==0.8.1
|
||||
|
||||
74
_code-samples/escrow/py/send_conditional_escrow.py
Normal file
74
_code-samples/escrow/py/send_conditional_escrow.py
Normal file
@@ -0,0 +1,74 @@
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from os import urandom
|
||||
|
||||
from cryptoconditions import PreimageSha256
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowCreate, EscrowFinish
|
||||
from xrpl.transaction import submit_and_wait
|
||||
from xrpl.utils import datetime_to_ripple_time
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
# Set up client and get a wallet
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
print("Funding new wallet from faucet...")
|
||||
wallet = generate_faucet_wallet(client, debug=True)
|
||||
destination_address = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Testnet faucet
|
||||
# Alternative: Get another account to send the escrow to. Use this if you get
|
||||
# a tecDIR_FULL error trying to create escrows to the Testnet faucet.
|
||||
# destination_address = generate_faucet_wallet(client, debug=True).address
|
||||
|
||||
# Create the crypto-condition for release -----------------------------------
|
||||
preimage = urandom(32)
|
||||
fulfillment = PreimageSha256(preimage=preimage)
|
||||
condition_hex = fulfillment.condition_binary.hex().upper()
|
||||
fulfillment_hex = fulfillment.serialize_binary().hex().upper()
|
||||
print("Condition:", condition_hex)
|
||||
print("Fulfillment:", fulfillment_hex)
|
||||
|
||||
# Set the escrow expiration -------------------------------------------------
|
||||
cancel_delay = 300
|
||||
cancel_after = datetime.now() + timedelta(seconds=cancel_delay)
|
||||
print("This escrow will expire after", cancel_after)
|
||||
cancel_after_rippletime = datetime_to_ripple_time(cancel_after)
|
||||
|
||||
# Send EscrowCreate transaction ---------------------------------------------
|
||||
escrow_create = EscrowCreate(
|
||||
account=wallet.address,
|
||||
destination=destination_address,
|
||||
amount="123456", # drops of XRP
|
||||
condition=condition_hex,
|
||||
cancel_after=cancel_after_rippletime
|
||||
)
|
||||
|
||||
print("Signing and submitting the EscrowCreate transaction.")
|
||||
response = submit_and_wait(escrow_create, client, wallet, autofill=True)
|
||||
print(json.dumps(response.result, indent=2))
|
||||
|
||||
# Check result of submitting ------------------------------------------------
|
||||
result_code = response.result["meta"]["TransactionResult"]
|
||||
if result_code != "tesSUCCESS":
|
||||
print(f"EscrowCreate failed with result code {result_code}")
|
||||
exit(1)
|
||||
|
||||
# Save the sequence number so you can identify the escrow later
|
||||
escrow_seq = response.result["tx_json"]["Sequence"]
|
||||
|
||||
# Send the EscrowFinish transaction -----------------------------------------
|
||||
escrow_finish = EscrowFinish(
|
||||
account=wallet.address,
|
||||
owner=wallet.address,
|
||||
offer_sequence=escrow_seq,
|
||||
condition=condition_hex,
|
||||
fulfillment=fulfillment_hex
|
||||
)
|
||||
print("Signing and submitting the EscrowFinish transaction.")
|
||||
response2 = submit_and_wait(escrow_finish, client, wallet, autofill=True)
|
||||
print(json.dumps(response2.result, indent=2))
|
||||
|
||||
result_code = response2.result["meta"]["TransactionResult"]
|
||||
if result_code != "tesSUCCESS":
|
||||
print(f"EscrowFinish failed with result code {result_code}")
|
||||
exit(1)
|
||||
|
||||
print("Escrow finished successfully.")
|
||||
86
_code-samples/escrow/py/send_timed_escrow.py
Normal file
86
_code-samples/escrow/py/send_timed_escrow.py
Normal file
@@ -0,0 +1,86 @@
|
||||
import json
|
||||
from datetime import datetime, timedelta
|
||||
from time import sleep
|
||||
from os import urandom
|
||||
|
||||
from xrpl.clients import JsonRpcClient
|
||||
from xrpl.models import EscrowCreate, EscrowFinish
|
||||
from xrpl.models.requests import Ledger
|
||||
from xrpl.transaction import submit_and_wait
|
||||
from xrpl.utils import datetime_to_ripple_time, ripple_time_to_datetime
|
||||
from xrpl.wallet import generate_faucet_wallet
|
||||
|
||||
# Set up client and get a wallet
|
||||
client = JsonRpcClient("https://s.altnet.rippletest.net:51234")
|
||||
print("Funding new wallet from faucet...")
|
||||
wallet = generate_faucet_wallet(client, debug=True)
|
||||
# destination_address = "rPT1Sjq2YGrBMTttX4GZHjKu9dyfzbpAYe" # Testnet faucet
|
||||
# Alternative: Get another account to send the escrow to. Use this if you get
|
||||
# a tecDIR_FULL error trying to create escrows to the Testnet faucet.
|
||||
destination_address = generate_faucet_wallet(client, debug=True).address
|
||||
|
||||
# Set the escrow finish time ------------------------------------------------
|
||||
delay = 30
|
||||
finish_after = datetime.now() + timedelta(seconds=delay)
|
||||
print("This escrow will expire after", finish_after)
|
||||
finish_after_rippletime = datetime_to_ripple_time(finish_after)
|
||||
|
||||
# Send EscrowCreate transaction ---------------------------------------------
|
||||
escrow_create = EscrowCreate(
|
||||
account=wallet.address,
|
||||
destination=destination_address,
|
||||
amount="123456", # drops of XRP
|
||||
finish_after=finish_after_rippletime
|
||||
)
|
||||
|
||||
print("Signing and submitting the EscrowCreate transaction.")
|
||||
response = submit_and_wait(escrow_create, client, wallet, autofill=True)
|
||||
print(json.dumps(response.result, indent=2))
|
||||
|
||||
# Check result of submitting ------------------------------------------------
|
||||
result_code = response.result["meta"]["TransactionResult"]
|
||||
if result_code != "tesSUCCESS":
|
||||
print(f"EscrowCreate failed with result code {result_code}")
|
||||
exit(1)
|
||||
|
||||
# Save the sequence number so you can identify the escrow later
|
||||
escrow_seq = response.result["tx_json"]["Sequence"]
|
||||
|
||||
# Wait for the escrow to be finishable --------------------------------------
|
||||
sleep(delay)
|
||||
|
||||
# Check if escrow can be finished -------------------------------------------
|
||||
escrow_ready = False
|
||||
while not escrow_ready:
|
||||
validated_ledger = client.request(Ledger(ledger_index="validated"))
|
||||
ledger_close_time = validated_ledger.result["ledger"]["close_time"]
|
||||
print("Latest validated ledger closed at",
|
||||
ripple_time_to_datetime(ledger_close_time)
|
||||
)
|
||||
if ledger_close_time > finish_after_rippletime:
|
||||
escrow_ready = True
|
||||
print("Escrow is mature.")
|
||||
else:
|
||||
time_difference = finish_after_rippletime - ledger_close_time
|
||||
if time_difference == 0:
|
||||
time_difference = 1
|
||||
print(f"Waiting another {time_difference} seconds.")
|
||||
sleep(time_difference)
|
||||
|
||||
|
||||
# Send the EscrowFinish transaction -----------------------------------------
|
||||
escrow_finish = EscrowFinish(
|
||||
account=wallet.address,
|
||||
owner=wallet.address,
|
||||
offer_sequence=escrow_seq
|
||||
)
|
||||
print("Signing and submitting the EscrowFinish transaction.")
|
||||
response2 = submit_and_wait(escrow_finish, client, wallet, autofill=True)
|
||||
print(json.dumps(response2.result, indent=2))
|
||||
|
||||
result_code = response2.result["meta"]["TransactionResult"]
|
||||
if result_code != "tesSUCCESS":
|
||||
print(f"EscrowFinish failed with result code {result_code}")
|
||||
exit(1)
|
||||
|
||||
print("Escrow finished successfully.")
|
||||
Reference in New Issue
Block a user