Skip to content
Business logic Audit finding Intermediate

Last buy might charge the wrong fee

In the final buy that completes a bonding curve, the protocol recomputes the SOL amount to match the curve, but the swap fee was already calculated from the original input amount. This can make the fee charged to the user incorrect, and if the recomputed amount is higher the user's account may not have enough lamports left to stay open.

shaflow2 Follow with 0xlookman, 13u9 and 4 others

Originally disclosed on Code4rena

Submitted by shaflow2, also found by 0xlookman, 13u9, Agontuk, ATH, GEEKS, and p0wd3r

https://github.com/code-423n4/2025-01-pump-science/blob/768ef58478724bf6b464c9f0952e3e5a3b2a2613/programs/pump-science/src/state/bonding_curve/curve.rs#L103

In the “last buy” process, the protocol automatically adjusts the price to fit the curve, ensuring precise SOL fundraising. This results in a change in the transaction price, which also alters the amount of SOL paid by the user. However, since the swap fee is calculated beforehand, this can lead to an incorrect fee calculation. And if the actual amount of SOL to be paid increases, we should recheck that ctx.accounts.user.get_lamports() >= exact_in_amount.checked_add(min_rent).unwrap() to ensure sufficient remaining funds and prevent the account from being closed.

Proof of Concept

The transaction fee is calculated before entering the apply_buy function, based on the exact_in_amount provided as input.

            // Check if slot is start slot and buyer is bonding_curve creator
            if clock.slot == bonding_curve.start_slot
                && ctx.accounts.user.key() == bonding_curve.creator
            {
                msg!("Dev buy");
                fee_lamports = 0;
                buy_amount_applied = exact_in_amount;
            } else {
                fee_lamports = bonding_curve.calculate_fee(exact_in_amount, clock.slot)?;
                msg!("Fee: {} SOL", fee_lamports);
                buy_amount_applied = exact_in_amount - fee_lamports;
            }

            let buy_result = ctx
                .accounts
                .bonding_curve
                .apply_buy(buy_amount_applied)
                .ok_or(ContractError::BuyFailed)?;

However, if it is the last buy, the price adjustment will cause the actual amount of SOL the user needs to pay (as reflected in buy_result) to change, leading to incorrect protocol fee calculation.

        if token_amount >= self.real_token_reserves {
            // Last Buy
            token_amount = self.real_token_reserves;

            // Temporarily store the current state
            let current_virtual_token_reserves = self.virtual_token_reserves;
            let current_virtual_sol_reserves = self.virtual_sol_reserves;

            // Update self with the new token amount
            self.virtual_token_reserves = (current_virtual_token_reserves as u128)
                .checked_sub(token_amount as u128)?
                .try_into()
                .ok()?;
            self.virtual_sol_reserves = 115_005_359_056; // Total raise amount at end

            let recomputed_sol_amount = self.get_sol_for_sell_tokens(token_amount)?;
            msg!("ApplyBuy: recomputed_sol_amount: {}", recomputed_sol_amount);
            sol_amount = recomputed_sol_amount;

            // Restore the state with the recomputed sol_amount
            self.virtual_token_reserves = current_virtual_token_reserves;
            self.virtual_sol_reserves = current_virtual_sol_reserves;

            // Set complete to true
            self.complete = true;
        }

Recommended mitigation steps

After the apply_buy call is completed, check whether the actual SOL paid by the user in buy_result matches buy_amount_applied. If they do not match, recalculate fee_lamports. Additionally, revalidate that ctx.accounts.user.get_lamports() >= exact_in_amount.checked_add(min_rent).unwrap().

It is also recommended to add a new slippage parameter to control the maximum SOL input.

Kulture (Pump Science) confirmed


The original report as published on Code4rena, by shaflow2. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

The fee is computed from the user-supplied input amount before apply_buy, but the last-buy branch inside apply_buy changes the actual SOL amount, so the fee no longer matches the real payment.

Impact

The protocol may charge an incorrect fee on the final buy, and if the recomputed SOL amount rises the user's account could be left below the rent minimum and closed.

What the attacker needed

Buyer performs the final buy on a curve

The fix

After apply_buy, compare the actual SOL paid in buy_result with buy_amount_applied, recalculate the fee if they differ, revalidate the user's remaining lamports against the rent minimum, and add a slippage parameter capping the maximum SOL input.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

A bonding curve is a pricing rule where each token costs a bit more as more are bought. On the very last purchase the program recalculates the exact price so the fundraising target is hit precisely, but the fee was worked out earlier from the amount the buyer typed in. So the fee can be wrong, and the buyer might be charged more than their account can safely cover.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by shaflow2

View all

12 more disclosed reports, best first.

More from Pump Science

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.