-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathbridge.cairo
More file actions
545 lines (475 loc) · 19.8 KB
/
bridge.cairo
File metadata and controls
545 lines (475 loc) · 19.8 KB
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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
#[starknet::contract]
mod bridge {
use array::{ArrayTrait, SpanTrait};
use core::byte_array::ByteArrayTrait;
use core::starknet::SyscallResultTrait;
use debug::PrintTrait;
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::access::ownable::interface::{IOwnableDispatcher, IOwnableDispatcherTrait};
use option::OptionTrait;
use poseidon::poseidon_hash_span;
use serde::Serde;
// events
use starklane::interfaces::{
DepositRequestInitiated, WithdrawRequestCompleted, CollectionDeployedFromL1,
ReplacedClassHash, BridgeEnabled, CollectionWhiteListUpdated, WhiteListEnabled,
BridgeL1AddressUpdated, ERC721ClassHashUpdated, L1L2CollectionMappingUpdated
};
use starklane::interfaces::{
IStarklane, IUpgradeable, IUpgradeableDispatcher, IUpgradeableDispatcherTrait,
IStarklaneCollectionAdmin
};
use starklane::request::{
Request, compute_request_header_v1, compute_request_hash, collection_type_from_header,
};
use starklane::token::{
interfaces::{
IERC721Dispatcher, IERC721DispatcherTrait, IERC721BridgeableDispatcher,
IERC721BridgeableDispatcherTrait,
},
collection_manager::{
CollectionType, deploy_erc721_bridgeable, verify_collection_address, erc721_metadata,
},
};
use starknet::contract_address::ContractAddressZeroable;
use starknet::eth_address::EthAddressZeroable;
use starknet::{ClassHash, ContractAddress, EthAddress};
use traits::{Into, TryInto};
use zeroable::Zeroable;
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
#[abi(embed_v0)]
impl OwnableTwoStepMixinImpl =
OwnableComponent::OwnableTwoStepMixinImpl<ContractState>;
impl OwnableInternalImpl = OwnableComponent::InternalImpl<ContractState>;
#[storage]
struct Storage {
// Bridge address on L1 (to allow it to consume messages).
bridge_l1_address: EthAddress,
// The class to deploy for ERC721 tokens.
erc721_bridgeable_class: ClassHash,
// Mapping between L2<->L1 collections addresses.
l2_to_l1_addresses: LegacyMap::<ContractAddress, EthAddress>,
// Mapping between L1<->L2 collections addresses.
l1_to_l2_addresses: LegacyMap::<EthAddress, ContractAddress>,
// Registry of escrowed token for collections.
// <(collection_l2_address, token_id), original_depositor_l2_address>
escrow: LegacyMap::<(ContractAddress, u256), ContractAddress>,
// White list enabled flag
white_list_enabled: bool,
// Registry of whitelisted collections
white_listed_list: LegacyMap::<ContractAddress, (bool, ContractAddress)>,
white_listed_head: ContractAddress,
// Bridge enabled flag
enabled: bool,
#[substorage(v0)]
ownable: OwnableComponent::Storage,
}
#[constructor]
fn constructor(
ref self: ContractState,
bridge_admin: ContractAddress,
bridge_l1_address: EthAddress,
erc721_bridgeable_class: ClassHash,
) {
self.ownable.initializer(bridge_admin);
// TODO: add validation of inputs.
self.bridge_l1_address.write(bridge_l1_address);
self.erc721_bridgeable_class.write(erc721_bridgeable_class);
self.white_list_enabled.write(false);
self.enabled.write(false); // disabled by default
}
#[event]
#[derive(Drop, starknet::Event)]
enum Event {
DepositRequestInitiated: DepositRequestInitiated,
CollectionDeployedFromL1: CollectionDeployedFromL1,
WithdrawRequestCompleted: WithdrawRequestCompleted,
ReplacedClassHash: ReplacedClassHash,
BridgeEnabled: BridgeEnabled,
CollectionWhiteListUpdated: CollectionWhiteListUpdated,
WhiteListEnabled: WhiteListEnabled,
BridgeL1AddressUpdated: BridgeL1AddressUpdated,
ERC721ClassHashUpdated: ERC721ClassHashUpdated,
L1L2CollectionMappingUpdated: L1L2CollectionMappingUpdated,
#[flat]
OwnableEvent: OwnableComponent::Event,
}
/// Process message from L1 to withdraw token.
///
/// # Arguments
///
/// `from_address` - L1 sender address, must be Starklane L1.
/// `req` - The request containing tokens to bridge.
///
/// TODO: isn't better to receive a raw Span<felt252>
/// to be more flexible? And the first felt25 being the header
/// defines how the deserialization takes place?
#[l1_handler]
fn withdraw_auto_from_l1(ref self: ContractState, from_address: felt252, req: Request) {
ensure_is_enabled(@self);
assert(self.bridge_l1_address.read().into() == from_address, 'Invalid L1 msg sender');
// TODO: recompute HASH to ensure data are not altered.
// TODO: Validate all fields the request (cf. FSM).
let collection_l2 = ensure_erc721_deployment(ref self, @req);
let _ctype = collection_type_from_header(req.header);
// TODO: check CollectionType to support ERC1155 + metadata.
let mut i = 0;
loop {
if i == req.ids.len() {
break ();
}
let token_id = *req.ids[i];
let to = req.owner_l2;
let from = starknet::get_contract_address();
let is_escrowed = !self.escrow.read((collection_l2, token_id)).is_zero();
if is_escrowed {
IERC721Dispatcher { contract_address: collection_l2 }
.transfer_from(from, to, token_id);
let null_value = starknet::contract_address_const::<0>();
self.escrow.write((collection_l2, token_id), null_value);
} else {
if (req.uris.len() != 0) {
let token_uri = req.uris[i];
IERC721BridgeableDispatcher { contract_address: collection_l2 }
.mint_from_bridge_uri(to, token_id, token_uri.clone());
} else {
IERC721BridgeableDispatcher { contract_address: collection_l2 }
.mint_from_bridge(to, token_id);
}
}
i += 1;
};
self
.emit(
WithdrawRequestCompleted {
hash: req.hash,
block_timestamp: starknet::info::get_block_timestamp(),
req_content: req
}
);
}
#[abi(embed_v0)]
impl BridgeUpgradeImpl of IUpgradeable<ContractState> {
fn upgrade(ref self: ContractState, class_hash: ClassHash) {
ensure_is_admin(@self);
match starknet::replace_class_syscall(class_hash) {
Result::Ok(_) => {
self
.emit(
ReplacedClassHash {
contract: starknet::get_contract_address(), class: class_hash,
}
)
},
Result::Err(revert_reason) => panic(revert_reason),
};
}
}
#[abi(embed_v0)]
impl BridgeImpl of IStarklane<ContractState> {
fn get_l1_collection_address(self: @ContractState, address: ContractAddress) -> EthAddress {
self.l2_to_l1_addresses.read(address)
}
fn get_l2_collection_address(self: @ContractState, address: EthAddress) -> ContractAddress {
self.l1_to_l2_addresses.read(address)
}
fn set_bridge_l1_addr(ref self: ContractState, address: EthAddress) {
ensure_is_admin(@self);
self.bridge_l1_address.write(address);
self.emit(BridgeL1AddressUpdated { address });
}
fn get_bridge_l1_addr(self: @ContractState) -> EthAddress {
self.bridge_l1_address.read()
}
fn set_erc721_class_hash(ref self: ContractState, class_hash: ClassHash) {
ensure_is_admin(@self);
self.erc721_bridgeable_class.write(class_hash);
self.emit(ERC721ClassHashUpdated { class_hash });
}
fn get_erc721_class_hash(self: @ContractState) -> ClassHash {
self.erc721_bridgeable_class.read()
}
/// Deposits tokens to be bridged on the L1.
///
/// # Arguments
///
/// * `salt` - Randome salt to compute request hash.
/// * `collection_l2` - Address of the collection on L2.
/// * `owner_l1` - Address of the owner on L1.
/// * `tokens_ids` - Tokens to be bridged on L1.
/// * `use_withdraw_auto` - Tokens are automatically withdrawn on L1 using Starklane
/// indexer.
/// * `use_deposit_burn_auto` - Tokens are automatically burnt on L2 once transferred using
/// Starklane indexer.
///
/// TODO: add new_owners, values and uris.
/// TODO: better to use a struct than too much arguments? (DepositParams/DepositInputs/???)
fn deposit_tokens(
ref self: ContractState,
salt: felt252,
collection_l2: ContractAddress,
owner_l1: EthAddress,
token_ids: Span<u256>,
use_withdraw_auto: bool,
use_deposit_burn_auto: bool,
) {
ensure_is_enabled(@self);
assert(!self.bridge_l1_address.read().is_zero(), 'Bridge is not open');
// TODO: we may have the "from" into the params, to allow an operator
// to deposit token for a user.
// This can open more possibilities.
// Because anyway, a token can't be moved if the owner of the token
// is not giving approval to the bridge.
let from = starknet::get_caller_address();
assert(_is_white_listed(@self, collection_l2), 'Collection not whitelisted');
// TODO: add support for 1155 and check the detected interface on the collection.
let ctype = CollectionType::ERC721;
let erc721_metadata = erc721_metadata(collection_l2, Option::Some(token_ids));
let (name, symbol, base_uri, uris) = match erc721_metadata {
Option::Some(data) => (data.name, data.symbol, data.base_uri, data.uris),
Option::None => ("", "", "", array![].span())
};
escrow_deposit_tokens(ref self, collection_l2, from, token_ids);
let collection_l1 = self.l2_to_l1_addresses.read(collection_l2);
let req = Request {
header: compute_request_header_v1(ctype, use_deposit_burn_auto, use_withdraw_auto),
hash: compute_request_hash(salt, collection_l2, owner_l1, token_ids),
collection_l1,
collection_l2,
owner_l1,
owner_l2: from,
name,
symbol,
base_uri,
ids: token_ids,
values: array![].span(),
uris,
new_owners: array![].span(),
};
let mut buf: Array<felt252> = array![];
req.serialize(ref buf);
starknet::send_message_to_l1_syscall(self.bridge_l1_address.read().into(), buf.span(),)
.unwrap_syscall();
self
.emit(
DepositRequestInitiated {
hash: req.hash,
block_timestamp: starknet::info::get_block_timestamp(),
req_content: req
}
);
}
fn enable_white_list(ref self: ContractState, enable: bool) {
ensure_is_admin(@self);
self.white_list_enabled.write(enable);
self.emit(WhiteListEnabled { enabled: enable });
}
fn is_white_list_enabled(self: @ContractState) -> bool {
self.white_list_enabled.read()
}
fn white_list_collection(
ref self: ContractState, collection: ContractAddress, enabled: bool
) {
ensure_is_admin(@self);
_white_list_collection(ref self, collection, enabled);
self.emit(CollectionWhiteListUpdated { collection, enabled, });
}
fn is_white_listed(self: @ContractState, collection: ContractAddress) -> bool {
_is_white_listed(self, collection)
}
fn get_white_listed_collections(self: @ContractState) -> Span<ContractAddress> {
let mut white_listed = array![];
let mut current = self.white_listed_head.read();
loop {
if current.is_zero() {
break;
}
let (enabled, next) = self.white_listed_list.read(current);
if !enabled {
break;
} else {
white_listed.append(current);
current = next;
}
};
white_listed.span()
}
fn enable(ref self: ContractState, enable: bool) {
ensure_is_admin(@self);
self.enabled.write(enable);
self.emit(BridgeEnabled { enable: enable });
}
fn is_enabled(self: @ContractState) -> bool {
self.enabled.read()
}
fn set_l1_l2_collection_mapping(
ref self: ContractState, collection_l1: EthAddress, collection_l2: ContractAddress
) {
ensure_is_admin(@self);
self.l1_to_l2_addresses.write(collection_l1, collection_l2);
self.l2_to_l1_addresses.write(collection_l2, collection_l1);
self.emit(L1L2CollectionMappingUpdated { collection_l1, collection_l2 });
}
}
#[abi(embed_v0)]
impl BridgeCollectionAdminImpl of IStarklaneCollectionAdmin<ContractState> {
fn collection_upgrade(
ref self: ContractState, collection: ContractAddress, class_hash: ClassHash
) {
ensure_is_admin(@self);
IUpgradeableDispatcher { contract_address: collection }.upgrade(class_hash);
}
// transfer owner of the given collection to the given address
fn collection_transfer_ownership(
ref self: ContractState, collection: ContractAddress, new_owner: ContractAddress
) {
ensure_is_admin(@self);
IOwnableDispatcher { contract_address: collection }.transfer_ownership(new_owner);
}
}
// *** INTERNALS ***
/// Ensures the caller is the bridge admin. Revert if it's not.
fn ensure_is_admin(self: @ContractState) {
self.ownable.assert_only_owner();
}
/// Ensures the bridge is enabled
fn ensure_is_enabled(self: @ContractState) {
assert!(self.enabled.read(), "Bridge disabled");
}
/// Deposit the given tokens into escrow.
///
/// # Arguments
///
/// * `contract_address` - Collection address on L2.
/// * `from` - Current owner of the token.
/// * `token_ids` - Tokens to deposit.
fn escrow_deposit_tokens(
ref self: ContractState,
contract_address: ContractAddress,
from: ContractAddress,
token_ids: Span<u256>,
) {
let to = starknet::get_contract_address();
let erc721 = IERC721Dispatcher { contract_address };
let mut i = 0_usize;
loop {
if i == token_ids.len() {
break ();
}
let token_id = *token_ids[i];
erc721.transfer_from(from, to, token_id);
self.escrow.write((contract_address, token_id), from);
i += 1;
};
}
/// Deploys the collection contract, if necessary.
/// Returns the address of the collection on l2.
///
/// * `req` - Request for bridging assets.
fn ensure_erc721_deployment(ref self: ContractState, req: @Request) -> ContractAddress {
let l1_req: EthAddress = *req.collection_l1;
let l2_req: ContractAddress = *req.collection_l2;
let collection_l2 = verify_collection_address(
l1_req,
l2_req,
self.l2_to_l1_addresses.read(l2_req),
self.l1_to_l2_addresses.read(l1_req),
);
if !collection_l2.is_zero() {
return collection_l2;
}
let hash = *req.hash;
let salt_data: Span<felt252> = array![hash.low.into(), hash.high.into()].span();
let salt = poseidon_hash_span(salt_data);
let l2_addr_from_deploy = deploy_erc721_bridgeable(
self.erc721_bridgeable_class.read(),
salt,
req.name.clone(),
req.symbol.clone(),
req.base_uri.clone(),
starknet::get_contract_address(),
);
self.l1_to_l2_addresses.write(l1_req, l2_addr_from_deploy);
self.l2_to_l1_addresses.write(l2_addr_from_deploy, l1_req);
self
.emit(
CollectionDeployedFromL1 {
l1_addr: *req.collection_l1,
l2_addr: l2_addr_from_deploy,
name: req.name.clone(),
symbol: req.symbol.clone()
}
);
// update whitelist if needed
let (already_white_listed, _) = self.white_listed_list.read(l2_addr_from_deploy);
if already_white_listed != true {
_white_list_collection(ref self, l2_addr_from_deploy, true);
self
.emit(
CollectionWhiteListUpdated { collection: l2_addr_from_deploy, enabled: true, }
);
}
l2_addr_from_deploy
}
fn _is_white_listed(self: @ContractState, collection: ContractAddress) -> bool {
let enabled = self.white_list_enabled.read();
if (enabled) {
let (ret, _) = self.white_listed_list.read(collection);
return ret;
}
true
}
fn _white_list_collection(ref self: ContractState, collection: ContractAddress, enabled: bool) {
let no_value = starknet::contract_address_const::<0>();
let (current, _) = self.white_listed_list.read(collection);
if current != enabled {
let mut prev = self.white_listed_head.read();
if enabled {
self.white_listed_list.write(collection, (enabled, no_value));
if prev.is_zero() {
self.white_listed_head.write(collection);
return;
}
// find last element
loop {
let (_, next) = self.white_listed_list.read(prev);
if next.is_zero() {
break;
}
let (active, _) = self.white_listed_list.read(next);
if !active {
break;
}
prev = next;
};
self.white_listed_list.write(prev, (true, collection));
} else {
// change head
if prev == collection {
let (_, next) = self.white_listed_list.read(prev);
self.white_listed_list.write(collection, (false, no_value));
self.white_listed_head.write(next);
return;
}
// removed element from linked list
loop {
let (active, next) = self.white_listed_list.read(prev);
if next.is_zero() {
// end of list
break;
}
if !active {
break;
}
if next == collection {
let (_, target) = self.white_listed_list.read(collection);
self.white_listed_list.write(prev, (active, target));
break;
}
prev = next;
};
self.white_listed_list.write(collection, (false, no_value));
}
}
}
}