User that delegate eth to RdpxV2Core will incur loss if his delegated eth fulfilled by decaying bonds
The bondWithDelegate function allows users to use decaying bonds, which lack the usual discount, causing the delegatee to pay more ETH and receive fewer bond tokens than with non-decaying bonds. An attacker could front-run by withdrawing delegated assets before such a request, causing the delegatee to incur a loss.
Submitted by said, also found by peakbolt and HChang26
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L843-L847
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L699-L744
Current design allow user call bondWithDelegate using decaying bonds, this will make delegatee incur loss because they will get less bond and paid more eth.
Proof of Concept
When users have decaying bonds, they can either utilize it via bond or bondWithDelegate. If user choose to use bondWithDelegate, they need to provide _delegateIds (delegated eth that can fulfill the request).
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L819-L885
function bondWithDelegate(
address _to,
uint256[] memory _amounts,
uint256[] memory _delegateIds,
uint256 rdpxBondId
) public returns (uint256 receiptTokenAmount, uint256[] memory) {
_whenNotPaused();
// Validate amount
_validate(_amounts.length == _delegateIds.length, 3);
uint256 userTotalBondAmount;
uint256 totalBondAmount;
uint256[] memory delegateReceiptTokenAmounts = new uint256[](
_amounts.length
);
for (uint256 i = 0; i < _amounts.length; i++) {
// Validate amount
_validate(_amounts[i] > 0, 4);
BondWithDelegateReturnValue
memory returnValues = BondWithDelegateReturnValue(0, 0, 0, 0);
>>> returnValues = _bondWithDelegate(
_amounts[i],
rdpxBondId,
_delegateIds[i]
);
delegateReceiptTokenAmounts[i] = returnValues.delegateReceiptTokenAmount;
userTotalBondAmount += returnValues.bondAmountForUser;
totalBondAmount += _amounts[i];
// purchase options
uint256 premium;
if (putOptionsRequired) {
premium = _purchaseOptions(returnValues.rdpxRequired);
}
// transfer the required rdpx
_transfer(
returnValues.rdpxRequired,
returnValues.wethRequired - premium,
_amounts[i],
rdpxBondId
);
}
// ....
}
Then it will call _bondWithDelegate to calculate rdpxRequired and wethRequired and reduce delegatee provided collateral by increasing activeCollateral with wethRequired. Then calculate the bond received by calling _calculateAmounts, this will based on delegate.fee, wethRequired and rdpxRequired.
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L699-L744
function _bondWithDelegate(
uint256 _amount,
uint256 rdpxBondId,
uint256 delegateId
) internal returns (BondWithDelegateReturnValue memory returnValues) {
// Compute the bond cost
>>> (uint256 rdpxRequired, uint256 wethRequired) = calculateBondCost(
_amount,
rdpxBondId
);
// update ETH token reserve
reserveAsset[reservesIndex["WETH"]].tokenBalance += wethRequired;
Delegate storage delegate = delegates[delegateId];
// update delegate active collateral
_validate(delegate.amount - delegate.activeCollateral >= wethRequired, 5);
delegate.activeCollateral += wethRequired;
// update total weth delegated
totalWethDelegated -= wethRequired;
// Calculate the amount of bond token to mint for the delegate and user based on the fee
>>> (uint256 amount1, uint256 amount2) = _calculateAmounts(
wethRequired,
rdpxRequired,
_amount,
delegate.fee
);
// update user amounts
// ETH token amount remaining after LP for the user
uint256 bondAmountForUser = amount1;
// Mint bond token for delegate
// ETH token amount remaining after LP for the delegate
uint256 delegateReceiptTokenAmount = _stake(delegate.owner, amount2);
returnValues = BondWithDelegateReturnValue(
delegateReceiptTokenAmount,
bondAmountForUser,
rdpxRequired,
wethRequired
);
}
If user using decaying bonds, when call calculateBondCost and calculating rdpxRequired and wethRequired, it will not use any discount, this will make wethRequired even more bigger considering that if putOptionsRequired is true. delegatee need to paid premium that based on rdpxRequired (that not discounted).
https://github.com/code-423n4/2023-08-dopex/blob/main/contracts/core/RdpxV2Core.sol#L1156-L1199
function calculateBondCost(
uint256 _amount,
uint256 _rdpxBondId
) public view returns (uint256 rdpxRequired, uint256 wethRequired) {
uint256 rdpxPrice = getRdpxPrice();
if (_rdpxBondId == 0) {
uint256 bondDiscount = (bondDiscountFactor *
Math.sqrt(IRdpxReserve(addresses.rdpxReserve).rdpxReserve()) *
1e2) / (Math.sqrt(1e18)); // 1e8 precision
_validate(bondDiscount < 100e8, 14);
rdpxRequired =
((RDPX_RATIO_PERCENTAGE - (bondDiscount / 2)) *
_amount *
DEFAULT_PRECISION) /
(DEFAULT_PRECISION * rdpxPrice * 1e2);
wethRequired =
((ETH_RATIO_PERCENTAGE - (bondDiscount / 2)) * _amount) /
(DEFAULT_PRECISION * 1e2);
} else {
// if rdpx decaying bonds are being used there is no discount
rdpxRequired =
(RDPX_RATIO_PERCENTAGE * _amount * DEFAULT_PRECISION) /
(DEFAULT_PRECISION * rdpxPrice * 1e2);
>>> wethRequired =
(ETH_RATIO_PERCENTAGE * _amount) /
(DEFAULT_PRECISION * 1e2);
}
uint256 strike = IPerpetualAtlanticVault(addresses.perpetualAtlanticVault)
.roundUp(rdpxPrice - (rdpxPrice / 4)); // 25% below the current price
uint256 timeToExpiry = IPerpetualAtlanticVault(
addresses.perpetualAtlanticVault
).nextFundingPaymentTimestamp() - block.timestamp;
if (putOptionsRequired) {
>>> wethRequired += IPerpetualAtlanticVault(addresses.perpetualAtlanticVault)
.calculatePremium(strike, rdpxRequired, timeToExpiry, 0);
}
}
This means, delegatee need to paid undiscounted wethRequired plus undiscounted premium!
This will make delegatee would not want to fulfill bondWithDelegate when decaying bond is used. Delegatee can front-run by withdrawing his delegated assets before bondWithDelegate with using decaying bonds calls.
PoC Foundry :
Comparing fulfilling bondWithDelegate requests in 2 scenarios, with decaying bonds and non-decaying bonds.
Add this test to Unit contract inside /tests/rdpxV2-core/Unit.t.sol, also add import "forge-std/console.sol"; in the contract :
function testBondWithDelegateCompare() public {
address alice = makeAddr("alice");
rdpx.mint(alice, 1000 * 1e18);
// test with rdpx decaying bonds
rdpxDecayingBonds.grantRole(
rdpxDecayingBonds.MINTER_ROLE(),
address(this)
);
rdpxDecayingBonds.mint(alice, block.timestamp + 10, 125 * 1e16);
assertEq(rdpxDecayingBonds.ownerOf(1), alice);
/// add to delegate with different fees
uint256 delegateId = rdpxV2Core.addToDelegate(50 * 1e18, 10 * 1e8);
// uint256 delgateId2 = rdpxV2Core.addToDelegate(10 * 1e18, 20 * 1e8);
// test bond with delegate
uint256[] memory _amounts = new uint256[](1);
uint256[] memory _delegateIds = new uint256[](1);
_delegateIds[0] = 0;
_amounts[0] = 1 * 1e18;
// address 1 bonds
/// check user balance
uint256 userRdpxBalance = rdpx.balanceOf(alice);
uint256 userWethBalance = weth.balanceOf(alice);
(uint256 rdpxRequired, uint256 wethRequired) = rdpxV2Core
.calculateBondCost(1 * 1e18, 0);
vm.startPrank(alice);
rdpx.approve(address(rdpxV2Core), type(uint256).max);
(uint256 userAmount, uint256[] memory delegateAmounts) = rdpxV2Core
.bondWithDelegate(alice, _amounts, _delegateIds, 0);
vm.stopPrank();
console.log("weth paid without decaying bond");
console.log(wethRequired);
console.log("delegatee received amount without decaying bond");
console.log(delegateAmounts[0]);
vm.startPrank(alice);
rdpx.approve(address(rdpxV2Core), type(uint256).max);
// with decaying bond
(uint256 rdpxRequired2, uint256 wethRequired2) = rdpxV2Core
.calculateBondCost(1 * 1e18, 1);
(uint256 userAmount2, uint256[] memory delegateAmounts2) = rdpxV2Core
.bondWithDelegate(alice, _amounts, _delegateIds, 1);
vm.stopPrank();
console.log("weth paid with decaying bond");
console.log(wethRequired2);
console.log("delegatee received amount with decaying bond");
console.log(delegateAmounts2[0]);
}
Run the test :
forge test --match-contract Unit --match-test testBondWithDelegateCompare -vvv
Test Output :
Logs:
weth paid without decaying bond
806250000000000000
delegatee received amount without decaying bond
197562425683709869
weth paid with decaying bond
812500000000000000
delegatee received amount with decaying bond
197058823529411765
It can be observed delegatee paid weth more with decaying bond and receive less bond share.
Recommended Mitigation Steps
Consider to restrict bondWithDelegate for only non-decaying bonds. Or also add discount but apply only to the weth part if called from bondWithDelegate.
Alex the Entreprenerd (Judge) commented:
Incur Loss == Mispriced
Making primary because this warden sent the POC before any request.
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
Likely the bondWithDelegate function does not apply the discount for decaying bonds when calculating the required ETH and RDPX, leading to higher costs for the delegatee.
Impact
The delegatee incurs a loss because they pay more ETH and receive fewer bond tokens when fulfilling a bondWithDelegate request that uses a decaying bond.
What the attacker needed
User has a decaying bond; delegatee has delegated ETH; delegatee can front-run
The fix
The bondWithDelegate function should be restricted to only non-decaying bonds, or a discount should be applied to the WETH part when called from bondWithDelegate.
How it was found
- A user with a decaying bond calls bondWithDelegate, specifying a delegate ID.
- The contract calculates the bond cost without applying the decaying bond discount, resulting in higher wethRequired.
- The delegatee's collateral is increased by the higher wethRequired, and they receive fewer bond tokens.
- The delegatee can front-run by withdrawing their delegated assets before the bondWithDelegate call, avoiding the loss.
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.
When someone delegates their ETH to help others bond, they expect a fair deal. But if the person bonding uses a special 'decaying bond' that doesn't get the normal discount, the delegate ends up paying more and getting less. This means the delegate loses out, and they might try to withdraw their ETH before the bond happens to avoid the loss.
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.