File tree Expand file tree Collapse file tree
tokens/betting-market/anchor/programs/betting-market/src/instructions Expand file tree Collapse file tree Original file line number Diff line number Diff 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 ;
You can’t perform that action at this time.
0 commit comments