Skip to content

Commit

Permalink
update the demo and execute function
Browse files Browse the repository at this point in the history
  • Loading branch information
Shumo Chu committed Feb 20, 2020
1 parent bed15e6 commit 0adc57b
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
67 changes: 65 additions & 2 deletions examples/periodic_payment.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
#!/usr/bin/env python3

from pyteal import *
import uuid, params, base64
from algosdk import algod, transaction, account, mnemonic

#-------------- using PyTeal program smart contract logic ----------------

#template variables
tmpl_fee = Int(1000)
Expand All @@ -27,6 +31,65 @@
Txn.first_valid() == tmpl_timeout,
Txn.amount() == Int(0))

periodic_pay_escrow = periodic_pay_core.And(periodic_pay_transfer.Or(periodic_pay_close))
periodic_pay_escrow = And(periodic_pay_core,
Or(periodic_pay_transfer, periodic_pay_close))

teal_source = periodic_pay_escrow.teal()
print("Teal Code:")
print(teal_source)

#--------- compile & send transaction using Goal and Python SDK ----------

# compile teal
teal_file = str(uuid.uuid4()) + ".teal"
with open(teal_file, "w+") as f:
f.write(teal_source)
lsig_fname = str(uuid.uuid4()) + ".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

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

# create algod clients
acl = algod.AlgodClient(params.algod_token, params.algod_address)

#Recover the account that is wanting to delegate signature
passphrase = "patrol crawl rule faculty enemy sick reveal embody trumpet win shy zero ill draw swim excuse tongue under exact baby moral kite spring absent double"
sk = mnemonic.to_private_key(passphrase)
addr = account.address_from_private_key(sk)
print( "Address of Sender/Delgator: " + addr )

# sign the logic signature with an account sk
lsig.sign(sk)

# get suggested parameters
params = acl.suggested_params()
gen = params["genesisID"]
gh = params["genesishashb64"]
startRound = params.lastRound - (params.lastRound % 50)
endRound = startRound + 1000
fee = 0
amount = 2000
receiver = "ZZAF5ARA4MEC5PVDOP64JM5O5MQST63Q2KOY2FLYFLXXD3PFSNJJBYAFZM"

# create a transaction
txn = transaction.PaymentTxn(addr, fee, startRound, endRound, gh, receiver, amount)

# Create the LogicSigTransaction with contract account LogicSig
lstx = transaction.LogicSigTransaction(txn, lsig)

# send raw LogicSigTransaction to network
txid = acl.send_transaction(lstx)
print("Transaction ID: " + txid)


print(periodic_pay_escrow.teal())
1 change: 1 addition & 0 deletions pyteal/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
from .ops import *
from .util import execute
16 changes: 13 additions & 3 deletions pyteal/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"""

from enum import Enum
import re
import re, subprocess


class TealType(Enum):
Expand Down Expand Up @@ -103,13 +103,23 @@ def valid_tmpl(s:str):

if pattern.fullmatch(s) is None:
raise TealInputError("{} is not a valid template variable".format(s))


label_count = 0


def new_label():
global label_count
new_l = "l{}".format(label_count)
label_count += 1
return new_l

def execute(args):
""" Execute in bash, return stdout and stderr in string
Arguments:
args: command and arguments to run, e.g. ['ls', '-l']
"""
process = subprocess.Popen(args, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)
stdout, stderr = process.communicate()

return (stdout.decode("utf-8"), stderr.decode("utf-8"))

0 comments on commit 0adc57b

Please sign in to comment.