Skip to content

Commit

Permalink
Account for block builder fee when generating excess tip balance (#247)
Browse files Browse the repository at this point in the history
  • Loading branch information
buffalu committed Feb 10, 2023
1 parent c56ad7f commit b2a013a
Showing 1 changed file with 25 additions and 15 deletions.
40 changes: 25 additions & 15 deletions tip-distributor/src/stake_meta_generator_workflow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,24 +164,36 @@ pub fn generate_stake_meta_collection(
// We assume that the rewards sitting in the tip program PDAs are cranked out by the time all of
// the rewards are claimed.
let tip_accounts = derive_tip_payment_pubkeys(tip_payment_program_id);
let account = bank.get_account(&tip_accounts.config_pda);
let maybe_tip_receiver: Option<Pubkey> = account
.and_then(|account| Config::try_deserialize(&mut account.data()).ok())
.map(|config| config.tip_receiver);
let account = bank
.get_account(&tip_accounts.config_pda)
.expect("config pda exists");

let config = Config::try_deserialize(&mut account.data()).expect("deserializes configuration");

let bb_commission_pct: u64 = config.block_builder_commission_pct;
let tip_receiver: Pubkey = config.tip_receiver;

// includes the block builder fee
let excess_tip_balances: u64 = tip_accounts
.tip_pdas
.iter()
.map(|pubkey| {
bank.get_account(pubkey)
.map(|acc| {
acc.lamports()
.checked_sub(bank.get_minimum_balance_for_rent_exemption(acc.data().len()))
.expect("tip balance underflow")
})
.unwrap_or_default()
let tip_account = bank.get_account(pubkey).expect("tip account exists");
tip_account
.lamports()
.checked_sub(bank.get_minimum_balance_for_rent_exemption(tip_account.data().len()))
.expect("tip balance underflow")
})
.sum();
// matches math in tip payment program
let block_builder_tips = excess_tip_balances
.checked_mul(bb_commission_pct)
.expect("block_builder_tips overflow")
.checked_div(100)
.expect("block_builder_tips division error");
let tip_receiver_fee = excess_tip_balances
.checked_sub(block_builder_tips)
.expect("tip_receiver_fee doesnt underflow");

let vote_pk_and_maybe_tdas: Vec<(
(Pubkey, &VoteAccount),
Expand All @@ -203,13 +215,11 @@ pub fn generate_stake_meta_collection(
.expect("deserialized TipDistributionAccount");
// this snapshot might have tips that weren't claimed by the time the epoch is over
// assume that it will eventually be cranked and credit the excess to this account
if maybe_tip_receiver.is_some()
&& tip_distribution_pubkey == maybe_tip_receiver.unwrap()
{
if tip_distribution_pubkey == tip_receiver {
account_data.set_lamports(
account_data
.lamports()
.checked_add(excess_tip_balances)
.checked_add(tip_receiver_fee)
.expect("tip overflow"),
);
}
Expand Down

0 comments on commit b2a013a

Please sign in to comment.