-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathASAIntegration.py
More file actions
224 lines (185 loc) · 8.32 KB
/
ASAIntegration.py
File metadata and controls
224 lines (185 loc) · 8.32 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import json
from algosdk import account, mnemonic, transaction
from algosdk.v2client import algod
from create_account import create_account
from closeout_account import closeout_account
from create_asset import create_asset
algod_address = "http://localhost:4001"
algod_token = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
algod_client = algod.AlgodClient(algod_token, algod_address)
def create_fund_accounts():
# For ease of reference, add account public and private keys to an accounts dict.
print("--------------------------------------------")
print("Creating Alice and Bob's accounts...")
mnemonics = []
mnemonics.append(create_account())
mnemonics.append(create_account())
accounts = []
for m in mnemonics:
accounts.append(
{
"pk": account.address_from_private_key(mnemonic.to_private_key(m)),
"sk": mnemonic.to_private_key(m),
}
)
return accounts
def transferAlgosToBob(algod_client, bob, alice):
print("--------------------------------------------")
print("Transfering Algos to Bob....")
account_info = algod_client.account_info(bob["pk"])
print("Account balance: {} microAlgos".format(account_info.get("amount")) + "\n")
# build transaction
params = algod_client.suggested_params()
# minimum balance 100000, plus 100000 for asset optin,
# plus 3000 for 3 tx (optin, transfer, algo closeout) = 203000 microalgos
# amount = 203000;
unsigned_txn = transaction.PaymentTxn(alice["pk"], params, bob["pk"], 203000)
# sign transaction
signed_txn = unsigned_txn.sign(alice["sk"])
# submit transaction
txid = algod_client.send_transaction(signed_txn)
print("Successfully sent transaction with txID: {}".format(txid))
# wait for confirmation
try:
confirmed_txn = transaction.wait_for_confirmation(algod_client, txid, 4)
print("TXID: ", txid)
print("Result confirmed in round: {}".format(confirmed_txn["confirmed-round"]))
except Exception as err:
print(err)
return
print("Transaction information: {}".format(json.dumps(confirmed_txn, indent=4)))
account_info = algod_client.account_info(bob["pk"])
print("Account balance: {} microAlgos".format(account_info.get("amount")) + "\n")
def optIn(algod_client, bob, asset_id):
print("--------------------------------------------")
print("Opt-in for Alice's token......")
# Check if asset_id is in Bob's asset holdings prior
# to opt-in
params = algod_client.suggested_params()
account_info = algod_client.account_info(bob["pk"])
holding = None
idx = 0
for my_account_info in account_info["assets"]:
scrutinized_asset = account_info["assets"][idx]
idx = idx + 1
if scrutinized_asset["asset-id"] == asset_id:
holding = True
break
if not holding:
# Use the AssetTransferTxn class to transfer assets and opt-in
txn = transaction.AssetTransferTxn(
sender=bob["pk"], sp=params, receiver=bob["pk"], amt=0, index=asset_id
)
stxn = txn.sign(bob["sk"])
txid = algod_client.send_transaction(stxn)
print(txid)
# Wait for the transaction to be confirmed
confirmed_txn = transaction.wait_for_confirmation(algod_client, txid, 4)
print("TXID: ", txid)
print("Result confirmed in round: {}".format(confirmed_txn["confirmed-round"]))
# Now check the asset holding for that account.
# This should now show a holding with a balance of 0.
print_asset_holding(algod_client, bob["pk"], asset_id)
def transferAssets(algod_client, alice, bob, asset_id):
print("--------------------------------------------")
print("Transfering Alice's token to Bob......")
params = algod_client.suggested_params()
txn = transaction.AssetTransferTxn(
sender=alice["pk"], sp=params, receiver=bob["pk"], amt=100, index=asset_id
)
stxn = txn.sign(alice["sk"])
txid = algod_client.send_transaction(stxn)
print(txid)
# Wait for the transaction to be confirmed
confirmed_txn = transaction.wait_for_confirmation(algod_client, txid, 4)
print("TXID: ", txid)
print("Result confirmed in round: {}".format(confirmed_txn["confirmed-round"]))
# The balance should now be 10.
print_asset_holding(algod_client, bob["pk"], asset_id)
def transferAssetsBack(algod_client, bob, alice, asset_id):
print("--------------------------------------------")
print("Transfering Alice's token back to Alice......")
params = algod_client.suggested_params()
txn = transaction.AssetTransferTxn(
sender=bob["pk"],
sp=params,
receiver=alice["pk"],
amt=100,
close_assets_to=alice["pk"],
index=asset_id,
)
stxn = txn.sign(bob["sk"])
txid = algod_client.send_transaction(stxn)
print(txid)
# Wait for the transaction to be confirmed
confirmed_txn = transaction.wait_for_confirmation(algod_client, txid, 4)
print("TXID: ", txid)
print("Result confirmed in round: {}".format(confirmed_txn["confirmed-round"]))
# The balance should now be 10.
print_asset_holding(algod_client, alice["pk"], asset_id)
def destroyAsset(algod_client, alice, asset_id):
print("--------------------------------------------")
print("Destroying Alice's token......")
params = algod_client.suggested_params()
# Asset destroy transaction
txn = transaction.AssetConfigTxn(
sender=alice["pk"], sp=params, index=asset_id, strict_empty_address_check=False
)
# Sign with secret key of creator
stxn = txn.sign(alice["sk"])
# Send the transaction to the network and retrieve the txid.
txid = algod_client.send_transaction(stxn)
print(txid)
# Wait for the transaction to be confirmed
confirmed_txn = transaction.wait_for_confirmation(algod_client, txid, 4)
print("TXID: ", txid)
print("Result confirmed in round: {}".format(confirmed_txn["confirmed-round"]))
print("Alice's Token is destroyed.")
# Utility function used to print created asset for account and assetid
def print_created_asset(algodclient, account, assetid):
# note: if you have an indexer instance available it is easier to just use this
# response = myindexer.accounts(asset_id = assetid)
# then use 'account_info['created-assets'][0] to get info on the created asset
account_info = algodclient.account_info(account)
idx = 0
for my_account_info in account_info["created-assets"]:
scrutinized_asset = account_info["created-assets"][idx]
idx = idx + 1
if scrutinized_asset["index"] == assetid:
print("Asset ID: {}".format(scrutinized_asset["index"]))
print(json.dumps(my_account_info["params"], indent=4))
break
# Utility function used to print asset holding for account and assetid
def print_asset_holding(algodclient, account, assetid):
# note: if you have an indexer instance available it is easier to just use this
# response = myindexer.accounts(asset_id = assetid)
# then loop thru the accounts returned and match the account you are looking for
account_info = algodclient.account_info(account)
idx = 0
for my_account_info in account_info["assets"]:
scrutinized_asset = account_info["assets"][idx]
idx = idx + 1
if scrutinized_asset["asset-id"] == assetid:
print("Asset ID: {}".format(scrutinized_asset["asset-id"]))
print(json.dumps(scrutinized_asset, indent=4))
break
# accounts[0] = Alice, accounts[1] = Bob
accounts = create_fund_accounts()
alice = accounts[0]
bob = accounts[1]
# Alice creates Asset - AssetConfigTxn
asset_id = create_asset(alice)
# Alice transfers Algos to Bob - PaymentTxn
transferAlgosToBob(algod_client, bob, alice)
# Bob Opts in order to receive Asset - AssetTransferTxn
optIn(algod_client, bob, asset_id)
# Alice transfers Asset to Bob - AssetTransferTxn
transferAssets(algod_client, alice, bob, asset_id)
# Bob transfers Asset back to Alice - AssetTransferTxn
transferAssetsBack(algod_client, bob, alice, asset_id)
# Alice Destroys Asset - AssetConfigTxn
destroyAsset(algod_client, alice, asset_id)
# Alice Closes account using closeout parameter - PaymentTxn
closeout_account(algod_client, alice)
# Bob Closes account using closeout parameter - PaymentTxn
closeout_account(algod_client, bob)