LIQUIDATORS Unchained!

MetalSwap
MetalSwap
Published in
4 min readAug 11, 2022
An high skilled Swapper can become a Liquidator and be Rewarded -This is permissionless!

Hi Swappers!
Today’s is a technical insight, for all those who want to get to the highest level of understanding of how Commodity Swaps work on MetalSwap ‘s platform: did you know that it is possible to become a Liquidator of positions that have crossed the Threshold? This is a trade in its own right: you compete with other liquidators to get to close a liquidatable Swap before the others and be Rewarded for that! You can try it now through our Testnet Phase3 dashboard!

This is not a job for newcomers: you need to have a good knowledge of Solidity & JavaScript programming along with docker & cloud systems.

⚡MetalSwap’s Liquidator Service is divided in four main tasks, one foreach pair (es. WBTC/USDT):

· FetchJob

· OraclePriceJob

· UpdateCoverJob

· ExecuteJob

Now we are going to discuss each one in detail.

FetchJob

This job is responsible for retrieve the Swaps from the blockchain and save them inside the DB for the liquidation/execution.

  1. Retrieve active epoch (epoch of last fetched swap)
  2. Fetch, thanks to the blockchain events, the Swaps data
    - a. “ + ” CreatedSwap
    - b. “ - ” ExecutedSwap
    - c. “ - ” ClosedSwap
    - d. “ - ” LiquidateSwap

“+”,”-” means filtering (so we filter the CreatedSwap removing the ExecutedSwap, ClosedSwap, UpdateSwap)

In order to achieve this task, we developed a function based on binary search. The main idea is from the active epoch to the latest block we execute one call to the node: if the answer is positive, this is the final answer; in any order case we split the interval into two subintervals…

(active epoch, (active epoch + latest block)/2) and ((active epoch + latest block)/2 + 1, latest block)

…and we recursively explore and combine them until we have retrieved the final answer.

3. Removing the swaps already present in the DB (based on the blockchain id)

4. Save the new swaps inside the DB

5. Update the active epoch with the latest fetched swap creation block

OraclePriceJob

Due to the relatively slow update time of the oracles and in order to reduce the number of calls to the node, this job reads the price regularly (es. every xxx ms).

1. We call the Chainlink oracle and retrieve the current price of the asset

2. We update the price inside the DB with the new one

UpdateCoverJob

This job retrieves and updates the Threshold in order to follow the users activity.

  1. Retrieve every Open Swaps from the DB
  2. Retrieve the minimum epoch of the Open Swaps from the DB
  3. Call the AddedCoverToSwap event from (minimum epoch, latest block)
  4. Each Swap will be check if there is an updated cover
    -i. No: Do nothing
    -
    ii. Yes: We retrieve the detail of the Swap from the blockchain with a new call. Inside the detail we found the new Threshold value. Therefore, we update the Swap (saving also the epoch of the latest updated Cover).

-ps: Most likely the next version of our Smart Contract, we’ll provide a more efficient way to retrieve the updated Threshold.

ExecuteJob

This job liquidates and processes every liquidable or expired Swaps

  1. Retrieve the lastest updated price from the DB
  2. Retrieve the current machine timestamp
  3. Retrieve the liquidable/executable Swaps
    - a. SwapType = 0 (AssetDirection) Threshold <= price
    - b. SwapType = 1 (CurrencyDirection) Threshold >= price
    - c. ExpiredSwaps executionTime <= current machine timestamp
  4. Lock all the liquidable/executable Swaps
  5. For foreach liquidable/executable Swap
    - a. SwapType = 0 (AssetDirection) we call the marginCall method from the Smart Contract
    - b. SwapType = 1 (CurrencyDirection) we call the marginCall method from the Smart Contract
    - c. ExcpiredSwap we call the ExecuteSwap method from the Smart Contract

Pay attention: for each call to a method of the SmartContract you will pay fee. To save $ETH we implemented a static call before every direct call. If the call is thus reversed, we avoid paying process fees on Ethereum. This mechanics could be a bottleneck in cases of high levels of competition.

The official liquidator is distributed as a docker image. In order to run a liquidator, you should fill the .env file properly and run the docker-compose file published from our side.

This Medium is an alpha coming from our private testnet , it will also be summarised soon in MetalSwap ‘s docs page!

What a Time to be in DEFI!

⚠️Warning⚠️

❗ 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.

To the MetalSwap!

… and beyond!

✎ What is MetalSwap?

MetalSwap is a decentralized platform that allows hedging swaps on financial markets with the aim of providing a coverage to those who work with commodities and an investment opportunity for those who contribute to increase the shared liquidity of the project. Allowing the protection for an increasing number of operators.

With MetalSwap we enable hedge swap transactions through the use of Smart Contracts, AMM style.

Kovan TestNet Phase3 infobox:

https://test.metalswap.finance/

USDTTest -> 0x6dC0A769E20b4Ff186003f264C03E10d23d1A2f7

WBTCTest -> 0x4fE0C5E225a3bE22f8736225469dD8D86aDC0881

XMTT3 -> 0x63cFf925F431AdEA8a5cb19559dE4924E737E3fE

Links

OFFICIAL WEBSITE 👉🏻 https://www.metalswap.finance/

YOUTUBE 👉🏻 https://www.youtube.com/channel/UCuFVZOpAOzuzlmJbPA4wdkw

MEDIUM 👉🏻 https://medium.com/metalswap

TWITTER 👉🏻 https://twitter.com/MetalSwapNet

DISCORD 👉🏻 https://discord.com/invite/eYay7NV6vF

TELEGRAM 👉🏻 https://t.me/metalswap

FACEBOOK 👉🏻https://www.facebook.com/MetalSwapOfficial/

TELEGRAM DISCUSSION GROUPS 👇🏻👇🏻👇🏻

🇬🇧 https://t.me/joinchat/YaSNSVggeOFhODQ0

🇮🇹 https://t.me/joinchat/w4xGj8TcB8ExZmY8

🇪🇸 https://t.me/joinchat/22zaxw3o0URlODJk

--

--