-
Notifications
You must be signed in to change notification settings - Fork 21
/
wrapped_neo.py
424 lines (331 loc) · 14.8 KB
/
wrapped_neo.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
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
from typing import Any
from boa3.builtin.compile_time import CreateNewEvent, NeoMetadata, public
from boa3.builtin.contract import Nep17TransferEvent, abort
from boa3.builtin.interop import runtime, storage
from boa3.builtin.interop.contract import GAS as GAS_SCRIPT, NEO as NEO_SCRIPT, call_contract
from boa3.builtin.nativecontract.contractmanagement import ContractManagement
from boa3.builtin.nativecontract.neo import NEO as NEO_TOKEN
from boa3.builtin.type import UInt160
# -------------------------------------------
# METADATA
# -------------------------------------------
def manifest_metadata() -> NeoMetadata:
"""
Defines this smart contract's metadata information
"""
meta = NeoMetadata()
meta.supported_standards = ['NEP-17']
meta.add_permission(methods=['onNEP17Payment'])
meta.author = "Mirella Medeiros, Ricardo Prado and Lucas Uezu. COZ in partnership with Simpli"
meta.description = "Wrapped NEO Example"
meta.email = "[email protected]"
return meta
# -------------------------------------------
# TOKEN SETTINGS
# -------------------------------------------
# The keys used to access the storage
OWNER_KEY = b'owner'
SUPPLY_KEY = b'totalSupply'
# Symbol of the Token
TOKEN_SYMBOL = 'zNEO'
# Number of decimal places
TOKEN_DECIMALS = 0
# Total Supply of tokens in the system
TOKEN_TOTAL_SUPPLY = 10_000_000 * 10 ** TOKEN_DECIMALS # 10m total supply * 10^0 (decimals)
# Allowance
ALLOWANCE_PREFIX = b'allowance'
# -------------------------------------------
# Events
# -------------------------------------------
on_transfer = Nep17TransferEvent
on_approval = CreateNewEvent(
[
('owner', UInt160),
('spender', UInt160),
('amount', int)
],
'Approval'
)
# -------------------------------------------
# Methods
# -------------------------------------------
@public(safe=True)
def symbol() -> str:
"""
Gets the symbols of the token.
This string must be valid ASCII, must not contain whitespace or control characters, should be limited to uppercase
Latin alphabet (i.e. the 26 letters used in English) and should be short (3-8 characters is recommended).
This method must always return the same value every time it is invoked.
:return: a short string representing symbol of the token managed in this contract.
"""
return TOKEN_SYMBOL
@public(safe=True)
def decimals() -> int:
"""
Gets the amount of decimals used by the token.
E.g. 8, means to divide the token amount by 100,000,000 (10 ^ 8) to get its user representation.
This method must always return the same value every time it is invoked.
:return: the number of decimals used by the token.
"""
return TOKEN_DECIMALS
@public(name='totalSupply', safe=True)
def total_supply() -> int:
"""
Gets the total token supply deployed in the system.
This number must not be in its user representation. E.g. if the total supply is 10,000,000 tokens, this method
must return 10,000,000 * 10 ^ decimals.
:return: the total token supply deployed in the system.
"""
return storage.get_int(SUPPLY_KEY)
@public(name='balanceOf', safe=True)
def balance_of(account: UInt160) -> int:
"""
Get the current balance of an address.
The parameter account must be a 20-byte address represented by a UInt160.
:param account: the account address to retrieve the balance for
:type account: bytes
"""
assert len(account) == 20
return storage.get_int(account)
@public
def transfer(from_address: UInt160, to_address: UInt160, amount: int, data: Any) -> bool:
"""
Transfers an amount of zNEO tokens from one account to another.
If the method succeeds, it must fire the `Transfer` event and must return true, even if the amount is 0,
or from and to are the same address.
:param from_address: the address to transfer from
:type from_address: UInt160
:param to_address: the address to transfer to
:type to_address: UInt160
:param amount: the amount of zNEO tokens to transfer
:type amount: int
:param data: whatever data is pertinent to the onPayment method
:type data: Any
:return: whether the transfer was successful
:raise AssertionError: raised if `from_address` or `to_address` length is not 20 or if `amount` if less than zero.
"""
# the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
assert len(from_address) == 20 and len(to_address) == 20
# the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
assert amount >= 0
# The function MUST return false if the from account balance does not have enough tokens to spend.
from_balance = storage.get_int(from_address)
if from_balance < amount:
return False
# The function should check whether the from address equals the caller contract hash.
# If so, the transfer should be processed;
# If not, the function should use the check_witness to verify the transfer.
if from_address != runtime.calling_script_hash:
if not runtime.check_witness(from_address):
return False
# skip balance changes if transferring to yourself or transferring 0 cryptocurrency
if from_address != to_address and amount != 0:
if from_balance == amount:
storage.delete(from_address)
else:
storage.put_int(from_address, from_balance - amount)
to_balance = storage.get_int(to_address)
storage.put_int(to_address, to_balance + amount)
# if the method succeeds, it must fire the transfer event
on_transfer(from_address, to_address, amount)
# if the to_address is a smart contract, it must call the contracts onPayment
post_transfer(from_address, to_address, amount, data, True)
# and then it must return true
return True
@public(name='transferFrom')
def transfer_from(spender: UInt160, from_address: UInt160, to_address: UInt160, amount: int, data: Any) -> bool:
"""
A spender transfers an amount of zNEO tokens allowed from one account to another.
If the method succeeds, it must fire the `Transfer` event and must return true, even if the amount is 0,
or from and to are the same address.
:param spender: the address that is trying to transfer zNEO tokens
:type spender: UInt160
:param from_address: the address to transfer from
:type from_address: UInt160
:param to_address: the address to transfer to
:type to_address: UInt160
:param amount: the amount of zNEO tokens to transfer
:type amount: int
:param data: whatever data is pertinent to the onPayment method
:type data: Any
:return: whether the transfer was successful
:raise AssertionError: raised if `spender`, `from_address` or `to_address` length is not 20 or if `amount` is less
than zero.
"""
# the parameters from and to should be 20-byte addresses. If not, this method should throw an exception.
assert len(spender) == 20 and len(from_address) == 20 and len(to_address) == 20
# the parameter amount must be greater than or equal to 0. If not, this method should throw an exception.
assert amount >= 0
# The function MUST return false if the from account balance does not have enough tokens to spend.
from_balance = storage.get_int(from_address)
if from_balance < amount:
return False
# The function MUST return false if the from account balance does not allow enough tokens to be spent by the spender.
allowed = allowance(from_address, spender)
if allowed < amount:
return False
# The function should check whether the spender address equals the caller contract hash.
# If so, the transfer should be processed;
# If not, the function should use the check_witness to verify the transfer.
if spender != runtime.calling_script_hash:
if not runtime.check_witness(spender):
return False
if allowed == amount:
storage.delete(ALLOWANCE_PREFIX + from_address + spender)
else:
storage.put_int(ALLOWANCE_PREFIX + from_address + spender, allowed - amount)
# skip balance changes if transferring to yourself or transferring 0 cryptocurrency
if from_address != to_address and amount != 0:
if from_balance == amount:
storage.delete(from_address)
else:
storage.put_int(from_address, from_balance - amount)
to_balance = storage.get_int(to_address)
storage.put_int(to_address, to_balance + amount)
# if the method succeeds, it must fire the transfer event
on_transfer(from_address, to_address, amount)
# if the to_address is a smart contract, it must call the contracts onPayment
post_transfer(from_address, to_address, amount, data, True)
# and then it must return true
return True
@public
def approve(owner: UInt160, spender: UInt160, amount: int) -> bool:
"""
Allows spender to spend from your account as many times as they want until it reaches the amount allowed.
The allowed amount will be overwritten if this method is called once more.
:param owner: the address that is allowing to use their zNEO
:type owner: UInt160
:param spender: the address that will be allowed to use your zNEO
:type spender: UInt160
:param amount: the total amount of zNEO that the spender can spent
:type amount: int
:raise AssertionError: raised if `from_address` length is not 20 or if `amount` if less than zero.
"""
assert len(owner) == 20
assert len(spender) == 20
assert amount >= 0
if not runtime.check_witness(owner):
return False
if balance_of(owner) >= amount:
storage.put_int(ALLOWANCE_PREFIX + owner + spender, amount)
on_approval(owner, spender, amount)
return True
return False
@public(safe=True)
def allowance(owner: UInt160, spender: UInt160) -> int:
"""
Gets the amount of zNEO from the owner that can be used by the spender.
:param owner: the address that allowed the spender to spend zNEO
:type owner: UInt160
:param spender: the address that can spend zNEO from the owner's account
:type spender: UInt160
"""
return storage.get_int(ALLOWANCE_PREFIX + owner + spender)
def post_transfer(from_address: UInt160 | None, to_address: UInt160 | None, amount: int, data: Any,
call_onPayment: bool):
"""
Checks if the one receiving NEP17 tokens is a smart contract and if it's one the onPayment method will be called.
:param from_address: the address of the sender
:type from_address: UInt160
:param to_address: the address of the receiver
:type to_address: UInt160
:param amount: the amount of cryptocurrency that is being sent
:type amount: int
:param data: any pertinent data that might validate the transaction
:type data: Any
:param call_onPayment: whether onPayment should be called or not
:type call_onPayment: bool
"""
if call_onPayment:
if to_address is not None:
contract = ContractManagement.get_contract(to_address)
if contract is not None:
call_contract(to_address, 'onNEP17Payment', [from_address, amount, data])
def mint(account: UInt160, amount: int):
"""
Mints new zNEO tokens.
:param account: the address of the account that is sending cryptocurrency to this contract
:type account: UInt160
:param amount: the amount of gas to be refunded
:type amount: int
:raise AssertionError: raised if amount is less than than 0
"""
assert amount >= 0
if amount != 0:
current_total_supply = total_supply()
account_balance = balance_of(account)
storage.put_int(SUPPLY_KEY, current_total_supply + amount)
storage.put_int(account, account_balance + amount)
on_transfer(None, account, amount)
post_transfer(None, account, amount, None, True)
@public
def burn(account: UInt160, amount: int):
"""
Burns zNEO tokens.
:param account: the address of the account that is pulling out cryptocurrency of this contract
:type account: UInt160
:param amount: the amount of gas to be refunded
:type amount: int
:raise AssertionError: raised if `account` length is not 20, amount is less than than 0 or the account doesn't have
enough zNEO to burn
"""
assert len(account) == 20
assert amount >= 0
if runtime.check_witness(account):
if amount != 0:
current_total_supply = total_supply()
account_balance = balance_of(account)
assert account_balance >= amount
storage.put_int(SUPPLY_KEY, current_total_supply - amount)
if account_balance == amount:
storage.delete(account)
else:
storage.put_int(account, account_balance - amount)
on_transfer(account, None, amount)
post_transfer(account, None, amount, None, False)
NEO_TOKEN.transfer(runtime.executing_script_hash, account, amount)
@public
def verify() -> bool:
"""
When this contract address is included in the transaction signature,
this method will be triggered as a VerificationTrigger to verify that the signature is correct.
For example, this method needs to be called when withdrawing token from the contract.
:return: whether the transaction signature is correct
"""
return runtime.check_witness(get_owner())
@public
def _deploy(data: Any, update: bool):
"""
Initializes the storage when the smart contract is deployed.
:return: whether the deploy was successful. This method must return True only during the smart contract's deploy.
"""
if not update:
container = runtime.script_container
storage.put_int(SUPPLY_KEY, TOKEN_TOTAL_SUPPLY)
storage.put_int(container.sender, TOKEN_TOTAL_SUPPLY)
storage.put_uint160(OWNER_KEY, container.sender)
on_transfer(None, container.sender, TOKEN_TOTAL_SUPPLY)
@public
def onNEP17Payment(from_address: UInt160, amount: int, data: Any):
"""
If this smart contract receives NEO, it will mint an amount of wrapped NEO
:param from_address: the address of the one who is trying to send cryptocurrency to this smart contract
:type from_address: UInt160
:param amount: the amount of cryptocurrency that is being sent to the this smart contract
:type amount: int
:param data: any pertinent data that might validate the transaction
:type data: Any
"""
# Use calling_script_hash to identify if the incoming token is NEO
if runtime.calling_script_hash == NEO_SCRIPT:
mint(from_address, amount)
elif runtime.calling_script_hash == GAS_SCRIPT:
# GAS is minted when transferring NEO
return
else:
abort()
def get_owner() -> UInt160:
"""
Gets the script hash of the owner (the account that deployed this smart contract)
"""
return storage.get_uint160(OWNER_KEY)