-
Notifications
You must be signed in to change notification settings - Fork 33
/
erc20.iele
240 lines (195 loc) · 7.42 KB
/
erc20.iele
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
// Copyright (c) 2017 Runtime Verification, Inc. All Rights Reserved.
//
// This contract implements the ERC-20 token standard.
contract ERC20 {
// constant used for shifts
@addressShift = 160
// account storage location for the supply variable. This variable holds
// the current total supply of the token
@supplyKey = 0
// The account storage is divided into two regions:
// - the balance region maps each account address to its balance
// - the allowance region maps each pair of owner and spender accounts to the
// allowance that the owner approves to be transferred by the spender
// Account storage keys that correspond to each of the regions use different
// bit prefixes as follows
@balanceRegion = 1
@allowanceRegion = 2
// hashes of event names
@approvalEvent = 0x7134692B230B9E1FFA39098904722134159652B09C5BC41D88D6698779D228FF
@transferEvent = 0xF099CD8BDE557814842A3121E8DDFD433A539B8C9F14BF31EBF108D12E6196E9
// computes a unique key in the specified account storage region for a
// particular account address. %region represents a unique bit prefix on the
// key that will be ored with the specified address.
define @mapKey(%region, %address) {
%addressMask = 20
%realAddress = twos %addressMask, %address
%shifted = shift %region, @addressShift
%ored = or %shifted, %realAddress
ret %ored
}
// sets the balance of the given account to the given value
define @setBalance(%account, %value) {
// get the key for the account in the balance region
%key = call @mapKey(@balanceRegion, %account)
// store the given value as the balance of the account
sstore %value, %key
ret void
}
// sets the approved allowance to be transfered from the given owner by the
// given spender
define @setAllowance(%owner, %spender, %allowance) {
// get the key for the pair of owner and spender accounts in the allowance
// region
%key1 = call @mapKey(@allowanceRegion, %owner)
%key2 = call @mapKey(%key1, %spender)
// store the given value as the approved allowance to be transfered from the
// owner by the spender
sstore %allowance, %key2
ret void
}
// logs an approval event
define @log.Approval(%owner, %spender, %allowance) {
// store the allowance value in the local execution memory
%logCell = 0
store %allowance, %logCell
// log an approval event, where the owner approves the allowance value to be
// transferred by the spender
log %logCell, @approvalEvent, %owner, %spender
ret void
}
// logs a transfer event
define @log.Transfer(%from, %to, %value) {
// store the transferred value in the local execution memory
%logCell = 0
store %value, %logCell
// log a transfer event between two accounts along with the transferred
// value
log %logCell, @transferEvent, %from, %to
ret void
}
// returns the total supply of the token
define public @totalSupply() {
%supply = sload @supplyKey
ret %supply
}
// initializes an ERC-20 compliant token with the given total supply
define @init(%supply) {
// store the total supply
sstore %supply, @supplyKey
// set the balance of the creator to the amount of the total supply
%caller = call @iele.caller()
call @setBalance(%caller, %supply)
ret void
}
// returns the current balance of an account
define public @balanceOf(%id) {
// get the key for the account in the balance region
%balanceRegion = 1
%key = call @mapKey(%balanceRegion, %id)
// get and return the account's current balance
%balance = sload %key
ret %balance
}
// returns the currently approved allowance to be transferred from the owner
// by the spender
define public @allowance(%owner, %spender) {
// get the key for the pair of owner and spender accounts in the allowance
// region
%allowanceRegion = 2
%key1 = call @mapKey(%allowanceRegion, %owner)
%key2 = call @mapKey(%key1, %spender)
// get and return the current allowance
%allowance = sload %key2
ret %allowance
}
// approves the given allowance to be transferred from the caller by the given
// spender account
define public @approve(%spender, %allowance) {
// ensure that the given allowance is not negative, this is only needed
// because IELE doesn't support unsigned integers
%lt = cmp lt %allowance, 0
br %lt, throw
// get the account address of the caller (the owner)
%owner = call @iele.caller() // written as msg.sender in Viper
// approve the allowance to be transferred from the owner by the spender
call @setAllowance(%owner, %spender, %allowance)
// log the approval
call @log.Approval(%owner, %spender, %allowance)
// should always return true
ret true
throw:
call @iele.invalid()
}
// transfers the given value from the caller to the given target account
define public @transfer(%to, %value) {
// ensure that the given value is not negative, this is only needed because
// IELE doesn't support unsigned integers
%lt = cmp lt %value, 0
br %lt, throw
// get the account address of the caller (the source account)
%from = call @iele.caller()
// get the current balance of the source and ensure that it can cover the
// value to be transferred
%balanceFrom = call @balanceOf(%from)
%lt = cmp lt %balanceFrom, %value
br %lt, throw
// set the new balance of the source after subtracting the value to be
// transferred
%balanceFrom = sub %balanceFrom, %value
call @setBalance(%from, %balanceFrom)
// set the new balance of the target by adding the value to be transferred to
// its existing balance
%balanceTo = call @balanceOf(%to)
%balanceTo = add %balanceTo, %value // no worries of overflow
call @setBalance(%to, %balanceTo)
// log the transfer
call @log.Transfer(%from, %to, %value)
// should either return true or throw in case of insufficient funds in the
// source
ret true
throw:
call @iele.invalid()
}
// transfers the given value from the given source to the given target account,
// the approved allowance to be transferred from the source by the caller should
// cover the value to be transferred
define public @transferFrom(%from, %to, %value) {
// ensure that the given value is not negative, this is only needed because
// IELE doesn't support unsigned integers
%lt = cmp lt %value, 0
br %lt, throw
// get the account address of the caller
%caller = call @iele.caller()
// get the current balance of the source and ensure that it can cover the
// value to be transferred
%balanceFrom = call @balanceOf(%from)
%lt = cmp lt %balanceFrom, %value
br %lt, throw
// get the current allowance for this caller and ensure that it can cover
// the value to be transferred
%allowance = call @allowance(%from, %caller)
%lt = cmp lt %allowance, %value
br %lt, throw
// set the new balance of the source after subtracting the value to be
// transferred
%balanceFrom = sub %balanceFrom, %value
call @setBalance(%from, %balanceFrom)
// set the new allowance for this caller after subtracting the value to be
// transferred
%allowance = sub %allowance, %value
call @setAllowance(%from, %caller, %allowance)
// set the new balance of the target by adding the value to be transferred to
// its existing balance
%balanceTo = call @balanceOf(%to)
%balanceTo = add %balanceTo, %value // no worries of overflow
call @setBalance(%to, %balanceTo)
// log the transfer
call @log.Transfer(%from, %to, %value)
// should either return true or throw in case of insufficient funds in the
// source or insufficient allowance for this caller
ret true
throw:
call @iele.invalid() // throwing exception
}
}