Skip to content

Commit

Permalink
Ported sdk
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen committed Apr 10, 2024
1 parent b2dcde0 commit 7fd35e9
Show file tree
Hide file tree
Showing 19 changed files with 552 additions and 678 deletions.
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
179 changes: 97 additions & 82 deletions market-contract/tests/functions/core/cancel_order.rs
Original file line number Diff line number Diff line change
@@ -1,115 +1,117 @@
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() {
#[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;
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 +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);
Expand All @@ -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);
Expand All @@ -208,6 +211,8 @@ mod success {
assert_eq!(orders.len(), 1);
assert_eq!(orders.pop().unwrap(), id);
assert_eq!(id, expected_id);

Ok(())
}
}

Expand All @@ -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]
Expand All @@ -240,20 +246,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

0 comments on commit 7fd35e9

Please sign in to comment.