Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
lana-shanghai committed Dec 12, 2024
1 parent 0c25ea2 commit 071b525
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 32 deletions.
80 changes: 48 additions & 32 deletions packages/onchain/src/orderbook/test_orderbook.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -67,38 +83,38 @@ 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);
}

#[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);
}

#[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);
Expand All @@ -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);
Expand All @@ -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);
Expand All @@ -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());
Expand All @@ -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);
Expand All @@ -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());
Expand Down
4 changes: 4 additions & 0 deletions packages/onchain/src/utils/constants.cairo
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,7 @@ pub fn SYMBOL() -> ByteArray {
pub fn OWNER() -> ContractAddress {
contract_address_const::<'owner'>()
}

pub fn UTU() -> ContractAddress {
contract_address_const::<'utu'>()
}

0 comments on commit 071b525

Please sign in to comment.