# Incorrectly assigned decimal1 parameter upon decoding

Basin Stable2.decodeWellData checks decimal0 twice instead of decimal1, so token1 decimals are mis-scaled, corrupting AMM reserve and LP supply math.

- Payout: Audit contest
- Severity: high
- Program: Basin
- Found by: ZanyBonzy
- Disclosed: 2024-09-03
- Write-up quality: 6/10
- Original: https://github.com/code-423n4/2024-07-basin-findings/issues/17
- Library page: https://bugbountylibrary.com/r/c4%3A2024-07-basin%3AH-02
- Tags: area:web3 domain:web3 impact:funds-loss kind:contest platform:code4rena sev:high tech:evm tech:solidity technique:source-review vuln:accounting-error vuln:precision-loss

**Target:** Stable2.sol decodeWellData and dependent AMM math functions
**Root cause:** The second zero-check in decodeWellData tests `decimal0 == 0` instead of `decimal1 == 0`, so decimal1 is never defaulted to 18 and is used as-is (including 0) in scaling.
**Steps:**
1. Read `decodeWellData` in Stable2.sol: `abi.decode(data,(uint256,uint256))` into decimal0/decimal1.
2. Observe both `if` blocks check `decimal0 == 0`; the second should check `decimal1 == 0`.
3. Call `calcLpTokenSupply([1e19,1e19], 0x00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000)` where decimal0=18, decimal1=0.
4. Observe revert `panic: arithmetic underflow or overflow (0x11)` instead of correct 18-decimal scaling.
**How found:** Code review of the decimal normalization logic in decodeWellData; noticed duplicated `decimal0 == 0` condition and confirmed via a Foundry test that decimal1=0 breaks calcLpTokenSupply.
**Insight:** When auditing decimal-normalization helpers, verify each branch checks its own variable; duplicated conditions in copy-pasted if-blocks silently skip defaulting and surface only as arithmetic panics in downstream math.

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

**Buggy decodeWellData decimal1 check**

```
function decodeWellData(bytes memory data) public view virtual returns (uint256[] memory decimals) {
        (uint256 decimal0, uint256 decimal1) = abi.decode(data, (uint256, uint256));

        // if well data returns 0, assume 18 decimals.
        if (decimal0 == 0) {
            decimal0 = 18;
        }
        if (decimal0 == 0) { //@audit
            decimal1 = 18;
        }
        if (decimal0 > 18 || decimal1 > 18) revert InvalidTokenDecimals();

        decimals = new uint256[](2);
        decimals[0] = decimal0;
        decimals[1] = decimal1;
    }
```

**Failing calcLpTokenSupply test trace**

```
[FAIL. Reason: panic: arithmetic underflow or overflow (0x11)] test_calcLpTokenSupply_sameDecimals2() (gas: 61838)
Traces:
  [61838] Stable2Test::test_calcLpTokenSupply_sameDecimals2()
    ├─ [4095] Stable2::calcLpTokenSupply([10000000000000000000 [1e19], 10000000000000000000 [1e19]], 0x00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000000) [staticcall]
    │   └─ ← [Revert] panic: arithmetic underflow or overflow (0x11)
```

