-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDERC20.cairo
258 lines (221 loc) · 6.14 KB
/
TDERC20.cairo
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
%lang starknet
%builtins pedersen range_check
from starkware.starknet.common.syscalls import get_caller_address
from starkware.cairo.common.cairo_builtins import HashBuiltin
from starkware.cairo.common.math import assert_not_zero
from starkware.cairo.common.uint256 import (
Uint256, uint256_add, uint256_sub, uint256_le, uint256_lt, uint256_check
)
from contracts.token.ERC20_base import (
ERC20_initializer,
ERC20_allowances,
ERC20_approve,
ERC20_transfer,
ERC20_mint,
ERC20_burn
)
@storage_var
func Teacher_accounts(account: felt) -> (balance: felt):
end
@storage_var
func setup_is_finished() -> (setup_is_finished : felt):
end
@constructor
func constructor{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(
name: felt,
symbol: felt,
initial_supply: Uint256,
recipient: felt,
owner: felt
):
ERC20_initializer(name, symbol, initial_supply, recipient)
let (caller) = get_caller_address()
Teacher_accounts.write(caller, 1)
return ()
end
#
# Externals
#
@external
func transfer{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(recipient: felt, amount: Uint256) -> (success: felt):
# These points should not be transferred
# Cairo equivalent to 'return (false)'
return (0)
end
@external
func transferFrom{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(
sender: felt,
recipient: felt,
amount: Uint256
) -> (success: felt):
# These points should not be transferred
# Cairo equivalent to 'return (false)'
return (0)
end
@external
func approve{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(spender: felt, amount: Uint256) -> (success: felt):
let (caller) = get_caller_address()
ERC20_approve(caller, spender, amount)
# Cairo equivalent to 'return (true)'
return (1)
end
@external
func increaseAllowance{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(spender: felt, added_value: Uint256) -> (success: felt):
alloc_locals
uint256_check(added_value)
let (local caller) = get_caller_address()
let (local current_allowance: Uint256) = ERC20_allowances.read(caller, spender)
# add allowance
let (local new_allowance: Uint256, is_overflow) = uint256_add(current_allowance, added_value)
assert (is_overflow) = 0
ERC20_approve(caller, spender, new_allowance)
# Cairo equivalent to 'return (true)'
return (1)
end
@external
func decreaseAllowance{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(spender: felt, subtracted_value: Uint256) -> (success: felt):
alloc_locals
uint256_check(subtracted_value)
let (local caller) = get_caller_address()
let (local current_allowance: Uint256) = ERC20_allowances.read(owner=caller, spender=spender)
let (local new_allowance: Uint256) = uint256_sub(current_allowance, subtracted_value)
# validates new_allowance < current_allowance and returns 1 if true
let (enough_allowance) = uint256_lt(new_allowance, current_allowance)
assert_not_zero(enough_allowance)
ERC20_approve(caller, spender, new_allowance)
# Cairo equivalent to 'return (true)'
return (1)
end
func only_teacher_or_exercice{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}():
let (caller) = get_caller_address()
let (permission) = Teacher_accounts.read(account=caller)
assert permission = 1
return ()
end
@external
func distribute_points{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(to: felt, amount: Uint256):
only_teacher_or_exercice()
ERC20_mint(to, amount)
return ()
end
@external
func remove_points{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(to: felt, amount: Uint256):
only_teacher_or_exercice()
ERC20_burn(to, amount)
return ()
end
@external
func set_teacher{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(account: felt, permission: felt):
only_teacher_or_exercice()
Teacher_accounts.write(account, permission)
return ()
end
@view
func isTeacher{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}(account: felt) -> (permission: felt):
let (permission: felt) = Teacher_accounts.read(account)
return (permission)
end
##
## Temporary functions, will remove once account contracts are live and usable with Nile
##
##
func only_during_setup{
syscall_ptr : felt*,
pedersen_ptr : HashBuiltin*,
range_check_ptr
}():
let (permission) = setup_is_finished.read()
assert permission = 0
return ()
end
@external
func finish_setup{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}():
only_during_setup()
Teacher_accounts.write(0, 0)
setup_is_finished.write(1)
return ()
end
@external
func set_teachers_temp{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(accounts_len: felt, accounts: felt*):
only_during_setup()
set_teacher_internal(accounts_len, accounts)
return ()
end
@external
func set_teacher_temp{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(account: felt):
only_during_setup()
Teacher_accounts.write(account, 1)
return ()
end
func set_teacher_internal{
syscall_ptr: felt*,
pedersen_ptr: HashBuiltin*,
range_check_ptr
}(accounts_len: felt, accounts: felt*):
only_during_setup()
if accounts_len == 0:
# Start with sum=0.
return ()
end
# If length is NOT zero, then the function calls itself again, by moving forward one slot
set_teacher_internal(accounts_len=accounts_len - 1, accounts=accounts + 1)
# This part of the function is first reached when length=0.
Teacher_accounts.write([accounts], 1)
return ()
end