Halving the number of failed txs: Swap improvements at DFS

Token swaps are an integral part of DeFi Saver, and we always put great effort into finding the best possible swap conditions for our users. What initially started as manual DEX aggregation on our end, eventually turned into advanced meta-aggregation for each individual swap. And while we've come a long way, we have come to realise that we don’t always take the time to explain just how big and impactful some of these changes are.

That said, one recent upgrade that we feel deserves more attention is our improved swap route optimisation with automated fallbacks. In just the first month of rolling it out, we managed to cut the number of failed swaps in half, from 12% down to 6%.

Today, we’ll walk you through how we made it happen.

Why are swaps more prone to reverts?

From the moment you enter the amount and token you want to exchange, the chances of your swap transaction failing grow until it's actually executed on-chain.

Let’s explain why this is the case with the following example:

  • Alex broke her phone and needs to buy a new one.
  • Unfortunately, most phone stores don’t accept ETH as payment, so she needs to swap her 1 ETH for DAI, with 1 ETH trading for around 2400 DAI atm.
  • She isn’t willing to deviate too much from that expected amount, so she set the slippage at 0.5%, effectively making 2388 DAI as the minimum acceptable amount.
  • Alex double-checks all the amounts, checks the gas settings, and finally clicks “Swap.” 
  • However, by the time she checked everything, signed and submitted the transaction, and it confirmed on-chain, the price of ETH fell to 2370 DAI. This results in the swap simply failing, with the transaction reverting and incurring some gas losses.

Sure, this is a simplified example, but market volatility is, of course, the main reason why any swaps will fail. It's just that how they fail is sometimes different.

For example, in times of high market volatility, what can happen is that (some) aggregators aren't quick enough to keep track of liquidity available in different DEX pools. Perhaps you're trying to swap $ 10,000 ETH to USDC, and the best rate is provided by an aggregator that routes you through SushiSwap at 2,400 USDC/DAI. However, that was actually used up two blocks ago, and the actual best available rate is closer to 2350 USDC/DAI.

You keep trying, but the transaction keeps failing because the liquidity is simply no longer there. Overall, you end up frustrated, having wasted energy, time, and let's not forget- gas fees.

We sought to tackle this exact problem with our swaps handling update and find a way to ensure that users aren't left chasing ghost liquidity. Our primary task was to create a system that checks pool availability and adjusts routes before execution, if needed.

Introducing automated fallbacks

Like before, DeFi Saver still searches through all supported aggregators (0x, 1inch, Odos, KyberSwap, Velora, and counting) to find the most optimal swap rate. However, it now also simulates the swap in case the route isn’t valid, and automatically switches to the next best available route. DeFi Saver will notify you of possible price changes, thus giving you the choice to accept or decline.

A preview of DeFi Saver's automated fallback notification.

So now, not only do you not have to manually check each DEX aggregator for the most favorable metrics, you don’t have to worry about wasting time (and gas) on searching for a backup if a swap fails. Of course, for those who prefer more hands-on control, you can still manually select a specific aggregator.

Overall, the most important thing for us was to ensure that users would not, at any point, be stuck in the process of handling their needed transactions. 

Extra tip: Protect your swaps 

Okay, so we minimised the chances of the swap transaction failing and did our best to be time-efficient. Is there anything else we can do to ensure that users are, by all means, getting the best possible rates? 

Keeping your transactions safe from MEV attacks might just be the extra step to take, especially when manually submitting transactions on-chain, on the Ethereum mainnet. With that said, if this applies to you, we recommend you use RPCs like MEV Blocker or Flashbots Protect to secure your Ethereum transactions.

However, for those who don’t mind giving up a bit of control for a safer and smoother DeFi trading experience, we recommend you give TxSaver a shot.

Exclusive to Safe smart wallet users, TxSaver is an option you can enable for all transactions that include swaps, before submitting them on-chain. The cool thing is that you only need to sign the transaction, with TxSaver doing the rest of the work for you, and handling both gas settings and using MEV-preventing RPCs, all while respecting your previously set minimum price and slippage limits. No extra fees whatsoever, just extra convenience for users.

We hope the recent Exchange update has made your trading experience easier and smoother. Don’t hesitate to try it out, and if you already have, drop by our Discord server and share your feedback!