-
Notifications
You must be signed in to change notification settings - Fork 100
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 amm revenue pool logic #398
Changes from all commits
75c7835
0cb3d63
1259b77
66663fa
b4110f2
4568788
969f29a
94791c2
2e7e101
0564ecd
d1769a5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -466,7 +466,7 @@ pub fn update_pool_balances( | |
.total_fee_minus_distributions | ||
.safe_sub(market.amm.total_fee_withdrawn.cast()?)?; | ||
|
||
if terminal_state_surplus < 0 { | ||
if terminal_state_surplus < FEE_POOL_TO_REVENUE_POOL_THRESHOLD.cast()? { | ||
// market can perform withdraw from revenue pool | ||
if spot_market.insurance_fund.last_revenue_settle_ts | ||
> market.insurance_claim.last_revenue_withdraw_ts | ||
|
@@ -484,6 +484,7 @@ pub fn update_pool_balances( | |
let max_revenue_withdraw_allowed = market | ||
.insurance_claim | ||
.max_revenue_withdraw_per_period | ||
.cast::<i64>()? | ||
.safe_sub(market.insurance_claim.revenue_withdraw_since_last_settle)? | ||
.cast::<u128>()?; | ||
|
||
|
@@ -518,28 +519,59 @@ pub fn update_pool_balances( | |
market.insurance_claim.last_revenue_withdraw_ts = now; | ||
} | ||
} else { | ||
let revenue_pool_transfer = get_total_fee_lower_bound(market)? | ||
.cast::<i128>()? | ||
.safe_add(market.amm.total_liquidation_fee.cast()?)? | ||
.safe_sub(market.amm.total_fee_withdrawn.cast()?)? | ||
let fee_pool_threshold = amm_fee_pool_token_amount_after | ||
.saturating_sub( | ||
FEE_POOL_TO_REVENUE_POOL_THRESHOLD.safe_add(market.amm.total_social_loss)?, | ||
) | ||
.cast()?; | ||
|
||
let total_liq_fees_for_revenue_pool: i128 = market | ||
.amm | ||
.total_liquidation_fee | ||
.min( | ||
amm_fee_pool_token_amount_after | ||
.saturating_sub(FEE_POOL_TO_REVENUE_POOL_THRESHOLD) | ||
market | ||
.insurance_claim | ||
.quote_settled_insurance | ||
.safe_add(market.insurance_claim.quote_max_insurance)? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why is this add? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. both numbers are positive cumulative numbers for insurance cumulative liquidation fees are only given for revenue pool up to the current external insurance levels provided + net losses already paid for by external insurance |
||
.cast()?, | ||
) | ||
.max(0) | ||
.unsigned_abs(); | ||
.cast()?; | ||
|
||
let max_revenue_to_settle = market | ||
.insurance_claim | ||
.revenue_withdraw_since_last_settle | ||
.safe_add( | ||
market | ||
.insurance_claim | ||
.max_revenue_withdraw_per_period | ||
.cast()?, | ||
)? | ||
.min(market.amm.net_revenue_since_last_funding) | ||
.max(0); | ||
|
||
let total_fee_for_if = get_total_fee_lower_bound(market)?.cast::<i128>()?; | ||
|
||
let revenue_pool_transfer = total_fee_for_if | ||
.safe_add(total_liq_fees_for_revenue_pool)? | ||
.safe_sub(market.amm.total_fee_withdrawn.cast()?)? | ||
.min(fee_pool_threshold) | ||
.min(max_revenue_to_settle.cast()?); | ||
|
||
transfer_spot_balance_to_revenue_pool( | ||
revenue_pool_transfer, | ||
revenue_pool_transfer.unsigned_abs(), | ||
spot_market, | ||
&mut market.amm.fee_pool, | ||
)?; | ||
|
||
market.insurance_claim.revenue_withdraw_since_last_settle = market | ||
.insurance_claim | ||
.revenue_withdraw_since_last_settle | ||
.safe_sub(revenue_pool_transfer.cast()?)?; | ||
|
||
market.amm.total_fee_withdrawn = market | ||
.amm | ||
.total_fee_withdrawn | ||
.safe_add(revenue_pool_transfer)?; | ||
.safe_add(revenue_pool_transfer.unsigned_abs())?; | ||
} | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why do you add social loss here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if a market has had social loss, its indication it needs to maintain a larger fee pool for internal insurance (rather than giving to external insurance)