-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy pathbibliotheca_marketplace.cairo
executable file
·411 lines (336 loc) · 10.3 KB
/
bibliotheca_marketplace.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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
// Declare this file as a StarkNet contract and set the required
// builtins.
%lang starknet
%builtins pedersen range_check bitwise
from starkware.cairo.common.cairo_builtins import HashBuiltin, SignatureBuiltin, BitwiseBuiltin
from starkware.cairo.common.alloc import alloc
from starkware.starknet.common.messages import send_message_to_l1
from starkware.starknet.common.syscalls import (
get_caller_address,
get_contract_address,
get_block_timestamp,
)
from starkware.cairo.common.math import assert_nn_le, unsigned_div_rem, assert_lt_felt
from starkware.cairo.common.uint256 import Uint256, uint256_le
from openzeppelin.token.erc20.IERC20 import IERC20
from openzeppelin.token.erc721.IERC721 import IERC721
from openzeppelin.access.ownable.library import Ownable
from openzeppelin.security.pausable.library import Pausable
from contracts.settling_game.utils.general import scale, unpack_data
from contracts.settling_game.utils.constants import (
SHIFT_NFT_1,
SHIFT_NFT_2,
SHIFT_NFT_3,
SHIFT_NFT_4,
SHIFT_NFT_5,
)
//###########
// MAPPINGS #
//###########
namespace TradeStatus {
const Open = 1;
const Executed = 2;
const Cancelled = 3;
}
struct Trade {
token_contract: felt,
token_id: Uint256,
expiration: felt,
price: felt,
poster: felt,
status: felt, // from TradeStatus
trade_id: felt,
}
// -----------------------------------
// Events
// -----------------------------------
@event
func TradeAction(trade: Trade) {
}
// -----------------------------------
// Storage
// -----------------------------------
// Indexed list of all trades
@storage_var
func _trades(idx: felt) -> (trade: Trade) {
}
// Contract Address of ERC20 used to purchase or sell items
@storage_var
func currency_token_address() -> (address: felt) {
}
// The current number of trades
@storage_var
func trade_counter() -> (value: felt) {
}
// Platform fee charged (in basis points)
@storage_var
func protocol_fee_bips() -> (basis_points: felt) {
}
// Treasury Account
@storage_var
func treasury_address() -> (address: felt) {
}
//##############
// CONSTRUCTOR #
//##############
@constructor
func constructor{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
address_of_currency_token: felt, _treasury_address: felt, owner: felt
) {
currency_token_address.write(address_of_currency_token);
trade_counter.write(1);
protocol_fee_bips.write(500);
treasury_address.write(_treasury_address);
Ownable.initializer(owner);
return ();
}
//##################
// TRADE FUNCTIONS #
//##################
@external
func fetch_trade_data{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(trade_data: felt, price: felt, poster: felt) -> (trade: Trade) {
alloc_locals;
// let (data) = get_trade(trade_data)
let (token_contract) = unpack_data(trade_data, 0, 127);
let (t_id) = unpack_data(trade_data, 7, 1048575);
let (expiration) = unpack_data(trade_data, 27, 33554431);
let (status) = unpack_data(trade_data, 52, 3);
let (trade_id) = unpack_data(trade_data, 54, 1048575);
// token_id needs to be Uint256
let token_id: Uint256 = Uint256(t_id, 0);
let trade = Trade(token_contract, token_id, expiration, price, poster, status, trade_id);
return (trade,);
}
@external
func open_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
_token_contract: felt, _token_id: Uint256, _price: felt, _expiration: felt
) {
alloc_locals;
Pausable.assert_not_paused();
let (caller) = get_caller_address();
let (contract_address) = get_contract_address();
let (owner_of) = IERC721.ownerOf(_token_contract, _token_id);
let (is_approved) = IERC721.isApprovedForAll(_token_contract, caller, contract_address);
let (trade_count) = trade_counter.read();
assert owner_of = caller;
assert is_approved = 1;
write_trade(
trade_count,
Trade(
_token_contract, _token_id, _expiration, _price, caller, TradeStatus.Open, trade_count
),
);
// increment
trade_counter.write(trade_count + 1);
return ();
}
@external
func execute_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(_trade: felt) {
alloc_locals;
Pausable.assert_not_paused();
let (currency) = currency_token_address.read();
let (caller) = get_caller_address();
let (_treasury_address) = treasury_address.read();
let (trade) = _trades.read(_trade);
let (fee_bips) = protocol_fee_bips.read();
assert trade.status = TradeStatus.Open;
assert_time_in_range(_trade);
// Fee is paid by seller
let (fee, remainder) = unsigned_div_rem(trade.price * fee_bips, 10000);
let base_seller_receives = trade.price - fee;
// transfer to poster
IERC20.transferFrom(currency, caller, trade.poster, Uint256(base_seller_receives, 0));
// transfer to treasury
IERC20.transferFrom(currency, caller, _treasury_address, Uint256(fee, 0));
// transfer item to buyer
IERC721.transferFrom(trade.token_contract, trade.poster, caller, trade.token_id);
write_trade(
_trade,
Trade(
trade.token_contract,
trade.token_id,
trade.expiration,
trade.price,
trade.poster,
TradeStatus.Executed,
_trade,
),
);
return ();
}
@external
func update_price{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
_trade: felt, _price: felt
) {
alloc_locals;
Pausable.assert_not_paused();
let (trade) = _trades.read(_trade);
assert trade.status = TradeStatus.Open;
assert_poster(_trade);
assert_time_in_range(_trade);
write_trade(
_trade,
Trade(
trade.token_contract,
trade.token_id,
trade.expiration,
_price,
trade.poster,
trade.status,
_trade,
),
);
return ();
}
@external
func cancel_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(_trade: felt) {
alloc_locals;
Pausable.assert_not_paused();
let (trade) = _trades.read(_trade);
assert trade.status = TradeStatus.Open;
assert_poster(_trade);
assert_time_in_range(_trade);
write_trade(
_trade,
Trade(
trade.token_contract,
trade.token_id,
trade.expiration,
trade.price,
trade.poster,
TradeStatus.Cancelled,
_trade,
),
);
return ();
}
@external
func pack_trade_data{
syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr, bitwise_ptr: BitwiseBuiltin*
}(trade: Trade) -> (trade_data: felt) {
alloc_locals;
let (nft_params: felt*) = alloc();
local id_1 = trade.token_contract * SHIFT_NFT_1;
nft_params[0] = id_1;
local t_id: Uint256 = trade.token_id;
let (local tid: felt) = _uint_to_felt(t_id);
local id_2 = tid * SHIFT_NFT_2;
nft_params[1] = id_2;
local id_3 = trade.expiration * SHIFT_NFT_3;
nft_params[2] = id_3;
local id_4 = trade.status * SHIFT_NFT_4;
nft_params[3] = id_4;
local id_5 = trade.trade_id * SHIFT_NFT_5;
nft_params[4] = id_5;
tempvar value = nft_params[4] + nft_params[3] + nft_params[2] + nft_params[1] + nft_params[0];
return (value,);
}
//##########
// HELPERS #
//##########
func write_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
_trade: felt, trade: Trade
) {
_trades.write(_trade, trade);
TradeAction.emit(trade);
return ();
}
func assert_time_in_range{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
_trade: felt
) {
let (block_timestamp) = get_block_timestamp();
let (trade) = _trades.read(_trade);
// check trade within
assert_nn_le(block_timestamp, trade.expiration);
return ();
}
func assert_poster{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(_trade: felt) {
let (caller) = get_caller_address();
let (trade) = _trades.read(_trade);
assert caller = trade.poster;
return ();
}
func _uint_to_felt{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
value: Uint256
) -> (value: felt) {
assert_lt_felt(value.high, 2 ** 123);
return (value.high * (2 ** 128) + value.low,);
}
//##########
// GETTERS #
//##########
@view
func get_trade{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(idx: felt) -> (
trade: Trade
) {
return _trades.read(idx);
}
@view
func get_trade_counter{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (
value: felt
) {
return trade_counter.read();
}
// Returns a trades status
@view
func get_trade_status{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
idx: felt
) -> (status: felt) {
let (trade) = _trades.read(idx);
return (trade.status,);
}
// Returns a trades token
@view
func get_trade_token_id{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
idx: felt
) -> (token_id: Uint256) {
let (trade) = _trades.read(idx);
return (trade.token_id,);
}
@view
func paused{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() -> (paused: felt) {
let (paused) = Pausable.is_paused();
return (paused,);
}
//##########
// SETTERS #
//##########
// Set basis points
@external
func set_basis_points{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
basis_points: felt
) -> (success: felt) {
Ownable.assert_only_owner();
protocol_fee_bips.write(basis_points);
return (1,);
}
@external
func set_treasury_address{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
address: felt
) -> (success: felt) {
Ownable.assert_only_owner();
treasury_address.write(address);
return (1,);
}
@external
func set_currency_address{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}(
address: felt
) -> (success: felt) {
Ownable.assert_only_owner();
currency_token_address.write(address);
return (1,);
}
@external
func pause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
Ownable.assert_only_owner();
Pausable._pause();
return ();
}
@external
func unpause{syscall_ptr: felt*, pedersen_ptr: HashBuiltin*, range_check_ptr}() {
Ownable.assert_only_owner();
Pausable._unpause();
return ();
}