Skip to content
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
8 changes: 1 addition & 7 deletions common/src/market/external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,7 @@ pub trait MarketExternalInterface {

fn get_configuration(&self) -> MarketConfiguration;
fn get_snapshots(&self, offset: Option<u32>, count: Option<u32>) -> Vec<&Snapshot>;
/// Takes current balance as an argument so that it can be called as view.
/// `borrow_asset_balance` should be retrieved from the borrow asset
/// contract specified in the market configuration.
fn get_borrow_asset_metrics(
&self,
borrow_asset_balance: BorrowAssetAmount,
) -> BorrowAssetMetrics;
fn get_borrow_asset_metrics(&self) -> BorrowAssetMetrics;

// TODO: Decide how to work with remote balances:
// Option 1:
Expand Down
14 changes: 6 additions & 8 deletions common/src/market/impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,22 +136,20 @@ impl Market {
}

#[allow(clippy::missing_panics_doc)]
pub fn get_borrow_asset_available_to_borrow(
&self,
current_contract_balance: BorrowAssetAmount,
) -> BorrowAssetAmount {
pub fn get_borrow_asset_available_to_borrow(&self) -> BorrowAssetAmount {
// Safe because factor is guaranteed to be <=1, so value must still fit in u128.
#[allow(clippy::unwrap_used)]
let must_retain = ((1u32 - self.configuration.borrow_asset_maximum_usage_ratio)
* self.borrow_asset_deposited.to_decimal())
.to_u128_ceil()
.unwrap();

let known_available = current_contract_balance
self.borrow_asset_deposited
.to_u128()
.saturating_sub(self.borrow_asset_in_flight.to_u128());

known_available.saturating_sub(must_retain).into()
.saturating_sub(self.borrow_asset_borrowed.to_u128())
.saturating_sub(self.borrow_asset_in_flight.to_u128())
.saturating_sub(must_retain)
.into()
}

pub fn get_interest_rate_for_snapshot(&self, snapshot: &Snapshot) -> Decimal {
Expand Down
4 changes: 3 additions & 1 deletion common/src/snapshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ pub struct Snapshot {

impl Snapshot {
pub fn usage_ratio(&self) -> Decimal {
if self.deposited.is_zero() {
if self.deposited.is_zero() || self.borrowed.is_zero() {
Decimal::ZERO
} else if self.borrowed >= self.deposited {
Decimal::ONE
} else {
self.borrowed.to_decimal() / self.deposited.to_decimal()
}
Expand Down
7 changes: 2 additions & 5 deletions contract/market/src/impl_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,12 @@ impl Contract {
}

#[private]
pub fn borrow_01_consume_balance_and_price(
pub fn borrow_01_consume_price(
&mut self,
account_id: AccountId,
amount: BorrowAssetAmount,
#[callback_result] current_balance: Result<BorrowAssetAmount, PromiseError>,
#[callback_result] oracle_response_result: Result<OracleResponse, PromiseError>,
) -> Promise {
let current_balance = current_balance
.unwrap_or_else(|_| env::panic_str("Failed to fetch borrow asset current balance."));
let oracle_response = oracle_response_result
.unwrap_or_else(|_| env::panic_str("Failed to fetch price data from oracle."));
let price_pair = self
Expand All @@ -145,7 +142,7 @@ impl Contract {
.unwrap_or_else(|e| env::panic_str(&e.to_string()));

// Ensure we have enough funds to dispense.
let available_to_borrow = self.get_borrow_asset_available_to_borrow(current_balance);
let available_to_borrow = self.get_borrow_asset_available_to_borrow();
require!(
amount <= available_to_borrow,
"Insufficient borrow asset available",
Expand Down
18 changes: 5 additions & 13 deletions contract/market/src/impl_market_external.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,9 @@ impl MarketExternalInterface for Contract {
.collect::<Vec<_>>()
}

fn get_borrow_asset_metrics(
&self,
borrow_asset_balance: BorrowAssetAmount,
) -> BorrowAssetMetrics {
fn get_borrow_asset_metrics(&self) -> BorrowAssetMetrics {
BorrowAssetMetrics {
available: self.get_borrow_asset_available_to_borrow(borrow_asset_balance),
available: self.get_borrow_asset_available_to_borrow(),
deposited: self.borrow_asset_deposited,
borrowed: self.borrow_asset_borrowed,
}
Expand Down Expand Up @@ -97,15 +94,10 @@ impl MarketExternalInterface for Contract {

let account_id = env::predecessor_account_id();

// -> (current asset balance, price data)
self.configuration
.borrow_asset
.current_account_balance()
.and(self.configuration.balance_oracle.retrieve_price_pair())
.then(
Self::ext(env::current_account_id())
.borrow_01_consume_balance_and_price(account_id, amount),
)
.balance_oracle
.retrieve_price_pair()
.then(Self::ext(env::current_account_id()).borrow_01_consume_price(account_id, amount))
}

fn withdraw_collateral(&mut self, amount: CollateralAssetAmount) -> Promise {
Expand Down
41 changes: 41 additions & 0 deletions contract/market/tests/untracked_funds.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use test_utils::*;

#[tokio::test]
#[should_panic = "Smart contract panicked: Insufficient borrow asset available"]
async fn cannot_borrow_untracked_funds() {
let SetupEverything {
c,
supply_user,
borrow_user,
..
} = setup_everything(|_| {}).await;

c.supply(&supply_user, 10_000).await;
c.borrow_asset_transfer(&supply_user, c.contract.id(), 10_000)
.await;
c.collateralize(&borrow_user, 20_000).await;
c.borrow(&borrow_user, 12_000).await;
}

#[tokio::test]
async fn can_withdraw_untracked_funds() {
let SetupEverything {
c,
supply_user,
borrow_user,
..
} = setup_everything(|_| {}).await;

c.supply(&supply_user, 10_000).await;
c.borrow_asset_transfer(&supply_user, c.contract.id(), 8_000)
.await;
c.collateralize(&borrow_user, 20_000).await;
c.borrow(&borrow_user, 8_000).await;

let balance_before = c.borrow_asset_balance_of(supply_user.id()).await;
c.create_supply_withdrawal_request(&supply_user, 10_000)
.await;
c.execute_next_supply_withdrawal_request(&supply_user).await;
let balance_after = c.borrow_asset_balance_of(supply_user.id()).await;
assert_eq!(balance_before + 10_000, balance_after);
}