diff --git a/_code-samples/escrow/py/create_escrow.py b/_code-samples/escrow/py/create_escrow.py deleted file mode 100644 index ecd75df9a2..0000000000 --- a/_code-samples/escrow/py/create_escrow.py +++ /dev/null @@ -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"]) diff --git a/_code-samples/escrow/py/requirements.txt b/_code-samples/escrow/py/requirements.txt index 617e07eb84..cbb2826eb7 100644 --- a/_code-samples/escrow/py/requirements.txt +++ b/_code-samples/escrow/py/requirements.txt @@ -1,2 +1,2 @@ xrpl-py>=3.0.0 -cryptoconditions +cryptoconditions==0.8.1 diff --git a/_code-samples/escrow/py/send_conditional_escrow.py b/_code-samples/escrow/py/send_conditional_escrow.py new file mode 100644 index 0000000000..8e33387c69 --- /dev/null +++ b/_code-samples/escrow/py/send_conditional_escrow.py @@ -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.") diff --git a/_code-samples/escrow/py/send_timed_escrow.py b/_code-samples/escrow/py/send_timed_escrow.py new file mode 100644 index 0000000000..2d164f2f58 --- /dev/null +++ b/_code-samples/escrow/py/send_timed_escrow.py @@ -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.")