summaryrefslogtreecommitdiff
path: root/test/scripts/e2e_subs/app-rekey.py
blob: 94bfcd22a588e388551c12010763a01a404408af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python

import os
import sys
from goal import Goal

from datetime import datetime

stamp = datetime.now().strftime("%Y%m%d_%H%M%S")
print(f"{os.path.basename(sys.argv[0])} start {stamp}")

goal = Goal(sys.argv[1], autosend=True)

joe = goal.new_account()
flo = goal.new_account()

txinfo, err = goal.pay(goal.account, joe, amt=500_000)
assert not err, err

# Turn off rewards for precise balance checking
txinfo, err = goal.keyreg(joe, nonpart=True)
assert not err, err
joeb = goal.balance(joe)

txinfo, err = goal.pay(goal.account, flo, amt=500_000)
assert not err, err

teal = """
#pragma version 6
 txn ApplicationID
 bz end
 // Use the rekeyed account to make a payment, and give it back
 itxn_begin
  int pay
  itxn_field TypeEnum

  txn Accounts 1
  itxn_field Sender

  txn Accounts 0
  itxn_field Receiver

  int 5
  itxn_field Amount

  txn Accounts 1
  itxn_field RekeyTo
 itxn_submit

end:
 int 1
"""

txinfo, err = goal.app_create(joe, goal.assemble(teal))
assert not err, err
joeb = joeb-1000
app_id = txinfo['application-index']
assert app_id

app_addr = goal.app_address(app_id)
# flo rekeys her account to the app, app spends from it, then rekeys it back
txinfo, err = goal.pay(flo, joe, amt=1, rekey_to=app_addr)
assert not err, err
assert goal.balance(joe) == joeb+1, goal.balance(joe)

# can no longer spend
txinfo, err = goal.pay(flo, joe, amt=1)
assert err
assert goal.balance(joe) == joeb+1, goal.balance(joe)

txinfo, err = goal.app_call(joe, app_id, accounts=[flo])
assert not err, err
joeb = joeb-1000
assert goal.balance(joe) == joeb+6, goal.balance(joe)

# can spend again
txinfo, err = goal.pay(flo, joe, amt=1)
assert not err, err
assert goal.balance(joe) == joeb+7, goal.balance(joe)

print(f"{os.path.basename(sys.argv[0])} OK {stamp}")