The curve of short leverage position is not smooth and may cause users to open positions that are different from expectations
The short leverage position calculation is not smooth: at 1.0x and 1.7x leverage the resulting position size is identical, and at 1.3x it is smaller than at 1.0x. This could cause users to open positions different from their expectations.
Submitted by cccz.
The current implementation allows users to open short leverage of 1x.
function _leverageCheck(uint256 _leverage, bool _long) internal pure {
uint256 _wad = WAD;
uint256 _leverageMax = WAD.mul(3);
_long // long can't be with 1x leverage
? require(
_leverage > _wad && _leverage <= _leverageMax,
"_leverageCheck{Long}: INVLAID LEVERAGE"
)
: require(
_leverage >= _wad && _leverage <= _leverageMax,
"_leverageCheck{Short}: INVLAID LEVERAGE"
);
}
When the user calls sellAllAmountWithLeverage with leverage == 1.0x , one 100% collateralization and borrowing is performed.
Consider the USDC collateralization factor of 0.7. The user uses 1000 USDC to short WBTC, leverage == 1.0x, the contract will collateralize 1000 USDC to borrow 700 USD WBTC, and then sell WBTC for 700 USDC, leaving the user with a position of 1700 USDC. When leverage == 1.3x, the contract borrows 300 USD worth of WBTC and then sells the WBTC for 300 USDC, leaving the user with a position of 1300 USDC. And when leverage == 1.7x, the contract borrows 700 USD worth of WBTC and then sells the WBTC for 700 USDC, leaving the user with a position of 1700 USDC. We note that when the leverage is 1.0x and 1.7x, the user’s position is the same, and the 1.3x position is smaller than the 1.0x position. This unsmoothed curve of short leverage position may cause the user to open a different position than expected.
Proof of Concept
The following POC shows that when the leverage is 1.0x and 1.7x, the user’s position is the same.
it("TESTME", async function () {
const { owner, testCoin, testStableCoin, Position } = await loadFixture(
deployPoolsUtilityFixture
);
await Position.connect(owner).sellAllAmountWithLeverage(
testStableCoin.address,
testCoin.address,
TUSDC_AMOUNT,
x1
);
// fetch position with id 1
const position = await Position.positions(1);
expect(await Position.owner()).to.equal(owner.address);
// position.asset == testStablecoin
expect(position[0]).to.equal(testStableCoin.address);
// position.quote == testCoin
expect(position[1]).to.equal(testCoin.address);
console.log("borrowedAmount : %s",position[2]);
});
it("TESTME2", async function () {
const { owner, testCoin, testStableCoin, Position } = await loadFixture(
deployPoolsUtilityFixture
);
const x1_7 = parseUnits("1.7");
await Position.connect(owner).sellAllAmountWithLeverage(
testStableCoin.address,
testCoin.address,
TUSDC_AMOUNT,
x1_7
);
// fetch position with id 1
const position = await Position.positions(1);
expect(await Position.owner()).to.equal(owner.address);
// position.asset == testStablecoin
expect(position[0]).to.equal(testStableCoin.address);
// position.quote == testCoin
expect(position[1]).to.equal(testCoin.address);
console.log("borrowedAmount : %s",position[2]);
});
Leverage positions Test
Pools Utility Test
Short positions 📉
borrowedAmount : 19444444444444444444
✓ TESTME
borrowedAmount : 19444444444444444444
✓ TESTME2
Recommended Mitigation Steps
Consider no longer supporting 1.0x short leverage.
Nice find!
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 leverage check allows 1.0x for shorts, and the borrowing calculation uses a formula that does not scale linearly with leverage, causing non-monotonic position sizes.
Impact
Users could open positions with different risk profiles than intended, potentially leading to unexpected losses or missed opportunities.
What the attacker needed
Access to the sellAllAmountWithLeverage function; sufficient collateral to open a position
The fix
The recommendation is to no longer support 1.0x short leverage.
How it was found
- Call sellAllAmountWithLeverage with leverage 1.0x and record the borrowed amount.
- Call sellAllAmountWithLeverage with leverage 1.7x and record the borrowed amount.
- Observe that both calls result in the same borrowed amount.
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.
In leveraged trading, you borrow money to increase your position size. The code that calculates how much to borrow for short positions has a flaw: the resulting position size does not increase smoothly with leverage. For example, using 1.0x and 1.7x leverage gives the same position size, which is unexpected and could lead to losses if users rely on the stated leverage.
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.