# User to sell the last supply will make the exchange contribution forever stuck

Selling the final supply token in BancorExchangeProvider applies an exit contribution to the last swap, leaving those reserve funds permanently stuck with zero supply to redeem.

- Payout: Audit contest
- Severity: medium
- Program: Mento Update
- Found by: 0x73696d616f
- Disclosed: 2024-10-17
- Write-up quality: 6/10
- Original: https://github.com/sherlock-audit/2024-10-mento-update-judging/issues/17
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-mento-update%3AM-1
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:sherlock sev:medium tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** BancorExchangeProvider swapIn / GoodDollarExchangeProvider
**Root cause:** _getScaledAmountOut applies the exit contribution to scaledAmountOut even when the swap drains the entire token supply, so the withheld reserve portion has no remaining supply to redeem it.
**Steps:**
1. Deploy/initialize BancorExchangeProvider and create an exchange with poolExchange1.
2. Call `swapIn(exchangeId, token, reserveToken, 300_000e18)` as broker to sell all supply.
3. Observe amountOut=59400e18, reserveBalanceAfter=600e18, tokenSupplyAfter=0.
4. Further `swapIn` reverts `ERR_INVALID_SUPPLY`; the 600e18 exit contribution is unredeemable.
**How found:** Code reading of _getScaledAmountOut noticed the exit contribution is applied unconditionally, then reasoned about the edge case where the swap reduces supply to zero.
**Exploitation:** Any user selling the last of the supply triggers the exit contribution deduction on the final swap; the withheld reserve amount remains in the contract with zero supply outstanding, permanently locked.
**Insight:** When a fee/contribution is deducted from output, check the terminal state where the deducted asset's backing supply reaches zero—the withheld portion becomes unredeemable; test boundary swaps that fully drain supply.

## Proof of concept (copied exactly from the original report)

**PoC test selling last supply leaves reserve stuck**

```
function test_POC_swapIn_whenTokenInIsToken_shouldSwapIn() public {
  BancorExchangeProvider bancorExchangeProvider = initializeBancorExchangeProvider();
  uint256 amountIn =  300_000 * 1e18;

  bytes32 exchangeId = bancorExchangeProvider.createExchange(poolExchange1);

  vm.startPrank(brokerAddress);
  uint256 amountOut = bancorExchangeProvider.swapIn(exchangeId, address(token), address(reserveToken), amountIn);

  (, , uint256 tokenSupplyAfter, uint256 reserveBalanceAfter, , ) = bancorExchangeProvider.exchanges(exchangeId);

  assertEq(amountOut, 59400e18);
  assertEq(reserveBalanceAfter, 600e18);
  assertEq(tokenSupplyAfter, 0);

  vm.expectRevert("ERR_INVALID_SUPPLY");
  bancorExchangeProvider.swapIn(exchangeId, address(token), address(reserveToken), 1e18);
}
```

