Skip to content

Commit

Permalink
starknet: scarb fmt
Browse files Browse the repository at this point in the history
  • Loading branch information
ptisserand committed Oct 14, 2024
1 parent 455aba1 commit 13d6154
Show file tree
Hide file tree
Showing 10 changed files with 167 additions and 194 deletions.
3 changes: 3 additions & 0 deletions apps/blockchain/starknet/Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,6 @@ casm = true
[tool.sncast.katana]
account = "katana_account"
url = "http://127.0.0.1:5050"

[tool.fmt]
sort-module-level-items = true
30 changes: 15 additions & 15 deletions apps/blockchain/starknet/src/bridge.cairo
Original file line number Diff line number Diff line change
@@ -1,33 +1,29 @@
#[starknet::contract]
mod bridge {
use core::byte_array::ByteArrayTrait;
use array::{ArrayTrait, SpanTrait};
use traits::{Into, TryInto};
use zeroable::Zeroable;
use serde::Serde;
use option::OptionTrait;
use debug::PrintTrait;
use core::byte_array::ByteArrayTrait;

use core::starknet::SyscallResultTrait;

use starknet::{ClassHash, ContractAddress, EthAddress};
use starknet::contract_address::ContractAddressZeroable;
use starknet::eth_address::EthAddressZeroable;
use debug::PrintTrait;

use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::access::ownable::interface::{IOwnableDispatcher, IOwnableDispatcherTrait};
use option::OptionTrait;

use starklane::interfaces::{
IStarklane, IUpgradeable, IUpgradeableDispatcher, IUpgradeableDispatcherTrait,
IStarklaneCollectionAdmin
};
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,
};
Expand All @@ -40,8 +36,12 @@ mod bridge {
CollectionType, deploy_erc721_bridgeable, verify_collection_address, erc721_metadata,
},
};
use starknet::contract_address::ContractAddressZeroable;
use starknet::eth_address::EthAddressZeroable;

use poseidon::poseidon_hash_span;
use starknet::{ClassHash, ContractAddress, EthAddress};
use traits::{Into, TryInto};
use zeroable::Zeroable;

component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);

Expand Down
14 changes: 9 additions & 5 deletions apps/blockchain/starknet/src/byte_array_extra.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ impl SpanFeltTryIntoByteArray of TryInto<Span<felt252>, ByteArray> {
}



#[cfg(test)]
mod tests {
use core::serde::Serde;
Expand Down Expand Up @@ -90,9 +89,14 @@ mod tests {
let mut a = ArrayTrait::new();
orig.serialize(ref a);
assert_eq!(*a[0], 1, "Wrong data len");
assert_eq!(*a[1], 0x546869732070616c696e64726f6d65206973206e6f7420617320676f6f642c, "Wrong data[0]");
assert_eq!(*a[2], 0x20627574206174206c656173742069742773206c6f6e6720656e6f756768, "Wrong pending word");
assert_eq!(
*a[1], 0x546869732070616c696e64726f6d65206973206e6f7420617320676f6f642c, "Wrong data[0]"
);
assert_eq!(
*a[2],
0x20627574206174206c656173742069742773206c6f6e6720656e6f756768,
"Wrong pending word"
);
assert_eq!(*a[3], 30, "Wrong pending word len");
}

}
}
2 changes: 1 addition & 1 deletion apps/blockchain/starknet/src/interfaces.cairo
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use array::{SpanTrait};
use starknet::{ClassHash, ContractAddress, EthAddress};
use starklane::request::Request;
use starknet::{ClassHash, ContractAddress, EthAddress};

#[starknet::interface]
trait IStarklane<T> {
Expand Down
6 changes: 3 additions & 3 deletions apps/blockchain/starknet/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
mod request;
mod interfaces;
mod bridge;
mod token;

mod byte_array_extra;
mod interfaces;
mod request;

mod tests;
mod token;
75 changes: 27 additions & 48 deletions apps/blockchain/starknet/src/request.cairo
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use array::{ArrayTrait, SpanTrait};
use keccak::{keccak_u256s_be_inputs, keccak_u256s_le_inputs};
use option::OptionTrait;
use poseidon::poseidon_hash_span;
///! Request to bridge tokens.

use serde::Serde;
use traits::{Into, TryInto};
use option::OptionTrait;
use array::{ArrayTrait, SpanTrait};
use starknet::{ContractAddress, EthAddress};
use poseidon::poseidon_hash_span;
use keccak::{keccak_u256s_be_inputs, keccak_u256s_le_inputs};

use starklane::token::collection_manager::CollectionType;
use starknet::{ContractAddress, EthAddress};
use traits::{Into, TryInto};

// Byte 1 of the header.
const HEADER_V1: u256 = 0x01;
Expand All @@ -30,37 +30,30 @@ struct Request {
header: felt252,
// Unique hash of the request.
hash: u256,

// Address of the collection on Ethereum.
collection_l1: EthAddress,
// Address of the collection on Starknet.
collection_l2: ContractAddress,

// Owner on Ethereum (for all the tokens in the request).
owner_l1: EthAddress,
// Owners on Starknet (for all the tokens in the request).
owner_l2: ContractAddress,

// Collection name (ERC1155: not used).
name: ByteArray,
// Collection symbol (ERC1155: not used).
symbol: ByteArray,
// Base URI for the collection.
base_uri: ByteArray,

// Tokens to be bridged.
ids: Span<u256>,

// Amounts for each token
// ERC721: not used.
// ERC1155: if empty, the amount is 1 for each token id, else length must match `ids`.
values: Span<u256>,

// URIs for each individual token
// ERC721: must be empty if `base_uri` is provided, else length must match `ids`.
// ERC1155: not used.
uris: Span<ByteArray>,

// New owners on starknet. This allows a batch migration of the tokens to
// different owners.
// Must be empty if `owner_l2` is not 0. Otherwise, length must match `ids`.
Expand Down Expand Up @@ -110,9 +103,7 @@ fn collection_type_from_header(header: felt252) -> CollectionType {
/// * `use_deposit_burn_auto` - Enables burn auto in the header.
/// * `use_withdraw_auto` - Enables withdraw auto in the header.
fn compute_request_header_v1(
ctype: CollectionType,
use_deposit_burn_auto: bool,
use_withdraw_auto: bool,
ctype: CollectionType, use_deposit_burn_auto: bool, use_withdraw_auto: bool,
) -> felt252 {
let mut header: u256 = HEADER_V1;

Expand Down Expand Up @@ -144,10 +135,7 @@ fn compute_request_header_v1(
/// * `to_l1_address` - New owner on Ethereum (L1).
/// * `token_ids` - List of token ids to be transferred.
fn compute_request_hash(
salt: felt252,
collection: ContractAddress,
to_l1_address: EthAddress,
token_ids: Span<u256>,
salt: felt252, collection: ContractAddress, to_l1_address: EthAddress, token_ids: Span<u256>,
) -> u256 {
let c_felt: felt252 = collection.into();
let mut buf: Array<u256> = array![salt.into(), c_felt.into(), to_l1_address.address.into()];
Expand All @@ -169,14 +157,10 @@ fn compute_request_hash(
let hash = keccak_u256s_be_inputs(span);

// Ensure keccak endianness compatibility.
u256 {
low: integer::u128_byte_reverse(hash.high),
high: integer::u128_byte_reverse(hash.low)
}
u256 { low: integer::u128_byte_reverse(hash.high), high: integer::u128_byte_reverse(hash.low) }
}



// **** TESTS ****
/// We have to use a contract as the syscall is not supported directly in tests.
#[starknet::interface]
Expand All @@ -192,14 +176,12 @@ trait IContractRequest<T> {

#[starknet::contract]
mod contract_req_test {
use super::{compute_request_hash, IContractRequest};
use array::{ArrayTrait, SpanTrait};
use starknet::{ContractAddress, EthAddress};
use super::{compute_request_hash, IContractRequest};

#[storage]
struct Storage {

}
struct Storage {}

#[abi(embed_v0)]
impl ContractTestImpl of IContractRequest<ContractState> {
Expand All @@ -217,24 +199,23 @@ mod contract_req_test {

#[cfg(test)]
mod tests {
use debug::PrintTrait;
use serde::Serde;
use array::{ArrayTrait, SpanTrait};
use debug::PrintTrait;
use integer::{U8IntoFelt252, U32IntoFelt252, Felt252TryIntoU32};
use traits::{Into, TryInto};
use option::OptionTrait;
use result::ResultTrait;
use serde::Serde;

use snforge_std::{declare, ContractClassTrait};

use starklane::token::collection_manager::CollectionType;
use starknet::{ContractAddress, EthAddress};
use super::{
Request, WITHDRAW_AUTO, DEPOSIT_AUTO_BURN, ERC721_TYPE, ERC1155_TYPE,
can_use_withdraw_auto, collection_type_from_header,
compute_request_header_v1, compute_request_hash,
Request, WITHDRAW_AUTO, DEPOSIT_AUTO_BURN, ERC721_TYPE, ERC1155_TYPE, can_use_withdraw_auto,
collection_type_from_header, compute_request_header_v1, compute_request_hash,
contract_req_test, IContractRequestDispatcher, IContractRequestDispatcherTrait,
};

use starklane::token::collection_manager::CollectionType;

use snforge_std::{declare, ContractClassTrait};
use traits::{Into, TryInto};

#[test]
fn can_use_withdraw_auto_test() {
Expand All @@ -252,7 +233,7 @@ mod tests {
}

#[test]
#[should_panic(expected: ('Invalid collection type', ))]
#[should_panic(expected: ('Invalid collection type',))]
fn collection_type_from_header_test_fail() {
let h: felt252 = 0;
assert(collection_type_from_header(h) == CollectionType::ERC721, 'Invalid ERC721 CT');
Expand Down Expand Up @@ -280,15 +261,13 @@ mod tests {
let contract_address = contract.deploy(@array![]).unwrap();
let disp = IContractRequestDispatcher { contract_address };

let hash = disp.compute_request_hash_from_contract(
salt,
collection,
to_l1_address,
ids.span()
);
let hash = disp
.compute_request_hash_from_contract(salt, collection, to_l1_address, ids.span());

assert(hash == 0xbb7ca67ee263bd2bb68dc88b530300222a3700bceca4e537079047fff89a0402_u256,
'Invalid req hash');
assert(
hash == 0xbb7ca67ee263bd2bb68dc88b530300222a3700bceca4e537079047fff89a0402_u256,
'Invalid req hash'
);
}

/// Should deserialize from buffer.
Expand Down
46 changes: 23 additions & 23 deletions apps/blockchain/starknet/src/tests/bridge_t.cairo
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
#[cfg(test)]
mod tests {
use snforge_std::{
cheatcodes::{events::EventFetcher, events::EventAssertions, l1_handler::L1HandlerTrait},
event_name_hash,
};
use array::{ArrayTrait, SpanTrait};

use core::traits::TryInto;
use array::{ArrayTrait, SpanTrait};
use traits::Into;
use result::ResultTrait;
use option::OptionTrait;
use serde::Serde;
use zeroable::Zeroable;

use openzeppelin::access::ownable::interface::{
IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait
};
use option::OptionTrait;
use result::ResultTrait;
use serde::Serde;
use snforge_std::{
cheatcodes::{events::EventFetcher, events::EventAssertions, l1_handler::L1HandlerTrait},
event_name_hash,
};

use starknet::{ContractAddress, ClassHash, EthAddress, class_hash_const};
use starklane::{
request::{Request, compute_request_hash},
interfaces::{
IStarklaneDispatcher, IStarklaneDispatcherTrait, IUpgradeableDispatcher,
IUpgradeableDispatcherTrait, IStarklaneCollectionAdminDispatcher,
IStarklaneCollectionAdminDispatcherTrait,
},
use snforge_std::{
declare, ContractClass, ContractClassTrait, start_prank, stop_prank, CheatTarget, L1Handler,
get_class_hash, spy_events, SpyOn, load, map_entry_address,
};
use starklane::bridge::bridge;
use starklane::token::{
interfaces::{
IERC721Dispatcher, IERC721DispatcherTrait, IERC721BridgeableDispatcher,
IERC721BridgeableDispatcherTrait, IERC721MintableDispatcher,
IERC721MintableDispatcherTrait,
},
};
use starklane::bridge::bridge;

use snforge_std::{
declare, ContractClass, ContractClassTrait, start_prank, stop_prank, CheatTarget, L1Handler,
get_class_hash, spy_events, SpyOn, load, map_entry_address,
use starklane::{
request::{Request, compute_request_hash},
interfaces::{
IStarklaneDispatcher, IStarklaneDispatcherTrait, IUpgradeableDispatcher,
IUpgradeableDispatcherTrait, IStarklaneCollectionAdminDispatcher,
IStarklaneCollectionAdminDispatcherTrait,
},
};

use starknet::{ContractAddress, ClassHash, EthAddress, class_hash_const};
use traits::Into;
use zeroable::Zeroable;

#[derive(Drop)]
struct BridgeDeployedConfig {
admin: ContractAddress,
Expand Down
2 changes: 1 addition & 1 deletion apps/blockchain/starknet/src/token.cairo
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mod interfaces;
mod collection_manager;
mod erc721_bridgeable;
mod interfaces;
Loading

0 comments on commit 13d6154

Please sign in to comment.