-
Notifications
You must be signed in to change notification settings - Fork 366
/
Copy patherc20.cairo
300 lines (274 loc) · 10.9 KB
/
erc20.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
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts for Cairo v0.9.0 (token/erc20/erc20.cairo)
/// # ERC20 Component
///
/// The ERC20 component provides an implementation of the IERC20 interface as well as
/// non-standard implementations that can be used to create an ERC20 contract. This
/// component is agnostic regarding how tokens are created, which means that developers
/// must create their own token distribution mechanism.
/// See [the documentation](https://docs.openzeppelin.com/contracts-cairo/0.9.0/guides/erc20-supply)
/// for examples.
#[starknet::component]
mod ERC20Component {
use integer::BoundedInt;
use openzeppelin::token::erc20::interface;
use starknet::ContractAddress;
use starknet::get_caller_address;
#[storage]
struct Storage {
ERC20_name: felt252,
ERC20_symbol: felt252,
ERC20_total_supply: u256,
ERC20_balances: LegacyMap<ContractAddress, u256>,
ERC20_allowances: LegacyMap<(ContractAddress, ContractAddress), u256>,
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
Transfer: Transfer,
Approval: Approval,
}
/// Emitted when tokens are moved from address `from` to address `to`.
#[derive(Drop, starknet::Event)]
struct Transfer {
#[key]
from: ContractAddress,
#[key]
to: ContractAddress,
value: u256
}
/// Emitted when the allowance of a `spender` for an `owner` is set by a call
/// to `approve`. `value` is the new allowance.
#[derive(Drop, starknet::Event)]
struct Approval {
#[key]
owner: ContractAddress,
#[key]
spender: ContractAddress,
value: u256
}
mod Errors {
const APPROVE_FROM_ZERO: felt252 = 'ERC20: approve from 0';
const APPROVE_TO_ZERO: felt252 = 'ERC20: approve to 0';
const TRANSFER_FROM_ZERO: felt252 = 'ERC20: transfer from 0';
const TRANSFER_TO_ZERO: felt252 = 'ERC20: transfer to 0';
const BURN_FROM_ZERO: felt252 = 'ERC20: burn from 0';
const MINT_TO_ZERO: felt252 = 'ERC20: mint to 0';
}
//
// External
//
#[embeddable_as(ERC20Impl)]
impl ERC20<
TContractState, +HasComponent<TContractState>
> of interface::IERC20<ComponentState<TContractState>> {
/// Returns the value of tokens in existence.
fn total_supply(self: @ComponentState<TContractState>) -> u256 {
self.ERC20_total_supply.read()
}
/// Returns the amount of tokens owned by `account`.
fn balance_of(self: @ComponentState<TContractState>, account: ContractAddress) -> u256 {
self.ERC20_balances.read(account)
}
/// Returns the remaining number of tokens that `spender` is
/// allowed to spend on behalf of `owner` through `transfer_from`.
/// This is zero by default.
/// This value changes when `approve` or `transfer_from` are called.
fn allowance(
self: @ComponentState<TContractState>, owner: ContractAddress, spender: ContractAddress
) -> u256 {
self.ERC20_allowances.read((owner, spender))
}
/// Moves `amount` tokens from the caller's token balance to `to`.
///
/// Requirements:
///
/// - `recipient` is not the zero address.
/// - The caller has a balance of at least `amount`.
///
/// Emits a `Transfer` event.
fn transfer(
ref self: ComponentState<TContractState>, recipient: ContractAddress, amount: u256
) -> bool {
let sender = get_caller_address();
self._transfer(sender, recipient, amount);
true
}
/// Moves `amount` tokens from `from` to `to` using the allowance mechanism.
/// `amount` is then deducted from the caller's allowance.
///
/// Requirements:
///
/// - `sender` is not the zero address.
/// - `sender` must have a balance of at least `amount`.
/// - `recipient` is not the zero address.
/// - The caller has an allowance of `sender`'s tokens of at least `amount`.
///
/// Emits a `Transfer` event.
fn transfer_from(
ref self: ComponentState<TContractState>,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256
) -> bool {
let caller = get_caller_address();
self._spend_allowance(sender, caller, amount);
self._transfer(sender, recipient, amount);
true
}
/// Sets `amount` as the allowance of `spender` over the caller’s tokens.
///
/// Requirements:
///
/// - `spender` is not the zero address.
///
/// Emits an `Approval` event.
fn approve(
ref self: ComponentState<TContractState>, spender: ContractAddress, amount: u256
) -> bool {
let caller = get_caller_address();
self._approve(caller, spender, amount);
true
}
}
#[embeddable_as(ERC20MetadataImpl)]
impl ERC20Metadata<
TContractState, +HasComponent<TContractState>
> of interface::IERC20Metadata<ComponentState<TContractState>> {
/// Returns the name of the token.
fn name(self: @ComponentState<TContractState>) -> felt252 {
self.ERC20_name.read()
}
/// Returns the ticker symbol of the token, usually a shorter version of the name.
fn symbol(self: @ComponentState<TContractState>) -> felt252 {
self.ERC20_symbol.read()
}
/// Returns the number of decimals used to get its user representation.
fn decimals(self: @ComponentState<TContractState>) -> u8 {
18
}
}
/// Adds camelCase support for `IERC20`.
#[embeddable_as(ERC20CamelOnlyImpl)]
impl ERC20CamelOnly<
TContractState, +HasComponent<TContractState>
> of interface::IERC20CamelOnly<ComponentState<TContractState>> {
fn totalSupply(self: @ComponentState<TContractState>) -> u256 {
self.total_supply()
}
fn balanceOf(self: @ComponentState<TContractState>, account: ContractAddress) -> u256 {
self.balance_of(account)
}
fn transferFrom(
ref self: ComponentState<TContractState>,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256
) -> bool {
self.transfer_from(sender, recipient, amount)
}
}
//
// Internal
//
#[generate_trait]
impl InternalImpl<
TContractState, +HasComponent<TContractState>
> of InternalTrait<TContractState> {
/// Initializes the contract by setting the token name and symbol.
/// To prevent reinitialization, this should only be used inside of a contract's constructor.
fn initializer(ref self: ComponentState<TContractState>, name: felt252, symbol: felt252) {
self.ERC20_name.write(name);
self.ERC20_symbol.write(symbol);
}
/// Internal method that moves an `amount` of tokens from `from` to `to`.
///
/// Requirements:
///
/// - `sender` is not the zero address.
/// - `sender` must have at least a balance of `amount`.
/// - `recipient` is not the zero address.
///
/// Emits a `Transfer` event.
fn _transfer(
ref self: ComponentState<TContractState>,
sender: ContractAddress,
recipient: ContractAddress,
amount: u256
) {
assert(!sender.is_zero(), Errors::TRANSFER_FROM_ZERO);
assert(!recipient.is_zero(), Errors::TRANSFER_TO_ZERO);
self.ERC20_balances.write(sender, self.ERC20_balances.read(sender) - amount);
self.ERC20_balances.write(recipient, self.ERC20_balances.read(recipient) + amount);
self.emit(Transfer { from: sender, to: recipient, value: amount });
}
/// Internal method that sets `amount` as the allowance of `spender` over the
/// `owner`s tokens.
///
/// Requirements:
///
/// - `owner` is not the zero address.
/// - `spender` is not the zero address.
///
/// Emits an `Approval` event.
fn _approve(
ref self: ComponentState<TContractState>,
owner: ContractAddress,
spender: ContractAddress,
amount: u256
) {
assert(!owner.is_zero(), Errors::APPROVE_FROM_ZERO);
assert(!spender.is_zero(), Errors::APPROVE_TO_ZERO);
self.ERC20_allowances.write((owner, spender), amount);
self.emit(Approval { owner, spender, value: amount });
}
/// Creates a `value` amount of tokens and assigns them to `account`.
///
/// Requirements:
///
/// - `recipient` is not the zero address.
///
/// Emits a `Transfer` event with `from` set to the zero address.
fn _mint(
ref self: ComponentState<TContractState>, recipient: ContractAddress, amount: u256
) {
assert(!recipient.is_zero(), Errors::MINT_TO_ZERO);
self.ERC20_total_supply.write(self.ERC20_total_supply.read() + amount);
self.ERC20_balances.write(recipient, self.ERC20_balances.read(recipient) + amount);
self.emit(Transfer { from: Zeroable::zero(), to: recipient, value: amount });
}
/// Destroys `amount` of tokens from `account`.
///
/// Requirements:
///
/// - `account` is not the zero address.
/// - `account` must have at least a balance of `amount`.
///
/// Emits a `Transfer` event with `to` set to the zero address.
fn _burn(ref self: ComponentState<TContractState>, account: ContractAddress, amount: u256) {
assert(!account.is_zero(), Errors::BURN_FROM_ZERO);
self.ERC20_total_supply.write(self.ERC20_total_supply.read() - amount);
self.ERC20_balances.write(account, self.ERC20_balances.read(account) - amount);
self.emit(Transfer { from: account, to: Zeroable::zero(), value: amount });
}
/// Updates `owner`s allowance for `spender` based on spent `amount`.
/// Does not update the allowance value in case of infinite allowance.
///
/// Requirements:
///
/// - `spender` must have at least an allowance of `amount` from `owner`.
///
/// Possibly emits an `Approval` event.
fn _spend_allowance(
ref self: ComponentState<TContractState>,
owner: ContractAddress,
spender: ContractAddress,
amount: u256
) {
let current_allowance = self.ERC20_allowances.read((owner, spender));
if current_allowance != BoundedInt::max() {
self._approve(owner, spender, current_allowance - amount);
}
}
}
}