Skip to content

Commit 621c230

Browse files
committedJun 27, 2023
Guía Cairo 1 Test
1 parent ff6e222 commit 621c230

File tree

3 files changed

+1640
-1632
lines changed

3 files changed

+1640
-1632
lines changed
 

‎README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1078,7 +1078,7 @@ Aquí deberemos pasar los argumentos del constructor, en este caso pasamos `name
10781078

10791079

10801080
```bash
1081-
starknet deploy --class_hash 0x0731affa00504b8311180a11585108f15296bf91b024c76a96057959c608d037 --inputs 336641417577 5128521 18 1000000000 0 1795950254530259382270168937734171348535331377400385313842303804539016002736 --account Nadai
1081+
starknet deploy --class_hash 0x0731affa00504b8311180a11585108f15296bf91b024c76a96057959c608d037 --inputs 336641417577 5128521 18 --account Nadai
10821082
```
10831083

10841084
Si todo ha ido bien deberá salir la transacción con la diección del contrato que ha desplagado.

‎src/ERC20.cairo

+77-117
Original file line numberDiff line numberDiff line change
@@ -1,166 +1,126 @@
1+
use starknet::ContractAddress;
2+
3+
trait IERC20 {
4+
fn name() -> felt252;
5+
fn symbol() -> felt252;
6+
fn decimals() -> u8;
7+
fn total_supply() -> u256;
8+
fn balanceOf(account: ContractAddress) -> u256;
9+
fn allowance(owner: ContractAddress, spender: ContractAddress) -> u256;
10+
fn transfer(recipient: ContractAddress, amount: u256) -> bool;
11+
fn transferFrom(sender: ContractAddress, recipient: ContractAddress, amount: u256) -> bool;
12+
fn approve(spender: ContractAddress, amount: u256) -> bool;
13+
}
14+
115
#[contract]
2-
mod ERC20Contract {
3-
////////////////////////////////
4-
// library imports
5-
////////////////////////////////
6-
use zeroable::Zeroable;
16+
mod ERC20 {
717
use starknet::get_caller_address;
818
use starknet::ContractAddress;
9-
use starknet::contract_address_try_from_felt252;
10-
use traits::Into;
11-
use traits::TryInto;
12-
use option::OptionTrait;
13-
14-
////////////////////////////////
15-
// storage variables
16-
////////////////////////////////
19+
use zeroable::Zeroable;
20+
use super::IERC20;
21+
1722
struct Storage {
18-
name: felt252,
19-
symbol: felt252,
20-
decimals: u8,
21-
total_supply: u256,
22-
balances: LegacyMap::<ContractAddress, u256>,
23-
allowances: LegacyMap::<(ContractAddress, ContractAddress), u256>,
23+
_name: felt252,
24+
_symbol: felt252,
25+
_decimals: u8,
26+
_total_supply: u256,
27+
_balances: LegacyMap<ContractAddress, u256>,
28+
_allowances: LegacyMap<(ContractAddress, ContractAddress), u256>,
2429
}
2530

26-
////////////////////////////////
27-
// Transfer event emitted on token transfer
28-
////////////////////////////////
2931
#[event]
3032
fn Transfer(from: ContractAddress, to: ContractAddress, value: u256) {}
3133

32-
////////////////////////////////
33-
// Approval event emitted on token approval
34-
////////////////////////////////
3534
#[event]
3635
fn Approval(owner: ContractAddress, spender: ContractAddress, value: u256) {}
3736

38-
////////////////////////////////
39-
// Constructor - initialized on deployment
40-
////////////////////////////////
4137
#[constructor]
42-
fn constructor(
43-
name_: felt252,
44-
symbol_: felt252,
45-
decimals_: u8,
46-
initial_supply: u256,
47-
recipient: ContractAddress
48-
){
49-
name::write(name_);
50-
symbol::write(symbol_);
51-
decimals::write(decimals_);
52-
assert(recipient.is_non_zero(), 'ERC20: mint to the 0 address');
53-
total_supply::write(initial_supply);
54-
balances::write(recipient, initial_supply);
55-
Transfer(Zeroable::zero(), recipient, initial_supply);
38+
fn constructor(name: felt252, symbol: felt252, decimals: u8) {
39+
_name::write(name);
40+
_symbol::write(symbol);
41+
_decimals::write(decimals);
5642
}
5743

58-
////////////////////////////////
59-
// get_name function returns token name
60-
////////////////////////////////
6144
#[view]
62-
fn get_name() -> felt252 {
63-
name::read()
45+
fn name() -> felt252 {
46+
_name::read()
6447
}
6548

66-
////////////////////////////////
67-
// get_symbol function returns token symbol
68-
////////////////////////////////
6949
#[view]
70-
fn get_symbol() -> felt252 {
71-
symbol::read()
50+
fn symbol() -> felt252 {
51+
_symbol::read()
7252
}
7353

74-
////////////////////////////////
75-
// get_decimals function returns token decimals
76-
////////////////////////////////
7754
#[view]
78-
fn get_decimals() -> u8 {
79-
decimals::read()
55+
fn decimals() -> u8 {
56+
_decimals::read()
8057
}
8158

82-
////////////////////////////////
83-
// get_total_supply function returns token total total_supply
84-
////////////////////////////////
8559
#[view]
86-
fn get_total_supply() -> u256 {
87-
total_supply::read()
60+
fn total_supply() -> u256 {
61+
_total_supply::read()
8862
}
8963

90-
////////////////////////////////
91-
// balance_of function returns balance of an account
92-
////////////////////////////////
9364
#[view]
94-
fn balance_of(account: ContractAddress) -> u256 {
95-
balances::read(account)
65+
fn balanceOf(account: ContractAddress) -> u256 {
66+
_balances::read(account)
9667
}
9768

98-
////////////////////////////////
99-
// allowance function returns total allowamnce given to an account
100-
////////////////////////////////
10169
#[view]
10270
fn allowance(owner: ContractAddress, spender: ContractAddress) -> u256 {
103-
allowances::read((owner, spender))
71+
_allowances::read((owner, spender))
10472
}
10573

106-
////////////////////////////////
107-
// transfer function for transferring tokens from one account to another
108-
////////////////////////////////
10974
#[external]
110-
fn transfer(recipient: ContractAddress, amount: u256) {
111-
let sender = get_caller_address();
112-
transfer_helper(sender, recipient, amount);
75+
fn approve(spender: ContractAddress, amount: u256) -> bool {
76+
let owner = get_caller_address();
77+
78+
_allowances::write((owner, spender), amount);
79+
80+
Approval(owner, spender, amount);
81+
82+
true
11383
}
11484

115-
////////////////////////////////
116-
// transfer_from function for transferring tokens on behalf of another user
117-
////////////////////////////////
11885
#[external]
119-
fn transfer_from(sender: ContractAddress, recipient: ContractAddress, amount: u256) {
120-
let caller = get_caller_address();
121-
spend_allowance(sender, caller, amount);
122-
transfer_helper(sender, recipient, amount);
86+
fn mint(amount: u256) {
87+
let sender = get_caller_address();
88+
89+
_total_supply::write(_total_supply::read() + amount);
90+
_balances::write(sender, _balances::read(sender) + amount);
12391
}
12492

125-
////////////////////////////////
126-
// approve function for approving another user to spend from an account balance
127-
////////////////////////////////
12893
#[external]
129-
fn approve(spender: ContractAddress, amount: u256) {
130-
let caller = get_caller_address();
131-
approve_helper(caller, spender, amount);
132-
}
94+
fn transfer(to: ContractAddress, amount: u256) -> bool {
95+
let from = get_caller_address();
96+
97+
_balances::write(from, _balances::read(from) - amount);
98+
_balances::write(to, _balances::read(to) + amount);
99+
100+
Transfer(from, to, amount);
133101

134-
////////////////////////////////
135-
// internal function that contains the tranfer logic
136-
////////////////////////////////
137-
fn transfer_helper(sender: ContractAddress, recipient: ContractAddress, amount: u256) {
138-
assert(!sender.is_zero(), 'ERC20: transfer from 0');
139-
assert(!recipient.is_zero(), 'ERC20: transfer to 0');
140-
balances::write(sender, balances::read(sender) - amount);
141-
balances::write(recipient, balances::read(recipient) + amount);
142-
Transfer(sender, recipient, amount);
102+
true
143103
}
144104

145-
////////////////////////////////
146-
// internal function implementing checks against unlimited allowance
147-
////////////////////////////////
148-
fn spend_allowance(owner: ContractAddress, spender: ContractAddress, amount: u256) {
149-
let current_allowance = allowances::read((owner, spender));
105+
#[external]
106+
fn transferFrom(from: ContractAddress, to: ContractAddress, amount: u256) -> bool {
107+
let caller = get_caller_address();
108+
let allowed: u256 = _allowances::read((from, caller));
109+
150110
let ONES_MASK = 0xffffffffffffffffffffffffffffffff_u128;
151-
let is_unlimited_allowance =
152-
current_allowance.low == ONES_MASK & current_allowance.high == ONES_MASK;
153-
if !is_unlimited_allowance {
154-
approve_helper(owner, spender, current_allowance - amount);
111+
112+
let is_max = (allowed.low == ONES_MASK) & (allowed.high == ONES_MASK);
113+
114+
if !is_max {
115+
_allowances::write((from, caller), allowed - amount);
116+
Approval(from, caller, allowed - amount);
155117
}
156-
}
157118

158-
////////////////////////////////
159-
// internal function containing the approval logic
160-
////////////////////////////////
161-
fn approve_helper(owner: ContractAddress, spender: ContractAddress, amount: u256) {
162-
assert(!spender.is_zero(), 'ERC20: approve from 0');
163-
allowances::write((owner, spender), amount);
164-
Approval(owner, spender, amount);
119+
_balances::write(from, _balances::read(from) - amount);
120+
_balances::write(to, _balances::read(to) + amount);
121+
122+
Transfer(from, to, amount);
123+
124+
true
165125
}
166126
}

0 commit comments

Comments
 (0)