# Malicious user may frontrun `GoodDollarExpansionController::mintUBIFromReserveBalance()` to make protocol funds stuck

Attacker buys all $G to zero tokenSupply, then mintUBIFromInterest adds reserve funds but mints 0 $G, stranding protocol funds.

- 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/33
- Library page: https://bugbountylibrary.com/r/sherlock%3A2024-10-mento-update%3AM-3
- 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:** GoodDollarExpansionController / GoodDollarExchangeProvider (Solidity)
**Root cause:** mintFromInterest returns 0 when exchange.tokenSupply is 0, and callers mintUBIFromInterest/mintUBIFromReserveBalance don't check amountMinted before transferring reserve assets.
**Steps:**
1. Attacker calls `Bancor::swapIn()`/`swapOut()` to buy all $G, driving `PoolExchange.tokenSupply` to 0.
2. Trigger `mintUBIFromInterest(exchangeId, reserveInterest)` or `mintUBIFromReserveBalance(exchangeId)`.
3. `mintFromInterest` computes amountToMintScaled = reserveInterest * tokenSupply / reserveBalance = 0.
4. Reserve asset is transferred in but 0 $G minted to distributionHelper; funds stuck.
**How found:** Code review of mintFromInterest: noticed multiplication by exchange.tokenSupply yields 0 when supply is zero, and callers don't validate amountMinted.
**Exploitation:** Buy out all $G supply via Bancor swap to zero tokenSupply, then let the expansion controller's mint path execute; reserve assets are added with no corresponding $G minted.
**Insight:** In Bancor-style bonding curve math, any formula multiplying by tokenSupply silently returns 0 at zero supply; check callers that transfer value based on that return value rather than reverting.

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

**mintUBIFromInterest missing zero-mint check**

```
function mintUBIFromInterest(bytes32 exchangeId, uint256 reserveInterest) external {
  require(reserveInterest > 0, "Reserve interest must be greater than 0");
  IBancorExchangeProvider.PoolExchange memory exchange = IBancorExchangeProvider(address(goodDollarExchangeProvider))
    .getPoolExchange(exchangeId);

  uint256 amountToMint = goodDollarExchangeProvider.mintFromInterest(exchangeId, reserveInterest);

  require(IERC20(exchange.reserveAsset).transferFrom(msg.sender, reserve, reserveInterest), "Transfer failed"); //@audit safeTransferFrom.  //@audit lost if reserve asset is also a stable asset
  IGoodDollar(exchange.tokenAddress).mint(address(distributionHelper), amountToMint);

  // Ignored, because contracts only interacts with trusted contracts and tokens
  // slither-disable-next-line reentrancy-events
  emit InterestUBIMinted(exchangeId, amountToMint);
}
```

**mintUBIFromReserveBalance missing zero-mint check**

```
function mintUBIFromReserveBalance(bytes32 exchangeId) external returns (uint256 amountMinted) {
  IBancorExchangeProvider.PoolExchange memory exchange = IBancorExchangeProvider(address(goodDollarExchangeProvider))
    .getPoolExchange(exchangeId);

  uint256 contractReserveBalance = IERC20(exchange.reserveAsset).balanceOf(reserve);
  uint256 additionalReserveBalance = contractReserveBalance - exchange.reserveBalance;
  if (additionalReserveBalance > 0) {
    amountMinted = goodDollarExchangeProvider.mintFromInterest(exchangeId, additionalReserveBalance);
    IGoodDollar(exchange.tokenAddress).mint(address(distributionHelper), amountMinted);

    // Ignored, because contracts only interacts with trusted contracts and tokens
    // slither-disable-next-line reentrancy-events
    emit InterestUBIMinted(exchangeId, amountMinted);
  }
}
```

**mintFromInterest returns 0 when tokenSupply is 0**

```
function mintFromInterest(
  bytes32 exchangeId,
  uint256 reserveInterest
) external onlyExpansionController whenNotPaused returns (uint256 amountToMint) {
  PoolExchange memory exchange = getPoolExchange(exchangeId);

  uint256 reserveinterestScaled = reserveInterest * tokenPrecisionMultipliers[exchange.reserveAsset];
  uint256 amountToMintScaled = unwrap(
    wrap(reserveinterestScaled).mul(wrap(exchange.tokenSupply)).div(wrap(exchange.reserveBalance))
  );
  amountToMint = amountToMintScaled / tokenPrecisionMultipliers[exchange.tokenAddress];

  exchanges[exchangeId].tokenSupply += amountToMintScaled;
  exchanges[exchangeId].reserveBalance += reserveinterestScaled;

  return amountToMint;
}
```

