Funds are not properly refunded to user which calls for swap on the dex aggregator
The dex aggregator wrapper only refunds leftover native ETH after a swap, but the underlying router can return unspent ERC20 source tokens to the wrapper contract on partial fills. Those ERC20 tokens stay stuck in the contract and are never forwarded to the user, so funds can be permanently frozen.
Plume Network
Originally disclosed on Immunefi
53022 sc critical funds are not properly refunded to user which calls for swap on the dex aggregator
- Submitted on: Aug 14th 2025 at 17:13:54 UTC by @valkvalue for Attackathon | Plume Network
- Report ID: #53022
- Report Type: Smart Contract
- Report severity: Critical
- Target: https://github.com/immunefi-team/attackathon-plume-network-nucleus-boring-vault/blob/main/src/helper/DexAggregatorWrapperWithPredicateProxy.sol
- Impacts: Permanent freezing of funds
Description
Brief / Intro
Funds are not properly refunded to a user who initiates a swap via the dex aggregator.
Vulnerability Details
The contract attempts to refund leftover ETH/native token balance, but it does not account for refunds that originate from the swap operation itself when those refunds are returned as ERC20 tokens. The code only handles native ETH refunds and has no logic to capture and re-send ERC20 tokens accidentally left in the aggregator contract by the routed swap.
Relevant excerpt from the 0x (Ork) dex router's swap implementation:
function swap(
IAggregationExecutor executor,
SwapDescription calldata desc,
bytes calldata data
)
external
payable
whenNotPaused()
returns (
uint256 returnAmount,
uint256 spentAmount
)
{
if (desc.minReturnAmount == 0) revert ZeroMinReturn();
IERC20 srcToken = desc.srcToken;
IERC20 dstToken = desc.dstToken;
bool srcETH = srcToken.isETH();
if (desc.flags & _REQUIRES_EXTRA_ETH != 0) {
if (msg.value <= (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
} else {
if (msg.value != (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
}
if (!srcETH) {
srcToken.safeTransferFromUniversal(msg.sender, desc.srcReceiver, desc.amount, desc.flags & _USE_PERMIT2 != 0);
}
returnAmount = _execute(executor, msg.sender, desc.amount, data);
spentAmount = desc.amount;
if (desc.flags & _PARTIAL_FILL != 0) {
uint256 unspentAmount = srcToken.uniBalanceOf(address(this));
if (unspentAmount > 1) {
// we leave 1 wei on the router for gas optimisations reasons
unchecked { unspentAmount--; }
spentAmount -= unspentAmount;
srcToken.uniTransfer(payable(msg.sender), unspentAmount);
}
if (returnAmount * desc.amount < desc.minReturnAmount * spentAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount * spentAmount / desc.amount);
} else {
if (returnAmount < desc.minReturnAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount);
}
address payable dstReceiver = (desc.dstReceiver == address(0)) ? payable(msg.sender) : desc.dstReceiver;
dstToken.uniTransfer(dstReceiver, returnAmount);
}
Note: when the order is partially filled, the router returns unspent srcToken to msg.sender (the dex aggregator contract in this flow). The aggregator contract's only refund logic is:
function _refundExcessEth(address payable _recipient) internal {
uint256 balance = address(this).balance;
if (balance > 0) {
(bool success,) = _recipient.call{ value: balance }("");
if (!success) {
revert DexAggregatorWrapper__EthRefundFailed();
}
}
// If balance is 0, do nothing.
}
This only handles native ETH. It does not handle ERC20 srcToken refunds that the router may have transferred to the aggregator contract, so those ERC20 tokens can remain stuck inside the aggregator indefinitely.
Impact Details
Permanent funds stuck in the aggregator contract — users' leftover ERC20 tokens (from partial fills or other swap-side refunds) are not returned and thus become unrecoverable through the intended flow.
References
- https://vscode.blockscan.com/42161/0x111111125421ca6dc452d289314280a0f8842a65
- https://vscode.blockscan.com/ethereum/0x2E1Dee213BA8d7af0934C49a23187BabEACa8764
{% hint style="danger" %} Severity: Critical — permanent freezing of user funds is possible. {% endhint %}
Proof of Concept
{% stepper %} {% step %}
Reproduce flow
- User calls one of the wrapper functions in DexAggregatorWrapperWithPredicateProxy:
- depositAndBridgeOkxUniversal
- depositOkxUniversal
- depositOneInch
- depositAndBridgeOneInch {% endstep %}
{% step %}
Partial fill or other swap-side refund occurs
- The dex router executes a swap that results in an unspent amount of the src token being transferred back to msg.sender (the aggregator contract).
- Those refunded tokens are ERC20 tokens, not native ETH. {% endstep %}
{% step %}
Aggregator refund logic
- The aggregator attempts to refund leftover ETH/native token via _refundExcessEth, but there is no logic to detect or transfer ERC20 tokens that were returned from the router to the aggregator. {% endstep %}
{% step %}
Result
- ERC20 tokens remain stuck in the aggregator contract and are not returned to the user, leading to permanently frozen funds. {% endstep %} {% endstepper %}
Suggested Remediation (high level)
- Detect and forward any ERC20 token balances that belong to the user (or were part of the swap flow) back to the user when appropriate, similar to how ETH refunds are handled.
- After a swap call, check balances of relevant ERC20 tokens (srcToken and possibly intermediary tokens) and transfer any unexpected balances to the intended recipient.
- Ensure the router's behavior (especially when using flags like PARTIAL_FILL) is accounted for: the aggregator should not assume only ETH/native refunds are possible.
- Consider explicit handling for all tokens involved in the swap (srcToken, dstToken, and any tokens the executor may refund) to avoid leaving tokens in the aggregator.
(Do not add any behavior not described here without further design review — the above are high-level mitigations derived from the observed issue.)
Context
The target is DexAggregatorWrapperWithPredicateProxy.sol, a helper contract in the Nucleus boring vault repository used during the Plume Network attackathon. The contract wraps calls to third-party DEX aggregators so that a user can deposit and, in some flows, bridge assets in a single transaction. The wrapper exposes several entry points that route a swap through an external aggregator router, and it contains a helper that returns leftover native ETH to the caller once the swap completes.
The report focuses on one specific responsibility of that wrapper: refunding the user. The contract contains a refund helper that returns leftover native ETH, but nothing that returns ERC20 tokens left behind by the routed swap. The impact category claimed is permanent freezing of funds, rated Critical.
Setup
The report does not describe any accounts, roles, tooling or versions used to reproduce the issue. The only concrete artefacts are the target file, two block explorer references for the router implementation, and the Solidity excerpts quoted below. The relevant precondition is structural rather than environmental: the swap must be routed through a router whose swap implementation can send unspent source tokens back to msg.sender, which in this flow is the aggregator wrapper contract.
Finding it
The researcher compared the router's swap implementation with the wrapper's refund logic. The router, described in the report as the 0x (Ork) DEX router, has a partial fill path that returns unspent source tokens to the caller:
function swap(
IAggregationExecutor executor,
SwapDescription calldata desc,
bytes calldata data
)
external
payable
whenNotPaused()
returns (
uint256 returnAmount,
uint256 spentAmount
)
{
if (desc.minReturnAmount == 0) revert ZeroMinReturn();
IERC20 srcToken = desc.srcToken;
IERC20 dstToken = desc.dstToken;
bool srcETH = srcToken.isETH();
if (desc.flags & _REQUIRES_EXTRA_ETH != 0) {
if (msg.value <= (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
} else {
if (msg.value != (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
}
if (!srcETH) {
srcToken.safeTransferFromUniversal(msg.sender, desc.srcReceiver, desc.amount, desc.flags & _USE_PERMIT2 != 0);
}
returnAmount = _execute(executor, msg.sender, desc.amount, data);
spentAmount = desc.amount;
if (desc.flags & _PARTIAL_FILL != 0) {
uint256 unspentAmount = srcToken.uniBalanceOf(address(this));
if (unspentAmount > 1) {
// we leave 1 wei on the router for gas optimisations reasons
unchecked { unspentAmount--; }
spentAmount -= unspentAmount;
srcToken.uniTransfer(payable(msg.sender), unspentAmount);
}
if (returnAmount * desc.amount < desc.minReturnAmount * spentAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount * spentAmount / desc.amount);
} else {
if (returnAmount < desc.minReturnAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount);
}
address payable dstReceiver = (desc.dstReceiver == address(0)) ? payable(msg.sender) : desc.dstReceiver;
dstToken.uniTransfer(dstReceiver, returnAmount);
}
The important branch is the _PARTIAL_FILL one. When the order is only partially filled, the router measures its own balance of srcToken, subtracts one wei (kept on the router for gas optimisation reasons), reduces spentAmount accordingly, and transfers the remainder back to msg.sender. In this flow msg.sender is the aggregator wrapper contract, not the end user.
The wrapper's only refund routine is:
function _refundExcessEth(address payable _recipient) internal {
uint256 balance = address(this).balance;
if (balance > 0) {
(bool success,) = _recipient.call{ value: balance }("");
if (!success) {
revert DexAggregatorWrapper__EthRefundFailed();
}
}
// If balance is 0, do nothing.
}
This reads address(this).balance, which is the native ETH balance only, and forwards it to the recipient. There is no equivalent read of an ERC20 balance and no transfer of any token. So when the router refunds an ERC20 srcToken, those tokens land in the wrapper and stay there.
Exploitation
The report frames this as a reproduction flow rather than an attack with an adversary. The steps below follow it exactly.
Step 1: Reproduce flow
A user calls one of the wrapper functions in DexAggregatorWrapperWithPredicateProxy:
depositAndBridgeOkxUniversaldepositOkxUniversaldepositOneInchdepositAndBridgeOneInch
Each of these routes a swap through the external aggregator, with the wrapper contract as the caller of the router.
Step 2: Partial fill or other swap-side refund occurs
The DEX router executes a swap that results in an unspent amount of the source token being transferred back to msg.sender, which is the aggregator contract. Those refunded tokens are ERC20 tokens, not native ETH. The _PARTIAL_FILL branch shown above is the concrete path: the router computes unspentAmount from its own srcToken balance, decrements it by one wei, and calls srcToken.uniTransfer(payable(msg.sender), unspentAmount).
Step 3: Aggregator refund logic
The aggregator attempts to refund leftover ETH or native token through _refundExcessEth, but there is no logic to detect or transfer ERC20 tokens that were returned from the router to the aggregator. The helper only inspects address(this).balance and only sends native value.
Step 4: Result
The ERC20 tokens remain stuck in the aggregator contract and are not returned to the user, which the report describes as permanently frozen funds.
Root cause
The wrapper assumes that any leftover value after a swap is native ETH. That assumption holds for the ETH refund path but not for the router's partial fill path, which refunds the source token as an ERC20 balance transfer to the caller. Because the wrapper never inspects ERC20 balances after the swap and never transfers them out, the refunded amount has no route back to the user. The existing _refundExcessEth defence does not cover this case at all: it is scoped to address(this).balance, so it cannot see or move tokens.
Impact
Users' leftover ERC20 tokens, arising from partial fills or other swap-side refunds, are not returned and become unrecoverable through the intended flow. The report classifies this as permanent freezing of user funds and rates it Critical. The condition is that the routed swap produces an ERC20 refund to the wrapper, for example when the router's _PARTIAL_FILL flag is set and the fill is incomplete.
Fix
The report gives high-level remediation rather than a patch, and explicitly asks that no behaviour beyond what is described be added without further design review. The suggested measures are:
- Detect and forward any ERC20 token balances that belong to the user, or that were part of the swap flow, back to the user when appropriate, in the same spirit as the ETH refund handling.
- After a swap call, check balances of the relevant ERC20 tokens (
srcTokenand possibly intermediary tokens) and transfer any unexpected balances to the intended recipient. - Account for the router's behaviour, especially when flags such as
PARTIAL_FILLare in use: the aggregator should not assume that only ETH or native refunds are possible. - Consider explicit handling for all tokens involved in the swap (
srcToken,dstToken, and any tokens the executor may refund) so that nothing is left in the aggregator.
Variants to test
- Other wrapper entry points on the same contract. Call each of
depositAndBridgeOkxUniversal,depositOkxUniversal,depositOneInchanddepositAndBridgeOneInchwith a swap that triggers a partial fill. A positive result is an ERC20 balance left in the wrapper after the transaction that the user cannot recover. - Other aggregator routers with a partial fill mode. Look for routers that return unspent source tokens to
msg.senderrather than to the original user. A positive result is the same pattern: the wrapper's refund logic only handles native value, so the token refund is stranded. - Intermediary token refunds. The report suggests considering any tokens the executor may refund, not just
srcToken. A positive result is any token balance, not just the source token, sitting in the wrapper after a completed swap. - Native versus ERC20 source tokens. Compare a swap where the source is native ETH with one where the source is an ERC20. A positive result is that the ETH path is refunded correctly while the ERC20 path is not, confirming the gap is specific to token refunds.
Hunting notes
This kind of bug hides in the seam between a wrapper contract and the external router it calls. The wrapper author reasons about the router's happy path and about native value, and misses the branches where the router returns assets to its caller. During review, the signal is a refund helper that reads only address(this).balance while the contract also handles ERC20 flows. Any contract that acts as msg.sender to a third-party router and then settles with the user is worth checking for the same asymmetry: enumerate every path in the router that transfers something back to the caller, then check whether the wrapper has a corresponding path to forward it.
The report ties the stranded balance to a concrete router branch (_PARTIAL_FILL) and to named user-facing entry points, and shows that the only refund routine cannot reach ERC20 balances. That combination, a reachable refund path plus a refund routine that cannot handle it, is the basis for the permanent freezing claim.
What happened, in plain words
A user of the Plume Network can deposit money through a helper program (a smart contract) that talks to an outside swapping service, and in some flows bridge it. Sometimes that outside service does not use all of the money the user offered, and it sends the unused part back. The helper program only knows how to send back the leftover in one particular form of money (the network's own coin, called ETH here). When the leftover comes back in a different form (a token), the helper has no code to pass it on to the user. The tokens sit in the helper forever, and the user cannot get them back through the normal route. The report rates this Critical because money is permanently frozen.
To follow the rest of this article you only need a browser and some patience. Every technical word is explained where it first appears, and there is a glossary at the end.
Background: the pieces involved
Before looking at the bug, here is what each piece is, in everyday terms.
- Smart contract: a small program that lives on a blockchain. A blockchain is a shared ledger that many computers keep copies of, so once a program is published and running, nobody can quietly change what it does. Think of a vending machine bolted to the pavement: anyone can use it, and its rules are fixed in advance.
- Solidity: the programming language used to write these programs. The code excerpts below are Solidity.
- ETH and native token: the blockchain's own built-in money. "Native" means it is part of the network itself, not something issued on top of it. It behaves a bit like cash in your pocket: the network can move it directly.
- ERC20 token: money issued on top of a blockchain by a project. It is not the network's own coin; it is a separate entry in a ledger that the network keeps for that project. The everyday comparison is a shop voucher versus cash: both have value, but the shop's till handles them differently. A contract cannot send an ERC20 token by simply attaching it to a call; it has to ask the token's own contract to move the balance.
- wei: the smallest unit of ETH. One ETH is a very large number of wei. Saying "one wei" is like saying "one penny", the smallest indivisible amount.
- DEX aggregator: a service that finds the best place to swap one token for another and routes the trade there. Think of a price comparison site that also places the order for you.
- Router: the contract inside the aggregator that actually carries out the swap. It receives the request, does the trade, and hands back the result.
- Wrapper contract: a contract that sits in front of another service and makes it easier to use. Here,
DexAggregatorWrapperWithPredicateProxy.solis a wrapper that lets a user deposit money and, in some flows, move it to another network ("bridge" it) in a single transaction, while the actual swapping is done by the outside aggregator. msg.sender: in Solidity, the address of whoever called the current function. It matters enormously here: when the wrapper calls the router, the router sees the wrapper asmsg.sender, not the human user. So anything the router sends "back to the caller" goes to the wrapper, not to the person.address(this): the contract's own address.address(this).balancemeans "how much native ETH does this contract currently hold". It says nothing about ERC20 tokens.- Partial fill: when a trade is only partly completed. If you ask to swap 100 units and only 80 can be matched, the remaining 20 are unspent. A well-behaved router gives the 20 back.
- Permanent freezing of funds: a security impact category meaning money is stuck in a contract with no legitimate way for its owner to retrieve it. It is not stolen, but it is lost to the user just the same.
Context
The target is DexAggregatorWrapperWithPredicateProxy.sol, a helper contract in the Nucleus boring vault repository, reviewed during the Plume Network attackathon. An attackathon is a time-boxed public competition where security researchers look for bugs in a project's code and report them.
The wrapper's job is to take a user's request, pass it to a third-party DEX aggregator, and then settle up with the user: it contains a helper that returns leftover native ETH to the person who asked for it. The report focuses on exactly that settling-up duty. The contract contains a refund helper that returns leftover native ETH, but nothing that returns ERC20 tokens left behind by the routed swap. The claimed impact is permanent freezing of funds, rated Critical.
Setup
The report does not describe any accounts, roles, tooling or versions used to reproduce the issue. The only concrete artefacts are the target file, two block explorer references for the router implementation, and the Solidity excerpts quoted below. A block explorer is a website that lets you look up contracts and transactions on a blockchain, like an online public register.
The precondition for the bug is structural rather than environmental: the swap must be routed through a router whose swap implementation can send unspent source tokens back to msg.sender, which in this flow is the aggregator wrapper contract. In other words, you do not need a special server or a special account; you need the right kind of trade to happen through the right kind of router.
Finding it
The researcher compared the router's swap implementation with the wrapper's refund logic. The router, described in the report as the 0x (Ork) DEX router, has a partial fill path that returns unspent source tokens to the caller. Here is that router code:
function swap(
IAggregationExecutor executor,
SwapDescription calldata desc,
bytes calldata data
)
external
payable
whenNotPaused()
returns (
uint256 returnAmount,
uint256 spentAmount
)
{
if (desc.minReturnAmount == 0) revert ZeroMinReturn();
IERC20 srcToken = desc.srcToken;
IERC20 dstToken = desc.dstToken;
bool srcETH = srcToken.isETH();
if (desc.flags & _REQUIRES_EXTRA_ETH != 0) {
if (msg.value <= (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
} else {
if (msg.value != (srcETH ? desc.amount : 0)) revert RouterErrors.InvalidMsgValue();
}
if (!srcETH) {
srcToken.safeTransferFromUniversal(msg.sender, desc.srcReceiver, desc.amount, desc.flags & _USE_PERMIT2 != 0);
}
returnAmount = _execute(executor, msg.sender, desc.amount, data);
spentAmount = desc.amount;
if (desc.flags & _PARTIAL_FILL != 0) {
uint256 unspentAmount = srcToken.uniBalanceOf(address(this));
if (unspentAmount > 1) {
// we leave 1 wei on the router for gas optimisations reasons
unchecked { unspentAmount--; }
spentAmount -= unspentAmount;
srcToken.uniTransfer(payable(msg.sender), unspentAmount);
}
if (returnAmount * desc.amount < desc.minReturnAmount * spentAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount * spentAmount / desc.amount);
} else {
if (returnAmount < desc.minReturnAmount) revert RouterErrors.ReturnAmountIsNotEnough(returnAmount, desc.minReturnAmount);
}
address payable dstReceiver = (desc.dstReceiver == address(0)) ? payable(msg.sender) : desc.dstReceiver;
dstToken.uniTransfer(dstReceiver, returnAmount);
}
Line by line, in plain words:
function swap(...)declares the router's swap function. It takes three inputs: anexecutor(the component that actually performs the trade), adesc(a description of the swap: which token goes in, which comes out, how much, and the minimum acceptable result), anddata(extra instructions passed along).external payablemeans anyone outside the contract can call it, and it can receive native ETH along with the call.whenNotPaused()is a guard: if the router has been paused, the function refuses to run.returns (uint256 returnAmount, uint256 spentAmount)says the function reports two numbers back: how much the user got out, and how much of the input was actually used.if (desc.minReturnAmount == 0) revert ZeroMinReturn();refuses a swap that sets no minimum. A minimum protects the user from a terrible exchange rate; setting it to zero would remove that protection.IERC20 srcToken = desc.srcToken;andIERC20 dstToken = desc.dstToken;pull out the input token and the output token from the description.bool srcETH = srcToken.isETH();asks whether the input is the network's own coin rather than an ERC20 token. This distinction drives everything that follows.- The next
if/elseblock checks how much ETH was attached to the call (msg.value). Depending on a flag called_REQUIRES_EXTRA_ETH, it either requires the attached amount to be at least the swap amount (when the input is ETH) or exactly equal to it. If the amount is wrong, the call is rejected withInvalidMsgValue. This is a sanity check on the money handed over. if (!srcETH) { srcToken.safeTransferFromUniversal(...); }handles the case where the input is an ERC20 token: the router pulls the tokens from the caller into the designated receiver. Note the first argument:msg.sender. The router takes the tokens from whoever called it, which in our flow is the wrapper.returnAmount = _execute(...)performs the actual trade and records how much came out.spentAmount = desc.amount;initially assumes the whole requested amount was used.- The
if (desc.flags & _PARTIAL_FILL != 0)block is the heart of the issue. It runs only when the partial fill flag is set. Inside,srcToken.uniBalanceOf(address(this))asks: how many of the input tokens does the router itself currently hold? Whatever is sitting there is money that was not used for the trade. if (unspentAmount > 1)means: if there is more than one wei of leftover, do something about it. The comment explains that one wei is deliberately kept on the router to save on transaction fees ("gas").unchecked { unspentAmount--; }subtracts that one wei from the leftover figure.spentAmount -= unspentAmount;corrects the record of how much was really spent.srcToken.uniTransfer(payable(msg.sender), unspentAmount);sends the leftover tokens back to the caller. This is the crucial line: the caller is the wrapper contract, so the tokens go to the wrapper, not to the human user.- The following
ifchecks the trade met the user's minimum acceptable result, scaled to the amount actually spent, and rejects the swap if not. Theelsebranch does the same check for a full fill. - Finally, the router works out who should receive the output (
dstReceiver, defaulting to the caller if none was given) and sends the output tokens there withdstToken.uniTransfer(dstReceiver, returnAmount);.
So the router does its job correctly: it returns unused input to whoever called it. The problem is what the caller does with that refund.
The wrapper's only refund routine is:
function _refundExcessEth(address payable _recipient) internal {
uint256 balance = address(this).balance;
if (balance > 0) {
(bool success,) = _recipient.call{ value: balance }("");
if (!success) {
revert DexAggregatorWrapper__EthRefundFailed();
}
}
// If balance is 0, do nothing.
}
Line by line:
function _refundExcessEth(address payable _recipient) internaldeclares an internal helper (only callable from inside the contract) that takes the address to refund.uint256 balance = address(this).balance;reads the contract's own native ETH balance. This is the key limitation: it counts ETH only. ERC20 tokens are tracked by separate token contracts and do not appear in this number at all.if (balance > 0)means: only do anything if there is ETH to send.(bool success,) = _recipient.call{ value: balance }("");sends the whole ETH balance to the recipient and records whether the send worked.if (!success) { revert DexAggregatorWrapper__EthRefundFailed(); }cancels the whole transaction with a named error if the send failed, so a failed refund cannot be silently ignored.- The comment notes that a zero balance means nothing to do.
There is no equivalent read of an ERC20 balance and no transfer of any token. So when the router refunds an ERC20 srcToken, those tokens land in the wrapper and stay there.
Exploitation
The report frames this as a reproduction flow rather than an attack with an adversary. Nobody has to be malicious: an ordinary user doing an ordinary swap can trigger it. The steps below follow the report exactly.
Step 1: Reproduce flow
A user calls one of the wrapper functions in DexAggregatorWrapperWithPredicateProxy:
depositAndBridgeOkxUniversaldepositOkxUniversaldepositOneInchdepositAndBridgeOneInch
Each of these routes a swap through the external aggregator, with the wrapper contract as the caller of the router. The names hint at what they do: "deposit" puts money in, "bridge" moves it to another network, and "Okx" and "OneInch" are the aggregator services being used. The user picks one of these entry points; the wrapper then calls the router on the user's behalf.
Step 2: Partial fill or other swap-side refund occurs
The DEX router executes a swap that results in an unspent amount of the source token being transferred back to msg.sender, which is the aggregator contract. Those refunded tokens are ERC20 tokens, not native ETH. The _PARTIAL_FILL branch shown above is the concrete path: the router computes unspentAmount from its own srcToken balance, decrements it by one wei, and calls srcToken.uniTransfer(payable(msg.sender), unspentAmount).
Why does a partial fill happen at all? Markets do not always have enough of the other side of a trade available at the price the user will accept. Rather than fail the whole order, the router can complete part of it and return the rest. That is a normal, intended behaviour, which is why the bug is easy to miss: the router is doing exactly what it was designed to do.
Step 3: Aggregator refund logic
The aggregator attempts to refund leftover ETH or native token through _refundExcessEth, but there is no logic to detect or transfer ERC20 tokens that were returned from the router to the aggregator. The helper only inspects address(this).balance and only sends native value. It is like a shop assistant who checks the cash drawer for change but never looks at the vouchers sitting next to it.
Step 4: Result
The ERC20 tokens remain stuck in the aggregator contract and are not returned to the user, which the report describes as permanently frozen funds. Because the wrapper has no logic that moves those tokens out, the money is unreachable through the intended flow.
Root cause
The wrapper assumes that any leftover value after a swap is native ETH. That assumption holds for the ETH refund path but not for the router's partial fill path, which refunds the source token as an ERC20 balance transfer to the caller. Because the wrapper never inspects ERC20 balances after the swap and never transfers them out, the refunded amount has no route back to the user. The existing _refundExcessEth defence does not cover this case at all: it is scoped to address(this).balance, so it cannot see or move tokens.
A useful comparison: imagine a cloakroom that returns coats but has no shelf for umbrellas. If a guest hands over an umbrella, it goes on the floor and is forgotten. The cloakroom is not broken for coats; it simply never accounted for a second kind of item.
Impact
Users' leftover ERC20 tokens, arising from partial fills or other swap-side refunds, are not returned and become unrecoverable through the intended flow. The report classifies this as permanent freezing of user funds and rates it Critical. The condition is that the routed swap produces an ERC20 refund to the wrapper, for example when the router's _PARTIAL_FILL flag is set and the fill is incomplete.
Fix
The report gives high-level remediation rather than a patch, and explicitly asks that no behaviour beyond what is described be added without further design review. The suggested measures are:
- Detect and forward any ERC20 token balances that belong to the user, or that were part of the swap flow, back to the user when appropriate, in the same spirit as the ETH refund handling.
- After a swap call, check balances of the relevant ERC20 tokens (
srcTokenand possibly intermediary tokens) and transfer any unexpected balances to the intended recipient. - Account for the router's behaviour, especially when flags such as
PARTIAL_FILLare in use: the aggregator should not assume that only ETH or native refunds are possible. - Consider explicit handling for all tokens involved in the swap (
srcToken,dstToken, and any tokens the executor may refund) so that nothing is left in the aggregator.
In plain terms: after every swap, look at what is actually sitting in the contract, not just the ETH, and send anything that does not belong there to the person it belongs to.
Variants to test
These are follow-up checks a reviewer could run to see how widespread the pattern is.
- Other wrapper entry points on the same contract. Call each of
depositAndBridgeOkxUniversal,depositOkxUniversal,depositOneInchanddepositAndBridgeOneInchwith a swap that triggers a partial fill. A positive result is an ERC20 balance left in the wrapper after the transaction that the user cannot recover. - Other aggregator routers with a partial fill mode. Look for routers that return unspent source tokens to
msg.senderrather than to the original user. A positive result is the same pattern: the wrapper's refund logic only handles native value, so the token refund is stranded. - Intermediary token refunds. The report suggests considering any tokens the executor may refund, not just
srcToken. A positive result is any token balance, not just the source token, sitting in the wrapper after a completed swap. - Native versus ERC20 source tokens. Compare a swap where the source is native ETH with one where the source is an ERC20. A positive result is that the ETH path is refunded correctly while the ERC20 path is not, confirming the gap is specific to token refunds.
Hunting notes
This kind of bug hides in the seam between a wrapper contract and the external router it calls. The wrapper author reasons about the router's happy path and about native value, and misses the branches where the router returns assets to its caller. During review, the signal is a refund helper that reads only address(this).balance while the contract also handles ERC20 flows. Any contract that acts as msg.sender to a third-party router and then settles with the user is worth checking for the same asymmetry: enumerate every path in the router that transfers something back to the caller, then check whether the wrapper has a corresponding path to forward it.
The report ties the stranded balance to a concrete router branch (_PARTIAL_FILL) and to named user-facing entry points, and shows that the only refund routine cannot reach ERC20 balances. That combination, a reachable refund path plus a refund routine that cannot handle it, is the basis for the permanent freezing claim.
Glossary
- Smart contract: a program stored on a blockchain that runs automatically and cannot be quietly altered once deployed.
- Blockchain: a shared ledger kept by many computers, used to record transactions and contract state.
- Solidity: the programming language used to write smart contracts on Ethereum-style networks.
- ETH: the native coin of the network in question.
- Native token: the blockchain's own built-in money, as opposed to a token issued on top of it.
- ERC20 token: a token issued on top of a blockchain, whose balances are managed by that token's own contract.
- wei: the smallest unit of ETH.
- DEX: a decentralised exchange, a service that swaps one token for another without a central operator.
- DEX aggregator: a service that finds and routes a swap across multiple exchanges to get a good result.
- Router: the contract inside an aggregator that carries out the swap.
- Wrapper contract: a contract that sits in front of another service to make it easier or safer to use.
- Bridge: to move assets from one blockchain network to another.
msg.sender: the address of the account or contract that called the current function.address(this): the current contract's own address.address(this).balance: the amount of native ETH held by the current contract; it does not include ERC20 tokens.- Partial fill: a trade that is only partly completed, leaving some of the input unused.
- Flag: a setting, usually a bit in a number, that switches a behaviour on or off.
_PARTIAL_FILL: the flag that tells the router a partial fill is allowed._REQUIRES_EXTRA_ETH: a flag controlling how much ETH must be attached to the call._USE_PERMIT2: a flag controlling an alternative approval mechanism for pulling tokens.revert: to cancel a transaction and undo its effects, usually with an error message.- Gas: the fee paid for computation on a blockchain.
- Block explorer: a website for looking up contracts and transactions on a blockchain.
- Attackathon: a time-boxed public competition where researchers hunt for bugs in a project's code.
- Permanent freezing of funds: a security impact where money becomes stuck in a contract with no legitimate way for its owner to retrieve it.
- Remediation: the recommended fix for a reported issue.