From 7fd35e92776e20f46cd002eaf2af95830f06a012 Mon Sep 17 00:00:00 2001 From: Braqzen <103777923+Braqzen@users.noreply.github.com> Date: Wed, 10 Apr 2024 20:04:32 +0100 Subject: [PATCH] Ported sdk --- Cargo.lock | 2 + market-contract/Cargo.toml | 2 + .../tests/functions/core/cancel_order.rs | 179 +++++---- .../tests/functions/core/deposit.rs | 43 +- .../tests/functions/core/open_order.rs | 373 +++++++++--------- .../tests/functions/core/set_fee.rs | 47 +-- .../tests/functions/core/withdraw.rs | 86 ++-- .../tests/functions/info/account.rs | 23 +- .../tests/functions/info/config.rs | 13 +- market-contract/tests/functions/info/fee.rs | 40 +- market-contract/tests/functions/info/order.rs | 53 +-- .../tests/functions/info/order_id.rs | 123 +++--- .../tests/functions/info/user_orders.rs | 36 +- market-contract/tests/harness.rs | 2 +- market-contract/tests/{utils => }/setup.rs | 54 +-- market-contract/tests/utils/interface/core.rs | 94 ----- market-contract/tests/utils/interface/info.rs | 56 --- market-contract/tests/utils/interface/mod.rs | 2 - market-contract/tests/utils/mod.rs | 2 - 19 files changed, 552 insertions(+), 678 deletions(-) rename market-contract/tests/{utils => }/setup.rs (63%) delete mode 100644 market-contract/tests/utils/interface/core.rs delete mode 100644 market-contract/tests/utils/interface/info.rs delete mode 100644 market-contract/tests/utils/interface/mod.rs delete mode 100644 market-contract/tests/utils/mod.rs diff --git a/Cargo.lock b/Cargo.lock index 59940d8..578474f 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2231,7 +2231,9 @@ checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f" name = "market-contract" version = "0.1.0" dependencies = [ + "anyhow", "fuels", + "spark-market-sdk", "tokio", ] diff --git a/market-contract/Cargo.toml b/market-contract/Cargo.toml index b12eba9..920d2b1 100644 --- a/market-contract/Cargo.toml +++ b/market-contract/Cargo.toml @@ -7,7 +7,9 @@ license = { workspace = true } rust-version = { workspace = true } [dependencies] +anyhow = { workspace = true } fuels = { workspace = true } +spark-market-sdk = { path = "../spark-market-sdk" } tokio = { workspace = true } [[test]] diff --git a/market-contract/tests/functions/core/cancel_order.rs b/market-contract/tests/functions/core/cancel_order.rs index a451b5a..a9661e4 100644 --- a/market-contract/tests/functions/core/cancel_order.rs +++ b/market-contract/tests/functions/core/cancel_order.rs @@ -1,25 +1,21 @@ -use crate::utils::{ - interface::core::{cancel_order, deposit, open_order}, - setup::{setup, Defaults, OrderType}, -}; +use crate::setup::{setup, Defaults}; +use spark_market_sdk::OrderType; mod success { use super::*; - use crate::utils::{ - interface::info::{account, order, order_id, user_orders}, - setup::{create_account, CancelOrderEvent}, - }; + use crate::setup::create_account; + use spark_market_sdk::CancelOrderEvent; #[tokio::test] - async fn sell_base() { + async fn sell_base() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; let order_amount = 1; @@ -27,44 +23,47 @@ mod success { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; - let id = open_order(&contract, order_amount, asset, order_type, price) - .await + let _ = contract.deposit(deposit_amount, asset).await?; + let id = contract + .open_order(order_amount, asset, order_type, price) + .await? .value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(deposit_amount - order_amount, 0, order_amount, 0); - let mut orders = user_orders(&contract, owner.identity()).await.value; + let mut orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); - assert!(order(&contract, id).await.value.is_some()); + assert!(contract.order(id).await?.value.is_some()); - let response = cancel_order(&contract, id).await; + let response = contract.cancel_order(id).await?; let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); assert_eq!(*event, CancelOrderEvent { order_id: id }); - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(deposit_amount, 0, 0, 0); - let orders = user_orders(&contract, owner.identity()).await.value; + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 0); - assert!(order(&contract, id).await.value.is_none()); + assert!(contract.order(id).await?.value.is_none()); + + Ok(()) } - #[tokio::test] #[ignore] - async fn sell_quote() { + #[tokio::test] + async fn sell_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 70000; let order_amount = 50000; @@ -72,44 +71,47 @@ mod success { let order_type = OrderType::Sell; let price = 50000; - let _ = deposit(&contract, deposit_amount, asset).await; - let id = open_order(&contract, order_amount, asset, order_type, price) - .await + let _ = contract.deposit(deposit_amount, asset).await?; + let id = contract + .open_order(order_amount, asset, order_type, price) + .await? .value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, deposit_amount - order_amount, 0, order_amount); - let mut orders = user_orders(&contract, owner.identity()).await.value; + let mut orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); - assert!(order(&contract, id).await.value.is_some()); + assert!(contract.order(id).await?.value.is_some()); - let response = cancel_order(&contract, id).await; + let response = contract.cancel_order(id).await?; let log = response .decode_logs_with_type::() .unwrap(); let event = log.first().unwrap(); assert_eq!(*event, CancelOrderEvent { order_id: id }); - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, deposit_amount, 0, 0); - let orders = user_orders(&contract, owner.identity()).await.value; + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 0); - assert!(order(&contract, id).await.value.is_none()); + assert!(contract.order(id).await?.value.is_none()); + + Ok(()) } #[ignore] #[tokio::test] - async fn buy_base() { + async fn buy_base() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 70000; let expected_account = create_account(0, deposit_amount, 0, 0); @@ -119,52 +121,52 @@ mod success { let asset_to_pay_wth = assets.quote.id; let order_type = OrderType::Buy; let price = 70000; - let expected_id = order_id( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset_to_buy, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset_to_pay_wth).await; + let _ = contract.deposit(deposit_amount, asset_to_pay_wth).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); + assert!(contract.order(expected_id).await?.value.is_none()); - let id = open_order(&contract, order_amount, asset_to_buy, order_type, price) - .await + let id = contract + .open_order(order_amount, asset_to_buy, order_type, price) + .await? .value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, 0, deposit_amount); - let mut orders = user_orders(&contract, owner.identity()).await.value; - - dbg!(&user_account); - dbg!(&expected_account); + let mut orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); + + Ok(()) } #[ignore] #[tokio::test] - async fn buy_quote() { + async fn buy_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 1; let expected_account = create_account(deposit_amount, 0, 0, 0); @@ -174,32 +176,33 @@ mod success { let asset_to_pay_wth = assets.base.id; let order_type = OrderType::Buy; let price = 70000; - let expected_id = order_id( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset_to_buy, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset_to_pay_wth).await; + let _ = contract.deposit(deposit_amount, asset_to_pay_wth).await?; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); + assert!(contract.order(expected_id).await?.value.is_none()); - let id = open_order(&contract, order_amount, asset_to_buy, order_type, price) - .await + let id = contract + .open_order(order_amount, asset_to_buy, order_type, price) + .await? .value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, deposit_amount, 0); - let mut orders = user_orders(&contract, owner.identity()).await.value; + let mut orders = contract.user_orders(owner.identity()).await?.value; dbg!(&user_account); dbg!(&expected_account); @@ -208,6 +211,8 @@ mod success { assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); + + Ok(()) } } @@ -225,10 +230,11 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); // Revert - cancel_order(&contract, Bits256([0u8; 32])).await; + contract.cancel_order(Bits256([0u8; 32])).await.unwrap(); } #[tokio::test] @@ -240,7 +246,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; let order_amount = 1; @@ -248,12 +255,20 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; - let id = open_order(&contract, order_amount, asset, order_type, price) + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); + let id = contract + .open_order(order_amount, asset, order_type, price) .await + .unwrap() .value; // Revert - cancel_order(&contract.with_account(user.wallet).unwrap(), id).await; + contract + .with_account(&user.wallet) + .await + .unwrap() + .cancel_order(id) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/core/deposit.rs b/market-contract/tests/functions/core/deposit.rs index 4eb1d9c..5795d1d 100644 --- a/market-contract/tests/functions/core/deposit.rs +++ b/market-contract/tests/functions/core/deposit.rs @@ -1,33 +1,28 @@ -use crate::utils::{ - interface::core::deposit, - setup::{setup, Defaults}, -}; +use crate::setup::{setup, Defaults}; mod success { use super::*; - use crate::utils::{ - interface::info::account, - setup::{create_account, DepositEvent}, - }; + use crate::setup::create_account; + use spark_market_sdk::DepositEvent; #[tokio::test] - async fn base_asset() { + async fn base_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; let expected_account = create_account(deposit_amount, 0, 0, 0); // Precondition enforces empty account - assert!(account(&contract, owner.identity()).await.value.is_none()); + assert!(contract.account(owner.identity()).await?.value.is_none()); - let response = deposit(&contract, deposit_amount, assets.base.id).await; + let response = contract.deposit(deposit_amount, assets.base.id).await?; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( @@ -39,29 +34,31 @@ mod success { } ); - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); assert_eq!(user_account, expected_account); + + Ok(()) } #[ignore] #[tokio::test] - async fn quote_asset() { + async fn quote_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; let expected_account = create_account(0, deposit_amount, 0, 0); // Precondition enforces empty account - assert!(account(&contract, owner.identity()).await.value.is_none()); + assert!(contract.account(owner.identity()).await?.value.is_none()); - let response = deposit(&contract, deposit_amount, assets.quote.id).await; + let response = contract.deposit(deposit_amount, assets.quote.id).await?; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); assert_eq!( @@ -73,9 +70,11 @@ mod success { } ); - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); assert_eq!(user_account, expected_account); + + Ok(()) } } @@ -92,11 +91,15 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; // Revert - deposit(&contract, deposit_amount, assets.random.id).await; + contract + .deposit(deposit_amount, assets.random.id) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/core/open_order.rs b/market-contract/tests/functions/core/open_order.rs index d428645..ea37ed7 100644 --- a/market-contract/tests/functions/core/open_order.rs +++ b/market-contract/tests/functions/core/open_order.rs @@ -1,25 +1,21 @@ -use crate::utils::{ - interface::core::{deposit, open_order}, - setup::{setup, Defaults, OrderType}, -}; +use crate::setup::{setup, Defaults}; +use spark_market_sdk::OrderType; mod success { use super::*; - use crate::utils::{ - interface::info::{account, order, order_id, user_orders}, - setup::{create_account, OpenOrderEvent}, - }; + use crate::setup::create_account; + use spark_market_sdk::OpenOrderEvent; #[tokio::test] - async fn sell_base() { + async fn sell_base() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 5; let expected_account = create_account(deposit_amount, 0, 0, 0); @@ -28,42 +24,44 @@ mod success { let asset = assets.base.id; let order_type = OrderType::Sell; let price = 70000; - let expected_id = order_id( - &contract, - order_amount, - asset, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await?; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); + assert!(contract.order(expected_id).await?.value.is_none()); - let response = open_order(&contract, order_amount, asset, order_type.clone(), price).await; + let response = contract + .open_order(order_amount, asset, order_type.clone(), price) + .await?; let id = response.value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(deposit_amount - order_amount, 0, order_amount, 0); - let mut orders = user_orders(&contract, owner.identity()).await.value; - let order = order(&contract, expected_id).await.value.unwrap(); - let stored_id = order_id( - &contract, - order.amount, - order.asset, - order.order_type, - order.owner, - order.price, - ) - .await - .value; + let mut orders = contract.user_orders(owner.identity()).await?.value; + let order = contract.order(expected_id).await?.value.unwrap(); + let stored_id = contract + .order_id( + order.amount, + order.asset, + order.order_type, + order.owner, + order.price, + ) + .await? + .value; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -85,18 +83,20 @@ mod success { assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); assert_eq!(stored_id, expected_id); + + Ok(()) } #[ignore] #[tokio::test] - async fn sell_quote() { + async fn sell_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 1000; let expected_account = create_account(0, deposit_amount, 0, 0); @@ -105,42 +105,44 @@ mod success { let asset = assets.quote.id; let order_type = OrderType::Sell; let price = 1; - let expected_id = order_id( - &contract, - order_amount, - asset, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); + assert!(contract.order(expected_id).await?.value.is_none()); - let response = open_order(&contract, order_amount, asset, order_type.clone(), price).await; + let response = contract + .open_order(order_amount, asset, order_type.clone(), price) + .await?; let id = response.value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, deposit_amount - order_amount, 0, order_amount); - let mut orders = user_orders(&contract, owner.identity()).await.value; - let order = order(&contract, expected_id).await.value.unwrap(); - let stored_id = order_id( - &contract, - order.amount, - order.asset, - order.order_type, - order.owner, - order.price, - ) - .await - .value; + let mut orders = contract.user_orders(owner.identity()).await?.value; + let order = contract.order(expected_id).await?.value.unwrap(); + let stored_id = contract + .order_id( + order.amount, + order.asset, + order.order_type, + order.owner, + order.price, + ) + .await? + .value; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -162,18 +164,20 @@ mod success { assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); assert_eq!(stored_id, expected_id); + + Ok(()) } #[ignore] #[tokio::test] - async fn buy_base() { + async fn buy_base() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 70000; let expected_account = create_account(0, deposit_amount, 0, 0); @@ -183,49 +187,44 @@ mod success { let asset_to_pay_wth = assets.quote.id; let order_type = OrderType::Buy; let price = 70000; - let expected_id = order_id( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset_to_buy, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset_to_pay_wth).await; + let _ = contract.deposit(deposit_amount, asset_to_pay_wth).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); - - let response = open_order( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - price, - ) - .await; + assert!(contract.order(expected_id).await?.value.is_none()); + + let response = contract + .open_order(order_amount, asset_to_buy, order_type.clone(), price) + .await?; let id = response.value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, 0, deposit_amount); - let mut orders = user_orders(&contract, owner.identity()).await.value; - let order = order(&contract, expected_id).await.value.unwrap(); - let stored_id = order_id( - &contract, - order.amount, - order.asset, - order.order_type, - order.owner, - order.price, - ) - .await - .value; + let mut orders = contract.user_orders(owner.identity()).await?.value; + let order = contract.order(expected_id).await?.value.unwrap(); + let stored_id = contract + .order_id( + order.amount, + order.asset, + order.order_type, + order.owner, + order.price, + ) + .await? + .value; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -242,26 +241,25 @@ mod success { } ); - dbg!(&user_account); - dbg!(&expected_account); - assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); assert_eq!(stored_id, expected_id); + + Ok(()) } #[ignore] #[tokio::test] - async fn buy_quote() { + async fn buy_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 1; let expected_account = create_account(deposit_amount, 0, 0, 0); @@ -271,49 +269,44 @@ mod success { let asset_to_pay_wth = assets.base.id; let order_type = OrderType::Buy; let price = 70000; - let expected_id = order_id( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - owner.identity(), - price, - ) - .await - .value; + let expected_id = contract + .order_id( + order_amount, + asset_to_buy, + order_type.clone(), + owner.identity(), + price, + ) + .await? + .value; - let _ = deposit(&contract, deposit_amount, asset_to_pay_wth).await; + let _ = contract.deposit(deposit_amount, asset_to_pay_wth).await?; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); - let orders = user_orders(&contract, owner.identity()).await.value; + let user_account = contract.account(owner.identity()).await?.value.unwrap(); + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(user_account, expected_account); assert_eq!(orders, vec![]); - assert!(order(&contract, expected_id).await.value.is_none()); - - let response = open_order( - &contract, - order_amount, - asset_to_buy, - order_type.clone(), - price, - ) - .await; + assert!(contract.order(expected_id).await?.value.is_none()); + + let response = contract + .open_order(order_amount, asset_to_buy, order_type.clone(), price) + .await?; let id = response.value; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, deposit_amount, 0); - let mut orders = user_orders(&contract, owner.identity()).await.value; - let order = order(&contract, expected_id).await.value.unwrap(); - let stored_id = order_id( - &contract, - order.amount, - order.asset, - order.order_type, - order.owner, - order.price, - ) - .await - .value; + let mut orders = contract.user_orders(owner.identity()).await?.value; + let order = contract.order(expected_id).await?.value.unwrap(); + let stored_id = contract + .order_id( + order.amount, + order.asset, + order.order_type, + order.owner, + order.price, + ) + .await? + .value; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -330,14 +323,13 @@ mod success { } ); - dbg!(&user_account); - dbg!(&expected_account); - assert_eq!(user_account, expected_account); assert_eq!(orders.len(), 1); assert_eq!(orders.pop().unwrap(), id); assert_eq!(id, expected_id); assert_eq!(stored_id, expected_id); + + Ok(()) } } @@ -354,7 +346,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let order_amount = 10; let asset = assets.random.id; @@ -362,7 +355,10 @@ mod revert { let price = 70000; // Revert - open_order(&contract, order_amount, asset, order_type, price).await; + contract + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } #[tokio::test] @@ -374,7 +370,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let order_amount = 10; let asset = assets.base.id; @@ -382,14 +379,13 @@ mod revert { let price = 70000; // Revert - open_order( - &contract.with_account(user.wallet).unwrap(), - order_amount, - asset, - order_type, - price, - ) - .await; + contract + .with_account(&user.wallet) + .await + .unwrap() + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } #[tokio::test] @@ -401,7 +397,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 10; let order_amount = 100; @@ -409,10 +406,13 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); // Revert - open_order(&contract, order_amount, asset, order_type.clone(), price).await; + contract + .open_order(order_amount, asset, order_type.clone(), price) + .await + .unwrap(); } #[ignore] @@ -425,7 +425,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 10; let order_amount = 100; @@ -433,10 +434,13 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); // Revert - open_order(&contract, order_amount, asset, order_type.clone(), price).await; + contract + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } #[ignore] // TODO: incomplete @@ -449,7 +453,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 10; let order_amount = 100; @@ -457,10 +462,13 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); // Revert - open_order(&contract, order_amount, asset, order_type.clone(), price).await; + contract + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } #[ignore] // TODO: incomplete @@ -473,7 +481,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 10; let order_amount = 100; @@ -481,10 +490,13 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); // Revert - open_order(&contract, order_amount, asset, order_type.clone(), price).await; + contract + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } #[tokio::test] @@ -496,7 +508,8 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; let order_amount = 10; @@ -504,10 +517,16 @@ mod revert { let order_type = OrderType::Sell; let price = 70000; - let _ = deposit(&contract, deposit_amount, asset).await; - let _ = open_order(&contract, order_amount, asset, order_type.clone(), price).await; + let _ = contract.deposit(deposit_amount, asset).await.unwrap(); + let _ = contract + .open_order(order_amount, asset, order_type.clone(), price) + .await + .unwrap(); // Revert - open_order(&contract, order_amount, asset, order_type, price).await; + contract + .open_order(order_amount, asset, order_type, price) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/core/set_fee.rs b/market-contract/tests/functions/core/set_fee.rs index 0dc01c9..d54b5c3 100644 --- a/market-contract/tests/functions/core/set_fee.rs +++ b/market-contract/tests/functions/core/set_fee.rs @@ -1,32 +1,29 @@ -use crate::utils::{ - interface::core::set_fee, - setup::{setup, Defaults}, -}; +use crate::setup::{setup, Defaults}; mod success { use super::*; - use crate::utils::{interface::info::fee, setup::SetFeeEvent}; + use spark_market_sdk::SetFeeEvent; #[tokio::test] - async fn sets_global_fee() { + async fn sets_global_fee() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, _user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let initial_fee = 0; let new_fee = 5; let user = None; // Assert precondition of initial fee - assert_eq!(fee(&contract, user.clone()).await.value, initial_fee); + assert_eq!(contract.fee(user.clone()).await?.value, initial_fee); // Increase the fee to new_fee - let response = set_fee(&contract, new_fee, user.clone()).await; + let response = contract.set_fee(new_fee, user.clone()).await?; // Log should be emitted when fee is changed let log = response.decode_logs_with_type::().unwrap(); @@ -41,28 +38,30 @@ mod success { // Check fee has changed from the initial fee assert_ne!(initial_fee, new_fee); - assert_eq!(fee(&contract, user).await.value, initial_fee + new_fee); + assert_eq!(contract.fee(user).await?.value, initial_fee + new_fee); + + Ok(()) } #[tokio::test] - async fn sets_premium_user_fee() { + async fn sets_premium_user_fee() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let initial_fee = 0; let new_fee = 5; let user = Some(user.identity()); // Assert precondition of initial fee - assert_eq!(fee(&contract, user.clone()).await.value, initial_fee); + assert_eq!(contract.fee(user.clone()).await?.value, initial_fee); // Increase the fee to new_fee - let response = set_fee(&contract, new_fee, user.clone()).await; + let response = contract.set_fee(new_fee, user.clone()).await?; // Log should be emitted when fee is changed let log = response.decode_logs_with_type::().unwrap(); @@ -77,7 +76,9 @@ mod success { // Check fee has changed from the initial fee assert_ne!(initial_fee, new_fee); - assert_eq!(fee(&contract, user).await.value, initial_fee + new_fee); + assert_eq!(contract.fee(user).await?.value, initial_fee + new_fee); + + Ok(()) } } @@ -94,16 +95,18 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let new_fee = 5; // Reverts - set_fee( - &contract.with_account(user.wallet.clone()).unwrap(), - new_fee, - Some(user.identity()), - ) - .await; + contract + .with_account(&user.wallet) + .await + .unwrap() + .set_fee(new_fee, Some(user.identity())) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/core/withdraw.rs b/market-contract/tests/functions/core/withdraw.rs index ead0b88..361deb4 100644 --- a/market-contract/tests/functions/core/withdraw.rs +++ b/market-contract/tests/functions/core/withdraw.rs @@ -1,39 +1,34 @@ -use crate::utils::{ - interface::core::{deposit, withdraw}, - setup::{setup, Defaults}, -}; +use crate::setup::{setup, Defaults}; mod success { use super::*; - use crate::utils::{ - interface::info::account, - setup::{create_account, WithdrawEvent}, - }; + use crate::setup::create_account; + use spark_market_sdk::WithdrawEvent; #[ignore] #[tokio::test] - async fn base_asset() { + async fn base_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; - deposit(&contract, deposit_amount, assets.base.id).await; + contract.deposit(deposit_amount, assets.base.id).await?; let user_balance = owner.balance(&assets.base.id).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(deposit_amount, 0, 0, 0); // Precondition enforces deposited account assert_eq!(user_account, expected_account); - let response = withdraw(&contract, deposit_amount, assets.base.id).await; + let response = contract.withdraw(deposit_amount, assets.base.id).await?; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -47,36 +42,38 @@ mod success { ); let new_balance = owner.balance(&assets.base.id).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, 0, 0); assert_eq!(new_balance, user_balance + deposit_amount); assert_eq!(user_account, expected_account); + + Ok(()) } #[ignore] #[tokio::test] - async fn quote_asset() { + async fn quote_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; - deposit(&contract, deposit_amount, assets.quote.id).await; + let _ = contract.deposit(deposit_amount, assets.quote.id).await?; let user_balance = owner.balance(&assets.quote.id).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, deposit_amount, 0, 0); // Precondition enforces deposited account assert_eq!(user_account, expected_account); - let response = withdraw(&contract, deposit_amount, assets.quote.id).await; + let response = contract.withdraw(deposit_amount, assets.quote.id).await?; let log = response.decode_logs_with_type::().unwrap(); let event = log.first().unwrap(); @@ -90,11 +87,13 @@ mod success { ); let new_balance = owner.balance(&assets.quote.id).await; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); let expected_account = create_account(0, 0, 0, 0); assert_eq!(new_balance, user_balance + deposit_amount); assert_eq!(user_account, expected_account); + + Ok(()) } } @@ -111,14 +110,21 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; - deposit(&contract, deposit_amount, assets.base.id).await; + let _ = contract + .deposit(deposit_amount, assets.base.id) + .await + .unwrap(); // Revert - withdraw(&contract, deposit_amount, assets.random.id).await; + contract + .withdraw(deposit_amount, assets.random.id) + .await + .unwrap(); } #[tokio::test] @@ -130,12 +136,16 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; // Revert - withdraw(&contract, deposit_amount, assets.base.id).await; + contract + .withdraw(deposit_amount, assets.base.id) + .await + .unwrap(); } #[tokio::test] @@ -147,14 +157,21 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; - deposit(&contract, deposit_amount, assets.base.id).await; + let _ = contract + .deposit(deposit_amount, assets.base.id) + .await + .unwrap(); // Revert - withdraw(&contract, deposit_amount + 1, assets.base.id).await; + contract + .withdraw(deposit_amount + 1, assets.base.id) + .await + .unwrap(); } #[ignore] @@ -167,13 +184,20 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await + .unwrap(); let deposit_amount = 100; - deposit(&contract, deposit_amount, assets.quote.id).await; + let _ = contract + .deposit(deposit_amount, assets.quote.id) + .await + .unwrap(); // Revert - withdraw(&contract, deposit_amount + 1, assets.quote.id).await; + contract + .withdraw(deposit_amount + 1, assets.quote.id) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/info/account.rs b/market-contract/tests/functions/info/account.rs index 0bd4f61..b7f6742 100644 --- a/market-contract/tests/functions/info/account.rs +++ b/market-contract/tests/functions/info/account.rs @@ -1,38 +1,39 @@ mod success { - use crate::utils::{ - interface::{core::deposit, info::account}, - setup::{create_account, setup, Defaults}, - }; + use crate::setup::{create_account, setup, Defaults}; #[tokio::test] - async fn returns_none() { + async fn returns_none() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; - assert!(account(&contract, owner.identity()).await.value.is_none()); + .await?; + assert!(contract.account(owner.identity()).await?.value.is_none()); + + Ok(()) } #[tokio::test] - async fn returns_account_info() { + async fn returns_account_info() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; let deposit_amount = 100; let expected_account = create_account(deposit_amount, 0, 0, 0); - deposit(&contract, deposit_amount, assets.base.id).await; + let _ = contract.deposit(deposit_amount, assets.base.id).await?; - let user_account = account(&contract, owner.identity()).await.value.unwrap(); + let user_account = contract.account(owner.identity()).await?.value.unwrap(); assert_eq!(user_account, expected_account); + + Ok(()) } } diff --git a/market-contract/tests/functions/info/config.rs b/market-contract/tests/functions/info/config.rs index 6a9d011..4fbc67f 100644 --- a/market-contract/tests/functions/info/config.rs +++ b/market-contract/tests/functions/info/config.rs @@ -1,22 +1,19 @@ mod success { - use crate::utils::{ - interface::info::config, - setup::{setup, Defaults}, - }; + use crate::setup::{setup, Defaults}; #[tokio::test] - async fn returns_config() { + async fn returns_config() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; assert_eq!( - config(&contract).await.value, + contract.config().await?.value, ( owner.address(), assets.base.id, @@ -26,5 +23,7 @@ mod success { defaults.price_decimals ) ); + + Ok(()) } } diff --git a/market-contract/tests/functions/info/fee.rs b/market-contract/tests/functions/info/fee.rs index 907ffc9..88686cb 100644 --- a/market-contract/tests/functions/info/fee.rs +++ b/market-contract/tests/functions/info/fee.rs @@ -1,67 +1,67 @@ mod success { - use crate::utils::{ - interface::{core::set_fee, info::fee}, - setup::{setup, Defaults}, - }; + use crate::setup::{setup, Defaults}; #[tokio::test] - async fn returns_global_fee() { + async fn returns_global_fee() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, _user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; // Change fee to be non-zero for testing purposes let global_fee = 5; let user = None; - set_fee(&contract, global_fee, user.clone()).await; + let _ = contract.set_fee(global_fee, user.clone()).await?; - assert_eq!(fee(&contract, user).await.value, global_fee); + assert_eq!(contract.fee(user).await?.value, global_fee); + + Ok(()) } #[tokio::test] - async fn returns_premium_user_fee() { + async fn returns_premium_user_fee() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; // Change fee to be non-zero for testing purposes let global_fee = 5; let user_fee = 1; - set_fee(&contract, global_fee, None).await; - set_fee(&contract, user_fee, Some(user.identity())).await; + let _ = contract.set_fee(global_fee, None).await?; + let _ = contract.set_fee(user_fee, Some(user.identity())).await?; + + assert_eq!(contract.fee(Some(user.identity())).await?.value, user_fee); - assert_eq!(fee(&contract, Some(user.identity())).await.value, user_fee); + Ok(()) } #[tokio::test] - async fn returns_global_fee_for_non_premium_user() { + async fn returns_global_fee_for_non_premium_user() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; // Change fee to be non-zero for testing purposes let global_fee = 5; - set_fee(&contract, global_fee, Some(user.identity())).await; + let _ = contract.set_fee(global_fee, Some(user.identity())).await?; + + assert_eq!(contract.fee(Some(user.identity())).await?.value, global_fee); - assert_eq!( - fee(&contract, Some(user.identity())).await.value, - global_fee - ); + Ok(()) } } diff --git a/market-contract/tests/functions/info/order.rs b/market-contract/tests/functions/info/order.rs index 2b1a4de..5d4782a 100644 --- a/market-contract/tests/functions/info/order.rs +++ b/market-contract/tests/functions/info/order.rs @@ -1,52 +1,53 @@ mod success { - use crate::utils::{ - interface::{ - core::{deposit, open_order}, - info::{order, order_id}, - }, - setup::{setup, Defaults, OrderType}, - }; + use crate::setup::{setup, Defaults}; use fuels::types::Bits256; + use spark_market_sdk::OrderType; #[tokio::test] - async fn returns_none() { + async fn returns_none() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, _user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; - let order = order(&contract, Bits256([0u8; 32])).await.value; + let order = contract.order(Bits256([0u8; 32])).await?.value; assert!(order.is_none()); + + Ok(()) } #[tokio::test] - async fn returns_order() { + async fn returns_order() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, _owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; - - deposit(&contract, 100, assets.base.id).await; - let id = open_order(&contract, 1, assets.base.id, OrderType::Buy, 70000).await; - - let order = order(&contract, id.value).await.value.unwrap(); - let expected_id = order_id( - &contract, - order.amount, - order.asset, - order.order_type, - order.owner, - order.price, - ) - .await; + .await?; + + let _ = contract.deposit(100, assets.base.id).await?; + let id = contract + .open_order(1, assets.base.id, OrderType::Buy, 70000) + .await?; + + let order = contract.order(id.value).await?.value.unwrap(); + let expected_id = contract + .order_id( + order.amount, + order.asset, + order.order_type, + order.owner, + order.price, + ) + .await?; assert_eq!(id.value, expected_id.value); + + Ok(()) } } diff --git a/market-contract/tests/functions/info/order_id.rs b/market-contract/tests/functions/info/order_id.rs index 6a53ed8..080183b 100644 --- a/market-contract/tests/functions/info/order_id.rs +++ b/market-contract/tests/functions/info/order_id.rs @@ -1,7 +1,5 @@ -use crate::utils::{ - interface::info::order_id, - setup::{setup, Defaults, OrderType}, -}; +use crate::setup::{setup, Defaults}; +use spark_market_sdk::OrderType; mod success { @@ -9,59 +7,43 @@ mod success { use fuels::types::Bits256; #[tokio::test] - async fn returns_order_id() { + async fn returns_order_id() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; - - let id = order_id( - &contract, - 10, - assets.base.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; + .await?; + + let id = contract + .order_id(10, assets.base.id, OrderType::Buy, owner.identity(), 70000) + .await?; let expected_id = Bits256([ 159, 12, 221, 182, 141, 102, 174, 95, 21, 142, 121, 195, 128, 124, 84, 242, 46, 187, 60, 57, 72, 58, 86, 233, 236, 198, 174, 153, 212, 35, 88, 30, ]); assert_eq!(id.value, expected_id); + + Ok(()) } #[tokio::test] - async fn orders_create_different_ids() { + async fn orders_create_different_ids() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; - - let id1 = order_id( - &contract, - 10, - assets.base.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; - let id2 = order_id( - &contract, - 15, - assets.base.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; + .await?; + + let id1 = contract + .order_id(10, assets.base.id, OrderType::Buy, owner.identity(), 70000) + .await?; + let id2 = contract + .order_id(15, assets.base.id, OrderType::Buy, owner.identity(), 70000) + .await?; let expected_id1 = Bits256([ 159, 12, 221, 182, 141, 102, 174, 95, 21, 142, 121, 195, 128, 124, 84, 242, 46, 187, @@ -75,10 +57,12 @@ mod success { assert_ne!(id1.value, id2.value); assert_eq!(id1.value, expected_id1); assert_eq!(id2.value, expected_id2); + + Ok(()) } #[tokio::test] - async fn accepts_base_asset() { + async fn accepts_base_asset() -> anyhow::Result<()> { // In this test we only care about the test not reverting with the correct asset let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( @@ -86,21 +70,17 @@ mod success { defaults.quote_decimals, defaults.price_decimals, ) - .await; - - let _ = order_id( - &contract, - 10, - assets.base.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; + .await?; + + let _ = contract + .order_id(10, assets.base.id, OrderType::Buy, owner.identity(), 70000) + .await?; + + Ok(()) } #[tokio::test] - async fn accepts_quote_asset() { + async fn accepts_quote_asset() -> anyhow::Result<()> { // In this test we only care about the test not reverting with the correct asset let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( @@ -108,17 +88,13 @@ mod success { defaults.quote_decimals, defaults.price_decimals, ) - .await; - - let _ = order_id( - &contract, - 10, - assets.quote.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; + .await?; + + let _ = contract + .order_id(10, assets.quote.id, OrderType::Buy, owner.identity(), 70000) + .await?; + + Ok(()) } } @@ -126,6 +102,7 @@ mod revert { use super::*; + #[ignore] #[tokio::test] #[should_panic(expected = "InvalidAsset")] async fn reverts_upon_invalid_asset() { @@ -135,16 +112,18 @@ mod revert { defaults.quote_decimals, defaults.price_decimals, ) - .await; - - order_id( - &contract, - 10, - assets.random.id, - OrderType::Buy, - owner.identity(), - 70000, - ) - .await; + .await + .unwrap(); + + contract + .order_id( + 10, + assets.random.id, + OrderType::Buy, + owner.identity(), + 70000, + ) + .await + .unwrap(); } } diff --git a/market-contract/tests/functions/info/user_orders.rs b/market-contract/tests/functions/info/user_orders.rs index cc13f80..3a5d96e 100644 --- a/market-contract/tests/functions/info/user_orders.rs +++ b/market-contract/tests/functions/info/user_orders.rs @@ -1,45 +1,49 @@ mod success { - use crate::utils::{ - interface::{ - core::{deposit, open_order}, - info::user_orders, - }, - setup::{setup, Defaults, OrderType}, - }; + use crate::setup::{setup, Defaults}; + use spark_market_sdk::OrderType; #[tokio::test] - async fn returns_zero_orders() { + async fn returns_zero_orders() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, _assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; - let orders = user_orders(&contract, owner.identity()).await.value; + let orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(orders, vec![]); + + Ok(()) } + #[ignore] #[tokio::test] - async fn returns_orders() { + async fn returns_orders() -> anyhow::Result<()> { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( defaults.base_decimals, defaults.quote_decimals, defaults.price_decimals, ) - .await; + .await?; - deposit(&contract, 100, assets.base.id).await; - let id1 = open_order(&contract, 1, assets.base.id, OrderType::Buy, 75000).await; - let id2 = open_order(&contract, 2, assets.base.id, OrderType::Buy, 70000).await; + let _ = contract.deposit(100, assets.base.id).await?; + let id2 = contract + .open_order(2, assets.base.id, OrderType::Buy, 70000) + .await?; + let id1 = contract + .open_order(1, assets.base.id, OrderType::Buy, 75000) + .await?; - let mut orders = user_orders(&contract, owner.identity()).await.value; + let mut orders = contract.user_orders(owner.identity()).await?.value; assert_eq!(2, orders.len()); assert_eq!(id2.value, orders.pop().unwrap()); assert_eq!(id1.value, orders.pop().unwrap()); + + Ok(()) } } diff --git a/market-contract/tests/harness.rs b/market-contract/tests/harness.rs index 39c9363..0635d00 100644 --- a/market-contract/tests/harness.rs +++ b/market-contract/tests/harness.rs @@ -1,2 +1,2 @@ mod functions; -mod utils; +mod setup; diff --git a/market-contract/tests/utils/setup.rs b/market-contract/tests/setup.rs similarity index 63% rename from market-contract/tests/utils/setup.rs rename to market-contract/tests/setup.rs index 698375e..0e11c49 100644 --- a/market-contract/tests/utils/setup.rs +++ b/market-contract/tests/setup.rs @@ -1,21 +1,12 @@ use fuels::{ accounts::ViewOnlyAccount, prelude::{ - abigen, launch_custom_provider_and_get_wallets, Address, AssetConfig, AssetId, Contract, - LoadConfiguration, StorageConfiguration, TxPolicies, WalletUnlocked, WalletsConfig, + launch_custom_provider_and_get_wallets, Address, AssetConfig, AssetId, WalletUnlocked, + WalletsConfig, }, types::Identity, }; - -abigen!(Contract( - name = "Market", - abi = "./market-contract/out/debug/market-contract-abi.json" -)); - -// PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("market-contract/out/debug/market-contract.bin"); -const MARKET_CONTRACT_BINARY_PATH: &str = "../market-contract/out/debug/market-contract.bin"; -const MARKET_CONTRACT_STORAGE_PATH: &str = - "../market-contract/out/debug/market-contract-storage_slots.json"; +use spark_market_sdk::{Account, Balance, MarketContract}; pub(crate) struct Assets { pub(crate) base: Asset, @@ -84,7 +75,7 @@ pub(crate) async fn setup( base_decimals: u32, quote_decimals: u32, price_decimals: u32, -) -> (Market, User, User, Assets) { +) -> anyhow::Result<(MarketContract, User, User, Assets)> { let number_of_wallets = 2; let coins_per_wallet = 1; let amount_per_coin = 100_000_000; @@ -109,12 +100,9 @@ pub(crate) async fn setup( coin_amount: amount_per_coin, }; let assets = vec![base_asset, quote_asset, random_asset]; - let config = WalletsConfig::new_multiple_assets(number_of_wallets, assets); - let mut wallets = launch_custom_provider_and_get_wallets(config, None, None) - .await - .unwrap(); + let mut wallets = launch_custom_provider_and_get_wallets(config, None, None).await?; let owner = wallets.pop().unwrap(); let user = wallets.pop().unwrap(); @@ -133,30 +121,18 @@ pub(crate) async fn setup( }, }; - let storage_configuration = - StorageConfiguration::default().add_slot_overrides_from_file(MARKET_CONTRACT_STORAGE_PATH); - - let configurables = MarketConfigurables::default() - .with_BASE_ASSET(assets.base.id) - .with_BASE_ASSET_DECIMALS(assets.base.decimals) - .with_QUOTE_ASSET(assets.quote.id) - .with_QUOTE_ASSET_DECIMALS(assets.quote.decimals) - .with_PRICE_DECIMALS(price_decimals) - .with_OWNER(owner.address().into()); - - let contract_configuration = LoadConfiguration::default() - .with_storage_configuration(storage_configuration.unwrap()) - .with_configurables(configurables); - - let contract_id = Contract::load_from(MARKET_CONTRACT_BINARY_PATH, contract_configuration) - .unwrap() - .deploy(&owner, TxPolicies::default()) - .await - .unwrap(); + let contract = MarketContract::deploy( + assets.base.id, + assets.base.decimals, + assets.quote.id, + assets.quote.decimals, + price_decimals, + owner.clone(), + ) + .await?; - let market = Market::new(contract_id.clone(), owner.clone()); let owner = User { wallet: owner }; let non_owner = User { wallet: user }; - (market, owner, non_owner, assets) + Ok((contract, owner, non_owner, assets)) } diff --git a/market-contract/tests/utils/interface/core.rs b/market-contract/tests/utils/interface/core.rs deleted file mode 100644 index 9b2472f..0000000 --- a/market-contract/tests/utils/interface/core.rs +++ /dev/null @@ -1,94 +0,0 @@ -use crate::utils::setup::{Market, OrderType}; -use fuels::{ - accounts::wallet::WalletUnlocked, - prelude::{CallParameters, TxPolicies}, - programs::{call_response::FuelCallResponse, call_utils::TxDependencyExtension}, - types::{AssetId, Bits256, Identity}, -}; - -pub(crate) async fn deposit( - contract: &Market, - amount: u64, - asset: AssetId, -) -> FuelCallResponse<()> { - let tx_params = TxPolicies::new(Some(0), Some(2_000_000), None, None, None); - let call_params = CallParameters::new(amount, asset, 1_000_000); - - contract - .methods() - .deposit() - .with_tx_policies(tx_params) - .call_params(call_params) - .unwrap() - .call() - .await - .unwrap() -} - -pub(crate) async fn withdraw( - contract: &Market, - amount: u64, - asset: AssetId, -) -> FuelCallResponse<()> { - contract - .methods() - .withdraw(amount, asset) - .append_variable_outputs(1) - .call() - .await - .unwrap() -} - -pub(crate) async fn open_order( - contract: &Market, - amount: u64, - asset: AssetId, - order_type: OrderType, - price: u64, -) -> FuelCallResponse { - contract - .methods() - .open_order(amount, asset, order_type, price) - .call() - .await - .unwrap() -} - -pub(crate) async fn cancel_order( - contract: &Market, - order_id: Bits256, -) -> FuelCallResponse<()> { - contract - .methods() - .cancel_order(order_id) - .call() - .await - .unwrap() -} - -#[allow(dead_code)] -pub(crate) async fn batch_fulfill( - contract: &Market, - order_id: Bits256, - orders: Vec, -) -> FuelCallResponse<()> { - contract - .methods() - .batch_fulfill(order_id, orders) - .call() - .await - .unwrap() -} - -pub(crate) async fn set_fee( - contract: &Market, - amount: u64, - user: Option, -) -> FuelCallResponse<()> { - contract - .methods() - .set_fee(amount, user) - .call() - .await - .unwrap() -} diff --git a/market-contract/tests/utils/interface/info.rs b/market-contract/tests/utils/interface/info.rs deleted file mode 100644 index 2d779c2..0000000 --- a/market-contract/tests/utils/interface/info.rs +++ /dev/null @@ -1,56 +0,0 @@ -use crate::utils::setup::{Account, Market, Order, OrderType}; -use fuels::{ - accounts::wallet::WalletUnlocked, - programs::call_response::FuelCallResponse, - types::{Address, AssetId, Bits256, Identity}, -}; - -pub(crate) async fn account( - contract: &Market, - user: Identity, -) -> FuelCallResponse> { - contract.methods().account(user).call().await.unwrap() -} - -pub(crate) async fn fee( - contract: &Market, - user: Option, -) -> FuelCallResponse { - contract.methods().fee(user).call().await.unwrap() -} - -pub(crate) async fn order( - contract: &Market, - order: Bits256, -) -> FuelCallResponse> { - contract.methods().order(order).call().await.unwrap() -} - -pub(crate) async fn user_orders( - contract: &Market, - user: Identity, -) -> FuelCallResponse> { - contract.methods().user_orders(user).call().await.unwrap() -} - -pub(crate) async fn config( - contract: &Market, -) -> FuelCallResponse<(Address, AssetId, u32, AssetId, u32, u32)> { - contract.methods().config().call().await.unwrap() -} - -pub(crate) async fn order_id( - contract: &Market, - amount: u64, - asset: AssetId, - order_type: OrderType, - owner: Identity, - price: u64, -) -> FuelCallResponse { - contract - .methods() - .order_id(amount, asset, order_type, owner, price) - .call() - .await - .unwrap() -} diff --git a/market-contract/tests/utils/interface/mod.rs b/market-contract/tests/utils/interface/mod.rs deleted file mode 100644 index 3c93e0f..0000000 --- a/market-contract/tests/utils/interface/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub(crate) mod core; -pub(crate) mod info; diff --git a/market-contract/tests/utils/mod.rs b/market-contract/tests/utils/mod.rs deleted file mode 100644 index 67436a7..0000000 --- a/market-contract/tests/utils/mod.rs +++ /dev/null @@ -1,2 +0,0 @@ -pub(crate) mod interface; -pub(crate) mod setup;