Repeated borrow-base sync adds gas to valid close commitments
A valid close commitment redundantly re-synchronises the borrow base after the settlement sidecar has already handled carry, so traders pay extra gas for no accounting change. No funds are at risk; the report is a gas-optimisation insight only.
- Disclosed after under an hour
#99454 [SC-Insight] Repeated borrow-base sync adds gas to valid close commitments
Submitted on Nov 13th 2026 at 18:16:31 UTC by @Psyone for Audit Competition | Plether
- Report ID: #99454
- Report Type: Smart Contract
- Report severity: Insight
- Target: https://github.com/immunefi-team/audit-comp-plether/tree/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src
- Impacts:
Description
Brief/Intro
A valid close commitment repeats borrow-base synchronization after its settlement sidecar has already collected carry. If carry consumes position margin, the carry path has updated the borrow base; otherwise the borrow-base inputs are unchanged. The final sync therefore adds trader-paid gas to an ordinary risk-reducing action without changing accounting. This is a code-optimization Insight; no fund loss is claimed.
Vulnerability Details
At the pinned revision, CfdEngine.reserveCloseOrderExecutionBounty delegates close admission to CfdEngineSettlementSidecar and then unconditionally calls _syncPositionBorrowBase(account, _positions[account]) at CfdEngine.sol:623. The sidecar plans the close, calls settlementRealizeCarry(account), and reads post-carry account buckets (CfdEngineSettlementSidecar.sol:325-352). It does not otherwise mutate position margin.
When accrued carry consumes margin, _realizeCarryFromSettlement already calls _syncPositionBorrowBaseToMargin(pos, marginBefore - marginConsumedAssets) at CfdEngine.sol:1428. When no margin is consumed, position margin and the stored borrow base stay unchanged. The outer call rereads the clearinghouse margin bucket, recomputes the same value, and updates side accounting and storage with no change.
The terminal-curve finalizer after this call remains necessary. The suggested optimization removes only the repeated borrow-base sync while preserving admission, carry collection, and _endTerminalCurveMutation.
Impact Details
Any trader with a live position pays this extra work when committing a valid close through the Router. In an independent pinned-revision replay, the existing public close test used 5,086,746 gas before the focused change and 5,083,669 gas after it: a 3,077-gas reduction. The test passed in both builds, and a separate zero-carry close control passed with the change. These are local test gas figures, not a deployed-transaction measurement. There is no demonstrated theft, freeze, or incorrect accounting; the selected impact is Code Optimizations and Enhancements Insight.
Mitigation
For the pinned settlement sidecar, remove the outer _syncPositionBorrowBase call after confirming that any margin-consuming carry branch synchronizes the borrow base before returning. Keep the terminal-curve mutation finalizer and all close checks. A different sidecar implementation should explicitly preserve that invariant.
References
- https://github.com/immunefi-team/audit-comp-plether/blob/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src/CfdEngine.sol#L598-L624
- https://github.com/immunefi-team/audit-comp-plether/blob/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src/CfdEngineSettlementSidecar.sol#L325-L352
- https://github.com/immunefi-team/audit-comp-plether/blob/a82d4fefb02768e5b3bddcc97dee371eee24fea8/packages/perps/src/CfdEngine.sol#L1407-L1449
Proof of Concept
Proof of Concept / condition of value
- At pinned commit a82d4fefb02768e5b3bddcc97dee371eee24fea8, fund a trader, open a live LONG, allow carry to accrue, and submit a valid close with OrderRouter.commitOrder. The repository's test_CloseCommit_FreshCarryCheckpointUsesPositionMargin in test/perps/spec/trader/OrderRouterCommitment.t.sol exercises this public path and asserts that carry reduces margin.
- Run: forge test --root packages/perps --match-path test/perps/spec/trader/OrderRouterCommitment.t.sol --match-test test_CloseCommit_FreshCarryCheckpointUsesPositionMargin --gas-report -vv
- In a disposable copy of the same revision, remove only this line after the settlement-sidecar call in CfdEngine.reserveCloseOrderExecutionBounty:
_syncPositionBorrowBase(account, _positions[account]);
- Re-run the same test. It should still pass its carry and margin assertions with lower gas. The unchanged _endTerminalCurveMutation call must remain. The zero-carry control test_DustFullResidualCloseCommit_Allowed also passed on the patched build. Its margin is unchanged, so this outer sync makes no accounting difference.
The improvement is valuable whenever a user commits a close for a live position; it does not depend on a malicious actor or unusual oracle data.
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 close-admission function unconditionally calls the borrow-base sync after the settlement sidecar, even though the carry path already syncs when margin is consumed and nothing changes otherwise.
Impact
Traders committing a valid close pay a small amount of extra gas (about 3,077 in local tests), with no theft, freeze, or incorrect accounting demonstrated.
What the attacker needed
A live open position; carry accrued on the position; a valid close commitment
The fix
The outer borrow-base sync should be removed once the margin-consuming carry branch is confirmed to sync before returning, keeping the terminal-curve finaliser and all close checks.
How it was found
- Fund a trader, open a live long position, let carry accrue, and submit a valid close through the order router.
- Run the existing close-commit test with the gas report enabled.
- In a disposable copy, remove only the outer borrow-base sync call after the sidecar call.
- Re-run the same test and confirm it still passes with lower gas.
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 a trader closes a position, the contract does some bookkeeping to keep track of borrowed amounts. This report shows that one of those bookkeeping steps runs twice, so the trader pays a little extra gas (the fee for running code on the blockchain) for nothing. It is a small efficiency issue, not a security hole.
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.