8.1 What is a Swap and why to Swap
Last updated
Last updated
MerlinProtocol, prefiguring itself as a system of management for private investment funds with index model, needs a periodic rebalancing of the assets maintained on the smart contract of the fund.
The rebalancing therefore allows the automatic sale of tokens no longer desirable according to the decision criteria of the fund, and the purchase of new tokens to make up the new composition of the funds managed by MerlinProtocol.
The rebalancing of individual funds is done through a swap function, where the goal is to execute an atomic transaction through Uniswap v3 that allows for the exchange of incoming and outgoing assets.
Uniswap v3 is a protocol for exchanges between ERC20 with automatic liquidity that guarantees users pools with good levels of liquidity with respect to the exchange pairs, a very important factor to avoid potential problems with automated trades linked to "Sandwich Attack".
Uniswap v3 has been chosen for the MerlinProtocol project instead of traditional crypto exchanges because of the innovative features of this DeFi protocol, which allows not only to directly exchange ERC20 token pairs even in non-standard formats (it is sufficient that they respect the official EIP specifications) and perform very fast operations through flash swapping, but also to easily implement decentralized oracles for reading market statistics.
The ability to directly exchange ERC20 tokens with other ERC20 tokens via "swap" allows to drastically reduce transaction and gas expenses resulting from the traditional ERC20_A -> ETH -> ERC20_B swap.
An example of a direct swap between two ERC20 tokens is shown in the image below:
From the point of view of the smart contracts of the funds that will forward the swap requests to the protocol, the procedure will be very straightforward: the choice of an input token is required, and one for the output token.
It is also necessary to specify the input quantity, after which the protocol calculates the corresponding amount of output token that the smart contract will receive with the swap.
The swap is executed quickly, with the output tokens received briefly by the smart contract, without the need for order books typical of traditional exchanges to represent the liquidity that is instead guaranteed by Uniswap itself through liquidity providers.