2

If ctx.accounts.game_match.won is true, I don't get any errors, but if I go to the else block, I get the error mentioned in the title. But why?

   if ctx.accounts.game_match.won {
            msg!("you won!");

            ctx.accounts.game.win_count += 1;
            ctx.accounts.game_match.won_amount = bet_amount * ctx.accounts.game.multiplier as u64;

            let transfer_amount = ctx.accounts.game_match.won_amount - bet_amount;

            msg!("transfer_amount: {}", transfer_amount);

            **ctx
                .accounts
                .game
                .to_account_info()
                .try_borrow_mut_lamports()? -= transfer_amount;

            **ctx
                .accounts
                .player
                .to_account_info()
                .try_borrow_mut_lamports()? += transfer_amount;
        } else {
            msg!("you lost :(");
            msg!("bet_amount: {}", bet_amount);

            ctx.accounts.game.lose_count += 1;

            **ctx
                .accounts
                .game
                .to_account_info()
                .try_borrow_mut_lamports()? += bet_amount;

            **ctx
                .accounts
                .player
                .to_account_info()
                .try_borrow_mut_lamports()? -= bet_amount;
        }

2 Answers 2

1

Make sure that player has at least bet_amount, otherwise you'll be doing an overflowing subtraction, which means that you could be setting player's lamports to a huge number close to u64::MAX!

Also, be sure that player is owned by your program so that it can properly decrement its lamports.

0

Shot in the dark, but... maybe decrement "player" first and then increment "game"? That is a big difference I see between your first and second scenario.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.