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

Contract fix #27

Closed
wants to merge 3 commits into from
Closed
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
99 changes: 30 additions & 69 deletions market-contract/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -70,66 +70,49 @@ impl Market for Contract {
#[payable]
#[storage(read, write)]
fn deposit() {
require(
msg_asset_id() == BASE_ASSET || msg_asset_id() == QUOTE_ASSET,
AssetError::InvalidAsset,
);
let asset = msg_asset_id();
let amount = msg_amount();

require(asset == BASE_ASSET || asset == QUOTE_ASSET, AssetError::InvalidAsset);
let asset_type = if asset == BASE_ASSET { AssetType::Base } else { AssetType::Quote };

let user = msg_sender().unwrap();
let (amount, asset_type) = match msg_asset_id() == BASE_ASSET {
true => (msg_amount() * 10.pow(BASE_ASSET_DECIMALS), AssetType::Base),
false => (msg_amount() * 10.pow(QUOTE_ASSET_DECIMALS), AssetType::Quote),
};

let mut account = match storage.account.get(user).try_read() {
Some(account) => account,
None => Account::new(),
};
let mut account = storage.account.get(user).try_read().unwrap_or(Account::new());

account.liquid.credit(msg_amount(), asset_type);
account.liquid.credit(amount, asset_type);
storage.account.insert(user, account);

log(DepositEvent {
amount: msg_amount(),
asset: msg_asset_id(),
amount,
asset,
user,
});
}

#[storage(read, write)]
fn withdraw(amount: u64, asset: AssetId) {
require(
asset == BASE_ASSET || asset == QUOTE_ASSET,
AssetError::InvalidAsset,
);
require(asset == BASE_ASSET || asset == QUOTE_ASSET, AssetError::InvalidAsset);
let asset_type = if asset == BASE_ASSET { AssetType::Base } else { AssetType::Quote };

let user = msg_sender().unwrap();
let account = storage.account.get(user).try_read();

require(account.is_some(), AccountError::InvalidUser);

let mut account = account.unwrap();

// TODO: Is this division correct?
let (internal_amount, asset_type) = match msg_asset_id() == 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,
});
}

#[storage(read, write)]
// TODO: what types should amount, price be?
fn open_order(
amount: u64,
asset: AssetId,
Expand All @@ -141,66 +124,44 @@ impl Market for Contract {
AssetError::InvalidAsset,
);

let asset_type = if asset == BASE_ASSET { AssetType::Base } else { AssetType::Quote };
let user = msg_sender().unwrap();
let account = storage.account.get(user).try_read();

// An order may be open if an account exists
require(account.is_some(), AccountError::InvalidUser);

let mut account = account.unwrap();
let asset_type = if asset == BASE_ASSET {
AssetType::Base
} else {
AssetType::Quote
};

match order_type {
OrderType::Sell => {
// If the account has enough liquidity of the asset that you already own then lock
// it for the new sell order

// TODO: use amount to lock funds
let _internal_amount = if asset == BASE_ASSET {
amount * BASE_ASSET_DECIMALS.as_u64()
let base_amount = if asset == BASE_ASSET {
// example open_order(0.5, btc, SELL, 70k)
amount
} else {
amount * QUOTE_ASSET_DECIMALS.as_u64()
// example open_order(35k, usdc, SELL, 70k)
quote_to_base_amount(amount, BASE_ASSET_DECIMALS, price, PRICE_DECIMALS, QUOTE_ASSET_DECIMALS)
};

account.liquid.debit(amount, asset_type);
account.locked.credit(amount, asset_type);
account.liquid.debit(base_amount, AssetType::Base);
account.locked.credit(base_amount, AssetType::Base);
}
OrderType::Buy => {
// Calculate amount to lock of the other asset
// TODO: these "amounts" do not return expected values
let (amount, asset_type) = match asset == BASE_ASSET {
true => {
let amount = base_to_quote_amount(
amount,
BASE_ASSET_DECIMALS,
price,
PRICE_DECIMALS,
QUOTE_ASSET_DECIMALS,
);
let asset_type = AssetType::Quote;
(amount, asset_type)
},
false => {
let amount = quote_to_base_amount(
amount,
BASE_ASSET_DECIMALS,
price,
PRICE_DECIMALS,
QUOTE_ASSET_DECIMALS,
);
let asset_type = AssetType::Base;
(amount, asset_type)
},

let quote_amount = if asset == BASE_ASSET {
// example open_order(0.5, btc, BUY, 70k)
base_to_quote_amount(amount, BASE_ASSET_DECIMALS, price, PRICE_DECIMALS, QUOTE_ASSET_DECIMALS)
} else {
// example open_order(35k, usdc, BUY, 70k)
amount
};

// The asset type is the opposite because you're calculating if you have enough of
// the opposite asset to use as payment
account.liquid.debit(amount, asset_type);
account.locked.credit(amount, asset_type);
account.liquid.debit(quote_amount, AssetType::Quote);
account.locked.credit(quote_amount, AssetType::Quote);
}
}

Expand Down