From 071b52552a3c21bb6f9504f226072894de0538c0 Mon Sep 17 00:00:00 2001 From: Lana Ivina Date: Thu, 12 Dec 2024 18:42:03 +0100 Subject: [PATCH] fix tests --- .../src/orderbook/test_orderbook.cairo | 80 +++++++++++-------- packages/onchain/src/utils/constants.cairo | 4 + 2 files changed, 52 insertions(+), 32 deletions(-) diff --git a/packages/onchain/src/orderbook/test_orderbook.cairo b/packages/onchain/src/orderbook/test_orderbook.cairo index 050bd78..aec56b5 100644 --- a/packages/onchain/src/orderbook/test_orderbook.cairo +++ b/packages/onchain/src/orderbook/test_orderbook.cairo @@ -13,41 +13,56 @@ use onchain::utils::{constants, erc20_utils}; fn setup_orderbook( - erc20_contract_address: ContractAddress, -) -> (OrderbookABIDispatcher, ContractAddress) { + erc20_contract_address: ContractAddress, relay_address: ContractAddress, +) -> OrderbookABIDispatcher { // declare Orderbook contract let contract_class = declare("Orderbook").unwrap().contract_class(); // deploy Orderbook contract let mut calldata = array![]; calldata.append_serde(erc20_contract_address); + calldata.append_serde(relay_address); let (contract_address, _) = contract_class.deploy(@calldata).unwrap(); - (OrderbookABIDispatcher { contract_address }, contract_address) + OrderbookABIDispatcher { contract_address } } -fn setup() -> ( - OrderbookABIDispatcher, ContractAddress, ERC20UpgradeableABIDispatcher, ContractAddress, -) { +fn setup_relay() -> ContractAddress { + // declare TransactionInclusion contract + let contract_class = declare("TransactionInclusion").unwrap().contract_class(); + + // deploy TransactionInclusion contract + let mut calldata = array![]; + calldata.append_serde(constants::UTU()); // TODO replace with deployed Utu contract + + let (relay_address, _) = contract_class.deploy(@calldata).unwrap(); + + relay_address +} + +fn setup() -> (OrderbookABIDispatcher, ERC20UpgradeableABIDispatcher) { // deploy an ERC20 - let (erc20_strk, erc20_address) = erc20_utils::setup_erc20(test_address()); + let (erc20_strk, _) = erc20_utils::setup_erc20(test_address()); + + // deploy relay contract + let relay_address = setup_relay(); // deploy Orderbook contract - let (orderbook, contract_address) = setup_orderbook(erc20_strk.contract_address); + let orderbook = setup_orderbook(erc20_strk.contract_address, relay_address); - (orderbook, contract_address, erc20_strk, erc20_address) + (orderbook, erc20_strk) } #[test] fn test_request_inscription_stored_and_retrieved() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "bc1p5d7rjq7g6r4jdyhzks9smlaqtedr4dekq08ge8ztwac72sfr9rusxg3297"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); orderbook_dispatcher.request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); @@ -56,7 +71,8 @@ fn test_request_inscription_stored_and_retrieved() { assert_eq!(expected, actual); let expected_contract_balance = 10; // the submitter fee transferred to the contract - let actual_contract_balance = token_dispatcher.balance_of(contract_address); + let actual_contract_balance = token_dispatcher + .balance_of(orderbook_dispatcher.contract_address); assert_eq!(expected_contract_balance, actual_contract_balance); let expected_user_balance = constants::SUPPLY - 10; // the user balance after the request call @@ -67,12 +83,12 @@ fn test_request_inscription_stored_and_retrieved() { #[test] #[should_panic] fn test_request_inscription_fails_wrong_currency() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); orderbook_dispatcher.request_inscription(test_data, test_taproot_address, 1, 'BTC'.into(), 10); } @@ -80,12 +96,12 @@ fn test_request_inscription_fails_wrong_currency() { #[test] #[should_panic] fn test_request_inscription_fails_insufficient_balance() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 2000); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 2000); orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 2000); @@ -93,12 +109,12 @@ fn test_request_inscription_fails_insufficient_balance() { #[test] fn test_lock_inscription_works() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let id = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); @@ -111,12 +127,12 @@ fn test_lock_inscription_works() { #[test] #[should_panic] fn test_lock_inscription_fails_prior_lock_not_expired() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let id = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); @@ -128,12 +144,12 @@ fn test_lock_inscription_fails_prior_lock_not_expired() { #[test] #[should_panic] fn test_lock_inscription_fails_inscription_not_found() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let _ = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); @@ -147,19 +163,19 @@ fn test_lock_inscription_fails_status_closed() { // TODO: when `submit_inscripti #[test] fn test_cancel_inscription_works() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let id = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); - start_cheat_caller_address_global(contract_address); + start_cheat_caller_address_global(orderbook_dispatcher.contract_address); // TODO: is this the correct way to set permissions? - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); stop_cheat_caller_address_global(); orderbook_dispatcher.cancel_inscription(id, 'STRK'.into()); @@ -168,12 +184,12 @@ fn test_cancel_inscription_works() { #[test] #[should_panic] fn test_cancel_inscription_fails_locked() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let id = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); @@ -189,18 +205,18 @@ fn test_cancel_inscription_fails_closed() { // TODO: when `submit_inscription` i #[test] #[should_panic] fn test_cancel_inscription_fails_canceled() { - let (orderbook_dispatcher, contract_address, token_dispatcher, _) = setup(); + let (orderbook_dispatcher, token_dispatcher) = setup(); let test_taproot_address: ByteArray = "test"; let test_data: ByteArray = "data"; - token_dispatcher.approve(contract_address, 100); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); let id = orderbook_dispatcher .request_inscription(test_data, test_taproot_address, 1, 'STRK'.into(), 10); - start_cheat_caller_address_global(contract_address); - token_dispatcher.approve(contract_address, 100); + start_cheat_caller_address_global(orderbook_dispatcher.contract_address); + token_dispatcher.approve(orderbook_dispatcher.contract_address, 100); stop_cheat_caller_address_global(); orderbook_dispatcher.cancel_inscription(id, 'STRK'.into()); diff --git a/packages/onchain/src/utils/constants.cairo b/packages/onchain/src/utils/constants.cairo index b586186..392a227 100644 --- a/packages/onchain/src/utils/constants.cairo +++ b/packages/onchain/src/utils/constants.cairo @@ -13,3 +13,7 @@ pub fn SYMBOL() -> ByteArray { pub fn OWNER() -> ContractAddress { contract_address_const::<'owner'>() } + +pub fn UTU() -> ContractAddress { + contract_address_const::<'utu'>() +}