Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update tests #31

Merged
merged 3 commits into from
Apr 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions market-contract/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]]
Expand Down
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
178 changes: 96 additions & 82 deletions market-contract/tests/functions/core/cancel_order.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,116 @@
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;
let asset = assets.base.id;
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::<CancelOrderEvent>()
.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() {
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;
let asset = assets.quote.id;
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::<CancelOrderEvent>()
.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);
Expand All @@ -119,52 +120,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);
Expand All @@ -174,32 +175,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);
Expand All @@ -208,6 +210,8 @@ mod success {
assert_eq!(orders.len(), 1);
assert_eq!(orders.pop().unwrap(), id);
assert_eq!(id, expected_id);

Ok(())
}
}

Expand All @@ -225,10 +229,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]
Expand All @@ -240,20 +245,29 @@ mod revert {
defaults.quote_decimals,
defaults.price_decimals,
)
.await;
.await
.unwrap();

let deposit_amount = 100;
let order_amount = 1;
let asset = assets.base.id;
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();
}
}
Loading
Loading