Skip to content

Commit 5fe327a

Browse files
committed
Break claim_winnings payout into named steps
Split the single pro-rata expression into intermediary variables (losing_pool_share_numerator, winnings, payout) so each step of the parimutuel split reads on its own line.
1 parent a6006a2 commit 5fe327a

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

  • tokens/betting-market/anchor/programs/betting-market/src/instructions

tokens/betting-market/anchor/programs/betting-market/src/instructions/claim_winnings.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,15 +59,19 @@ pub fn handle_claim_winnings(context: Context<ClaimWinnings>) -> Result<()> {
5959
);
6060

6161
let stake = context.accounts.bet.amount;
62+
// Total staked on the winning outcome, and the losers' stakes after the fee.
6263
let winning_pool = context.accounts.event.winning_pool;
6364
let distributable_losing_pool = context.accounts.event.distributable_losing_pool;
6465

65-
// Pro-rata share of the losing pool, on top of the returned stake. u128
66-
// intermediate avoids overflow; the floor leaves at most a few base units
67-
// of dust in the vault.
68-
let winnings_share =
69-
(stake as u128 * distributable_losing_pool as u128 / winning_pool as u128) as u64;
70-
let payout = stake + winnings_share;
66+
// Parimutuel split: winners share the losing pool in proportion to their
67+
// own stake. Work in u128 and divide once, after the multiply, so the
68+
// result is floored a single time — dividing first would throw away
69+
// precision. The floor leaves at most a few base units of dust in the vault.
70+
let losing_pool_share_numerator = stake as u128 * distributable_losing_pool as u128;
71+
let winnings = (losing_pool_share_numerator / winning_pool as u128) as u64;
72+
73+
// Winners always get their own stake back on top of their winnings.
74+
let payout = stake + winnings;
7175

7276
let event_id = context.accounts.event.event_id;
7377
let event_bump = context.accounts.event.bump;

0 commit comments

Comments
 (0)