From ee3f4958c678f876e7b4dd9872d06ad727e28462 Mon Sep 17 00:00:00 2001 From: Braqzen <103777923+Braqzen@users.noreply.github.com> Date: Thu, 11 Apr 2024 18:03:40 +0100 Subject: [PATCH] Updated some tests and SDK, minor contract fix --- market-contract/src/main.sw | 6 ++-- .../tests/functions/core/cancel_order.rs | 1 - .../tests/functions/core/deposit.rs | 1 - .../tests/functions/core/open_order.rs | 26 +++++++++------- .../tests/functions/core/withdraw.rs | 5 +--- market-contract/tests/functions/info/fee.rs | 2 +- market-contract/tests/functions/info/order.rs | 3 +- .../tests/functions/info/order_id.rs | 5 ++-- .../tests/functions/info/user_orders.rs | 5 ++-- .../tests/{setup.rs => setup/mod.rs} | 30 +++++++------------ spark-market-sdk/src/lib.rs | 3 ++ 11 files changed, 40 insertions(+), 47 deletions(-) rename market-contract/tests/{setup.rs => setup/mod.rs} (84%) diff --git a/market-contract/src/main.sw b/market-contract/src/main.sw index f1172fe..2d19457 100644 --- a/market-contract/src/main.sw +++ b/market-contract/src/main.sw @@ -111,7 +111,7 @@ impl Market for Contract { let mut account = account.unwrap(); // TODO: Is this division correct? - let (internal_amount, asset_type) = match msg_asset_id() == BASE_ASSET { + let (internal_amount, asset_type) = match asset == BASE_ASSET { true => (amount / 10.pow(BASE_ASSET_DECIMALS), AssetType::Base), false => (amount / 10.pow(QUOTE_ASSET_DECIMALS), AssetType::Quote), }; @@ -119,10 +119,10 @@ impl Market for Contract { account.liquid.debit(amount, asset_type); storage.account.insert(user, account); - transfer(user, asset, internal_amount); + transfer(user, asset, amount); log(WithdrawEvent { - amount: internal_amount, + amount, asset, user, }); diff --git a/market-contract/tests/functions/core/cancel_order.rs b/market-contract/tests/functions/core/cancel_order.rs index a9661e4..a1eb27c 100644 --- a/market-contract/tests/functions/core/cancel_order.rs +++ b/market-contract/tests/functions/core/cancel_order.rs @@ -54,7 +54,6 @@ mod success { Ok(()) } - #[ignore] #[tokio::test] async fn sell_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); diff --git a/market-contract/tests/functions/core/deposit.rs b/market-contract/tests/functions/core/deposit.rs index 5795d1d..d794e2d 100644 --- a/market-contract/tests/functions/core/deposit.rs +++ b/market-contract/tests/functions/core/deposit.rs @@ -41,7 +41,6 @@ mod success { Ok(()) } - #[ignore] #[tokio::test] async fn quote_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); diff --git a/market-contract/tests/functions/core/open_order.rs b/market-contract/tests/functions/core/open_order.rs index ea37ed7..564bf93 100644 --- a/market-contract/tests/functions/core/open_order.rs +++ b/market-contract/tests/functions/core/open_order.rs @@ -87,7 +87,6 @@ mod success { Ok(()) } - #[ignore] #[tokio::test] async fn sell_quote() -> anyhow::Result<()> { let defaults = Defaults::default(); @@ -415,7 +414,6 @@ mod revert { .unwrap(); } - #[ignore] #[tokio::test] #[should_panic(expected = "InsufficientBalance")] async fn when_insufficient_quote_balance_to_sell() { @@ -443,7 +441,6 @@ mod revert { .unwrap(); } - #[ignore] // TODO: incomplete #[tokio::test] #[should_panic(expected = "InsufficientBalance")] async fn when_insufficient_base_balance_to_buy() { @@ -458,20 +455,23 @@ mod revert { let deposit_amount = 10; let order_amount = 100; - let asset = assets.base.id; - let order_type = OrderType::Sell; + let deposit_asset = assets.base.id; + let buy_asset = assets.quote.id; + let order_type = OrderType::Buy; let price = 70000; - let _ = contract.deposit(deposit_amount, asset).await.unwrap(); + let _ = contract + .deposit(deposit_amount, deposit_asset) + .await + .unwrap(); // Revert contract - .open_order(order_amount, asset, order_type, price) + .open_order(order_amount, buy_asset, order_type, price) .await .unwrap(); } - #[ignore] // TODO: incomplete #[tokio::test] #[should_panic(expected = "InsufficientBalance")] async fn when_insufficient_quote_balance_to_buy() { @@ -486,15 +486,19 @@ mod revert { let deposit_amount = 10; let order_amount = 100; - let asset = assets.base.id; + let deposit_asset = assets.quote.id; + let buy_asset = assets.base.id; let order_type = OrderType::Sell; let price = 70000; - let _ = contract.deposit(deposit_amount, asset).await.unwrap(); + let _ = contract + .deposit(deposit_amount, deposit_asset) + .await + .unwrap(); // Revert contract - .open_order(order_amount, asset, order_type, price) + .open_order(order_amount, buy_asset, order_type, price) .await .unwrap(); } diff --git a/market-contract/tests/functions/core/withdraw.rs b/market-contract/tests/functions/core/withdraw.rs index 361deb4..9bddd7f 100644 --- a/market-contract/tests/functions/core/withdraw.rs +++ b/market-contract/tests/functions/core/withdraw.rs @@ -6,7 +6,6 @@ mod success { use crate::setup::create_account; use spark_market_sdk::WithdrawEvent; - #[ignore] #[tokio::test] async fn base_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); @@ -19,7 +18,7 @@ mod success { let deposit_amount = 100; - contract.deposit(deposit_amount, assets.base.id).await?; + let _ = contract.deposit(deposit_amount, assets.base.id).await?; let user_balance = owner.balance(&assets.base.id).await; let user_account = contract.account(owner.identity()).await?.value.unwrap(); @@ -51,7 +50,6 @@ mod success { Ok(()) } - #[ignore] #[tokio::test] async fn quote_asset() -> anyhow::Result<()> { let defaults = Defaults::default(); @@ -174,7 +172,6 @@ mod revert { .unwrap(); } - #[ignore] #[tokio::test] #[should_panic(expected = "InsufficientBalance")] async fn when_quote_amount_greater_than_available() { diff --git a/market-contract/tests/functions/info/fee.rs b/market-contract/tests/functions/info/fee.rs index 88686cb..7f499b7 100644 --- a/market-contract/tests/functions/info/fee.rs +++ b/market-contract/tests/functions/info/fee.rs @@ -58,7 +58,7 @@ mod success { // Change fee to be non-zero for testing purposes let global_fee = 5; - let _ = contract.set_fee(global_fee, Some(user.identity())).await?; + let _ = contract.set_fee(global_fee, None).await?; assert_eq!(contract.fee(Some(user.identity())).await?.value, global_fee); diff --git a/market-contract/tests/functions/info/order.rs b/market-contract/tests/functions/info/order.rs index 5d4782a..e7f4525 100644 --- a/market-contract/tests/functions/info/order.rs +++ b/market-contract/tests/functions/info/order.rs @@ -14,8 +14,7 @@ mod success { ) .await?; - let order = contract.order(Bits256([0u8; 32])).await?.value; - assert!(order.is_none()); + assert!(contract.order(Bits256([0u8; 32])).await?.value.is_none()); Ok(()) } diff --git a/market-contract/tests/functions/info/order_id.rs b/market-contract/tests/functions/info/order_id.rs index 080183b..3115b10 100644 --- a/market-contract/tests/functions/info/order_id.rs +++ b/market-contract/tests/functions/info/order_id.rs @@ -102,9 +102,10 @@ mod revert { use super::*; - #[ignore] #[tokio::test] - #[should_panic(expected = "InvalidAsset")] + #[should_panic] + // TODO: Fuels SDK .simulate() does not propagate the error correctly + // #[should_panic(expected = "InvalidAsset")] async fn reverts_upon_invalid_asset() { let defaults = Defaults::default(); let (contract, owner, _user, assets) = setup( diff --git a/market-contract/tests/functions/info/user_orders.rs b/market-contract/tests/functions/info/user_orders.rs index 3a5d96e..e1a70eb 100644 --- a/market-contract/tests/functions/info/user_orders.rs +++ b/market-contract/tests/functions/info/user_orders.rs @@ -19,7 +19,6 @@ mod success { Ok(()) } - #[ignore] #[tokio::test] async fn returns_orders() -> anyhow::Result<()> { let defaults = Defaults::default(); @@ -31,10 +30,10 @@ mod success { .await?; let _ = contract.deposit(100, assets.base.id).await?; - let id2 = contract + let id1 = contract .open_order(2, assets.base.id, OrderType::Buy, 70000) .await?; - let id1 = contract + let id2 = contract .open_order(1, assets.base.id, OrderType::Buy, 75000) .await?; diff --git a/market-contract/tests/setup.rs b/market-contract/tests/setup/mod.rs similarity index 84% rename from market-contract/tests/setup.rs rename to market-contract/tests/setup/mod.rs index 50d3408..11f6f6c 100644 --- a/market-contract/tests/setup.rs +++ b/market-contract/tests/setup/mod.rs @@ -1,5 +1,3 @@ -#![allow(dead_code)] - use fuels::{ accounts::ViewOnlyAccount, prelude::{ @@ -86,28 +84,22 @@ pub(crate) async fn setup( let quote_asset_id = AssetId::new([1; 32]); let random_asset_id = AssetId::new([2; 32]); - let base_asset = AssetConfig { - id: base_asset_id, - num_coins: coins_per_wallet, - coin_amount: amount_per_coin, - }; - let quote_asset = AssetConfig { - id: quote_asset_id, - num_coins: coins_per_wallet, - coin_amount: amount_per_coin, - }; - let random_asset = AssetConfig { - id: random_asset_id, - num_coins: coins_per_wallet, - coin_amount: amount_per_coin, - }; - let assets = vec![base_asset, quote_asset, random_asset]; + let ids = vec![base_asset_id, quote_asset_id, random_asset_id]; + let mut assets: Vec = Vec::with_capacity(3); + + for id in ids { + assets.push(AssetConfig { + id, + num_coins: coins_per_wallet, + coin_amount: amount_per_coin, + }); + } let config = WalletsConfig::new_multiple_assets(number_of_wallets, assets); let mut wallets = launch_custom_provider_and_get_wallets(config, None, None).await?; - let owner = wallets.pop().unwrap(); let user = wallets.pop().unwrap(); + let assets = Assets { base: Asset { id: base_asset_id, diff --git a/spark-market-sdk/src/lib.rs b/spark-market-sdk/src/lib.rs index 955dac8..ff86539 100644 --- a/spark-market-sdk/src/lib.rs +++ b/spark-market-sdk/src/lib.rs @@ -80,12 +80,15 @@ impl MarketContract { amount: u64, asset: AssetId, ) -> anyhow::Result> { + // TODO: configurable? let call_params = CallParameters::new(amount, asset, 1_000_000); + let tx_policies = TxPolicies::default().with_gas_price(1); Ok(self .instance .methods() .deposit() + .with_tx_policies(tx_policies) .call_params(call_params)? .call() .await?)