Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix: Integration tests #74

Merged
merged 4 commits into from
Jul 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions contracts/src/contracts/hooks/protocol_fee.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ pub mod protocol_fee {
use hyperlane_starknet::contracts::libs::message::Message;
use hyperlane_starknet::interfaces::{IPostDispatchHook, Types, IProtocolFee, ETH_ADDRESS};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{
ERC20ABI, ERC20ABIDispatcher, ERC20ABIDispatcherTrait
};
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::{ContractAddress, contract_address_const, get_contract_address};
component!(path: OwnableComponent, storage: ownable, event: OwnableEvent);
Expand Down Expand Up @@ -149,9 +151,9 @@ pub mod protocol_fee {
/// Collects protocol fees from the contract.
/// Fees are sent to the beneficary address
fn collect_protocol_fees(ref self: ContractState) {
let token_dispatcher = IERC20Dispatcher { contract_address: self.fee_token.read() };
let token_dispatcher = ERC20ABIDispatcher { contract_address: self.fee_token.read() };
let contract_address = get_contract_address();
let balance = token_dispatcher.balance_of(contract_address);
let balance = token_dispatcher.balanceOf(contract_address);
assert(balance != 0, Errors::INSUFFICIENT_BALANCE);
token_dispatcher.transfer(self.beneficiary.read(), balance);
}
Expand Down
12 changes: 7 additions & 5 deletions contracts/src/contracts/mailbox.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ pub mod mailbox {
IMessageRecipientDispatcher, IMessageRecipientDispatcherTrait, ETH_ADDRESS,
};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{
ERC20ABI, ERC20ABIDispatcher, ERC20ABIDispatcherTrait
};
use openzeppelin::upgrades::{interface::IUpgradeable, upgradeable::UpgradeableComponent};
use starknet::{
ContractAddress, ClassHash, get_caller_address, get_block_number, contract_address_const,
Expand Down Expand Up @@ -269,21 +271,21 @@ pub mod mailbox {
let required_hook = IPostDispatchHookDispatcher {
contract_address: required_hook_address
};
let token_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let token_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };

let mut required_fee = required_hook
.quote_dispatch(hook_metadata.clone(), message.clone());
if (required_fee > 0) {
assert(_fee_amount >= required_fee, Errors::NOT_ENOUGH_FEE_PROVIDED);
let contract_address = get_contract_address();
let user_balance = token_dispatcher.balance_of(caller_address);
let user_balance = token_dispatcher.balanceOf(caller_address);
assert(user_balance >= _fee_amount, Errors::INSUFFICIENT_BALANCE);
assert(
token_dispatcher.allowance(caller_address, contract_address) >= _fee_amount,
Errors::INSUFFICIENT_ALLOWANCE
);

token_dispatcher.transfer_from(caller_address, required_hook_address, required_fee);
token_dispatcher.transferFrom(caller_address, required_hook_address, required_fee);
}

required_hook.post_dispatch(hook_metadata.clone(), message.clone(), required_fee);
Expand All @@ -297,7 +299,7 @@ pub mod mailbox {
.post_dispatch(hook_metadata.clone(), message.clone(), default_fee);
}
if (_fee_amount - required_fee >= default_fee) {
token_dispatcher.transfer_from(caller_address, hook, default_fee);
token_dispatcher.transferFrom(caller_address, hook, default_fee);
hook_dispatcher.post_dispatch(hook_metadata, message.clone(), default_fee);
}
}
Expand Down
4 changes: 1 addition & 3 deletions contracts/src/contracts/mocks/fee_token.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ pub mod mock_fee_token {
component!(path: ERC20Component, storage: erc20, event: ERC20Event);

#[abi(embed_v0)]
impl ERC20Impl = ERC20Component::ERC20Impl<ContractState>;
#[abi(embed_v0)]
impl ERC20MetadataImpl = ERC20Component::ERC20MetadataImpl<ContractState>;
impl ERC20MixinImpl = ERC20Component::ERC20MixinImpl<ContractState>;
impl InternalImpl = ERC20Component::InternalImpl<ContractState>;
#[storage]
struct Storage {
Expand Down
10 changes: 5 additions & 5 deletions contracts/src/tests/hooks/test_protocol_fee.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use hyperlane_starknet::tests::setup::{
setup_mock_token
};
use openzeppelin::access::ownable::interface::{IOwnableDispatcher, IOwnableDispatcherTrait};
use openzeppelin::token::erc20::interface::{IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use snforge_std::{start_prank, CheatTarget, stop_prank};


Expand Down Expand Up @@ -79,12 +79,12 @@ fn test_collect_protocol_fee() {

// First transfer the token to the contract
fee_token.transfer(protocol_fee.contract_address, PROTOCOL_FEE);
assert_eq!(fee_token.balance_of(protocol_fee.contract_address), PROTOCOL_FEE);
assert_eq!(fee_token.balanceOf(protocol_fee.contract_address), PROTOCOL_FEE);
stop_prank(CheatTarget::One(ownable.contract_address));

protocol_fee.collect_protocol_fees();
assert_eq!(fee_token.balance_of(BENEFICIARY()), PROTOCOL_FEE);
assert_eq!(fee_token.balance_of(protocol_fee.contract_address), 0);
assert_eq!(fee_token.balanceOf(BENEFICIARY()), PROTOCOL_FEE);
assert_eq!(fee_token.balanceOf(protocol_fee.contract_address), 0);
}

#[test]
Expand Down Expand Up @@ -113,7 +113,7 @@ fn test_supports_metadata() {
#[test]
#[should_panic(expected: ('Invalid metadata variant',))]
fn test_post_dispatch_fails_if_invalid_variant() {
let fee_token = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let fee_token = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
let (_, post_dispatch_hook) = setup_protocol_fee();
let ownable = IOwnableDispatcher { contract_address: fee_token.contract_address };
let mut metadata = BytesTrait::new_empty();
Expand Down
6 changes: 3 additions & 3 deletions contracts/src/tests/setup.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use hyperlane_starknet::interfaces::{
ETH_ADDRESS
};
use openzeppelin::account::utils::signature::EthSignature;
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{ERC20ABI, ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use snforge_std::{
declare, ContractClassTrait, CheatTarget, EventSpy, EventAssertions, spy_events, SpyOn
};
Expand Down Expand Up @@ -543,15 +543,15 @@ pub fn get_merkle_message_and_signature() -> (u256, Array<felt252>, Array<EthSig
(msg_hash, validators_array, signatures)
}

pub fn setup_mock_token() -> IERC20Dispatcher {
pub fn setup_mock_token() -> ERC20ABIDispatcher {
let fee_token_class = declare("mock_fee_token").unwrap();
let (fee_token_addr, _) = fee_token_class
.deploy_at(
@array![INITIAL_SUPPLY.low.into(), INITIAL_SUPPLY.high.into(), OWNER().into()],
ETH_ADDRESS()
)
.unwrap();
IERC20Dispatcher { contract_address: fee_token_addr }
ERC20ABIDispatcher { contract_address: fee_token_addr }
}

pub fn setup_protocol_fee() -> (IProtocolFeeDispatcher, IPostDispatchHookDispatcher) {
Expand Down
16 changes: 8 additions & 8 deletions contracts/src/tests/test_mailbox.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use hyperlane_starknet::tests::setup::{
};
use openzeppelin::access::ownable::OwnableComponent;
use openzeppelin::access::ownable::interface::{IOwnableDispatcher, IOwnableDispatcherTrait};
use openzeppelin::token::erc20::interface::{IERC20, IERC20Dispatcher, IERC20DispatcherTrait};
use openzeppelin::token::erc20::interface::{ERC20ABI, ERC20ABIDispatcher, ERC20ABIDispatcherTrait};
use snforge_std::cheatcodes::events::EventAssertions;
use snforge_std::{start_prank, CheatTarget, stop_prank};

Expand Down Expand Up @@ -176,7 +176,7 @@ fn test_dispatch_with_protocol_fee_hook() {
Option::Some(protocol_fee_hook.contract_address),
Option::Some(mock_hook.contract_address)
);
let erc20_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let erc20_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
let ownable = IOwnableDispatcher { contract_address: ETH_ADDRESS() };
start_prank(CheatTarget::One(ownable.contract_address), OWNER());
erc20_dispatcher.approve(MAILBOX(), PROTOCOL_FEE);
Expand Down Expand Up @@ -229,7 +229,7 @@ fn test_dispatch_with_protocol_fee_hook() {
);

// balance check
assert_eq!(erc20_dispatcher.balance_of(OWNER()), INITIAL_SUPPLY - PROTOCOL_FEE);
assert_eq!(erc20_dispatcher.balanceOf(OWNER()), INITIAL_SUPPLY - PROTOCOL_FEE);
assert(mailbox.get_latest_dispatched_id() == message_id, 'Failed to fetch latest id');
}

Expand All @@ -243,7 +243,7 @@ fn test_dispatch_with_two_fee_hook() {
Option::Some(protocol_fee_hook.contract_address),
Option::Some(mock_hook.contract_address)
);
let erc20_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let erc20_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
let ownable = IOwnableDispatcher { contract_address: ETH_ADDRESS() };
start_prank(CheatTarget::One(ownable.contract_address), OWNER());
// (mock_fee_hook consummes 3 * PROTOCOL_FEE)
Expand Down Expand Up @@ -297,7 +297,7 @@ fn test_dispatch_with_two_fee_hook() {
);

// balance check
assert_eq!(erc20_dispatcher.balance_of(OWNER()), INITIAL_SUPPLY - 4 * PROTOCOL_FEE);
assert_eq!(erc20_dispatcher.balanceOf(OWNER()), INITIAL_SUPPLY - 4 * PROTOCOL_FEE);
assert(mailbox.get_latest_dispatched_id() == message_id, 'Failed to fetch latest id');
}

Expand All @@ -315,7 +315,7 @@ fn test_dispatch_with_protocol_fee_hook_fails_if_provided_fee_lower_than_require
let ownable = IOwnableDispatcher { contract_address: ETH_ADDRESS() };
start_prank(CheatTarget::One(ownable.contract_address), OWNER());
// We transfer some token to the new owner
let erc20_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let erc20_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
erc20_dispatcher.transfer(NEW_OWNER(), PROTOCOL_FEE - 10);

// The new owner has has PROTOCOL_FEE -10 tokens so the required hook post dispatch fails
Expand Down Expand Up @@ -355,7 +355,7 @@ fn test_dispatch_with_protocol_fee_hook_fails_if_user_balance_lower_than_fee_amo
let ownable = IOwnableDispatcher { contract_address: ETH_ADDRESS() };
start_prank(CheatTarget::One(ownable.contract_address), OWNER());
// We transfer some token to the new owner
let erc20_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let erc20_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
erc20_dispatcher.transfer(NEW_OWNER(), PROTOCOL_FEE - 10);

// The new owner has has PROTOCOL_FEE -10 tokens so the required hook post dispatch fails
Expand Down Expand Up @@ -397,7 +397,7 @@ fn test_dispatch_with_protocol_fee_hook_fails_if_insufficient_allowance() {
let ownable = IOwnableDispatcher { contract_address: ETH_ADDRESS() };
start_prank(CheatTarget::One(ownable.contract_address), OWNER());
// We transfer some token to the new owner
let erc20_dispatcher = IERC20Dispatcher { contract_address: ETH_ADDRESS() };
let erc20_dispatcher = ERC20ABIDispatcher { contract_address: ETH_ADDRESS() };
erc20_dispatcher.transfer(NEW_OWNER(), PROTOCOL_FEE);

// The new owner has has PROTOCOL_FEE -10 tokens so the required hook post dispatch fails
Expand Down
2 changes: 1 addition & 1 deletion rust/abis/FastHypERC20Collateral.json
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@
"name": "wrappedToken",
"outputs": [
{
"internalType": "contract IERC20",
"internalType": "contract ERC20ABI",
"name": "",
"type": "address"
}
Expand Down
4 changes: 2 additions & 2 deletions rust/tests/contracts/eth/bind/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ pub mod fast_hyp_erc20_collateral;
#[allow(clippy::all)]
pub mod mailbox;
#[allow(clippy::all)]
pub mod test_merkle_tree_hook;
#[allow(clippy::all)]
pub mod test_mock_ism;
#[allow(clippy::all)]
pub mod test_mock_msg_receiver;
#[allow(clippy::all)]
pub mod test_merkle_tree_hook;
18 changes: 8 additions & 10 deletions rust/tests/contracts/strk/ism.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,19 +47,17 @@ impl Ism {
owner: &StarknetAccount,
deployer: &StarknetAccount,
) -> eyre::Result<FieldElement> {
let res = deploy_contract(codes.ism_multisig, vec![owner.address()], deployer).await;

let contract = messageid_multisig_ism::new(res.0, owner);
contract
.set_validators(
&set.validators
let params: Vec<FieldElement> = std::iter::once(owner.address())
.chain(
set.validators
.iter()
.map(|v| v.eth_addr())
.collect::<Vec<_>>(),
.map(|validator| validator.eth_addr().0),
)
.send()
.await?;
.collect();
let res: (FieldElement, starknet::core::types::InvokeTransactionResult) =
deploy_contract(codes.ism_multisig, params, deployer).await;

let contract = messageid_multisig_ism::new(res.0, owner);
Ok(res.0)
}

Expand Down
2 changes: 1 addition & 1 deletion rust/tests/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ where
&DOMAIN_EVM,
&cainome::cairo_serde::ContractAddress(FieldElement::from_bytes_be(&receiver).unwrap()),
&to_strk_message_bytes(msg_body),
&cainome::cairo_serde::U256 { low: 0, high: 0 },
&None,
&None,
)
Expand Down Expand Up @@ -235,7 +236,6 @@ async fn test_mailbox_strk_to_evm() -> eyre::Result<()> {

// init eth env
let anvil = eth::setup_env(DOMAIN_EVM).await?;

let _ = send_msg_strk_to_evm(&strk, &anvil).await?;

Ok(())
Expand Down
Loading