-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patherc721Base.cairo
190 lines (165 loc) · 5.95 KB
/
erc721Base.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
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::Event;
use starknet::Span;
use starknet::felt252;
#[starknet::interface]
pub trait ERC721ABI<TContractState> {
fn name(self: @TContractState) -> ByteArray;
fn symbol(self: @TContractState) -> ByteArray;
fn token_uri(self: @TContractState, token_id: u256) -> ByteArray;
fn balance_of(self: @TContractState, account: ContractAddress) -> u256;
fn owner_of(self: @TContractState, token_id: u256) -> ContractAddress;
fn safe_transfer_from(
ref self: TContractState,
from: ContractAddress,
to: ContractAddress,
token_id: u256,
data: Span<felt252>,
);
fn transfer_from(
ref self: TContractState, from: ContractAddress, to: ContractAddress, token_id: u256
);
fn approve(ref self: TContractState, to: ContractAddress, token_id: u256);
fn set_approval_for_all(ref self: TContractState, operator: ContractAddress, approved: bool);
fn get_approved(self: @TContractState, token_id: u256) -> ContractAddress;
fn is_approved_for_all(
self: @TContractState, owner: ContractAddress, operator: ContractAddress
) -> bool;
}
#[starknet::contract]
pub mod ERC721 {
use starknet::ContractAddress;
use starknet::get_caller_address;
use starknet::storage::{Map, StorageMapReadAccess, StorageMapWriteAccess};
use starknet::Event;
#[storage]
pub struct Storage {
_name: ByteArray,
_symbol: ByteArray,
_owners: Map<u256, ContractAddress>,
_balances: Map<ContractAddress, u256>,
_token_approvals: Map<u256, ContractAddress>,
_operator_approvals: Map<(ContractAddress, ContractAddress), bool>,
_base_uri: ByteArray,
}
#[event]
#[derive(Drop, PartialEq, starknet::Event)]
pub enum Event {
Transfer: Transfer,
Approval: Approval,
ApprovalForAll: ApprovalForAll,
}
/// Emitted when `token_id` token is transferred from `from` to `to`.
#[derive(Drop, PartialEq, starknet::Event)]
pub struct Transfer {
#[key]
pub from: ContractAddress,
#[key]
pub to: ContractAddress,
#[key]
pub token_id: u256,
}
/// Emitted when `owner` enables `approved` to manage the `token_id` token.
#[derive(Drop, PartialEq, starknet::Event)]
pub struct Approval {
#[key]
pub owner: ContractAddress,
#[key]
pub approved: ContractAddress,
#[key]
pub token_id: u256,
}
/// Emitted when `owner` enables or disables (`approved`) `operator` to manage
/// all of its assets.
#[derive(Drop, PartialEq, starknet::Event)]
pub struct ApprovalForAll {
#[key]
pub owner: ContractAddress,
#[key]
pub operator: ContractAddress,
pub approved: bool,
}
#[abi(embed_v0)]
impl ERC721Impl of super::ERC721ABI<ContractState> {
fn name(self: @ContractState) -> ByteArray {
self._name.read()
}
fn symbol(self: @ContractState) -> ByteArray {
self._symbol.read()
}
fn token_uri(self: @ContractState, token_id: u256) -> ByteArray {
self._base_uri.read()
}
fn balance_of(self: @ContractState, account: ContractAddress) -> u256 {
self._balances.read(account)
}
fn owner_of(self: @ContractState, token_id: u256) -> ContractAddress {
self._owners.read(token_id)
}
fn transfer_from(
ref self: ContractState, from: ContractAddress, to: ContractAddress, token_id: u256
) {
let caller = get_caller_address();
assert!(
caller == from
|| self.get_approved(token_id) == caller
|| self.is_approved_for_all(from, caller),
"Not authorized to transfer"
);
self._owners.write(token_id, to);
self._balances.write(from, self._balances.read(from) - 1);
self._balances.write(to, self._balances.read(to) + 1);
// Emit Transfer event
self.emit(Event::Transfer(Transfer {
from: from,
to: to,
token_id: token_id,
}));
}
fn safe_transfer_from(
ref self: ContractState,
from: ContractAddress,
to: ContractAddress,
token_id: u256,
_data: Span<felt252>
) {
self.transfer_from(from, to, token_id);
}
fn approve(ref self: ContractState, to: ContractAddress, token_id: u256) {
let owner = self.owner_of(token_id);
let caller = get_caller_address();
assert!(
caller == owner || self.is_approved_for_all(owner, caller),
"Caller is not owner nor approved"
);
self._token_approvals.write(token_id, to);
// Emit Approval event
self.emit(Event::Approval(Approval {
owner: owner,
approved: to,
token_id: token_id,
}));
}
fn set_approval_for_all(
ref self: ContractState, operator: ContractAddress, approved: bool
) {
let owner = get_caller_address();
self._operator_approvals.write((owner, operator), approved);
self.emit(Event::ApprovalForAll(ApprovalForAll {
owner: owner,
operator: operator,
approved: approved,
}));
}
fn get_approved(self: @ContractState, token_id: u256) -> ContractAddress {
self._token_approvals.read(token_id)
}
fn is_approved_for_all(
self: @ContractState, owner: ContractAddress, operator: ContractAddress
) -> bool {
self._operator_approvals.read((owner, operator))
}
}
}