At MetalSwap, we are always on the cutting edge of DeFi innovation, integrating the latest technologies to enhance our platform's capabilities. One of the MetalSwap Hedging Contracts’ most important integrations is with Chainlink, the leading decentralized oracle network on Ethereum. We have been using Chainlink's price feeds to provide accurate and reliable data for our platform, as detailed in our previous blog post.
On May 26th, 2023, MetalSwap's contributors had the opportunity to speak about these exciting developments at the "Spaghetti ETH" bootcamp live event in Naples. Recognizing the importance of these topics, we are now bringing this discussion to our online audience through this article, aiming to deepen the understanding of these crucial topics in the DeFi landscape.
The MEV Issue
In the DeFi landscape, Miner Extractable Value (MEV) has become a significant concern. MEV refers to the profit a miner can make through their ability to arbitrarily include, exclude, or re-order transactions within the blocks they produce. In the context of liquidations, this can lead to inefficiencies and potential losses for users as miners can front-run liquidation transactions for their gain.
In the case of MetalSwap’s Hedging Swaps Smart Contracts, we deployed a system like this, let us draw it simply:
Liquidation process
As illustrated above, a user opens a Swap with a threshold value of X.
If the market price reaches the Threshold, OR the execution time is reached, a Liquidator picks up the moment before the others that are looking to liquidable Hedging Swaps. The Liquidator can then call the liquidateSwap or executeSwap method on the Hedging Swap's Smart Contracts, taking the Settlement Fee that the user paid to open the new Swap (a little percentage of the Premium). It's important to note that the Settlement Fee is part of the Premium charged to a user who wants to open a new Swap.
Everything is well set, in theory. What happens on chain?
…the MEV comes to play!
The Impact of MEV on the Liquidation Process
MEV's activity on-chain
The presence of MEV adds some steps to the liquidation process we saw above. When the original Liquidator calls the profitable liquidateSwap OR executeSwap method transaction, the MEV sees it in the Mempool before it goes to the blockchain. The transaction is then copied from there and pasted from a different account with higher transaction fees to front-run the original transaction. The original transaction fails because the MEV transaction arrives just before on-chain, and the MEV takes the profit.
While this doesn't pose a problem from the user's perspective yet, it becomes unprofitable for the Liquidator to monitor changes on Active Swaps and spread the original liquidation transaction. This scenario could pose a serious problem for the management of MetalSwap’s Active Swaps Management. The MEV excludes the incentive to the liquidators to work for MetalSwap. Without the incentive, the liquidation is not in the way of decentralization.
Solution 1: Hashing msg.sender
Hashing solution process
One potential solution to this problem is to hash the Liquidator's address and SwapID using the keccak256 hashing algorithm, and then call the liquidateSwap or executeSwap methods on the Smart Contract. In this way, only the liquidation transaction that comes with that hash is accepted. This approach makes it very difficult for the MEV to recreate the transaction using the copy/paste strategy we discussed above. As a result, the Swap Position is correctly closed, and the Liquidator can profitably execute their role.
Solution 2: Chainlink Automation
Chainlink Automation enables conditional execution of your smart contracts functions through a hyper-reliable and decentralized automation platform that uses the same external network of node operators that secures billions in value. Building on Chainlink Automation will help you get to market faster so you don't have to deal with the setup cost, ongoing maintenance, and risks associated with a centralized automation stack.
https://docs.chain.link/chainlink-automation/introduction
So, a second possibility may come integrating the Chainlink Automation services while leaving the execution method public but removing the settlement fee.
The settlement fee will be lowered and used instead to replenish the Chainlink automation smart contract by converting swap assets to LINK tokens.
Chainlink Automation solution processes
Let’s introduce a middleware into the flow.
It calls the checkUpkeep function on Active Swaps until it returns true, at which point it calls the performUpkeep() function, which in turn calls the liquidateSwap OR executeSwap method on the Smart Contract. The transaction then goes directly from the middleware to the mempool.
This transaction does not assign settlement fees to a liquidator, so a MEV will not pick it up. With the Chainlink Automation's transaction accepted, the Active Swap is now closed, and the platform management works as intended. An interesting aspect of this flow is that the settlement fees are converted into LINK tokens and used to refinance the Chainlink Automation liquidation service.
Integrating the Chainlink Automation services while leaving the execution method public. The settlement fee will be lowered and used instead to replenish the Chainlink automation smart contract by converting swap assets to LINK tokens.
Chainlink Automation presents a solution to our issue. It enables conditional execution of smart contract functions through a hyper-reliable and decentralized automation platform. This means that MetalSwap’s liquidations can be Automated in a way that is secure, reliable, and resistant to MEV.
Enhancing Position Management with Chainlink
Beyond addressing the MEV issue, Chainlink Automation can also significantly enhance position management on MetalSwap. With Chainlink, we can automate numerous processes that currently require manual intervention, such as setting stop-loss / take-profit triggers, or “automaticly add Cover" trigger on Active Positions.
This not only simplifies the user experience but also enhances the efficiency and effectiveness of our Hedging Swap tool. Users can set their parameters, and Chainlink Automation will take care of the rest, executing actions based on predefined conditions in a secure and reliable manner.
What we seek with our solution
Collaborating for a Brighter Future
At MetalSwap, we're committed to staying at the forefront of DeFi innovation. Our ongoing collaboration with the Chainlink team is a testament to this commitment. Together, we're exploring the latest developments in DeFi and how they can be integrated into our platform to provide the best possible experience for our users.
We're excited about the potential of Chainlink Automation and look forward to seeing how it can help shape the future of DeFi. By solving the MEV issue and enhancing position management, we believe that Chainlink Automation can play a crucial role in creating new "Lego Pieces" in the DeFi space, contributing to a more robust and efficient ecosystem.
As we continue to work closely with the Chainlink team, we're confident that we can lead the way in DeFi innovation, creating a brighter future for all.
As a reminder, this article is taking the topics discussed at the "Spaghetti ETH" bootcamp live event in Naples, and just like our contributors at the end of the speech, we would like to ask you:
Which is the best ?
What a time to be in DeFi!
Goodbye volatility!
To the MetalSwap
… and beyond!
-The DeFi Foundation
❗ METALSWAP ‘s admins or moderators will NEVER contact you directly on any social under any kind of circumstances.
❗ Everything concerning $XMT ‘s smart contracts and addresses is shared by our official channels (list below), do not trust any different info from any different source.
With MetalSwap we enable Hedging Swap transactions through the use of Smart Contracts, AMM style.