最終更新:
編集

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

Prerequisites

Download the Python Modular Code Samples.

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

Create Escrow

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

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

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, 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

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

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, above.
  2. Click Get Escrows to get the escrow information.
  3. Copy the PreviousTxnLgrSeq value from the results. Transaction ID in Get Escrows results
  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

Code Walkthrough

You can download the Python Modular Code Samplesin 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.

def add_seconds(numOfSeconds):

Create a new Python date object.

    new_date = datetime.now()

Convert the date variable for the Ripple epoch.

    if new_date != '':
        new_date = xrpl.utils.datetime_to_ripple_time(new_date)

Add your seconds to the date.

        new_date = new_date + int(numOfSeconds)

Return the resulting date value.

    return new_date

create_time_escrow

Call the create_time_escrow function, passing the seed, amount, destination, finish interval, and cancel interval.

def create_time_escrow(seed, amount, destination, finish, cancel):

Get the client wallet.

    wallet=Wallet.from_seed(seed)

Connect to Testnet.

    client=JsonRpcClient(testnet_url)

Create variables for the finish and cancel dates using the add_seconds function.

    finish_date = add_seconds(finish)
    cancel_date = add_seconds(cancel)

Define the EscrowCreate transaction.

    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.

    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.

def finish_time_escrow(seed, owner, sequence):

Instantiate the wallet and client.

    wallet=Wallet.from_seed(seed)
    client=JsonRpcClient(testnet_url)

Define the EscrowFinish transaction.

    finish_tx=xrpl.models.transactions.EscrowFinish(
        account=wallet.address,
        owner=owner,
        offer_sequence=int(sequence)
    )

Submit the transaction and report the results.

    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.

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.

    client=JsonRpcClient(testnet_url)

Define the AccountObjects request, specifying objects of type escrow.

    acct_escrows=AccountObjects(
        account=account,
        ledger_index="validated",
        type="escrow"
    )

Submit the request and return the results.

    response=client.request(acct_escrows)
    return response.result

cancel_time_escrows

Pass the issuer account seed, the owner account, and the escrow sequence number.

def cancel_time_escrow(seed, owner, sequence):

Get the wallet and instantiate a client on Testnet.

    wallet=Wallet.from_seed(seed)
    client=JsonRpcClient(testnet_url)

Define the cancel transaction

    cancel_tx=xrpl.models.transactions.EscrowCancel(
        account=wallet.address,
        owner=owner,
        offer_sequence=int(sequence)
    )

Submit the transaction and report the results

    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.

def get_transaction(account, ledger_index):

Create a client instance.

    client=JsonRpcClient(testnet_url)

Create the AccountTx request.

    tx_info=AccountTx(
        account=account,
        ledger_index=int(ledger_index)
    )

Send the request and report the results.

    response=client.request(tx_info)
    return response.result

lesson8-time-escrow.py

This module builds on lesson1-send-xrp.py. Changes are noted below.

import tkinter as tk
import xrpl
import json

from mod1 import get_account, get_account_info, send_xrp

Import new functions from mod8.py.

from mod8 import create_time_escrow, finish_time_escrow, get_escrows, cancel_time_escrow, get_transaction

Module 8 Handlers

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.

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.

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.

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.

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.

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.

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()