Skip to content

Commit

Permalink
span to byteArray deserialization
Browse files Browse the repository at this point in the history
  • Loading branch information
byteZorvin committed Sep 13, 2024
1 parent 074330f commit 92d81e9
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 90 deletions.
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
scarb 2.8.0
scarb 2.8.2
starknet-foundry 0.27.0
1 change: 1 addition & 0 deletions Scarb.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ snforge_std = { git = "https://github.com/foundry-rs/starknet-foundry", tag = "v

[[target.starknet-contract]]
casm = true
allowed-libfuncs-list.name = "experimental"


[scripts]
Expand Down
34 changes: 27 additions & 7 deletions src/bridge/tests/messaging_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use piltover::messaging::interface::IMessagingDispatcher;
use starknet_bridge::bridge::{
tests::constants::{L3_BRIDGE_ADDRESS, OWNER, USDC_MOCK_ADDRESS, DELAY_TIME}
};

use piltover::messaging::types::MessageToAppchainStatus;
use starknet_bridge::bridge::tests::utils::setup::{deploy_erc20, mock_state_testing};
use starknet_bridge::bridge::tests::utils::message_payloads;
Expand All @@ -23,17 +22,38 @@ fn deploy_message_payload_ok() {
let usdc_address = deploy_erc20("USDC", "USDC");
let calldata = TokenBridge::deployment_message_payload(usdc_address);

// With byteArray
// [
// 1395567803262486866641834792347783460559299057717595230314827200011451862040,
// 0,
// 1431520323,
// 4,
// 0,
// 1431520323,
// 4,
// 18
// ]
//
// With syscall direct serialisation
// [
// 1395567803262486866641834792347783460559299057717595230314827200011451862040,
// 3, 0, 1431520323, 4,
// 3, 0, 1431520323, 4,
// 18
// ]

let expected_calldata: Span<felt252> = array![
327360033215303420453874031627788877836422131767619347074434581266068999983, // usdc_address
1395567803262486866641834792347783460559299057717595230314827200011451862040, // usdc_address
0,
1431520323, // -- USDC
4,
1431520323,
4, // "USDC"
0,
1431520323, // USDC
4,
18 // Decimals
1431520323,
4, // "USDC"
18
]
.span();

assert(calldata == expected_calldata, 'Incorrect serialisation');
}

Expand Down
30 changes: 25 additions & 5 deletions src/bridge/token_bridge.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub mod TokenBridge {
IERC20MetadataDispatcherTrait
};
use starknet::syscalls::call_contract_syscall;
use core::to_byte_array::FormatAsByteArray;

use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::upgrades::UpgradeableComponent;
Expand Down Expand Up @@ -402,15 +403,34 @@ pub mod TokenBridge {
let dispatcher = IERC20MetadataDispatcher { contract_address: token };
token.serialize(ref calldata);

// Openzeppelin erc20 used felt252 as return types for `name()` and `symbol()` before while
// `ByteArray` is used currently.
// So we use underlying syscalls, to support both the interface.
// The returned span is deserialized into to a ByteArray in both cases to make it consistent
// In case of ByteArray the length of returned span will be at least 3, while exactly 1 for
// felt252
let name_selector = selector!("name");
let name = call_contract_syscall(token, name_selector, array![].span()).unwrap_syscall();
name.serialize(ref calldata);
let mut name = call_contract_syscall(token, name_selector, array![].span())
.unwrap_syscall();
if (name.len() == 1) {
let name_deserialised = name[0].format_as_byte_array(10);
name_deserialised.serialize(ref calldata);
} else {
let name_deserialised = Serde::<ByteArray>::deserialize(ref name).unwrap();
name_deserialised.serialize(ref calldata);
}

// dispatcher.name().serialize(ref calldata);
let symbol_selector = selector!("symbol");
let symbol = call_contract_syscall(token, symbol_selector, array![].span())
let mut symbol = call_contract_syscall(token, symbol_selector, array![].span())
.unwrap_syscall();
symbol.serialize(ref calldata);

if (symbol.len() == 1) {
let symbol_deserialised = symbol[0].format_as_byte_array(10);
symbol_deserialised.serialize(ref calldata);
} else {
let symbol_deserialised = Serde::<ByteArray>::deserialize(ref symbol).unwrap();
symbol_deserialised.serialize(ref calldata);
}

dispatcher.decimals().serialize(ref calldata);
calldata.span()
Expand Down
2 changes: 1 addition & 1 deletion src/withdrawal_limit/tests/withdrawal_limit_test.cairo
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use core::num::traits::Bounded;
use snforge_std as snf;
use snforge_std::{ContractClassTrait, EventSpy, EventSpyAssertionsTrait};
use starknet_bridge::bridge::tests::constants::{OWNER,};
use starknet_bridge::bridge::tests::constants::{OWNER};
use starknet_bridge::mocks::withdrawal_limit_mock::{
IMockWithdrawalLimitDispatcher, IMockWithdrawalLimitDispatcherTrait
};
Expand Down
31 changes: 7 additions & 24 deletions tests/deposit_test.cairo
Original file line number Diff line number Diff line change
@@ -1,34 +1,17 @@
use core::num::traits::zero::Zero;
use core::array::ArrayTrait;
use core::serde::Serde;
use core::result::ResultTrait;
use core::option::OptionTrait;
use core::traits::TryInto;
use snforge_std as snf;
use snforge_std::{
ContractClassTrait, EventSpy, EventSpyTrait, EventsFilterTrait, EventSpyAssertionsTrait
};
use snforge_std::{EventSpy, EventSpyAssertionsTrait};
use starknet::ContractAddress;
use starknet_bridge::mocks::{
messaging::{IMockMessagingDispatcherTrait, IMockMessagingDispatcher}, erc20::ERC20
};
use piltover::messaging::{IMessaging, IMessagingDispatcher, IMessagingDispatcherTrait};
use starknet_bridge::mocks::messaging::IMockMessagingDispatcher;
use starknet_bridge::bridge::{
ITokenBridge, ITokenBridgeAdmin, ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait,
ITokenBridgeAdminDispatcher, ITokenBridgeAdminDispatcherTrait, IWithdrawalLimitStatusDispatcher,
IWithdrawalLimitStatusDispatcherTrait, TokenBridge, TokenBridge::Event,
types::{TokenStatus, TokenSettings}
};
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::Event as OwnableEvent,
interface::{IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait}
ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait, ITokenBridgeAdminDispatcher,
ITokenBridgeAdminDispatcherTrait, TokenBridge, TokenBridge::Event,
};

use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use starknet::contract_address::{contract_address_const};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS, DELAY_TIME};
use super::constants::{OWNER};
use starknet_bridge::bridge::tests::utils::setup::{
deploy_erc20, deploy_token_bridge_with_messaging, deploy_token_bridge, enroll_token_and_settle
deploy_erc20, deploy_token_bridge_with_messaging, enroll_token_and_settle
};

fn setup() -> (ITokenBridgeDispatcher, EventSpy, ContractAddress, IMockMessagingDispatcher) {
Expand Down
20 changes: 5 additions & 15 deletions tests/enroll_token_test.cairo
Original file line number Diff line number Diff line change
@@ -1,21 +1,11 @@
use snforge_std as snf;
use snforge_std::{ContractClassTrait, EventSpy, EventSpyTrait, EventSpyAssertionsTrait};
use starknet::ContractAddress;
use starknet_bridge::mocks::{
messaging::{IMockMessagingDispatcherTrait, IMockMessagingDispatcher}, erc20::ERC20, hash
};
use snforge_std::{ContractClassTrait, EventSpyAssertionsTrait};
use starknet_bridge::mocks::{hash};
use starknet_bridge::bridge::{
ITokenBridge, ITokenBridgeAdmin, ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait,
ITokenBridgeAdminDispatcher, ITokenBridgeAdminDispatcherTrait, IWithdrawalLimitStatusDispatcher,
IWithdrawalLimitStatusDispatcherTrait, TokenBridge, TokenBridge::Event,
types::{TokenStatus, TokenSettings}
};
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::Event as OwnableEvent,
interface::{IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait}
ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait, TokenBridge, TokenBridge::Event,
types::TokenStatus
};
use starknet::contract_address::{contract_address_const};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS, USDC_MOCK_ADDRESS, DELAY_TIME};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS};
use starknet_bridge::bridge::tests::utils::setup::{deploy_erc20, deploy_token_bridge};
use starknet_bridge::bridge::tests::utils::message_payloads;
use starknet_bridge::constants;
Expand Down
22 changes: 5 additions & 17 deletions tests/token_bridge_test.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,15 @@
use core::array::ArrayTrait;
use core::serde::Serde;
use core::result::ResultTrait;
use core::option::OptionTrait;
use core::traits::TryInto;
use snforge_std as snf;
use snforge_std::{ContractClassTrait, EventSpy, EventSpyTrait, EventSpyAssertionsTrait};
use starknet::ContractAddress;
use starknet_bridge::mocks::{
messaging::{IMockMessagingDispatcherTrait, IMockMessagingDispatcher}, erc20::ERC20
};
use snforge_std::EventSpyAssertionsTrait;
use starknet_bridge::bridge::{
ITokenBridge, ITokenBridgeAdmin, ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait,
ITokenBridgeAdminDispatcher, ITokenBridgeAdminDispatcherTrait, IWithdrawalLimitStatusDispatcher,
IWithdrawalLimitStatusDispatcherTrait, TokenBridge, TokenBridge::Event,
types::{TokenStatus, TokenSettings}
ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait, ITokenBridgeAdminDispatcher,
ITokenBridgeAdminDispatcherTrait, TokenBridge, TokenBridge::Event,
};
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::Event as OwnableEvent,
interface::{IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait}
};
use starknet::contract_address::{contract_address_const};
use starknet_bridge::bridge::tests::utils::setup::{deploy_erc20, deploy_token_bridge};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS, USDC_MOCK_ADDRESS, DELAY_TIME};
use starknet_bridge::bridge::tests::utils::setup::{deploy_token_bridge};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS, USDC_MOCK_ADDRESS};


#[test]
Expand Down
2 changes: 1 addition & 1 deletion tests/withdraw_test.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use snforge_std::EventSpy;
use starknet_bridge::mocks::{messaging::{IMockMessagingDispatcherTrait, IMockMessagingDispatcher},};
use starknet_bridge::bridge::{
ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait, ITokenBridgeAdminDispatcher,
ITokenBridgeAdminDispatcherTrait, IWithdrawalLimitStatusDispatcher,
ITokenBridgeAdminDispatcherTrait,
};

use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
Expand Down
22 changes: 3 additions & 19 deletions tests/withdrawal_limit_bridge_test.cairo
Original file line number Diff line number Diff line change
@@ -1,27 +1,11 @@
use core::array::ArrayTrait;
use core::serde::Serde;
use core::result::ResultTrait;
use core::option::OptionTrait;
use core::traits::TryInto;
use snforge_std as snf;
use snforge_std::{ContractClassTrait, EventSpy, EventSpyTrait, EventSpyAssertionsTrait};
use starknet::ContractAddress;
use starknet_bridge::mocks::{
messaging::{IMockMessagingDispatcherTrait, IMockMessagingDispatcher}, erc20::ERC20
};
use snforge_std::EventSpyAssertionsTrait;
use starknet_bridge::bridge::{
ITokenBridge, ITokenBridgeAdmin, ITokenBridgeDispatcher, ITokenBridgeDispatcherTrait,
ITokenBridgeAdminDispatcher, ITokenBridgeAdminDispatcherTrait, IWithdrawalLimitStatusDispatcher,
IWithdrawalLimitStatusDispatcherTrait, TokenBridge, TokenBridge::Event,
types::{TokenStatus, TokenSettings}
};
use openzeppelin::access::ownable::{
OwnableComponent, OwnableComponent::Event as OwnableEvent,
interface::{IOwnableTwoStepDispatcher, IOwnableTwoStepDispatcherTrait}
};
use starknet::contract_address::{contract_address_const};
use super::constants::{OWNER, L3_BRIDGE_ADDRESS, USDC_MOCK_ADDRESS, DELAY_TIME};
use starknet_bridge::bridge::tests::utils::setup::{deploy_erc20, deploy_token_bridge};
use super::constants::{OWNER, USDC_MOCK_ADDRESS};
use starknet_bridge::bridge::tests::utils::setup::{deploy_token_bridge};


#[test]
Expand Down

0 comments on commit 92d81e9

Please sign in to comment.