forked from camohe90/python_algokit
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
175 lines (140 loc) · 4.38 KB
/
main.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
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
from algokit_utils.beta.algorand_client import (
AlgorandClient,
AssetCreateParams,
AssetOptInParams,
AssetTransferParams,
PayParams,
AssetFreezeParams,
)
# Client to connect to localnet
algorand = AlgorandClient.default_local_net()
# Import dispenser from KMD
dispenser = algorand.account.dispenser()
print("Dispenser Address: ", dispenser.address)
# Create a wallet for the creator of the token
creator = algorand.account.random()
print("Creator Address: ",creator.address)
# Get account info about creator
print(algorand.account.get_information(creator.address))
# Fund creator address with algo
algorand.send.payment(
PayParams(
sender=dispenser.address,
receiver=creator.address,
amount=10_000_000
)
)
# Check out the creator account changes after funding
print(algorand.account.get_information(creator.address))
# Create Algorand Standard Asset
sent_txn = algorand.send.asset_create(
AssetCreateParams(
sender=creator.address,
total= 1000,
asset_name="nameofasset",
unit_name="NOA",
manager=creator.address,
clawback=creator.address,
freeze=creator.address
)
)
# Extracting the confirmation and asset index of the asset creation transaction to get asset ID
asset_id= sent_txn["confirmation"]["asset-index"]
print("Asset ID: ", asset_id)
# Create receiver account
receiver_acct = algorand.account.random()
print("Receiver Account: ", receiver_acct.address)
# Fund receiver account
algorand.send.payment(
PayParams(
sender=dispenser.address,
receiver=receiver_acct.address,
amount=10_000_000
)
)
print(algorand.account.get_information(receiver_acct.address))
# Atomic transfer segment - optin txn / payment txn / asset transfer txn
# Create a new transaction group
group_tx = algorand.new_group()
# Add an asset opt-in transaction to the group
group_tx.add_asset_opt_in(
AssetOptInParams(
sender=receiver_acct.address,
asset_id=asset_id # The ID of the asset to opt in to
)
)
# Add a payment transaction to the group
group_tx.add_payment(
PayParams(
sender=receiver_acct.address,
receiver=creator.address,
amount=1_000_000
))
# Add an asset transfer transaction to the group
group_tx.add_asset_transfer(
AssetTransferParams(
sender=creator.address,
receiver=receiver_acct.address,
asset_id=asset_id,
amount=10
)
)
# Execute the transaction group
group_tx.execute()
# Print the entire information from the Receiver Account
print(algorand.account.get_information(receiver_acct.address))
# Print the amount of the asset the receiver account holds after the transactions
print("Receiver Account Asset Balance:",algorand.account.get_information(receiver_acct.address)['assets'][0]['amount'])
# Print the remaining balance of the creator account after the transactions
print("Creator Account Balance:", algorand.account.get_information(creator.address)['amount'])
#-------------------------------------------------------
# Additional Information (Freeze & Clawback)
# Freeze
algorand.send.asset_freeze(
AssetFreezeParams(
sender=creator.address,
asset_id=asset_id,
account=receiver_acct.address,
frozen= True
)
)
# Test freeze error
""" algorand.send.asset_transfer(
AssetTransferParams(
sender=receiver_acct.address,
receiver=creator.address,
asset_id=asset_id,
amount=2
)
)
"""
# UnFreeze
algorand.send.asset_freeze(
AssetFreezeParams(
sender=creator.address,
asset_id=asset_id,
account=receiver_acct.address,
frozen= False
)
)
# Send asset
algorand.send.asset_transfer(
AssetTransferParams(
sender=receiver_acct.address,
receiver=creator.address,
asset_id=asset_id,
amount=2
)
)
print(algorand.account.get_information(receiver_acct.address)['assets'][0]['amount'])
# Clawback
algorand.send.asset_transfer(
AssetTransferParams(
sender= creator.address,
receiver= creator.address,
asset_id=asset_id,
amount=2,
clawback_target= receiver_acct.address
)
)
print(algorand.account.get_information(receiver_acct.address)['assets'][0]['amount'])