Skip to content

Commit

Permalink
finish recurring_swap example
Browse files Browse the repository at this point in the history
  • Loading branch information
stechu committed Apr 7, 2020
1 parent 6cc534b commit b42159a
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 16 deletions.
37 changes: 21 additions & 16 deletions examples/recurring_swap.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,27 @@

tmpl_buyer = Addr("6ZHGHH5Z5CTPCF5WCESXMGRSVK7QJETR63M3NY5FJCUYDHO57VTCMJOBGY")
tmpl_provider = Addr("7Z5PWO2C6LFNQFGHWKSK5H47IQP5OJW2M3HA2QPXTY3WTNP5NU2MHBW27M")
tmpl_ppk = Byte("base32", "GFFQ47565WX6VEIJXXIB4W5JNOS2UODKPASUO5T3N3RXLSBR2CEA")
tmpl_amount = Int(500)
tmpl_amount = Int(100000)
tmpl_fee = Int(1000)
tmpl_timeout = Int(100000)

fee_cond = Txn.fee() <= tmpl_fee
type_cond = Txn.type_enum() == Int(1)
recv_cond = And(Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_provider,
Txn.amount() == tmpl_amount,
Ed25519Verify(Itob(Txn.first_valid()), Arg(0), tmpl_ppk),
Txn.lease() == Itob(Txn.first_valid()))

close_cond = And(Txn.close_remainder_to() == tmpl_buyer,
Txn.amount() == Int(0),
Txn.first_valid() >= tmpl_timeout)

recurring_swap = And(fee_cond, type_cond, Or(recv_cond, close_cond))
print(recurring_swap.teal())
def recurring_swap(tmpl_buyer=tmpl_buyer,
tmpl_provider=tmpl_provider,
tmpl_amount=tmpl_amount,
tmpl_fee=tmpl_fee,
tmpl_timeout=tmpl_timeout):
fee_cond = Txn.fee() <= tmpl_fee
type_cond = Txn.type_enum() == Int(1)
recv_cond = And(Txn.close_remainder_to() == Global.zero_address(),
Txn.receiver() == tmpl_provider,
Txn.amount() == tmpl_amount,
Ed25519Verify(Itob(Txn.first_valid()), Arg(0), tmpl_provider),
Txn.lease() == Sha256(Itob(Txn.first_valid())))

close_cond = And(Txn.close_remainder_to() == tmpl_buyer,
Txn.amount() == Int(0),
Txn.first_valid() >= tmpl_timeout)

return And(fee_cond, type_cond, Or(recv_cond, close_cond)).teal()

# print(recurring_swap())
84 changes: 84 additions & 0 deletions examples/recurring_swap_deploy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
#!/usr/bin/env python3

from pyteal import *
from nacl import encoding, hash
from recurring_swap import recurring_swap, tmpl_provider
from algosdk import algod, account
from algosdk.future import transaction
import uuid, params, re, base64

# ------- generate provider's account -----------------------------------------------
key_fn = str(uuid.uuid4()) + ".key"
execute(["algokey", "generate", "-f", key_fn])
stdout, stderr = execute(["algokey", "export", "-f", key_fn])

if stderr != "":
print(stderr)
raise

result = re.search(r'key: \w+', stdout)
provider_addr = result.group(0)[5:]
print("provider addr: {}".format(provider_addr))

# ------- instantiate template, compile teal source, get escrow address -------------

teal_source = recurring_swap(tmpl_provider=Addr(provider_addr))
# print(teal_source)

# compile teal
teal_base = str(uuid.uuid4())
teal_file = teal_base + ".teal"
with open(teal_file, "w+") as f:
f.write(teal_source)
lsig_fname = teal_base + ".tealc"

stdout, stderr = execute(["goal", "clerk", "compile", "-o", lsig_fname,
teal_file])

if stderr != "":
print(stderr)
raise
elif len(stdout) < 59:
print("error in compile teal")
raise

result = re.search(r': \w+', stdout)
escrow_addr = result.group(0)[2:]
print("Dispense at least 101000 microAlgo to {}".format(escrow_addr))
input("Make sure you did that. Press Enter to continue...")

# now, as a provider, you can withdraw Algo from the escrow if you sign the first valid
acl = algod.AlgodClient(params.algod_token, params.algod_address)

sp = acl.suggested_params_as_object()
first_valid = sp.first
data = first_valid.to_bytes(8, byteorder='big')
lease = hash.sha256(data)
lease_bytes = encoding.HexEncoder.decode(lease)


txn = transaction.PaymentTxn(escrow_addr, sp, provider_addr, 100000, lease=lease_bytes)

with open(lsig_fname, "rb") as f:
teal_bytes = f.read()
lsig = transaction.LogicSig(teal_bytes)
lstx = transaction.LogicSigTransaction(txn, lsig)

assert(lstx.verify())

# send LogicSigTransaction to network
transaction.write_to_file([lstx], "r_s.txn")

stdout, stderr = execute(["goal", "clerk", "tealsign", "--data-b64", base64.b64encode(data),
"--lsig-txn", "r_s.txn", "--keyfile", key_fn, "--set-lsig-arg-idx",
"0"])
if stderr != "":
print(stderr)
raise

print(stdout)

lstx = transaction.retrieve_from_file("r_s.txn")
txid = acl.send_transactions(lstx)

print("Succesfull! txid:{}".format(txid))

0 comments on commit b42159a

Please sign in to comment.