Skip to content
Business logic Audit finding Intermediate

Listing potential can not be purchased with discounted price

A listing with a fixed discount could have a discounted price so low that the calculated payment amount rounds down to zero, causing the purchase transaction to revert. This prevents buyers from purchasing discounted vesting entries.

0xc0ffEE Follow with 0xastronatey, 0xIconart and 25 others

Originally disclosed on Code4rena

Submitted by 0xc0ffEE, also found by 0xastronatey, 0xIconart, 0xNirix, 0XRolko, agadzhalov, AshishLach, BenRai, c0pp3rscr3w3r, ChainProof, ChainSentry, CrazyMoose, farismaulana, itsabinashb, IvanAlexandur, montecristo, mrMorningstar, Olami978355, queen, Sabit, safie, Shinobi, the_code_doctor, TheFabled, trailongoswami, X0sauce, zanderbyte, and zzebra83

https://github.com/code-423n4/2024-12-secondswap/blob/214849c3517eb26b31fe194bceae65cb0f52d2c0/contracts/SecondSwap_Marketplace.sol#L459-L471

https://github.com/code-423n4/2024-12-secondswap/blob/214849c3517eb26b31fe194bceae65cb0f52d2c0/contracts/SecondSwap_Marketplace.sol#L413-L422

In the function SecondSwap_Marketplace::spotPurchase(), depending on the listing’s discount type, the price is computed accordingly:

    function _getDiscountedPrice(Listing storage listing, uint256 _amount) private view returns (uint256) {
        uint256 discountedPrice = listing.pricePerUnit;

        if (listing.discountType == DiscountType.LINEAR) {
            discountedPrice = (discountedPrice * (BASE - ((_amount * listing.discountPct) / listing.total))) / BASE;
        } else if (listing.discountType == DiscountType.FIX) {
            discountedPrice = (discountedPrice * (BASE - listing.discountPct)) / BASE;
        }
        return discountedPrice;
    }

And then the baseAmount that the buyer needs to pay is calculated from the discounted price. Although there is a check to enforce listing value is not too small with the original price, but it still can be too small with the discounted price because indeed the discounted price is lower than the original price. So in that case, the buyer won’t be able to purchase listed sale.

    function _handleTransfers(
        Listing storage listing,
        uint256 _amount,
        uint256 discountedPrice,
        uint256 bfee,
        uint256 sfee,
        address _referral
    ) private returns (uint256 buyerFeeTotal, uint256 sellerFeeTotal, uint256 referralFeeCost) {
@>        uint256 baseAmount = (_amount * discountedPrice) /
            uint256(
                10 **
                    (
                        IERC20Extended(
                            address(
                                IVestingManager(IMarketplaceSetting(marketplaceSetting).vestingManager())
                                    .getVestingTokenAddress(listing.vestingPlan)
                            )
                        ).decimals()
                    )
            ); // 3.1. Rounding issue leads to total drain of vesting entries
@>        require(baseAmount > 0, "SS_Marketplace: Amount too little"); // 3.1. Rounding issue leads to total drain of vesting entries
...

Example with a simple vulnerable path:

  1. Alice lists vesting with amount = 1e15 (assume the token has 18 decimals), currency is USDT, price = 1200 ($0.0012), with fixed discount = 20% and the listing type is Single.
  2. Bob tries to purchase Alice’s sale, but the transaction fails because baseAmount is 0 in this case: baseAmount = 1e15 * 1200 * 80% / 1e18 = 0.

Impacts:

  • Users are potentially unable to purchase discounted vestings

Proof of Concept

Add this test under the describe("Purchase Lot")

        it.only("can not buy with discounted price", async function () {
            const {
                vesting,
                token,
                marketplace,
                marketplaceSetting,
                user1,
                manager
            } = await loadFixture(deployProxyFixture);
            const [user2, user3] = await hre.viem.getWalletClients();

            // Test parameters
            const amount = parseEther("1") / 1000n;
            const cost = 1200n; // 0,0012 $
            const discountPct = BigInt(4000); // 20% discount
            const listingType = 1;  // Single fill
            const discountType = 2; // Fixed discount
            const maxWhitelist = BigInt(0);
            const privateListing = false;
            const minPurchaseAmt = parseEther("1") / 1000n;

            const updatedSettings = await manager.read.vestingSettings([vesting.address]);
            expect(updatedSettings[0]).to.be.true; // Check if sellable is true

            // List vesting
            await marketplace.write.listVesting([
                vesting.address,
                amount,
                cost,
                discountPct,
                listingType,
                discountType,
                maxWhitelist,
                token.address,
                minPurchaseAmt,
                privateListing
            ], { account: user1.account });

            // // Make purchase => revert
            await expect(marketplace.write.spotPurchase([
                vesting.address,
                BigInt(0),
                amount,
                "0x0000000000000000000000000000000000000000"
            ], {account: user3.account}))
            .to.reverted;

});

Run the test and it succeeded.

It means that the spotPurchase() transaction failed.

Recommended mitigation steps

Consider updating the check for baseAmount in function listVesting() to take discount into account.

TechticalRAM (SecondSwap) confirmed


The original report as published on Code4rena, by 0xc0ffEE. 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

Likely the minimum purchase check uses the original price instead of the discounted price, so a discounted purchase can round down to zero and revert.

Impact

Users are potentially unable to purchase discounted vestings.

What the attacker needed

A listing with a discount; a small purchase amount

The fix

The check for baseAmount in listVesting() should take the discount into account.

How it was found

  1. Alice lists a vesting with a small amount, a low price, and a fixed discount.
  2. Bob tries to purchase the listing.
  3. The transaction reverts because the calculated baseAmount is zero.

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.

When you buy something, the contract calculates how much you owe. If the price is very small and you buy a tiny amount, the maths can round the total down to zero. The contract refuses to accept a zero payment, so the purchase fails and you cannot buy the discounted item.

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 0xc0ffEE

View all

27 more disclosed reports, best first.

More from SecondSwap

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.