Skip to content

Commit

Permalink
Pre simplification changes
Browse files Browse the repository at this point in the history
  • Loading branch information
Braqzen committed Apr 15, 2024
1 parent 033a846 commit d8848d4
Show file tree
Hide file tree
Showing 5 changed files with 376 additions and 53 deletions.
10 changes: 9 additions & 1 deletion market-contract/Forc.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,18 @@
name = "core"
source = "path+from-root-566CA1D5F8BEAFBF"

[[package]]
name = "fixed_point"
source = "git+https://github.com/FuelLabs/sway-libs?tag=v0.18.0#8d196e9379463d4596ac582a20a84ed52ff58c69"
dependencies = ["std"]

[[package]]
name = "market-contract"
source = "member"
dependencies = ["std"]
dependencies = [
"fixed_point",
"std",
]

[[package]]
name = "std"
Expand Down
1 change: 1 addition & 0 deletions market-contract/Forc.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ license = "Apache-2.0"
name = "market-contract"

[dependencies]
fixed_point = { git = "https://github.com/FuelLabs/sway-libs", tag = "v0.18.0" }
12 changes: 11 additions & 1 deletion market-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ impl Market for Contract {
alice_account_delta,
bob_order_amount_decrease,
bob_account_delta,
bob_unlock_amount,
) = trade.unwrap();

// Update the order quantities with the amounts that can be traded
Expand All @@ -364,7 +365,7 @@ impl Market for Contract {
alice_account.locked.debit(alice_account_delta, asset_1);
alice_account.liquid.credit(bob_account_delta, asset_2);

bob_account.locked.debit(bob_account_delta, asset_2);
bob_account.locked.debit(bob_account_delta, asset_2); //todo
bob_account.liquid.credit(alice_account_delta, asset_1);

// Save bob's account because his order is finished
Expand All @@ -376,6 +377,15 @@ impl Market for Contract {
require(storage.orders.remove(bob_id), OrderError::FailedToRemove);
remove_user_order(bob.owner, bob_id);

// Edge case to rescue funds when bob's price was greater and they reduced alice
// amount to 0
// Ex. Alice sell 1 BTC @ 70k, Bob buy 1 BTC @ 71k. Rescue 1k worth of locked funds
if bob_unlock_amount != 0 {
bob_account.locked.debit(bob_unlock_amount, asset_2); //todo
bob_account.liquid.credit(bob_unlock_amount, asset_2);
storage.account.insert(bob.owner, bob_account);
}

// TODO: Emit event
// log(TradeEvent {
// order_id: bob_id,
Expand Down
22 changes: 14 additions & 8 deletions market-contract/src/math.sw
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use ::data_structures::{asset_type::AssetType, order::Order, order_type::OrderTy
use ::errors::TradeError;

use std::u128::U128;
use fixed_point::ufp128::UFP128;

impl u64 {
pub fn mul_div(self, mul_to: u64, div_to: u64) -> u64 {
Expand All @@ -26,9 +27,9 @@ impl u64 {
}

fn calc_amount(buy_amount: u64, buy_price: u64, sell_price: u64) -> u64 {
let price_ratio = U128::from((0, buy_price)) / U128::from((0, sell_price));
let amount = price_ratio * U128::from((0, buy_amount));
amount.as_u64().unwrap()
let price_ratio = UFP128::from((0, buy_price)) / UFP128::from((0, sell_price));
let amount = price_ratio * UFP128::from((0, buy_amount));
U128::from(amount.into()).as_u64().unwrap()
}

pub fn attempt_trade(
Expand All @@ -37,7 +38,7 @@ pub fn attempt_trade(
base_asset_decimals: u32,
quote_asset_decimals: u32,
price_decimals: u32,
) -> Result<(u64, u64, u64, u64), TradeError> {
) -> Result<(u64, u64, u64, u64, u64), TradeError> {
// In this function:
// Decrease the order size for alice and bob until they are 0 == their orders are fulfilled
// Track the amount that each account has to transfer for their trade
Expand All @@ -50,9 +51,10 @@ pub fn attempt_trade(
mut seller_account_delta,
mut buyer_order_amount_decrease,
mut buyer_account_delta,
mut bob_unlock_amount,
) = match alice.order_type {
OrderType::Sell => (alice, bob, 0, 0, 0, 0),
OrderType::Buy => (bob, alice, 0, 0, 0, 0),
OrderType::Sell => (alice, bob, 0, 0, 0, 0, 0),
OrderType::Buy => (bob, alice, 0, 0, 0, 0, 0),
};

if buyer.price < seller.price {
Expand All @@ -73,11 +75,12 @@ pub fn attempt_trade(
price_decimals,
quote_asset_decimals,
);
bob_unlock_amount = buyer.price - seller.price;
} else if buyer_buy_amount < seller.amount {
seller_order_amount_decrease = buyer_buy_amount;
buyer_order_amount_decrease = buyer_buy_amount;
buyer_order_amount_decrease = buyer.amount;
buyer_account_delta = base_to_quote_amount(
buyer_order_amount_decrease,
seller_order_amount_decrease,
base_asset_decimals,
buyer.price,
price_decimals,
Expand Down Expand Up @@ -110,6 +113,7 @@ pub fn attempt_trade(
price_decimals,
quote_asset_decimals,
);
bob_unlock_amount = buyer.price - seller.price;
} else if buyer_buy_amount < seller.amount {
seller_order_amount_decrease = buyer_buy_amount;
buyer_order_amount_decrease = buyer_buy_amount;
Expand Down Expand Up @@ -143,12 +147,14 @@ pub fn attempt_trade(
seller_account_delta,
buyer_order_amount_decrease,
buyer_account_delta,
bob_unlock_amount,
)),
OrderType::Buy => Result::Ok((
buyer_order_amount_decrease,
buyer_account_delta,
seller_order_amount_decrease,
seller_account_delta,
bob_unlock_amount,
)),
}
}
Expand Down
Loading

0 comments on commit d8848d4

Please sign in to comment.