project screenshot 1
project screenshot 2
project screenshot 3

2inch

Leverage the power of the multi-chain world to get the best possible price on your token swaps using 2inch - the first cross-chain DEX aggregator powered by the #1 DEX aggregator you already know and love.

2inch

Created At

ETHNewYork 2022

Winner of

trophy

🥈 1Inch — Best Use

Project Description

2inch is the first multi-chain DEX aggregator powered by 1inch. This project combines the the power of DEX aggregators and instant cross-chain bridges to bring the ultimate token swapping experience for the guaranteed best prices. Here's how it works: Just like with 1inch, when you enter the token pairs you wish to swap between, 2inch will check all feasible paths the swap can take to find the best overall price - rather than reinventing the wheel, we achieve this by leveraging the work done by 1inch. However, instead of considering just the pools on your chain, 2inch will also compute the possible prices obtained from sending tokens to other chains using fast cross-chain bridges (currently using Hop Exchange, which supports Ethereum mainnet, Polygon, Arbitrum, Optimism, and Gnosis, but we also hope to integrate more bridge protocols in the future), then utilizing 1inch on the other chain, and then sending the swapped tokens back to the user back on the original chain. With this approach, the complex path computations are entirely abstracted away from the user - all they see is that they got a better swap price than anything possible on DEXs within their chain. If leveraging other chains yields no better price, then 2inch simply operates as if you used 1inch normally, so you can only get better prices by using 2inch.

To make things clearer, we think it'd be easiest to provide a real-life practical example that you can even try for yourself right now. As of the time of writing this, on Optimism 1inch, if you swap 300 ETH (worth around $371k USD) for DAI you will only receive ~345k DAI, representing ~7% slippage. This is obviously a terrible price and the reason for this is because there simply isn't enough liquidity on Optimism, so even 1inch's routing algorithms can't help much. Nothing about this is specific to Optimism - unsurprisingly, all layer 2 protocols see significantly lower TVL compared to the mainnet counterparts.

Instead, if you used 2inch's cross chain routing, one possible path it could route your swap would be to first transfer your ETH from Optimism onto Ethereum mainnet using the Hop Bridge, swap your ETH for DAI using mainnet 1inch, and then send the DAI back to Optimism using the Hop Bridge in the other direction. Again using actual real numbers, sending 300 ETH from Optimism to Ethereum mainnet currently costs a fee of about 0.2 ETH, and then swapping 299.8 ETH on mainnet 1inch results in 370.4k DAI (no slippage). Bridging that DAI back to Optimism incurs a fee of just 0.4k DAI, so you end up with ~370k DAI back on Optimism, meaning you were able to swap your 300 ETH on Optimism with essentially no slippage, while the best alternative solution results in massive slippage (a roughly $30k USD price improvement). Of course, if you swapped even larger amounts, the numbers would get far more extreme - to the point where naively swapping on a layer 2 could result in you losing almost all your funds (eg. swapping 10k ETH for DAI on Optimism currently results in ~95% slippage, while the same swap on mainnet only has 0.5% slippage). By utilizing 2inch, users are able to harness liquidity across all chains, no matter what chain they are on. Being able to access mainnet liquidity while on a small layer 2 is a powerful paradigm that everyone benefits from: layer 2s become much more accessible, users get massively better prices, and overall DeFi capital efficiency increases significantly.

Both of us are passionate believers and long-time users of DeFi. While we're both still college students, we also happen to be working at a quantitative trading firm, so we spend a lot of time thinking about optimal execution and approaches for price improvement. While what DeFi has accomplished thus far is incredible, it's no secret that compared to tradFi, DeFi is still wildly capital inefficient. We believe in helping users get the best prices possible and know that reducing capital inefficiency in our increasingly multi-chain world is crucial. We're big fans of 1inch - they've done a phenomenal job at providing such access thus far, but we want to take it a step further by leveraging this massive additional dimension. We are committed to continuing work on 2inch (maybe with a more original name!) as we think this is a necessary part of the DeFi infrastructure that will power the future.

How it's Made

2inch wouldn't be possible without the incredible foundations laid by DeFi pioneers. From a protocol perspective, the main ones we leverage are 1inch and Hop Exchange. We use 1inch to find the best swaps within a single chain and we used Hop Exchange to bridge tokens from one chain to another. It's important to note that Hop provides more than just being a hub of cross-chain bridges: by leveraging bonders and liquidity providers, they make the cross-chain transfer process nearly instant (just a few seconds) rather than the minutes or potentially up to days many L2s natively support. This is obviously necessary for 2inch to function as it would be pretty impractical to have to wait such a long time for a token swap. All our contracts were developed in Solidity and we used tools like Tenderly, Foundry, and Hardhat to support our development. Our frontend was designed using Figma and we wrote several JavaScript scripts to piece everything together. To better understand the nitty-gritty of our implementation, here's a step-by-step on the flow a swap would go through: the user selects the two tokens they wish to swap between and the quantity. 2inch then makes queries to fetch two values: the 1inch API is used to fetch the calldata needed to be passed into 1inch contract's swap function (this is how 1inch designed their contract), and the Hop API is used to query the bonder fee needed for a bridge transaction for a specific token and quantity. These values and the user parameters are passed into our smart contract through a function call. The contract determines the best swap route and if necessary, sends the tokens to the appropriate bridge by calling the Hop contract. The contract on the other chain is invoked (much more detail on this below) and performs the best possible swap on that chain (using 1inch) and calls the Hop contract to send the funds back to the user on their original chain. In more complex situations, the contract could split up the funds across multiple chains even for a single swap and do the same process on each of them, if this would be more optimal (though we didn't implement this in our proof-of-concept). All of this happens in just a few seconds thanks to Hop's fast bridging, and the user receives the swapped tokens without ever even realizing that their swap was routed through multiple pools across multiple chains.

One of the greatest design decisions revolved around how to "complete" the return transfer of a cross-chain swap. To illustrate the issue, imaging if the best swap route involved sending your token to a different chain. Sending the token to the other chain is easy (that's done as part of the original swap() invocation in our contract). The problem is, how do you perform the swap on the other side and then send the funds back to the user on the original chain? One possible solution would be to ask the user to call a function on the other chain to "complete" the swap and send the funds back, but this puts an unreasonable burden on the user and exposes them to the complexity of the underlying cross-chain routing (also, would be far too impractical if the route passed through multiple chains). For the purposes of creating a proof-of-concept, we opted for a semi-centralized solution, where we maintain a server that is constantly listening to the 2inch contracts and immediately performs the function invocations on the other chain to send the funds back to the original user. This way, the user doesn't have to have any knowledge of what happens on other chains as the funds are always automatically sent back to their wallet on the original chain. In the spirit of censorship-resistance, to ensure the safety of user funds, we could implement a rescue function that the original user can call in emergency situations to recover their funds, even if the centralized mechanism misbehaves. While this solution works great, we eventually would like to implement a fully decentralized alternative that works as follows: anyone (searchers) can invoke the function to complete the swap, and in return, the searcher receives a small fee. We could even add an auction mechanism similar to MakerDAO's liquidation auction system to ensure the fee is as small as possible. With this approach, incentive engineering in a world of rational actors is able to replicate the functionality of a centralized server, while being fully censorship-resistant and decentralized.

We're proud of the fact that we were actually able to execute on our vision (especially in such a short time) - our project actually works! While it certainly is just a proof-of-concept at its current state, it already performs the core functionality. After hours of non-stop work, we were ecstatic to have made the first-ever successful 2inch swap: we were able to swap a few Optimism USDC for Optimism DAI, where the actual swap involved bridging the Optimism USDC to Polygon USDC, swapping USDC for DAI on Polygon, and then sending the DAI back to Optimism. All this occurred seamlessly through a single call to the swap() function on the Optimism 2inch contract - the end user doesn't need to have any knowledge of Polygon (or any other chain), nor do they need to take any special action; the UX is identical to any other DEX. But behind the scenes, 2inch is leveraging multiple chains to find a better price than you could possibly find on just the chain you're using.

The development process was a fun yet challenging experience. Integrating with the different protocols proved difficult at some points (especially working through the intricacies of the 1inch API), but ultimately we learned a lot in the process and are super proud of our functioning end product.

background image mobile

Join the mailing list

Get the latest news and updates