Skip to content

Commit

Permalink
test: more SN erc20 unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
uri-99 committed Apr 19, 2024
1 parent 12f8a1d commit 32dcbea
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 18 deletions.
40 changes: 23 additions & 17 deletions contracts/starknet/src/tests/test_escrow_erc20.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ mod Escrow {
Serde::serialize(@order_id, ref payload_buffer);
Serde::serialize(@order_erc20.recipient_address, ref payload_buffer);
Serde::serialize(@order_erc20.amount_l1, ref payload_buffer);
Serde::serialize(@order_erc20.uri_token.contract_address, ref payload_buffer); //wrong, sent to uri_token instead of l1_erc20_address
Serde::serialize(@order_erc20.l2_erc20_address, ref payload_buffer); //wrong, sent to l2_erc20_address instead of l1_erc20_address

l1_handler.from_address = ETH_TRANSFER_CONTRACT().into();
l1_handler.payload = payload_buffer.span();
Expand All @@ -193,37 +193,43 @@ mod Escrow {
}

#[test]
fn test_fail_random_eth_user_calls_l1_handler() {
let (escrow, _, _) = setup_with_erc20();
let data: Array<felt252> = array![1, MM_ETHEREUM().into(), 3, L1_ERC20_ADDRESS().into(), 5];
let mut payload_buffer: Array<felt252> = ArrayTrait::new();
data.serialize(ref payload_buffer);
fn test_fail_claim_erc20_wrong_erc20() {
let (escrow, eth_token, uri_token) = setup_with_erc20();

//set order
start_prank(CheatTarget::One(escrow.contract_address), USER());
let order_erc20 = OrderERC20 { recipient_address: ETH_USER(), amount_l2: 200, amount_l1: 100, fee: 10, l2_erc20_address: uri_token.contract_address, l1_erc20_address: L1_ERC20_ADDRESS() };
let order_id = escrow.set_order_erc20(order_erc20);
stop_prank(CheatTarget::One(escrow.contract_address));


let mut l1_handler = L1HandlerTrait::new(
contract_address: escrow.contract_address,
function_name: 'claim_payment_erc20',
function_name: 'claim_payment_erc20'
);
l1_handler.from_address = ETH_USER().into();

//claim payment
let mut payload_buffer: Array<felt252> = ArrayTrait::new();
Serde::serialize(@order_id, ref payload_buffer);
Serde::serialize(@order_erc20.recipient_address, ref payload_buffer);
Serde::serialize(@order_erc20.amount_l2, ref payload_buffer); //wrong, sent amount_l2 instead of amount_l1
Serde::serialize(@order_erc20.l1_erc20_address, ref payload_buffer);

l1_handler.from_address = ETH_TRANSFER_CONTRACT().into();
l1_handler.payload = payload_buffer.span();

// same as "Should Panic" but for the L1 handler function
match l1_handler.execute() {
Result::Ok(_) => panic_with_felt252('shouldve panicked'),
Result::Err(RevertedTransaction) => {
assert(*RevertedTransaction.panic_data.at(0) == 'Only PAYMENT_REGISTRY_CONTRACT', *RevertedTransaction.panic_data.at(0));
assert(*RevertedTransaction.panic_data.at(0) == 'amount_l1 not match L1', *RevertedTransaction.panic_data.at(0));
}
}
}

#[test]
fn test_fail_call_erc20_l1_handler_while_paused() {
fn test_fail_random_eth_user_calls_l1_handler() {
let (escrow, _, _) = setup_with_erc20();
let pausable = IPausableDispatcher { contract_address: escrow.contract_address };

start_prank(CheatTarget::One(escrow.contract_address), OWNER());
escrow.pause();
stop_prank(CheatTarget::One(escrow.contract_address));

let data: Array<felt252> = array![1, MM_ETHEREUM().into(), 3, L1_ERC20_ADDRESS().into(), 5];
let mut payload_buffer: Array<felt252> = ArrayTrait::new();
data.serialize(ref payload_buffer);
Expand All @@ -239,7 +245,7 @@ mod Escrow {
match l1_handler.execute() {
Result::Ok(_) => panic_with_felt252('shouldve panicked'),
Result::Err(RevertedTransaction) => {
assert(*RevertedTransaction.panic_data.at(0) == 'Pausable: paused', *RevertedTransaction.panic_data.at(0));
assert(*RevertedTransaction.panic_data.at(0) == 'Only PAYMENT_REGISTRY_CONTRACT', *RevertedTransaction.panic_data.at(0));
}
}
}
Expand Down
31 changes: 30 additions & 1 deletion contracts/starknet/src/tests/test_escrow_pause.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mod Escrow {

use yab::tests::utils::{
constants::EscrowConstants::{
USER, OWNER, MM_STARKNET, MM_ETHEREUM, ETH_TRANSFER_CONTRACT, ETH_USER
USER, OWNER, MM_STARKNET, MM_ETHEREUM, ETH_TRANSFER_CONTRACT, ETH_USER, L1_ERC20_ADDRESS
},
};

Expand Down Expand Up @@ -268,4 +268,33 @@ mod Escrow {
}
}
}

#[test]
fn test_fail_call_erc20_l1_handler_while_paused() {
let (escrow, _) = setup();
let pausable = IPausableDispatcher { contract_address: escrow.contract_address };

start_prank(CheatTarget::One(escrow.contract_address), OWNER());
escrow.pause();
stop_prank(CheatTarget::One(escrow.contract_address));

let data: Array<felt252> = array![1, MM_ETHEREUM().into(), 3, L1_ERC20_ADDRESS().into(), 5];
let mut payload_buffer: Array<felt252> = ArrayTrait::new();
data.serialize(ref payload_buffer);
let mut l1_handler = L1HandlerTrait::new(
contract_address: escrow.contract_address,
function_name: 'claim_payment_erc20',
);
l1_handler.from_address = ETH_USER().into();

l1_handler.payload = payload_buffer.span();

// same as "Should Panic" but for the L1 handler function
match l1_handler.execute() {
Result::Ok(_) => panic_with_felt252('shouldve panicked'),
Result::Err(RevertedTransaction) => {
assert(*RevertedTransaction.panic_data.at(0) == 'Pausable: paused', *RevertedTransaction.panic_data.at(0));
}
}
}
}

0 comments on commit 32dcbea

Please sign in to comment.