Skip to content

Commit

Permalink
fix: calculations with collateral asset and base asset decimals (#201)
Browse files Browse the repository at this point in the history
Co-authored-by: martines3000 <[email protected]>
  • Loading branch information
Vid201 and martines3000 authored Nov 11, 2024
1 parent 17e0239 commit 485d442
Show file tree
Hide file tree
Showing 25 changed files with 697 additions and 105 deletions.
21 changes: 8 additions & 13 deletions contracts/market/src/main.sw
Original file line number Diff line number Diff line change
Expand Up @@ -701,7 +701,6 @@ impl Market for Contract {
let len = storage.collateral_configurations_keys.len();
let market_configuration = storage.market_configuration.read();


while index < len {
let collateral_configuration = storage.collateral_configurations.get(storage.collateral_configurations_keys.get(index).unwrap().read()).read();

Expand All @@ -714,13 +713,13 @@ impl Market for Contract {

let price = get_price_internal(collateral_configuration.price_feed_id, PricePosition::LowerBound); // decimals: price.exponent
let price_exponent = price.exponent;
let price_scale = u256::from(10_u64).pow(price.exponent);
let price = u256::from(price.price); // decimals: price.exponent
let amount = balance * collateral_configuration.borrow_collateral_factor / FACTOR_SCALE_18; // decimals: collateral_configuration.decimals
let scale = u256::from(10_u64).pow(
collateral_configuration.decimals + price_exponent - market_configuration.base_token_decimals,
);
let collateral_scale = u256::from(10_u64).pow(collateral_configuration.decimals);
let base_asset_scale = u256::from(10_u64).pow(market_configuration.base_token_decimals);

borrow_limit += amount * price / scale; // decimals: base_token_decimals
borrow_limit += amount * price * base_asset_scale / collateral_scale / price_scale; // decimals: base_token_decimals
index += 1;
};

Expand Down Expand Up @@ -891,15 +890,11 @@ impl Market for Contract {
let base_price = get_price_internal(market_configuration.base_token_price_feed_id, PricePosition::Middle); // decimals: base_price.exponent
let base_price_scale = u256::from(10_u64).pow(base_price.exponent);
let base_price = u256::from(base_price.price); // decimals: base_price.exponent
let scale = u256::from(10_u64).pow(
collateral_configuration
.decimals - storage
.market_configuration
.read()
.base_token_decimals,
);
let collateral_scale = u256::from(10_u64).pow(collateral_configuration.decimals);
let base_asset_scale = u256::from(10_u64).pow(market_configuration.base_token_decimals);

let collateral_value = asset_price_discounted * collateral_amount.into() * base_price_scale / asset_price_scale / base_price / scale;
let asset_discounted = asset_price_discounted * collateral_amount.into() / asset_price_scale; // decimals: collateral_asset.decimals
let collateral_value = asset_discounted * base_asset_scale * base_price_scale / base_price / collateral_scale;

// Native assets are in u64
<u64 as TryFrom<u256>>::try_from(collateral_value).unwrap()
Expand Down
Loading

0 comments on commit 485d442

Please sign in to comment.