Skip to content

Commit

Permalink
Update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen authored Apr 11, 2024
2 parents 88aff81 + ee3f495 commit ffa7972
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 47 deletions.
6 changes: 3 additions & 3 deletions market-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -111,18 +111,18 @@ 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),
};

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,
});
Expand Down
1 change: 0 additions & 1 deletion market-contract/tests/functions/core/cancel_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ mod success {
Ok(())
}

#[ignore]
#[tokio::test]
async fn sell_quote() -> anyhow::Result<()> {
let defaults = Defaults::default();
Expand Down
1 change: 0 additions & 1 deletion market-contract/tests/functions/core/deposit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ mod success {
Ok(())
}

#[ignore]
#[tokio::test]
async fn quote_asset() -> anyhow::Result<()> {
let defaults = Defaults::default();
Expand Down
26 changes: 15 additions & 11 deletions market-contract/tests/functions/core/open_order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,6 @@ mod success {
Ok(())
}

#[ignore]
#[tokio::test]
async fn sell_quote() -> anyhow::Result<()> {
let defaults = Defaults::default();
Expand Down Expand Up @@ -415,7 +414,6 @@ mod revert {
.unwrap();
}

#[ignore]
#[tokio::test]
#[should_panic(expected = "InsufficientBalance")]
async fn when_insufficient_quote_balance_to_sell() {
Expand Down Expand Up @@ -443,7 +441,6 @@ mod revert {
.unwrap();
}

#[ignore] // TODO: incomplete
#[tokio::test]
#[should_panic(expected = "InsufficientBalance")]
async fn when_insufficient_base_balance_to_buy() {
Expand All @@ -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() {
Expand All @@ -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();
}
Expand Down
5 changes: 1 addition & 4 deletions market-contract/tests/functions/core/withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand Down Expand Up @@ -51,7 +50,6 @@ mod success {
Ok(())
}

#[ignore]
#[tokio::test]
async fn quote_asset() -> anyhow::Result<()> {
let defaults = Defaults::default();
Expand Down Expand Up @@ -174,7 +172,6 @@ mod revert {
.unwrap();
}

#[ignore]
#[tokio::test]
#[should_panic(expected = "InsufficientBalance")]
async fn when_quote_amount_greater_than_available() {
Expand Down
2 changes: 1 addition & 1 deletion market-contract/tests/functions/info/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 1 addition & 2 deletions market-contract/tests/functions/info/order.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
Expand Down
5 changes: 3 additions & 2 deletions market-contract/tests/functions/info/order_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
5 changes: 2 additions & 3 deletions market-contract/tests/functions/info/user_orders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ mod success {
Ok(())
}

#[ignore]
#[tokio::test]
async fn returns_orders() -> anyhow::Result<()> {
let defaults = Defaults::default();
Expand All @@ -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?;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
#![allow(dead_code)]

use fuels::{
accounts::ViewOnlyAccount,
prelude::{
Expand Down Expand Up @@ -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<AssetConfig> = 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,
Expand Down
3 changes: 3 additions & 0 deletions spark-market-sdk/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,12 +80,15 @@ impl MarketContract {
amount: u64,
asset: AssetId,
) -> anyhow::Result<FuelCallResponse<()>> {
// 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?)
Expand Down

0 comments on commit ffa7972

Please sign in to comment.