WIP Documentation For Adding New Pool Types To SOR
SOR repo is here. Please make any PRs against develop
branch.
New Pool Type:
A new pool should be added under ./src/pools/newPool implementing PoolBase
See ./src/pools/weightedPool/weightedPool.ts as an example
Required maths functions:
_exactTokenInForTokenOut
- Computes how many tokens can be taken out of a pool if exactTokenIn is sent. Should be EVM equivalent (we are currently moving from BigNumber.js to BigInt for these for more efficiency).
_tokenInForExactTokenOut
- Computes how many tokens must be sent to a pool in order to take exactTokenOut. Should be EVM equivalent (we are currently moving from BigNumber.js to BigInt for these for more efficiency).
Balancer's SOR calculates the mathematically optimal solution that ends up bringing all sources of liquidity used to the same spotPrice after the trade happens. This way there is no arbitrage opportunity possible, which means the trade was the best possible given the liquidity sources used.
To make this possible, all pools need to define a method that determines the spotPrice (_spotPriceAfterSwapExactTokenInForTokenOut
& _spotPriceAfterSwapTokenInForExactTokenOut
) and also the derivative of the spotPrice (_derivativeSpotPriceAfterSwapExactTokenInForTokenOut
& _derivativeSpotPriceAfterSwapTokenInForExactTokenOut
) after any trade. The derivative is necessary because the SOR uses a newtonian method for efficiency.
(For more information on how the SOR works please visit https://docs.balancer.fi/developers/smart-order-router)
in the case swapExactIn, let's say that the swap outcome function is
a_out ( a_in )
The spotPriceAfterSwap function is equal to
1 / (a_out)'
which is a function of a_in.
_derivativeSpotPriceAfterSwap is the derivative of this last function.
in the case swapExactOut, let's say that the swap outcome function is
a_in ( a_out )
The spotPriceAfterSwap function is equal to
(a_in)'
which is a function of a_out
_derivativeSpotPriceAfterSwap is the derivative of this last function.
normalizedLiquidity
:
Tests - at a minimum please add tests based off following template to ./test:
Onchain Data:
Further Code Updates:
Test/Debug:
Running example in test/testScripts/swapExample.ts
is useful to test/debug issues. You can add your own tokens that exist in the custom pool. SUBGRAPH_URLS
can be changed to use any beta subgraph url.