-
Notifications
You must be signed in to change notification settings - Fork 0
/
TDERC721_metadata.cairo
209 lines (182 loc) · 5.45 KB
/
TDERC721_metadata.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
%lang starknet
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin
from starkware.cairo.common.uint256 import (
Uint256,
uint256_add,
uint256_sub,
uint256_le,
uint256_lt,
uint256_check,
uint256_eq,
)
from openzeppelin.token.erc721.library import ERC721
from openzeppelin.introspection.ERC165 import ERC165
from openzeppelin.access.ownable import Ownable
from contracts.token.ERC721.ERC721_Metadata_base import (
ERC721_Metadata_initializer,
ERC721_Metadata_tokenURI,
ERC721_Metadata_setBaseTokenURI,
)
//
// Constructor
//
@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
name: felt,
symbol: felt,
owner: felt,
base_token_uri_len: felt,
base_token_uri: felt*,
token_uri_suffix: felt,
) {
ERC721.initializer(name, symbol);
ERC721_Metadata_initializer();
Ownable.initializer(owner);
ERC721_Metadata_setBaseTokenURI(base_token_uri_len, base_token_uri, token_uri_suffix);
let one_as_uint = Uint256(1, 0);
next_token_id_storage.write(one_as_uint);
return ();
}
//
// Storage vars
//
@storage_var
func next_token_id_storage() -> (next_token_id: Uint256) {
}
//
// Getters
//
@view
func next_token_id{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
next_token_id: Uint256
) {
let (next_token_id) = next_token_id_storage.read();
return (next_token_id=next_token_id);
}
@view
func getOwner{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (owner: felt) {
let (owner) = Ownable.owner();
return (owner=owner);
}
@view
func supportsInterface{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
interface_id: felt
) -> (success: felt) {
let (success) = ERC165.supports_interface(interface_id);
return (success,);
}
@view
func name{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (name: felt) {
let (name) = ERC721.name();
return (name,);
}
@view
func symbol{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (symbol: felt) {
let (symbol) = ERC721.symbol();
return (symbol,);
}
@view
func balanceOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(owner: felt) -> (
balance: Uint256
) {
let (balance: Uint256) = ERC721.balance_of(owner);
return (balance,);
}
@view
func ownerOf{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
token_id: Uint256
) -> (owner: felt) {
let (owner: felt) = ERC721.owner_of(token_id);
return (owner,);
}
@view
func getApproved{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
token_id: Uint256
) -> (approved: felt) {
let (approved: felt) = ERC721.get_approved(token_id);
return (approved,);
}
@view
func isApprovedForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
owner: felt, operator: felt
) -> (is_approved: felt) {
let (is_approved: felt) = ERC721.is_approved_for_all(owner, operator);
return (is_approved,);
}
@view
func tokenURI{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
token_id: Uint256
) -> (token_uri_len: felt, token_uri: felt*) {
let (token_uri_len, token_uri) = ERC721_Metadata_tokenURI(token_id);
return (token_uri_len=token_uri_len, token_uri=token_uri);
}
//
// Externals
//
@external
func approve{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(
to: felt, token_id: Uint256
) {
ERC721.approve(to, token_id);
return ();
}
@external
func setApprovalForAll{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
operator: felt, approved: felt
) {
ERC721.set_approval_for_all(operator, approved);
return ();
}
@external
func transferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(
_from: felt, to: felt, token_id: Uint256
) {
ERC721.transfer_from(_from, to, token_id);
return ();
}
@external
func safeTransferFrom{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(
_from: felt, to: felt, token_id: Uint256, data_len: felt, data: felt*
) {
ERC721.safe_transfer_from(_from, to, token_id, data_len, data);
return ();
}
@external
func setTokenURI{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(
base_token_uri_len: felt, base_token_uri: felt*, token_uri_suffix: felt
) {
Ownable.assert_only_owner();
ERC721_Metadata_setBaseTokenURI(base_token_uri_len, base_token_uri, token_uri_suffix);
return ();
}
@external
func mint{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(
to: felt, token_id: Uint256
) {
Ownable.assert_only_owner();
ERC721._mint(to, token_id);
return ();
}
@external
func claim{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(to: felt) {
let (token_id) = next_token_id_storage.read();
let one_as_uint = Uint256(1, 0);
let (next_token_id, _) = uint256_add(one_as_uint, token_id);
next_token_id_storage.write(next_token_id);
ERC721._mint(to, token_id);
return ();
}
@external
func burn{pedersen_ptr: HashBuiltin*, syscall_ptr: felt*, range_check_ptr}(token_id: Uint256) {
Ownable.assert_only_owner();
ERC721._burn(token_id);
return ();
}
@external
func transferOwnership{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
new_owner: felt
) -> (new_owner: felt) {
// Ownership check is handled by this function
Ownable.transfer_ownership(new_owner);
return (new_owner=new_owner);
}