What Happens When a DEX Transaction Fails?
From the front-end of a decentralized exchange, a token swap might seem straightforward. Link a wallet and select two tokens, type in an amount and hit confirm.
In the background, the blockchain needs to check the transaction, interpret the instructions of the smart contracts and modify the relevant balances and pool state.
If either of the above is not true, the transaction may fail.
Knowing the reasons behind a failed transaction can help traders identify when a transaction failed, or if a transaction is pending, it may have failed due to a normal reason that can be resolved. Knowing the reasons for a failed transaction can help traders discern when the transaction failed, as well as if a transaction is pending, it may have failed for a normal reason, which can be resolved.
What Is a DEX Transaction?
A DEX transaction is an on-chain instruction that interacts with smart contracts.
For a token swap, the transaction can involve several steps:
- Approving a token for the relevant contract
- Calling the swap function
- Checking the user's balance
- Checking the permitted slippage
- Updating pool reserves
- Sending the output tokens to the trader
The exact process is dependent on the blockchain and the architecture of the DEX.
A DEX isn't run by a single operator, and that operator can't just negate a failed trade, for example. Whether the requested operation is successful or not depends on the rules for blockchain execution.
For instance, in the developer documentation of Dexlyn, they have functions that swap with minimum and maximum output values. These conditions enable the transaction to be reversed if it fails to meet the trader's requirements.
Why Does a DEX Transaction Fail?
There are several common reasons.
1. Insufficient Balance
The wallet may not contain enough of the token being sold.
A transaction can also require the blockchain's native token to pay network fees. Having enough tokens for the swap does not necessarily mean the wallet has enough balance to complete the transaction.
2. Insufficient Allowance
Certain token standards need for the user to approve a smart contract before it can spend tokens on their behalf.
It is possible for the approved amount to be less than the amount being swapped, which could result in the transaction failing.
This is why a token swap can sometimes involve two separate wallet confirmations:
Approval → Swap
The approval gives the contract permission to use the token. The second transaction performs the actual trade.
3. Slippage Limit Is Triggered
A trader normally sets a maximum amount of price movement they are willing to accept.
Suppose a trader expects to receive 1,000 tokens but sets a minimum acceptable amount of 990.
If the swap can only produce 980 when the transaction executes, the smart contract can reject the transaction.
This protection is useful because the market can change between the moment a quote is displayed and the moment the transaction is executed.
Dexlyn's swap documentation offers adjustable slippage options, and its swap capabilities provide minimum-output protection.
What Causes Slippage to Change?
A quoted swap price is not necessarily guaranteed until the transaction executes.
Several things can change the expected result:
- Another trader moves the pool price
- Available liquidity changes
- The trade itself produces price impact
- The selected route changes
- The transaction takes longer than expected
- Market activity increases
This matters particularly for thin liquidity pools.
Liquidity needed for a big trade can be significant and move the price more than a small trade.
What Does "Reverted" Mean?
If a transaction made with a smart contract fails, the desired state change does not occur.
For instance, let's say a trader wants to convert 1000 USDC into another token, but the minimum required amount of the output token is not fulfilled.
The contract can revert instead of completing the trade at an unacceptable price.
This is not the same as a transaction that completes successfully, but returns a lower value than anticipated.
A reverted transaction will not imply that the tokens were swapped successfully and lost. The important distinction is whether the transaction reached a successful execution state.
The network fee associated with attempting the transaction may still be charged, depending on the blockchain, because validators or other block producers had to process the transaction.
What Is a Pending Transaction?
A pending transaction is different from a failed transaction.
A pending transaction has been submitted but has not yet reached a final execution state.
It may be waiting to be included in a block or processed by the network.
During this period, the DEX interface may continue showing the transaction as pending.
Traders should avoid immediately submitting the same transaction again without checking the original transaction status. It's possible that they duplicate transactions or make unnecessary approvals if they don't.
What Happens If the Transaction Is Successful but the Price Changes?
This is another situation that can confuse traders.
Suppose a DEX shows an expected output of 500 tokens.
The trader confirms the transaction.
Before execution, the market moves. If the final output remains above the trader's minimum acceptable amount, the transaction can still execute successfully.
The trader may receive less than the original quote while remaining within the selected slippage tolerance.
This is not the same as a failed transaction.
The difference is:
Failed transaction: The execution conditions were not satisfied.
Successful transaction: The trade completed within the conditions specified by the transaction.
Can Network Congestion Cause a DEX Swap to Fail?
Network conditions can affect transaction execution.
This could take longer in times of heavy activity. This can enhance the likelihood of the market shifting prior to the trade based on the blockchain and trade settings.
Transaction fees and confirmation times can also be impacted by network congestion.
The DEX itself may not control these network conditions.
The blockchain is still in charge of transaction ordering, execution and changes of the final state for a decentralized trading platform.
What Is the Difference Between a Failed Swap and a Wrong Token?
This is particularly significant with assets that are not known to the applicant.
A failed swap usually means the smart contract rejected the transaction.
Unexpected token can be a whole different problem.
For instance, the name or symbol of two assets might be identical, but each asset must be on a distinct contract address. A trader that has chosen the wrong asset can successfully complete a transaction and get an asset that they did not want to purchase.
That is the reason why traders should always check the token contract address and not merely based on its name.
How Can Traders Reduce Failed Transactions?
There are a number of checks that can prevent unnecessary failures.
Check the Wallet Balance
Make sure there is enough of the input token and enough native network currency for transaction fees where required.
Check Token Approval
If an approval is needed, ensure that the appropriate contract has adequate spending authority.
Review Slippage
A very tight slippage setting can cause legitimate transactions to revert during volatile market conditions. A very high setting can expose the trader to worse execution.
Check Liquidity
Large trades against shallow pools are more likely to experience substantial price movement.
Review the Transaction Details
Before signing, check the token being sold, token being received, amount, minimum received and network.
Verify the Transaction Status
Once signed, check to see if a transaction has been accepted, rejected or pending on the blockchain explorer or DEX interface.
What Should Traders Do After a Failed Swap?
First, check the transaction hash.
If it fails, check the wallet, DEX interface or blockchain explorer for the reason of the transaction failure.
Then check:
- Wallet balance
- Token approval
- Slippage tolerance
- Pool liquidity
- Network status
- Transaction parameters
Do not immediately increase slippage without understanding why the transaction failed.
A higher slippage setting may allow a trade to execute, but it also permits a larger difference between the expected and minimum acceptable output.
Failed Transactions Are Part of On-Chain Trading
No DEX will be able to ensure that all transactions submitted will be executed.
Smart contracts are used to impose conditions, and the blockchain records transactions according to the conditions it applies to.
The important difference for traders is in pending, reverted and successful trades.
Many transactions end up in failure due to a particular condition like not enough balance, missing approval, too much price swing, or slippage limit.
Understanding the conditions helps you to navigate trade problems more easily, without having to resubmit trades and/or adjust settings that might expose you to more risk in trade.
