# Registry.sol#updateHoldingPosition remove position logic is incorrect: should use ownerConnector instead of calculatorConnector when calculating holdingPositionId

Registry.updateHoldingPosition recomputes the swapped position's ID with calculatorConnector instead of ownerConnector, corrupting the index map and zeroing TVL.

- Payout: Audit contest
- Severity: high
- Program: NOYA
- Found by: pkqs90
- Disclosed: 2025-02-17
- Write-up quality: 7/10
- Original: https://github.com/code-423n4/2024-04-noya-findings/issues/1093
- Library page: https://bugbountylibrary.com/r/c4%3A2024-04-noya%3AH-12
- Tags: area:web3 domain:web3 impact:integrity kind:contest platform:code4rena sev:high tech:solidity technique:source-review vuln:accounting-error vuln:business-logic

**Target:** Registry.sol updateHoldingPosition / vault holdingPositions array
**Root cause:** When swapping the last holdingPosition into a removed slot, the code recomputes isPositionUsed key using calculatorConnector rather than ownerConnector, so the moved position's index is never updated.
**Steps:**
1. Create 3 connectors [1,2,3], deal 10e6 USDC each; TVL=30e6.
2. Move all USDC from connector 1 to 3 via `transferPositionToAnotherConnector`; connector 1 removed, array becomes [3,2].
3. Move all USDC from connector 3 to 2; connector 3's stale positionIndex still points to old slot, so `pop()` removes connector 2 instead.
4. TVL now reads 0 despite connector 2 holding all USDC.
**How found:** Code review of the swap-and-pop branch in updateHoldingPosition, noticing the key recomputation used calculatorConnector while the original key used msg.sender (ownerConnector).
**Exploitation:** Any connector removal that is not the last index leaves a stale isPositionUsed mapping; subsequent removals pop the wrong position, corrupting the array and TVL accounting.
**Insight:** In swap-and-pop array patterns, verify the index-map key is recomputed with the exact same fields used at insertion; connector vs calculator address mismatch silently desyncs the map.

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

**Foundry test proving TVL drops to zero**

```
function testRemoveConnectorBug() public {
        vm.startPrank(owner);
        BaseConnector connector3;
        connector3 = new BaseConnector(BaseConnectorCP(registry, 0, swapHandler, noyaOracle));
        addConnectorToRegistry(vaultId, address(connector3));

        // Step 1: Deal 10e6 USDC tokens to all 3 connectors.
        uint256 amount = 10 * 1e6;
        _dealWhale(baseToken, address(connector), USDC_Whale, amount);
        _dealWhale(baseToken, address(connector2), USDC_Whale, amount);
        _dealWhale(baseToken, address(connector3), USDC_Whale, amount);
        vm.startPrank(owner);
        connector.updateTokenInRegistry(USDC);
        connector2.updateTokenInRegistry(USDC);
        connector3.updateTokenInRegistry(USDC);

        // Make sure the TVL is 30e6.
        uint tvl = accountingManager.TVL();
        assertEq(tvl, 30e6);

        // Step 2: Move all USDC from connector 1 to connector 3. TVL is still 30e6.
        address[] memory tokens = new address[](1);
        tokens[0] = USDC;
        uint256[] memory amounts = new uint256[](1);
        amounts[0] = amount;
        {
            connector.transferPositionToAnotherConnector(tokens, amounts, "", address(connector3));
            uint tvl = accountingManager.TVL();
            assertEq(tvl, 30e6);
        }

        // Step 3: Move all USDC from connector 3 to connector 2. TVL is now ZERO.
        {
            amounts[0] = 2 * amount;
            connector3.transferPositionToAnotherConnector(tokens, amounts, "", address(connector2));
            uint tvl = accountingManager.TVL();
            assertEq(tvl, 0);
        }
    }
```

