-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patherc20_call.cairo
90 lines (73 loc) · 2.65 KB
/
erc20_call.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
use core::num::traits::Zero;
use openzeppelin_token::erc20::{ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use starknet::syscalls::{call_contract_syscall, deploy_syscall};
use starknet::{ClassHash, ContractAddress, SyscallResultTrait};
#[derive(Copy, Debug, Drop, Serde)]
pub struct ERC20Contract {
pub address: ContractAddress,
}
impl ContractAddressIntoTokenAddr of Into<ContractAddress, ERC20Contract> {
fn into(self: ContractAddress) -> ERC20Contract {
ERC20Contract { address: self }
}
}
impl ERC20ContractIntoFelt252 of Into<ERC20Contract, felt252> {
fn into(self: ERC20Contract) -> felt252 {
self.address.into()
}
}
#[generate_trait]
pub impl ERC20ContractImpl of ERC20ContractTrait {
fn dispatcher(self: @ERC20Contract) -> ERC20ABIDispatcher {
ERC20ABIDispatcher { contract_address: *self.address }
}
fn create(
class_hash: ClassHash,
salt: felt252,
name: ByteArray,
symbol: ByteArray,
decimals: u8,
owner: ContractAddress,
) -> ERC20Contract {
let mut call_data = array![];
(name, symbol, decimals, owner).serialize(ref call_data);
let (address, _) = deploy_syscall(class_hash, salt, call_data.span(), false)
.unwrap_syscall();
ERC20Contract { address }
}
fn transfer(self: @ERC20Contract, recipient: ContractAddress, amount: u256) -> bool {
self.dispatcher().transfer(recipient, amount)
}
fn transfer_from(
self: @ERC20Contract, sender: ContractAddress, recipient: ContractAddress, amount: u256,
) -> bool {
self.dispatcher().transfer_from(sender, recipient, amount)
}
fn mint(self: @ERC20Contract, amount: u256) {
let mut calldata = array![];
amount.serialize(ref calldata);
call_contract_syscall(*self.address, selector!("mint"), calldata.span()).unwrap_syscall();
}
fn burn(self: @ERC20Contract, amount: u256) {
let mut calldata = array![];
amount.serialize(ref calldata);
call_contract_syscall(*self.address, selector!("burn"), calldata.span()).unwrap_syscall();
}
fn balance_of(self: @ERC20Contract, from_account: ContractAddress) -> u256 {
self.dispatcher().balance_of(from_account)
}
fn total_supply(self: @ERC20Contract) -> u256 {
self.dispatcher().total_supply()
}
}
impl ERC20ContractZero of Zero<ERC20Contract> {
fn zero() -> ERC20Contract {
ERC20Contract { address: 0.try_into().unwrap() }
}
fn is_zero(self: @ERC20Contract) -> bool {
self.address.is_zero()
}
fn is_non_zero(self: @ERC20Contract) -> bool {
!self.is_zero()
}
}