Inspiration

Very often user needs to have MATIC native token in order to make swaps on a Decentralized Exchange (DEX) on Polygon. IN many cases, especially when new wallets are created or ERC20 tokens bridged, users are finding themselves short of MATIC to pay gas for swap.

How we can improve the end-user experience when swapping on DEX, making it easier ?

What if someone else can pay MATIC for the swap transaction, making it an fast, gasless swap experience for the end-user ?

How can we make the user experience closer to the UX from the CEX where they only need to hold the assets being swapped, but still have the security / self-custody of trading on a DEX ?

What it does

It does few things:

  1. It aggregates liquidity from DEX protocols and aggregators, choosing the best swap rate between the protocols/aggregators. Hence, the name MetaDex Aggregator = MetaDexa.
  2. When a best swap rate is obtained, a swap transaction is build for the end-user. The end-user is then confirming the transaction by SIGNING a message with their wallet. The end-user is NOT executing the swap transaction themselves. The swap transaction is passed to and executed by a Relayer who pays the gas fees. From end-user perspective the swap is gasless i.e no MATIC gas fees are being paid.
  3. Instead the users are being charged a fee from the amounts swapped to compensate for the gas fee. Same experience when trading on a CEX, but implemented on a DEX. User only need at most one on-chain approval transaction to approve the tokens for spending/swappig.
  4. The smart contracts also supports permit tokens following: EIP Permit Standard. If a token swapped follows the EIP permit standard then the token approval is ALSO gasless, making the UX full gasless.

How we built it

MetaDexa consists of several components that interacts with each other to offer an instant, gasless swap experience transparent to the end-user:

  1. MetaDexa smart contracts - verifies the forwarding / relayer logic, the logic for distributing service fee and swaps. Uses an adapter pattern for meta-swaps. Coded in Solidity and deployed on Polygon mainnet. There are 4 main contracts deployed on Polygon:

MetaSwapRouter

GaslessSwapAdapter

SwapAggregatorAdapter

GaslessForwarder

  1. MetaDexa Aggregator API - offchain service that aggregates from different liquidity sources and builds a best swap quotes for the end-user to sign and relay to the Relayers. Coded in NodeJs and hosted on Amazon AWS EC2 instance. Available via: MetaDexa API Since the relayers are compensated directly from the tokens swapped, aggregator API is a crucial component of the system that does a unique offchain simulation for the swap transaction to make sure the tokens are valid ERC20 tokens ( they are not honeypots, they can be swapped, transferred with no tax etc). This is to make sure the relayers can accept those tokens as fees.

  2. MetaDexa Dapp - user interface where the end-user can swap tokens. Coded in React and hosted on IPFS/Web3.storage. Available via: MetaDEXA App

  3. MetaDexa Relayers - implemented using Biconomy Relayers.

Challenges we ran into

  1. How to charge a service fees from the tokens swapped ? Some of the tokens swapped can be tax-on transfer tokens, some can be malicious tokens, which means not a standard ERC20 tokens. The challenge was: how to make sure that the service fee charged by the Relayer can be redeemed for MATIC afterwards? This needed a complex off-chain simulation before the Relayer can execute the swap transaction on-chain. This off-chain simulation is done by the MetaDexa Aggregator API before the swap quote is built and returned to the end-user for signing.

  2. Next challenge was how to develop an extendable smart contract architecture where custom swap logic can be added or the swap logic extended (for example support both gasless and non-gasless swaps, cross-chain swaps etc) The metaswap smart contract consist of the following components:

metaswap router smart contract - contract that is the entry point of the swap and the target contract for setting ERC20 allowance.

flash wallet - temporary wallet used for swapping funds within a single blockchain transaction. this is a security feature to prevent malicious allowance having an access to the funds swapped.

adapter(s) - metaswap contract has a map of adapters by id, supporting many different swap logics within the metaswap. There are 2 adapters created by default:

GaslessSwapAdapter - used for gasless swaps SwapAggregatorAdapter - used for non-gasless swaps.

Accomplishments that we're proud of

  1. Being able to build an extendable swap adapter architecture where the system can be extended with different adapters and swap logic.
  2. Also the off-chain simulation that was build is very complex one, which basically pre-simulate the transaction end to end, before it is being returned to the user for signing.
  3. At the end, we are very proud that we managed to build an end-to-end solution for gasless swaps combining it with a DEX swap aggregator feature, offering the users best swap rates with NO gas fees.

(Note: when testing pay attention to slippage settings, the transaction simulation fails and transaction is not submitted to the relayer if they gonna fail because of the slippage).

What we learned

We learned a lot on the technical side. We learned a lot for other liquidity sources and aggregators while building the DEX swap aggregator feature. We learned a lot about meta-transactions and how they work, along with a lot of EIP standards focused around EIP-712.

What's next for MetaDexa.io

We believe there are some gas cost optimizations that can be made on the smart contracts to make them efficient and cheaper. Also right now, MetaDexa uses Biconomy relayers for the relaying transactions. The idea is in future to allow anyone to build and host their relayers, allowing for relayer competition, further reducing the fees charged or even temporary removing them, if some relayers decide not to charge fees to their end users to subsidize certain swaps.

Share this project:

Updates