-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_usage.py
118 lines (100 loc) · 3.64 KB
/
example_usage.py
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
import threading
import subprocess
from algosdk.logic import get_application_address
from algosdk import mnemonic, account
from payment.operations import (
createPaymentApp,
setupPaymentApp,
transact,
signState,
payment_node_process,
)
from payment.util import (
getBalances,
)
from payment.testing.setup import getAlgodClient
from payment.testing.resources import (
getTemporaryAccount)
if __name__ == "__main__":
# alice_seed_phrase = "auction palm thumb shuffle aim fade cover glass fire spawn course harbor moon decline shed shop envelope virtual visa attitude hand december portion abstract labor"
# bob_seed_phrase = "prize struggle destroy tray harvest wear century length thought diagram rubber page bridge weasel same ocean team index skin volume witness record cinnamon able machine"
client = getAlgodClient()
print("Generating temporary accounts...")
# alice = getTemporaryAccount(client, seed_phrase=alice_seed_phrase)
# bob = getTemporaryAccount(client, seed_phrase=bob_seed_phrase)
alice = getTemporaryAccount(client)
bob = getTemporaryAccount(client)
print("Alice (funding account):", alice.getAddress())
print("Bob (channel counterparty account):", bob.getAddress(), "\n")
aliceBalancesBefore = getBalances(client, alice.getAddress())
print("Alice's balances:", aliceBalancesBefore)
bobBalancesBefore = getBalances(client, alice.getAddress())
print("Bob's balances:", bobBalancesBefore, "\n")
print("Alice creates a payment smart contract with Bob's address...")
appID = createPaymentApp(
client=client,
sender=alice,
counterparty=bob.getAddress(),
penalty_reserve=100_000,
dispute_window=1000,
)
print(
"Done. The payment app ID is",
appID,
"and the payment account is",
get_application_address(appID),
"\n",
)
channelCapacity = 2_000_000_000
print(f"Alice is setting up and funding the Payment Contract with {channelCapacity} microAlgos...")
setupPaymentApp(
client=client,
appID=appID,
funder=alice,
channelCapacity=channelCapacity,
)
print("Done\n")
amount1 = 300
print(f"Alice is sending {amount1} microAlgos to Bob...")
(aliceBalance, bobBalance) = transact(
client=client,
appID=appID,
sender=alice,
amount=amount1,
)
print(f"Alice's balance is now {aliceBalance} microAlgos and Bob's balance is now {bobBalance} microAlgos\n")
amount2 = 50
print(f"Bob tries sending {amount2} microAlgos to Alice...")
try:
(aliceBalance, bobBalance) = transact(
client=client,
appID=appID,
sender=bob,
amount=amount2,
)
print(f"Alice's balance is still {aliceBalance} microAlgos and Bob's balance is still {bobBalance} microAlgos\n")
except Exception as e:
print("\n Bob's transaction failed:", e)
amount3 = 1_000_000_000
print(f"Alice is sending {amount3} microAlgos to Bob...")
(aliceBalance, bobBalance) = transact(
client=client,
appID=appID,
sender=alice,
amount=amount3,
)
print(f"Alice's balance is now {aliceBalance} microAlgos and Bob's balance is now {bobBalance} microAlgos\n")
print(f"Bob is sending {amount2} microAlgos to Alice...")
(aliceBalance, bobBalance) = transact(
client=client,
appID=appID,
sender=bob,
amount=amount2,
)
print(f"Alice's balance is now {aliceBalance} microAlgos and Bob's balance is now {bobBalance} microAlgos\n")
print("Alice signs state data...")
signState(client=client, app_id=appID, alice=alice, bob=bob, alice_balance=1_999_999_900, bob_balance=100)
alice_thread = threading.Thread(target=payment_node_process, kwargs={"participant_name": "Alice", "is_creator": True})
alice_thread.start()
bob_thread = threading.Thread(target=payment_node_process, kwargs={"participant_name": "Bob", "is_creator": False})
bob_thread.start()