-
Notifications
You must be signed in to change notification settings - Fork 176
Wphan/builder codes #1805
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
Open
wphan
wants to merge
24
commits into
master
Choose a base branch
from
wphan/builder_codes
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+4,973
−135
Open
Wphan/builder codes #1805
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
010e8a3
add RevenueShare and RevenueShareEscrow accounts an init ixs
wphan dfe4330
fix multiple array zc account, and handling different message types i…
wphan aa88106
decoding error
wphan 6afc60c
recording orders in RevenueShareEscrow workin
wphan e93bc21
cancel and fill orders
wphan 2e247b5
Merge remote-tracking branch 'origin/master' into wphan/builder_codes
wphan 35486e9
idl
wphan 35350ba
fix sdk build
wphan f0e06af
fix math
wphan 2ef60ce
update RevenueShareOrder bitflags, store builder_idx instead of pubkey
wphan 57e335d
merge RevenueShareOrders on add
wphan b2fe670
remove builder accounts from cancel ixs, wip settle impl
wphan 7b688ad
dont fail settlpnl if no builder users provided
wphan d11de9e
finish settle, rename RevenueShare->Builder, RevenueShareEscrow->Buil…
wphan 464ff9b
add more bankrun tests, clean up
wphan 97ef7f7
Merge branch 'master' into wphan/builder_codes
wphan 8ffb821
clean up, fix tests
wphan 7971ab5
why test fail
wphan 28860b1
add subaccountid to BuilderOrder
wphan e4aafb0
reduce diff
wphan fabe2ff
add referrals
wphan 84e7c46
add test can fill settle user with no builderescrow
wphan 29fb679
add referral builder feature flag and referral migration method
wphan 8870bad
fix cargo tests, try fix bankrun test timing issue
wphan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
use anchor_lang::prelude::*; | ||
|
||
use crate::controller::spot_balance; | ||
use crate::math::safe_math::SafeMath; | ||
use crate::math::spot_balance::get_token_amount; | ||
use crate::state::builder::{BuilderEscrowZeroCopyMut, BuilderOrder, BuilderOrderBitFlag}; | ||
use crate::state::builder_map::BuilderMap; | ||
use crate::state::events::BuilderSettleRecord; | ||
use crate::state::perp_market_map::PerpMarketMap; | ||
use crate::state::spot_market::SpotBalance; | ||
use crate::state::spot_market_map::SpotMarketMap; | ||
use crate::state::user::MarketType; | ||
|
||
/// Runs through the user's BuilderEscrow account and sweeps any accrued fees to the corresponding | ||
/// builders and referrer. | ||
pub fn sweep_completed_builder_fees_for_market<'a>( | ||
market_index: u16, | ||
builder_escrow: &mut BuilderEscrowZeroCopyMut, | ||
perp_market_map: &PerpMarketMap<'a>, | ||
spot_market_map: &SpotMarketMap<'a>, | ||
builder_map: BuilderMap<'a>, | ||
now_ts: i64, | ||
) -> crate::error::DriftResult<()> { | ||
let perp_market = &mut perp_market_map.get_ref_mut(&market_index)?; | ||
let quote_spot_market = &mut spot_market_map.get_quote_spot_market_mut()?; | ||
|
||
spot_balance::update_spot_market_cumulative_interest(quote_spot_market, None, now_ts)?; | ||
|
||
let orders_len = builder_escrow.orders_len(); | ||
for i in 0..orders_len { | ||
let ( | ||
is_completed, | ||
is_referral_order, | ||
order_market_type, | ||
order_market_index, | ||
fees_accrued, | ||
builder_idx, | ||
) = { | ||
let ord_ro = match builder_escrow.get_order(i) { | ||
Ok(o) => o, | ||
Err(_) => { | ||
continue; | ||
} | ||
}; | ||
( | ||
ord_ro.is_completed(), | ||
ord_ro.is_referral_order(), | ||
ord_ro.market_type, | ||
ord_ro.market_index, | ||
ord_ro.fees_accrued, | ||
ord_ro.builder_idx, | ||
) | ||
}; | ||
|
||
if is_referral_order { | ||
if fees_accrued == 0 { | ||
continue; | ||
} | ||
} else if !(is_completed | ||
&& order_market_type == MarketType::Perp | ||
&& order_market_index == market_index | ||
&& fees_accrued > 0) | ||
{ | ||
continue; | ||
} | ||
|
||
let pnl_pool_token_amount = get_token_amount( | ||
perp_market.pnl_pool.scaled_balance, | ||
quote_spot_market, | ||
perp_market.pnl_pool.balance_type(), | ||
)?; | ||
|
||
// TODO: should we add buffer on pnl pool? | ||
if pnl_pool_token_amount < fees_accrued as u128 { | ||
msg!( | ||
"market {} PNL pool has insufficient balance to sweep fees for builder", | ||
market_index | ||
); | ||
break; | ||
} | ||
|
||
if is_referral_order { | ||
let referrer_authority = if let Some(referrer_authority) = builder_escrow.get_referrer() | ||
{ | ||
referrer_authority | ||
} else { | ||
continue; | ||
}; | ||
|
||
let referrer_user = builder_map.get_user_ref_mut(&referrer_authority); | ||
let referrer_builder = builder_map.get_builder_account_mut(&referrer_authority); | ||
|
||
if referrer_user.is_ok() && referrer_builder.is_ok() { | ||
let mut referrer_user = referrer_user.unwrap(); | ||
let mut referrer_builder = referrer_builder.unwrap(); | ||
|
||
spot_balance::transfer_spot_balances( | ||
fees_accrued as i128, | ||
quote_spot_market, | ||
&mut perp_market.pnl_pool, | ||
referrer_user.get_quote_spot_position_mut(), | ||
)?; | ||
|
||
referrer_builder.total_referrer_rewards = referrer_builder | ||
.total_referrer_rewards | ||
.safe_add(fees_accrued as u64)?; | ||
|
||
emit!(BuilderSettleRecord { | ||
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. would use emit stack |
||
ts: now_ts, | ||
builder: None, | ||
referrer: Some(referrer_authority), | ||
fee_settled: fees_accrued as u64, | ||
market_index: order_market_index, | ||
market_type: order_market_type, | ||
builder_total_referrer_rewards: referrer_builder.total_referrer_rewards, | ||
builder_total_builder_rewards: referrer_builder.total_builder_rewards, | ||
builder_sub_account_id: referrer_user.sub_account_id, | ||
}); | ||
|
||
// zero out the order | ||
if let Ok(builder_order) = builder_escrow.get_order_mut(i) { | ||
builder_order.fees_accrued = 0; | ||
} | ||
} | ||
} else { | ||
let builder_authority = match builder_escrow | ||
.get_approved_builder_mut(builder_idx) | ||
.map(|builder| builder.authority) | ||
{ | ||
Ok(auth) => auth, | ||
Err(_) => { | ||
continue; | ||
} | ||
}; | ||
|
||
let builder_user = builder_map.get_user_ref_mut(&builder_authority); | ||
let builder = builder_map.get_builder_account_mut(&builder_authority); | ||
|
||
if builder_user.is_ok() && builder.is_ok() { | ||
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. probably good to log when these are none for debugging? |
||
let mut builder_user = builder_user.unwrap(); | ||
let mut builder_revenue_share = builder.unwrap(); | ||
|
||
spot_balance::transfer_spot_balances( | ||
fees_accrued as i128, | ||
quote_spot_market, | ||
&mut perp_market.pnl_pool, | ||
builder_user.get_quote_spot_position_mut(), | ||
)?; | ||
|
||
builder_revenue_share.total_builder_rewards = builder_revenue_share | ||
.total_builder_rewards | ||
.safe_add(fees_accrued as u64)?; | ||
|
||
emit!(BuilderSettleRecord { | ||
ts: now_ts, | ||
builder: Some(builder_authority), | ||
referrer: None, | ||
fee_settled: fees_accrued as u64, | ||
market_index: order_market_index, | ||
market_type: order_market_type, | ||
builder_total_referrer_rewards: builder_revenue_share.total_referrer_rewards, | ||
builder_total_builder_rewards: builder_revenue_share.total_builder_rewards, | ||
builder_sub_account_id: builder_user.sub_account_id, | ||
}); | ||
|
||
// remove order | ||
if let Ok(builder_order) = builder_escrow.get_order_mut(i) { | ||
*builder_order = BuilderOrder::default(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
pub mod amm; | ||
pub mod builder; | ||
pub mod funding; | ||
pub mod insurance; | ||
pub mod liquidation; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
good q for @0xbigz