The Magic Behind Totle’s Seamless Swaps

How Totle swaps your token from A to B

Totle
Totle
5 min readAug 5, 2019

--

The Totle Swap process abstracts the complexity of decentralized exchange to offer end-users a seamless experience.

The User Experience

Whether you’re using Totle Swap, Edge, Midas, LiquidSwap, or any other DeFi tool integrated with Totle, the swap process will be pretty similar.

You’ll select the token you want swapped and the token you want to acquire, then type in the amount you wish to have converted.

Once you approve the swap, the tokens are processed in a near-instant transaction and your new tokens are sent directly to your wallet.

You will have just acquired your tokens at the best price available on the DEX market. At no point were you exposed to a centralized intermediary.

Under the Hood

If you check your transaction on Etherscan, you may be surprised to find that the swap did not actually happen in a single step, it only felt like it did.

As you can see in the picture above, this user’s swap underwent 7 separate steps in the transaction before the converted tokens were sent to their wallet.

This is an outline of what happened at each step of the swap:

  1. Totle’s Token Transfer Proxy transferred the tokens from the User’s Wallet to the Totle Primary
  2. Totle’s Primary Contract sent the tokens to a DEX handler
  3. The DEX handler transferred the tokens to the DEX
  4. The DEX executed the order
  5. The DEX sent the tokens back to the DEX handler
  6. The DEX handler sent tokens back to Totle Primary
  7. Totle Primary transferred the new tokens to the User’s Wallet

Note that not every transaction will follow the same process but users can expect a minimum of three different transfers to occur.

The Totle Swap Process

Once swap requirements are set up and a user has executed a swap with Totle, a domino effect is started.

Totle’s Token Transfer Proxy informs Totle Primary what your trade requirements are and simultaneously transfers it your tokens. The Token Transfer Proxy remembers pre-set allowances so that Totle can improve and update the Totle Primary without disrupting trades.

The Primary Contract conducts all of the necessary validations to ensure that the order can be successfully executed before moving on to the next step. This safety check ensures that the swap has a high probability of being successful. If the Primary contract determines that the swap will likely fail, no funds are lost and the tokens are automatically returned to the user’s wallet.

Totle’s Primary Contract then sends funds to the DEX handler offering the best price for the swap. The DEX Handler acts as a portal to the DEX, it ensures that orders are properly executed.

The Handler then passes the order to the corresponding trade inside of the DEX. The swap is conducted by the DEX and your funds are now converted into the asset you want to acquire. The DEX then returns your newly swapped funds to the DEX handler.

At this point, the DEX Handler relays the funds back to Totle’s Primary contract. Now, Totle can validate if the trade has met all the requirements specified by the user (some of the requirements may have been pre-set by the application facilitating the transaction).

Requirements can include the trade meeting the minimum fill percentage, whether it attained the desired slippage rate, and more. If the requirements have not been met, the trade is reverted and the user’s funds are returned to their wallet in their original state, no funds are lost aside from the gas needed to facilitate this process. If the requirements have been successfully met, the transaction moves forward to the next step.

Totle’s Primary returns the swapped funds to your wallet. Only a couple of seconds or so have gone by since the user clicked the button to execute the transaction.

For a complete overview of Totle’s smart contracts, click here: Architecture

Multiple Swaps

The steps above outline the process of a trade when a single swap is being made. If an order demands that multiple trades are to be executed, multiple orders would be sent from Totle’s Primary Contract to the specific DEX Handlers with the best prices.

Totle automatically communicates the trade requirements for each swap to the corresponding exchanges with the best prices and the order feels as though multiple trades were executed in a single swap.

Protection

A major benefit to using Totle is that your funds are heavily protected in the event that there is a conflict with either a DEX or Totle’s smart contracts.

If a DEX encounters an issue, Totle Primary will revert the order and your funds will be safe.

If Totle’s smart contracts encounter an unforeseen bug, swaps are insured with Nexus Mutual for up to 92 ETH.

The Pros and Cons

Pro: By executing a trade with Totle, users are guaranteed to get the best price on the DEX market without ever exposing funds to a centralized intermediary. This simplifies the trading process by removing the need to conduct pricing research or understand how to interact with a multiple DEXs.

Pro: Order requirements can be meticulously outlined and the trade will not be completed unless these requirements are met. This makes swaps highly customizable and reliable.

Pro: As mentioned above, user’s funds are heavily protected by Totle in the event that either a DEX or Totle’s smart contracts encounter an error.

Con: Due to the number of transfers that can occur during a swap, additional gas is needed to facilitate each step of the process. Luckily, gas is typically priced at a few cents of USD.

Get Started

To start using Totle, access the API here: Build with Totle

Easily convert tokens with Totle here: Totle Swap

Follow us on Twitter, Facebook, Telegram, or Medium for the most up-to-date news on Totle.

--

--