use uint64 for balance, like other examples

This commit is contained in:
Jacek Sieka 2019-04-11 13:44:47 -06:00
parent f8cf19c4bb
commit f5b28ebd77
No known key found for this signature in database
GPG Key ID: 6299FEB3EB6FA465
1 changed files with 15 additions and 11 deletions

View File

@ -1,16 +1,16 @@
## ewasm “WRC20” token contract coding challenge
## https://gist.github.com/axic/16158c5c88fbc7b1d09dfa8c658bc363
import ../eth_contracts, stint
import ../eth_contracts, endians
proc do_balance() =
if getCallDataSize() != 24:
revert(nil, 0)
var address: array[20, byte]
var address{.noinit.}: array[20, byte]
callDataCopy(address, 4)
var balance: array[32, byte]
var balance{.noinit.}: array[32, byte]
storageLoad(address, addr balance)
finish(addr balance, sizeof(balance).int32)
@ -22,7 +22,7 @@ proc do_transfer() =
getCaller(addr sender)
var recipient: array[20, byte]
callDataCopy(recipient, 4)
var value: uint64
var value: array[8, byte]
callDataCopy(value, 24)
var senderBalance: array[32, byte]
@ -30,18 +30,22 @@ proc do_transfer() =
var recipientBalance: array[32, byte]
storageLoad(recipient, addr recipientBalance)
var sb = readUintBE[256](senderBalance)
var rb = readUintBE[256](recipientBalance)
let v = value.u256
var sb, rb, v: uint64
bigEndian64(addr v, addr value)
bigEndian64(addr sb, addr senderBalance[32 - 8])
if sb < v:
revert(nil, 0)
sb -= v
rb += v
bigEndian64(addr rb, addr recipientBalance[32 - 8])
sb -= v
rb += v # TODO there's an overflow possible here..
bigEndian64(addr senderBalance[32 - 8], addr sb)
bigEndian64(addr recipientBalance[32 - 8], addr rb)
senderBalance = sb.toByteArrayBE()
recipientBalance = rb.toByteArrayBE()
storageStore(sender, addr senderBalance)
storageStore(recipient, addr recipientBalance)