-
Notifications
You must be signed in to change notification settings - Fork 14
/
dai.cairo
378 lines (316 loc) · 10.4 KB
/
dai.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
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
// amarna: disable=arithmetic-sub,must-check-caller-address,must-check-overflow
//
// SPDX-License-Identifier: AGPL-3.0-or-later
// Copyright (C) 2021 Dai Foundation
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, BitwiseBuiltin
from starkware.cairo.common.math import assert_not_equal, assert_not_zero
from starkware.starknet.common.syscalls import get_caller_address, get_contract_address
from starkware.cairo.common.uint256 import (
Uint256,
uint256_add,
uint256_sub,
uint256_eq,
uint256_le,
uint256_check,
)
const ALL_ONES = 2 ** 128 - 1;
@event
func Rely(user: felt) {
}
@event
func Deny(user: felt) {
}
@event
func Transfer(sender: felt, recipient: felt, value: Uint256) {
}
@event
func Approval(owner: felt, spender: felt, value: Uint256) {
}
@storage_var
func _wards(user: felt) -> (res: felt) {
}
@storage_var
func _balances(user: felt) -> (res: Uint256) {
}
@storage_var
func _total_supply() -> (res: Uint256) {
}
@storage_var
func _allowances(owner: felt, spender: felt) -> (res: Uint256) {
}
@view
func decimals{}() -> (res: felt) {
return (18,);
}
@view
func name{}() -> (res: felt) {
return ('Dai Stablecoin',);
}
@view
func symbol{}() -> (res: felt) {
return ('DAI',);
}
@view
func totalSupply{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
res: Uint256
) {
let (res) = _total_supply.read();
return (res,);
}
@view
func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(user: felt) -> (
res: Uint256
) {
let (res) = _balances.read(user=user);
return (res,);
}
@view
func allowance{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
owner: felt, spender: felt
) -> (res: Uint256) {
let (res) = _allowances.read(owner, spender);
return (res,);
}
@view
func wards{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(user: felt) -> (
res: felt
) {
let (res) = _wards.read(user);
return (res,);
}
@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(ward: felt) {
_wards.write(ward, 1);
Rely.emit(ward);
return ();
}
@external
func mint{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(account: felt, amount: Uint256) {
auth();
// check valid recipient
with_attr error_message("dai/invalid-recipient") {
assert_not_zero(account);
let (contract_address) = get_contract_address();
assert_not_equal(account, contract_address);
}
// check valid amount
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
// update balance
let (balance) = _balances.read(account);
// overflow check disabled since later amount + total_supply is checked for overflow
// and total_supply >= balance
let (new_balance, _) = uint256_add(balance, amount);
_balances.write(account, new_balance);
// update total supply
let (total) = _total_supply.read();
let (new_total) = uint256_add_safe(total, amount);
_total_supply.write(new_total);
Transfer.emit(0, account, amount);
return ();
}
@external
func burn{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(account: felt, amount: Uint256) {
alloc_locals;
let (local caller) = get_caller_address();
// check valid amount
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
// update balance
let (local balance) = _balances.read(account);
with_attr error_message("dai/insufficient-balance") {
assert_uint256_le(amount, balance);
}
let (new_balance) = uint256_sub(balance, amount);
_balances.write(account, new_balance);
// decrease supply
let (local total_supply) = _total_supply.read();
// underflow check disabled since amount <= balance <= total_supply
let (new_total_supply) = uint256_sub(total_supply, amount);
_total_supply.write(new_total_supply);
Transfer.emit(account, 0, amount);
if (caller != account) {
let (allowance) = _allowances.read(account, caller);
let MAX = Uint256(low=ALL_ONES, high=ALL_ONES);
let (eq) = uint256_eq(allowance, MAX);
if (eq == 0) {
with_attr error_message("dai/insufficient-allowance") {
assert_uint256_le(amount, allowance);
}
let (new_allowance) = uint256_sub(allowance, amount);
_allowances.write(account, caller, new_allowance);
return ();
} else {
return ();
}
}
return ();
}
@external
func rely{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(user: felt) {
auth();
_wards.write(user, 1);
Rely.emit(user);
return ();
}
@external
func deny{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(user: felt) {
auth();
_wards.write(user, 0);
Deny.emit(user);
return ();
}
@external
func transfer{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(recipient: felt, amount: Uint256) -> (res: felt) {
let (caller) = get_caller_address();
_transfer(caller, recipient, amount);
return (res=1);
}
@external
func transferFrom{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(sender: felt, recipient: felt, amount: Uint256) -> (res: felt) {
alloc_locals;
let (local caller) = get_caller_address();
_transfer(sender, recipient, amount);
if (caller != sender) {
let (allowance) = _allowances.read(sender, caller);
let MAX = Uint256(low=ALL_ONES, high=ALL_ONES);
let (max_allowance) = uint256_eq(allowance, MAX);
if (max_allowance == 0) {
with_attr error_message("dai/insufficient-allowance") {
assert_uint256_le(amount, allowance);
}
let (new_allowance: Uint256) = uint256_sub(allowance, amount);
_allowances.write(sender, caller, new_allowance);
return (res=1);
} else {
return (res=1);
}
}
return (res=1);
}
@external
func approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
spender: felt, amount: Uint256
) -> (res: felt) {
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
let (caller) = get_caller_address();
_approve(caller, spender, amount);
return (res=1);
}
@external
func increaseAllowance{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(spender: felt, amount: Uint256) -> (res: felt) {
alloc_locals;
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
let (local caller) = get_caller_address();
let (allowance) = _allowances.read(caller, spender);
let (new_allowance) = uint256_add_safe(amount, allowance);
_approve(caller, spender, new_allowance);
return (res=1);
}
@external
func decreaseAllowance{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(spender: felt, amount: Uint256) -> (res: felt) {
alloc_locals;
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
let (local caller) = get_caller_address();
let (local allowance) = _allowances.read(caller, spender);
with_attr error_message("dai/insufficient-allowance") {
assert_uint256_le(amount, allowance);
}
let (new_allowance) = uint256_sub(allowance, amount);
_approve(caller, spender, new_allowance);
return (res=1);
}
func auth{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
let (caller) = get_caller_address();
let (ward) = _wards.read(caller);
with_attr error_message("dai/not-authorized") {
assert ward = 1;
}
return ();
}
func _transfer{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(sender: felt, recipient: felt, amount: Uint256) {
alloc_locals;
// check valid amount
with_attr error_message("dai/invalid-amount") {
uint256_check(amount);
}
with_attr error_message("dai/invalid-recipient") {
assert_not_zero(recipient);
let (contract_address) = get_contract_address();
assert_not_equal(recipient, contract_address);
}
// decrease sender balance
let (local sender_balance) = _balances.read(sender);
with_attr error_message("dai/insufficient-balance") {
assert_uint256_le(amount, sender_balance);
}
let (new_balance) = uint256_sub(sender_balance, amount);
_balances.write(sender, new_balance);
// increase recipient balance
let (recipient_balance) = _balances.read(recipient);
let (sum) = uint256_add_safe(recipient_balance, amount);
_balances.write(recipient, sum);
Transfer.emit(sender, recipient, amount);
return ();
}
func _approve{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
caller: felt, spender: felt, amount: Uint256
) {
with_attr error_message("dai/invalid-recipient") {
assert_not_zero(spender);
}
_allowances.write(caller, spender, amount);
Approval.emit(caller, spender, amount);
return ();
}
func uint256_add_safe{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
a: Uint256, b: Uint256
) -> (sum: Uint256) {
let (sum, carry) = uint256_add(a, b);
with_attr error_message("dai/uint256-overflow") {
assert carry = 0;
}
return (sum,);
}
func assert_uint256_le{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
a: Uint256, b: Uint256
) {
let (is_le) = uint256_le(a, b);
assert is_le = 1;
return ();
}