# Create Time-based Escrows Using Python This example shows how to: 1. Create escrow payments that become available at a specified time and expire at a specified time. 2. Finish an escrow payment. 3. Retrieve information on escrows attached to an account. 4. Cancel an escrow payment and return the XRP to the sending account. [![Escrow Tester Form](/assets/quickstart-py-escrow1.c09f970c8d52041d54c1ae367a50bd5f464af2f8f2acd64d12d4eb6908048501.ac57e6ef.png)](/assets/quickstart-py-escrow1.c09f970c8d52041d54c1ae367a50bd5f464af2f8f2acd64d12d4eb6908048501.ac57e6ef.png) ## Prerequisites Download the [Python Modular Code Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/_code-samples/quickstart/py/). ## Usage To get test accounts: 1. Open `lesson8-time-escrow.py.` 2. Get test accounts. 1. If you have existing account seeds 1. Paste Standby account seed in the **Standby Seed** field. 2. Click **Get Standby Account**. 3. Click **Get Standby Account Info**. 4. Paste Operational account seed in the **Operational Seed** field. 5. Click **Get Operational Account**. 6. Click **Get Op Account Info**. 2. If you do not have account seeds: 1. Click **Get Standby Account**. 2. Click **Get Standby Account Info**. 3. Click **Get Operational Account**. 4. Click **Get Op Account Info**. [![Escrow Tester with Account Information](/assets/quickstart-py-escrow2.2f2b8a00c8bbfc8d411d82e2feb33f38aa940e79cfd17370ec92a13b166c1adb.ac57e6ef.png)](/assets/quickstart-py-escrow2.2f2b8a00c8bbfc8d411d82e2feb33f38aa940e79cfd17370ec92a13b166c1adb.ac57e6ef.png) ## Create Escrow div iframe You can create a time-based escrow with a minimum time to finish the escrow and a cancel time after which the funds in escrow are no longer available to the recipient. This is a test harness: while a practical scenario might express time in days or weeks, this form lets you set the finish and cancel times in seconds so that you can quickly run through a variety of scenarios. (There are 86,400 seconds in a day, if you want to play with longer term escrows.) To create a time-based escrow: 1. Enter an **Amount** to transfer. For example, *100000000*. 2. Copy the **Operational Account** value. 3. Paste it in the **Destination Account** field. 4. Set the **Escrow Finish (seconds)** value. For example, enter *10*. 5. Set the **Escrow Cancel (seconds)** value. For example, enter *120*. 6. Click **Create Time-based Escrow**. 7. Copy the *Sequence Number* of the escrow called out in the **Standby Result** field. The escrow is created on the XRP Ledger instance, reserving 100 XRP plus the transaction cost and reserve requirements. When you create an escrow, capture and save the **Sequence Number** so that you can use it to finish the escrow transaction. [![Completed Create Escrow Transaction](/assets/quickstart-py-escrow3.4a744d08c738abdbdd51eaf7e2282de543d2da2300c9f48730c74ca99f0bd90b.ac57e6ef.png)](/assets/quickstart-py-escrow3.4a744d08c738abdbdd51eaf7e2282de543d2da2300c9f48730c74ca99f0bd90b.ac57e6ef.png) ## Finish Escrow The recipient of the XRP held in escrow can finish the transaction any time within the time window after the Escrow Finish date and time but before the Escrow Cancel date and time. Following on the example above, you can use the *Sequence Number* to finish the transaction once 10 seconds have passed. To finish a time-based escrow: 1. Paste the sequence number in the Operational account **Sequence Number** field. 2. Click **Finish Escrow**. 3. Click **Get Op Account Info** and **Get Standby Account Info** to update their **XRP Balance**. The transaction completes and balances are updated for both the Standby and Operational accounts. [![Completed Escrow Transaction](/assets/quickstart-py-escrow4.b718dd422c4329ca8132fa844589ddd9f473842082fb68fa7f2ef0a23787a64a.ac57e6ef.png)](/assets/quickstart-py-escrow4.b718dd422c4329ca8132fa844589ddd9f473842082fb68fa7f2ef0a23787a64a.ac57e6ef.png) ## Get Escrows Click **Get Escrows** to see the current list of escrows for the Operational account. If you click the button now, there are no escrows at the moment. For the purposes of this tutorial, you can follow the steps in [Create Escrow](#create-escrow), above, to create a new escrow transaction that you can then look up. Remember to capture the *Sequence Number* from the transaction results. [![Get Escrows results](/assets/quickstart-py-escrow5.560775fd940fbee1580e031860a5fe053e7a547819e57bec25e1303e89fd09fd.ac57e6ef.png)](/assets/quickstart-py-escrow5.560775fd940fbee1580e031860a5fe053e7a547819e57bec25e1303e89fd09fd.ac57e6ef.png) ## Cancel Escrow When the Escrow Cancel time passes, the escrow is no longer available to the recipient. The initiator of the escrow can reclaim the XRP. If you try to cancel the transaction prior to the **Escrow Cancel** time, you are charged for the transaction, but the actual escrow cannot be cancelled until the time limit is reached. You can wait the allotted time for the escrow you created in the previous step, then use it to try out the **Cancel Escrow** button To cancel an expired escrow: 1. Enter the sequence number in the Standby **Sequence Number** field. 2. Copy the **Standby Account** value and paste it in the **Escrow Owner** field. 3. Click **Cancel Time-based Escrow**. The funds are returned to the Standby account, less the initial transaction fee. [![Cancel Escrow results](/assets/quickstart-py-escrow6.004f5adecc53325db5f38d33fef84d90c0e8914e61cd17354fe67950a6596247.ac57e6ef.png)](/assets/quickstart-py-escrow6.004f5adecc53325db5f38d33fef84d90c0e8914e61cd17354fe67950a6596247.ac57e6ef.png) ## Oh No! I Forgot to Save the Sequence Number! If you forget to save the sequence number, you can find it in the escrow transaction record. 1. Create a new escrow as described in [Create Escrow](#create-escrow), above. 2. Click **Get Escrows** to get the escrow information. 3. Copy the *PreviousTxnLgrSeq* value from the results. ![Transaction ID in Get Escrows results](/assets/quickstart-py-escrow7.a2236a4513daea643cc47cd52f4520a12d6b26ea2966d8ca206674568cb7fad5.ac57e6ef.png) 4. Paste the *PreviousTxnLgrSeq* in the **Transaction to Look Up** field. 5. Click **Get Transaction**. 6. Locate the *Sequence* value in the results. ![Sequence number in results](/assets/quickstart-py-escrow8.cdcd89126523badc05288689cd5a631cea1d4a00358d386dfe151c244ad87420.ac57e6ef.png) # Code Walkthrough You can download the [Python Modular Code Samples](https://github.com/XRPLF/xrpl-dev-portal/tree/master/_code-samples/quickstart/py/)in the source repository for this website. ## mod8.py This example can be used with the XRP Ledger network, *Testnet*. You can update the code to choose different or additional XRP Ledger networks. ### add_seconds This function accomplishes two things. It creates a new date object and adds the number of seconds taken from a form field. Then, it adjusts the date from the Python format to the XRP Ledger format. Provide the *numOfSeconds* argument. ```python def add_seconds(numOfSeconds): ``` Create a new Python date object. ```python new_date = datetime.now() ``` Convert the date variable for the Ripple epoch. ```python if new_date != '': new_date = xrpl.utils.datetime_to_ripple_time(new_date) ``` Add your seconds to the date. ```python new_date = new_date + int(numOfSeconds) ``` Return the resulting date value. ```python return new_date ``` ### create_time_escrow Call the create_time_escrow function, passing the *seed*, *amount*, *destination*, *finish* interval, and *cancel* interval. ```python def create_time_escrow(seed, amount, destination, finish, cancel): ``` Get the client wallet. ```python wallet=Wallet.from_seed(seed) ``` Connect to Testnet. ```python client=JsonRpcClient(testnet_url) ``` Create variables for the finish and cancel dates using the add_seconds function. ```python finish_date = add_seconds(finish) cancel_date = add_seconds(cancel) ``` Define the **EscrowCreate** transaction. ```python escrow_tx=xrpl.models.transactions.EscrowCreate( account=wallet.address, amount=amount, destination=destination, finish_after=finish_date, cancel_after=cancel_date ) ``` Submit the transaction and report the results. ```python reply="" try: response=xrpl.transaction.submit_and_wait(escrow_tx,client,wallet) reply=response.result except xrpl.transaction.XRPLReliableSubmissionException as e: reply=f"Submit failed: {e}" return reply ``` ### finish_time_escrow Pass the operational account *seed*, escrow *owner* (in these examples, the standby address), and the escrow *sequence* number. ```python def finish_time_escrow(seed, owner, sequence): ``` Instantiate the wallet and client. ```python wallet=Wallet.from_seed(seed) client=JsonRpcClient(testnet_url) ``` Define the **EscrowFinish** transaction. ```python finish_tx=xrpl.models.transactions.EscrowFinish( account=wallet.address, owner=owner, offer_sequence=int(sequence) ) ``` Submit the transaction and report the results. ```python reply="" try: response=xrpl.transaction.submit_and_wait(finish_tx,client,wallet) reply=response.result except xrpl.transaction.XRPLReliableSubmissionException as e: reply=f"Submit failed: {e}" return reply ``` ### get_escrows This request only requires the *account* argument. ```python def get_escrows(account): ``` Since this is a request, there's no need to sign in with an account to perform the query. You can just instantiate a client on Testnet. ```python client=JsonRpcClient(testnet_url) ``` Define the **AccountObjects** request, specifying objects of type *escrow*. ```python acct_escrows=AccountObjects( account=account, ledger_index="validated", type="escrow" ) ``` Submit the request and return the results. ```python response=client.request(acct_escrows) return response.result ``` ### cancel_time_escrows Pass the issuer account *seed*, the *owner* account, and the escrow *sequence* number. ```python def cancel_time_escrow(seed, owner, sequence): ``` Get the wallet and instantiate a client on *Testnet*. ```python wallet=Wallet.from_seed(seed) client=JsonRpcClient(testnet_url) ``` Define the cancel transaction ```python cancel_tx=xrpl.models.transactions.EscrowCancel( account=wallet.address, owner=owner, offer_sequence=int(sequence) ) ``` Submit the transaction and report the results ```python reply="" try: response=xrpl.transaction.submit_and_wait(cancel_tx,client,wallet) reply=response.result except xrpl.transaction.XRPLReliableSubmissionException as e: reply=f"Submit failed: {e}" return reply ``` ### get_transaction Pass the requesting account number and the previous transaction ledger sequence number. ```python def get_transaction(account, ledger_index): ``` Create a client instance. ```python client=JsonRpcClient(testnet_url) ``` Create the **AccountTx** request. ```python tx_info=AccountTx( account=account, ledger_index=int(ledger_index) ) ``` Send the request and report the results. ```python response=client.request(tx_info) return response.result ``` ## lesson8-time-escrow.py This module builds on `lesson1-send-xrp.py`. Changes are noted below. ```python import tkinter as tk import xrpl import json from mod1 import get_account, get_account_info, send_xrp ``` Import new functions from mod8.py. ```python from mod8 import create_time_escrow, finish_time_escrow, get_escrows, cancel_time_escrow, get_transaction ``` Module 8 Handlers ```python def standby_create_time_escrow(): results = create_time_escrow( ent_standby_seed.get(), ent_standby_amount.get(), ent_standby_destination.get(), ent_standby_escrow_finish.get(), ent_standby_escrow_cancel.get() ) text_standby_results.delete("1.0", tk.END) text_standby_results.insert("1.0", json.dumps(results, indent=4)) def operational_finish_time_escrow(): results = finish_time_escrow( ent_operational_seed.get(), ent_operational_escrow_owner.get(), ent_operational_sequence_number.get() ) text_operational_results.delete("1.0", tk.END) text_operational_results.insert("1.0", json.dumps(results, indent=4)) def operational_get_escrows(): results = get_escrows(ent_operational_account.get()) text_operational_results.delete("1.0", tk.END) text_operational_results.insert("1.0", json.dumps(results, indent=4)) def standby_cancel_time_escrow(): results = cancel_time_escrow( ent_standby_seed.get(), ent_standby_escrow_owner.get(), ent_standby_escrow_sequence_number.get() ) text_standby_results.delete("1.0", tk.END) text_standby_results.insert("1.0", json.dumps(results, indent=4)) def operational_get_transaction(): results = get_transaction(ent_operational_account.get(), ent_operational_look_up.get()) text_operational_results.delete("1.0", tk.END) text_operational_results.insert("1.0", json.dumps(results, indent=4)) ## Mod 1 Handlers def get_standby_account(): new_wallet = get_account(ent_standby_seed.get()) ent_standby_account.delete(0, tk.END) ent_standby_seed.delete(0, tk.END) ent_standby_account.insert(0, new_wallet.classic_address) ent_standby_seed.insert(0, new_wallet.seed) def get_standby_account_info(): accountInfo = get_account_info(ent_standby_account.get()) ent_standby_balance.delete(0, tk.END) ent_standby_balance.insert(0,accountInfo['Balance']) text_standby_results.delete("1.0", tk.END) text_standby_results.insert("1.0",json.dumps(accountInfo, indent=4)) def standby_send_xrp(): response = send_xrp(ent_standby_seed.get(),ent_standby_amount.get(), ent_standby_destination.get()) text_standby_results.delete("1.0", tk.END) text_standby_results.insert("1.0",json.dumps(response.result, indent=4)) get_standby_account_info() get_operational_account_info() def get_operational_account(): new_wallet = get_account(ent_operational_seed.get()) ent_operational_account.delete(0, tk.END) ent_operational_account.insert(0, new_wallet.classic_address) ent_operational_seed.delete(0, tk.END) ent_operational_seed.insert(0, new_wallet.seed) def get_operational_account_info(): accountInfo = get_account_info(ent_operational_account.get()) ent_operational_balance.delete(0, tk.END) ent_operational_balance.insert(0,accountInfo['Balance']) text_operational_results.delete("1.0", tk.END) text_operational_results.insert("1.0",json.dumps(accountInfo, indent=4)) def operational_send_xrp(): response = send_xrp(ent_operational_seed.get(),ent_operational_amount.get(), ent_operational_destination.get()) text_operational_results.delete("1.0", tk.END) text_operational_results.insert("1.0",json.dumps(response.result,indent=4)) get_standby_account_info() get_operational_account_info() # Create a new window with the title "Quickstart Module 1" window = tk.Tk() window.title("Time-based Escrow Example") # Form frame frm_form = tk.Frame(relief=tk.SUNKEN, borderwidth=3) frm_form.pack() # Create the Label and Entry widgets for "Standby Account" lbl_standy_seed = tk.Label(master=frm_form, text="Standby Seed") ent_standby_seed = tk.Entry(master=frm_form, width=50) lbl_standby_account = tk.Label(master=frm_form, text="Standby Account") ent_standby_account = tk.Entry(master=frm_form, width=50) lbl_standy_amount = tk.Label(master=frm_form, text="Amount") ent_standby_amount = tk.Entry(master=frm_form, width=50) lbl_standby_destination = tk.Label(master=frm_form, text="Destination") ent_standby_destination = tk.Entry(master=frm_form, width=50) lbl_standby_balance = tk.Label(master=frm_form, text="XRP Balance") ent_standby_balance = tk.Entry(master=frm_form, width=50) ``` Add supporting fields for escrow commands. ```python lbl_standby_escrow_finish = tk.Label(master=frm_form, text="Escrow Finish (seconds)") ent_standby_escrow_finish = tk.Entry(master=frm_form, width=50) lbl_standby_escrow_cancel = tk.Label(master=frm_form, text="Escrow Cancel (seconds)") ent_standby_escrow_cancel = tk.Entry(master=frm_form, width=50) lbl_standby_escrow_sequence_number = tk.Label(master=frm_form, text="Sequence Number") ent_standby_escrow_sequence_number = tk.Entry(master=frm_form, width=50) lbl_standby_escrow_owner = tk.Label(master=frm_form, text="Escrow Owner") ent_standby_escrow_owner = tk.Entry(master=frm_form, width=50) lbl_standby_results = tk.Label(master=frm_form, text="Results") text_standby_results = tk.Text(master=frm_form, height = 20, width = 65) # Place fields in a grid. lbl_standy_seed.grid(row=0, column=0, sticky="e") ent_standby_seed.grid(row=0, column=1) lbl_standby_account.grid(row=2, column=0, sticky="e") ent_standby_account.grid(row=2, column=1) lbl_standy_amount.grid(row=3, column=0, sticky="e") ent_standby_amount.grid(row=3, column=1) lbl_standby_destination.grid(row=4, column=0, sticky="e") ent_standby_destination.grid(row=4, column=1) lbl_standby_balance.grid(row=5, column=0, sticky="e") ent_standby_balance.grid(row=5, column=1) ``` Add supporting fields for escrow to the standby side of the form. ```python lbl_standby_escrow_finish.grid(row=6, column=0, sticky="e") ent_standby_escrow_finish.grid(row=6, column=1) lbl_standby_escrow_cancel.grid(row=7, column=0, sticky="e") ent_standby_escrow_cancel.grid(row=7, column=1) lbl_standby_escrow_sequence_number.grid(row=8, column=0, sticky="e") ent_standby_escrow_sequence_number.grid(row=8, column=1) lbl_standby_escrow_owner.grid(row=9, column=0, sticky="e") ent_standby_escrow_owner.grid(row=9, column=1) lbl_standby_results.grid(row=10, column=0, sticky="ne") text_standby_results.grid(row=10, column=1, sticky="nw") ############################################### ## Operational Account ######################## ############################################### # Create the Label and Entry widgets for "Operational Account" lbl_operational_seed = tk.Label(master=frm_form, text="Operational Seed") ent_operational_seed = tk.Entry(master=frm_form, width=50) lbl_operational_account = tk.Label(master=frm_form, text="Operational Account") ent_operational_account = tk.Entry(master=frm_form, width=50) lbl_operational_amount = tk.Label(master=frm_form, text="Amount") ent_operational_amount = tk.Entry(master=frm_form, width=50) lbl_operational_destination = tk.Label(master=frm_form, text="Destination") ent_operational_destination = tk.Entry(master=frm_form, width=50) lbl_operational_balance = tk.Label(master=frm_form, text="XRP Balance") ent_operational_balance = tk.Entry(master=frm_form, width=50) ``` Define escrow supporting fields for the operational side of the form. ```python lbl_operational_sequence_number = tk.Label(master=frm_form, text="Sequence Number") ent_operational_sequence_number = tk.Entry(master=frm_form, width=50) lbl_operational_escrow_owner=tk.Label(master=frm_form, text="Escrow Owner") ent_operational_escrow_owner=tk.Entry(master=frm_form, width=50) lbl_operational_look_up = tk.Label(master=frm_form, text="Transaction to Look Up") ent_operational_look_up = tk.Entry(master=frm_form, width=50) lbl_operational_results = tk.Label(master=frm_form,text='Results') text_operational_results = tk.Text(master=frm_form, height = 20, width = 65) #Place the widgets in a grid lbl_operational_seed.grid(row=0, column=4, sticky="e") ent_operational_seed.grid(row=0, column=5, sticky="w") lbl_operational_account.grid(row=2,column=4, sticky="e") ent_operational_account.grid(row=2,column=5, sticky="w") lbl_operational_amount.grid(row=3, column=4, sticky="e") ent_operational_amount.grid(row=3, column=5, sticky="w") lbl_operational_destination.grid(row=4, column=4, sticky="e") ent_operational_destination.grid(row=4, column=5, sticky="w") lbl_operational_balance.grid(row=5, column=4, sticky="e") ent_operational_balance.grid(row=5, column=5, sticky="w") ``` Add supporting fields for escrow to the operational side of the form. ```python lbl_operational_sequence_number.grid(row=6, column=4, sticky="e") ent_operational_sequence_number.grid(row=6, column=5, sticky="w") lbl_operational_escrow_owner.grid(row=7, column=4, sticky="e") ent_operational_escrow_owner.grid(row=7, column=5, sticky="w") lbl_operational_look_up.grid(row=8, column=4, sticky="e") ent_operational_look_up.grid(row=8, column=5, sticky="w") lbl_operational_results.grid(row=10, column=4, sticky="ne") text_operational_results.grid(row=10, column=5, sticky="nw") ############################################# ## Buttons ################################## ############################################# # Create the Get Standby Account Buttons btn_get_standby_account = tk.Button(master=frm_form, text="Get Standby Account", command = get_standby_account) btn_get_standby_account.grid(row = 0, column = 2, sticky = "nsew") btn_get_standby_account_info = tk.Button(master=frm_form, text="Get Standby Account Info", command = get_standby_account_info) btn_get_standby_account_info.grid(row = 1, column = 2, sticky = "nsew") btn_standby_send_xrp = tk.Button(master=frm_form, text="Send XRP >", command = standby_send_xrp) btn_standby_send_xrp.grid(row = 2, column = 2, sticky = "nsew") ``` Add buttons for escrow activity on the standby side of the form. ```python btn_standby_create_escrow = tk.Button(master=frm_form, text="Create Time-based Escrow", command = standby_create_time_escrow) btn_standby_create_escrow.grid(row = 4, column = 2, sticky="nsew") btn_standby_cancel_escrow = tk.Button(master=frm_form, text="Cancel Time-based Escrow", command = standby_cancel_time_escrow) btn_standby_cancel_escrow.grid(row=5,column = 2, sticky="nsew") # Create the Operational Account Buttons btn_get_operational_account = tk.Button(master=frm_form, text="Get Operational Account", command = get_operational_account) btn_get_operational_account.grid(row=0, column=3, sticky = "nsew") btn_get_op_account_info = tk.Button(master=frm_form, text="Get Op Account Info", command = get_operational_account_info) btn_get_op_account_info.grid(row=1, column=3, sticky = "nsew") btn_op_send_xrp = tk.Button(master=frm_form, text="< Send XRP", command = operational_send_xrp) btn_op_send_xrp.grid(row=2, column = 3, sticky = "nsew") ``` Add buttons to support escrow activity on the operational side of the form. ```python btn_op_finish_escrow = tk.Button(master=frm_form, text="Finish Escrow", command = operational_finish_time_escrow) btn_op_finish_escrow.grid(row = 4, column = 3, sticky="nsew") btn_op_finish_escrow = tk.Button(master=frm_form, text="Get Escrows", command = operational_get_escrows) btn_op_finish_escrow.grid(row = 5, column = 3, sticky="nsew") btn_op_get_transaction = tk.Button(master=frm_form, text="Get Transaction", command = operational_get_transaction) btn_op_get_transaction.grid(row = 6, column = 3, sticky = "nsew") # Start the application window.mainloop() ```