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

bigz/update-spot-tvl-invariant-check #525

Merged
merged 34 commits into from
Jul 31, 2023
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
4b9bd45
bigz/update-spot-tvl-invariant-check
0xbigz Jul 7, 2023
4dda906
loosen check in update_spot_balances
0xbigz Jul 10, 2023
3e303cd
Merge branch 'master' into bigz/update-spot-tvl-invariant-check
0xbigz Jul 12, 2023
a7e7eac
add tests/spotWithdrawUtil100.ts
0xbigz Jul 13, 2023
360acb8
Merge branch 'master' into bigz/update-spot-tvl-invariant-check
0xbigz Jul 18, 2023
2b58568
in fill spot order, account for tvl leaving system
crispheaney Jul 20, 2023
2058677
test for fulfill_spot_order_with_match with max utilization
crispheaney Jul 20, 2023
61e7de5
more debugging for test
0xbigz Jul 19, 2023
180baf9
sdk: fix bug in transfer account creation (#532)
evanpipta Jul 17, 2023
b9cef29
sdk: release v2.34.1-beta.5
github-actions[bot] Jul 17, 2023
70e0633
sdk: event subscriber avoids web3.js to get performance boost (#542)
crispheaney Jul 17, 2023
ea9715d
sdk: release v2.34.1-beta.6
github-actions[bot] Jul 17, 2023
cabd9b3
sdk: remove extra console.log in fetchLogs
crispheaney Jul 17, 2023
dfddf20
sdk: release v2.34.1-beta.7
github-actions[bot] Jul 17, 2023
21570f3
program: add post only slide for perps (#541)
crispheaney Jul 18, 2023
89f2ed8
sdk: release v2.34.1-beta.8
github-actions[bot] Jul 18, 2023
986ad5d
program: add cancel_orders_by_ids (#540)
crispheaney Jul 18, 2023
fed9249
sdk: release v2.34.1-beta.9
github-actions[bot] Jul 18, 2023
be29a11
v2.35.0
crispheaney Jul 18, 2023
c1e80a2
sdk: release v2.35.1-beta.0
github-actions[bot] Jul 18, 2023
9b2d776
sdk: fixes for lp (liq price and lp share burn) (#522)
lowkeynicc Jul 19, 2023
fb06394
sdk: release v2.35.1-beta.1
github-actions[bot] Jul 19, 2023
edddbef
intermediate testing
0xbigz Jul 20, 2023
1942913
more test progress
0xbigz Jul 21, 2023
b63374d
merge master + cleanup dlogs
0xbigz Jul 24, 2023
d010ef6
merge master
0xbigz Jul 24, 2023
2beb7ac
CHANGELOG fix
crispheaney Jul 26, 2023
2818f8a
fix use of is_leaving_drift
crispheaney Jul 26, 2023
53e203b
more consistent naming
crispheaney Jul 26, 2023
4eab9ee
more consistent naming
crispheaney Jul 26, 2023
4316e65
revert test change
crispheaney Jul 26, 2023
a32d487
merge master
0xbigz Jul 31, 2023
9469385
Merge branch 'master' into bigz/update-spot-tvl-invariant-check
crispheaney Jul 31, 2023
38e9104
CHANGELOG
crispheaney Jul 31, 2023
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
6 changes: 3 additions & 3 deletions programs/drift/src/controller/insurance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,17 +555,17 @@ pub fn settle_revenue_to_insurance_fund(
)?;

let depositors_claim =
validate_spot_market_vault_amount(spot_market, spot_market_vault_amount)?.cast::<u128>()?;
validate_spot_market_vault_amount(spot_market, spot_market_vault_amount)?;

let mut token_amount = get_token_amount(
spot_market.revenue_pool.scaled_balance,
spot_market,
&SpotBalanceType::Deposit,
)?;

if depositors_claim < token_amount {
if depositors_claim < token_amount.cast()? {
// only allow half of withdraw available when utilization is high
token_amount = depositors_claim.safe_div(2)?;
token_amount = depositors_claim.max(0).cast::<u128>()?.safe_div(2)?;
}

if spot_market.insurance_fund.user_shares > 0 {
Expand Down
7 changes: 6 additions & 1 deletion programs/drift/src/controller/pnl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,12 @@ pub fn settle_pnl(
user_unsettled_pnl,
now,
)?;

crate::dlog!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would be nice to remove if not intentional

net_user_pnl,
max_pnl_pool_excess,
user_unsettled_pnl,
pnl_to_settle_with_user
);
if user_unsettled_pnl == 0 {
msg!("User has no unsettled pnl for market {}", market_index);
return Ok(());
Expand Down
18 changes: 10 additions & 8 deletions programs/drift/src/controller/spot_balance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn update_spot_balances(
}
}

if let SpotBalanceType::Borrow = update_direction {
if force_round_up && update_direction == &SpotBalanceType::Borrow {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we change the name for force_round_up to better capture when it should be on/off?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dont we need to change fulfill_with_external_market for spot to use this?

let deposit_token_amount = get_token_amount(
spot_market.deposit_balance,
spot_market,
Expand Down Expand Up @@ -293,13 +293,15 @@ pub fn transfer_spot_balances(
return Ok(());
}

validate!(
spot_market.deposit_balance >= from_spot_balance.balance(),
ErrorCode::InvalidSpotMarketState,
"spot_market.deposit_balance={} lower than individual spot balance={}",
spot_market.deposit_balance,
from_spot_balance.balance()
)?;
if from_spot_balance.balance_type() == &SpotBalanceType::Deposit {
validate!(
spot_market.deposit_balance >= from_spot_balance.balance(),
ErrorCode::InvalidSpotMarketState,
"spot_market.deposit_balance={} lower than individual spot balance={}",
spot_market.deposit_balance,
from_spot_balance.balance()
)?;
}

update_spot_balances(
token_amount.unsigned_abs(),
Expand Down
24 changes: 13 additions & 11 deletions programs/drift/src/math/spot_withdraw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ pub fn get_max_withdraw_for_market_with_token_amount(
max_withdraw_amount.safe_add(borrow_limit)
}

pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult<u64> {
pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult<i64> {
let depositors_amount: u64 = get_token_amount(
spot_market.deposit_balance,
spot_market,
Expand All @@ -266,13 +266,13 @@ pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult<u64> {
)?
.cast()?;

validate!(
depositors_amount >= borrowers_amount,
ErrorCode::SpotMarketBalanceInvariantViolated,
"depositors_amount={} less than borrowers_amount={}",
depositors_amount,
borrowers_amount
)?;
// validate!(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

delete

// depositors_amount >= borrowers_amount,
// ErrorCode::SpotMarketBalanceInvariantViolated,
// "depositors_amount={} less than borrowers_amount={}",
// depositors_amount,
// borrowers_amount
// )?;

let revenue_amount: u64 = get_token_amount(
spot_market.revenue_pool.scaled_balance,
Expand All @@ -281,7 +281,9 @@ pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult<u64> {
)?
.cast()?;

let depositors_claim = depositors_amount - borrowers_amount;
let depositors_claim = depositors_amount
.cast::<i64>()?
.safe_sub(borrowers_amount.cast()?)?;

validate!(
revenue_amount <= depositors_amount,
Expand All @@ -299,11 +301,11 @@ pub fn validate_spot_balances(spot_market: &SpotMarket) -> DriftResult<u64> {
pub fn validate_spot_market_vault_amount(
spot_market: &SpotMarket,
vault_amount: u64,
) -> DriftResult<u64> {
) -> DriftResult<i64> {
let depositors_claim = validate_spot_balances(spot_market)?;

validate!(
vault_amount >= depositors_claim,
vault_amount.cast::<i64>()? >= depositors_claim,
ErrorCode::SpotMarketVaultInvariantViolated,
"spot market vault ={} holds less than remaining depositor claims = {}",
vault_amount,
Expand Down
106 changes: 53 additions & 53 deletions test-scripts/run-anchor-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,59 +4,59 @@ if [ "$1" != "--skip-build" ]; then
fi

test_files=(
oracleFillPriceGuardrails.ts
perpLpJit.ts
spotSwap.ts
maxLeverageOrderParams.ts
multipleMakerOrders.ts
postOnlyAmmFulfillment.ts
imbalancePerpPnl.ts
delistMarket.ts
delistMarketLiq.ts
triggerSpotOrder.ts
serumTest.ts
phoenixTest.ts
liquidityProvider.ts
tradingLP.ts
insuranceFundStake.ts
liquidateSpot.ts
liquidateSpotSocialLoss.ts
referrer.ts
liquidatePerpPnlForDeposit.ts
liquidateBorrowForPerpPnl.ts
liquidatePerp.ts
liquidatePerpAndLp.ts
order.ts
spotDepositWithdraw.ts
prepegMarketOrderBaseAssetAmount.ts
updateAMM.ts
repegAndSpread.ts
driftClient.ts
ordersWithSpread.ts
marketOrder.ts
triggerOrders.ts
stopLimits.ts
userOrderId.ts
postOnly.ts
placeAndMakePerp.ts
placeAndMakeSpotOrder.ts
roundInFavorBaseAsset.ts
marketOrderBaseAssetAmount.ts
oracleOffsetOrders.ts
userDelegate.ts
subaccounts.ts
pyth.ts
userAccount.ts
admin.ts
assetTier.ts
pauseExchange.ts
whitelist.ts
updateK.ts
curve.ts
cappedSymFunding.ts
maxDeposit.ts
cancelAllOrders.ts
modifyOrder.ts
# oracleFillPriceGuardrails.ts
# perpLpJit.ts
# spotSwap.ts
# maxLeverageOrderParams.ts
# multipleMakerOrders.ts
# postOnlyAmmFulfillment.ts
# imbalancePerpPnl.ts
# delistMarket.ts
# delistMarketLiq.ts
# triggerSpotOrder.ts
# serumTest.ts
# phoenixTest.ts
# liquidityProvider.ts
# tradingLP.ts
# insuranceFundStake.ts
# liquidateSpot.ts
# liquidateSpotSocialLoss.ts
# referrer.ts
# liquidatePerpPnlForDeposit.ts
# liquidateBorrowForPerpPnl.ts
# liquidatePerp.ts
# liquidatePerpAndLp.ts
# order.ts
spotWithdrawUtil100.ts
# prepegMarketOrderBaseAssetAmount.ts
# updateAMM.ts
# repegAndSpread.ts
# driftClient.ts
# ordersWithSpread.ts
# marketOrder.ts
# triggerOrders.ts
# stopLimits.ts
# userOrderId.ts
# postOnly.ts
# placeAndMakePerp.ts
# placeAndMakeSpotOrder.ts
# roundInFavorBaseAsset.ts
# marketOrderBaseAssetAmount.ts
# oracleOffsetOrders.ts
# userDelegate.ts
# subaccounts.ts
# pyth.ts
# userAccount.ts
# admin.ts
# assetTier.ts
# pauseExchange.ts
# whitelist.ts
# updateK.ts
# curve.ts
# cappedSymFunding.ts
# maxDeposit.ts
# cancelAllOrders.ts
# modifyOrder.ts
)

for test_file in ${test_files[@]}; do
Expand Down
Loading
Loading