Skip to content
Audit finding Intermediate

Division in Bonding.sol._openTradingOnUniswap() results in an incorrect lpSupply , higher vaultSupply , and dust AgentTokens getting locked in FPair

A rounding error in the Bonding contract's graduation logic causes the liquidity pool supply to be truncated, making the vault supply too large and leaving dust agent tokens permanently locked in the FPair contract. An attacker cannot directly profit, but the protocol loses a small amount of tokens and the accounting becomes inconsistent.

BowTiedOriole Follow with YouCrossTheLineAlfie

Originally disclosed on Code4rena

Submitted by BowTiedOriole, also found by YouCrossTheLineAlfie

When a Prototype Agent graduates to a Sentient Agent, agent tokens are minted to the FPair contract. Users are then able to call Bonding.unwrapToken() to claim their agent tokens.

Upon graduation, the Bonding contract will divide the token supply as well as the tokenBalance by 10 ** 18.

Bonding.sol#L390-L395

address agentToken = IAgentFactoryV3(agentFactory).executeBondingCurveApplication(
    id,
    _token.data.supply / (10 ** token_.decimals()),
    tokenBalance / (10 ** token_.decimals()),
    pairAddress
);

Within AgentFactoryV3.executeBondingCurveApplication(), these parameters are used to calculate the vault supply.

AgentFactoryV3.sol#L466-480

bytes memory tokenSupplyParams = abi.encode(
    totalSupply,
    lpSupply,
    /* vaultSupply */ totalSupply - lpSupply,
    totalSupply,
    totalSupply,
    0,
    vault
);

Then, within AgentToken.initialize(), the provided numbers will be multiplied by 10 ** 18.

AgentToken.sol#L95-L96

uint256 lpSupply = supplyParams.lpSupply * (10 ** decimals());
uint256 vaultSupply = supplyParams.vaultSupply * (10 ** decimals());

When lpSupply is calculated as tokenBalance / (10 ** token_.decimals()) it will truncate any remainder after dividing by 1 ether. Then it will be multiplied by 10 ** token_.decimals() which results in a value that is smaller than the original lpSupply. This will result in vaultSupply being too big and AgentTokens being stuck in the FPair contract.

Recommended mitigation steps

Remove the division and then multiplication by 10 ** token_.decimals(). Simply pass through the full wei value.

Proof of Concept

// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";

interface IERC20 {
    function transfer(address to, uint256 amount) external returns (bool);
    function mint(address to, uint256 amount) external;
    function approve(address spender, uint256 amount) external returns (bool);
    function balanceOf(address account) external view returns (uint256);
    function totalSupply() external view returns (uint256);
}

interface IBonding {
    function launch(
        string memory _name,
        string memory _ticker,
        uint8[] memory cores,
        string memory desc,
        string memory img,
        string[4] memory urls,
        uint256 purchaseAmount
    ) external returns (address, address, uint);

    function router() external view returns (address);

    function buy(uint256 amountIn, address tokenAddress) external payable returns (bool);
}

contract BondingRoundingPOC is Test {
    IERC20 virtualToken = IERC20(0x0b3e328455c4059EEb9e3f84b5543F74E24e7E1b);
    address bridge = 0x4200000000000000000000000000000000000010;
    IBonding bonding = IBonding(0xF66DeA7b3e897cD44A5a231c61B6B4423d613259);

    string BASE_RPC_URL = vm.envString("BASE_RPC_URL");
    address alice = makeAddr("alice");
    address bob = makeAddr("bob");
    address charlie = makeAddr("charlie");
    uint256 virtualAmount = 45000 ether;

    function setUp() public {
        uint256 forkId = vm.createFork(BASE_RPC_URL, 29_519_750);
        vm.selectFork(forkId);

        vm.startPrank(bridge);
        virtualToken.mint(alice, virtualAmount);
        virtualToken.mint(bob, .5 ether);
        vm.stopPrank();
    }

    function test_rounding_error_upon_graduate() public {
        vm.startPrank(alice);
        virtualToken.approve(address(bonding), virtualAmount);
        string[4] memory urls = ["test", "test", "test", "test"];
        uint8[] memory cores = new uint8[](1);
        cores[0] = 1;
        (address token, address pair, ) = bonding.launch("test agent", "TA", cores, "test", "test", urls, 45000 ether);
        vm.stopPrank();

        vm.startPrank(bob);
        virtualToken.approve(bonding.router(), .5 ether);
        bonding.buy(.5 ether, token);

        (bool success, bytes memory data) = address(bonding).call(abi.encodeWithSignature("tokenInfo(address)", token));
        require(success, "Failed to get token info");

        (
            ,
            ,
            ,
            address agentToken
        ) = abi.decode(data, (address, address, address, address));

        console.log("agentToken balance of pair  :", IERC20(agentToken).balanceOf(pair));
        console.log("token balance of alice + bob:", IERC20(token).balanceOf(alice) + IERC20(token).balanceOf(bob));
        assertNotEq(IERC20(agentToken).balanceOf(pair), IERC20(token).balanceOf(alice) + IERC20(token).balanceOf(bob));
    }
}

The original report as published on Code4rena, by BowTiedOriole. 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 code divides the token balance by 10^decimals and later multiplies by 10^decimals, causing truncation that reduces lpSupply and increases vaultSupply.

What the attacker needed

A token launch with a purchase amount; a subsequent buy to trigger graduation

The fix

The division and subsequent multiplication by 10^decimals should be removed, passing the full wei value directly.

How it was found

  1. Alice launches a new agent token with a large purchase amount.
  2. Bob buys a small amount of the agent token.
  3. The token graduates, triggering the division and multiplication in Bonding._openTradingOnUniswap().
  4. The test checks that the agent token balance of the pair does not equal the sum of alice and bob's token balances, showing locked tokens.

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 a token 'graduates' to a new phase, the contract calculates how many tokens go to the liquidity pool and how many go to a vault. It divides a number and then multiplies it back, but the division loses a tiny remainder (like losing a penny when splitting a bill). That lost amount means the vault gets slightly more tokens than it should, and a few tokens get stuck forever in the pool contract.

More by BowTiedOriole

View all

4 more disclosed reports, best first.

More from Virtuals Protocol

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.