UniswapV2Library contract is a contract that helps the router, or any other Uniswap Client Contract to interact with the Pair contract. It helps sort tokens (remember that for the pair contract it’s not the same token0 than token1), get reserves for an specific pair, and calculates the optimal in/out amount someone needs to send to swap an specific quantity of an specific token.
In the following we are going to analize each function or characteristic of the UniswapV2Library contract, originally written in Solidity for EVM’s, and we are going to think on how we could implement those functionalities and functions in rust for Soroban:
Safe Math:
In Solidity, the SafeMath library is used to validate arithmetic operations and prevent integer overflow/underflow. Should such a situation arise, the library throws an exception, which effectively reverts the transaction. In Rust, we can achieve similar functionality by activating the overflow check flag during the compilation process with the following code:checked_add, checked_mul, checked_div, and checked_sub. You can explore these functions and test their functionality in this repository: Overflow Soroban Repository
Also, we have overflow-safe functions checked_add, checked_mul, checked_div and checked_sub
You can check and test these techniques in the following repository: Overflow Soroban Repository
In conclusion, Soroswap prevents overflows by leveraging these techniques.
The sortTokens function:
This function returns sorted token addresses, used to handle return values from pairs sorted in this order.
In Solidity the code is like this:
require statement won’t be implemented.
The pairFor function
This function calculates the CREATE2 address for a pair without making any external calls. This is because UniswapV2Factory uses the CREATE2 opcode by carefully selecting a unique salt value in order to have a deterministic contract address for each (factory, token0, token1) combination (and of course the same pair contract code)
The whole idea of this function is to avoid an external call to the Factory contract in order to ask it what the pair address is… In Soroban this external call won’t be expensive… and it’s not event direct that calculating this address will be cheaper than the external call
Can we do the same with Soroban? The answer is yes!
In our SoroswapFactory contract, when we deploy a new Pair contract, we do it like this:
salt is an unique identifier that will be used to calculate the new smart contract address. In fact, the new smart contract address depends only in the combination of the deployer address and the salt.
In fact, in the salt in the factory is created by a combination of the token_0 and token_1 address:
The getReserves function
This function fetches and sorts the reserves for a pair.
It’s code in Solidity is:
The quote function
This function given some amount of an asset and pair reserves, returns an equivalent amount of the other asset
It’s code in Solidity is:
The getAmount... functions
There are four important functions in Uniswap’s code: getAmountOut, getAmountIn, getAmountsOut and getAmountsIn that are similar. These functions are used for making trades and managing liquidity in Uniswap.
- `getAmountOut“ calculates how much of one token you can get when you put in another token, considering fees and available reserves.
getAmountIndoes the opposite, determining how much you need to put in to get a specific amount of another token.getAmountsOutand `getAmountsIn“ help with more complex trades that involve multiple steps, like going from Token A to Token B to Token C. They make sure you get the right amount at each step.