off-chain calculation for different DEXs
dexc is a go sdk to calculate swaps off-chain for various DEXs like uniswap-v2 + forks / uniswap-v3 / ...
The calculations are thread-save, to update the state for a specific pair, you need to use the setter functions.
Installation
go get github.com/tripolious/dexc
Uniswap V2 and forks
To create a dex-pair you just need the fee the fork raises (30 = 0.3%, 25 = 0.25%, ...)
Before a calculation you need to set the current state (reserve0, reserve1 *uint256.Int)
You can find an example here: uniswapV2 example
Benchmark
Benchmark_uniswapV2_GetAmountOut_not_zeroForOne-10 18144783 58.87 ns/op 32 B/op 1 allocs/op
Benchmark_uniswapV2_GetAmountOut_zeroForOne-10 20268572 58.75 ns/op 32 B/op 1 allocs/op
Uniswap V3
To create a pool you need the tickSpacing and fee from the pool.
Before a calculation you need to set the current state (tick int, sqrtPriceX96, liquidity *uint256.Int) and the tickList (tickList []Tick)
You can find an example here: uniswapV3 example
Benchmark
Benchmark_uniswapV3_GetAMountOut_in_range_not_zeroForOne-10 321951 3593 ns/op 2648 B/op 83 allocs/op
Benchmark_uniswapV3_GetAMountOut_in_range_zeroForOne-10 374823 3134 ns/op 2424 B/op 76 allocs/op
Benchmark_uniswapV3_GetAMountOut_out_of_range-10 33720 35531 ns/op 20032 B/op 620 allocs/op
Benchmark_uniswapV3_GetAMountOut_out_of_range_no_limit-10 382 3119899 ns/op 2070209 B/op 67729 allocs/op
Next
- Add tests for uniswapV3
- Next DEXs
Contributing
Your help is welcome here!
- If you have a question, feedback or a bug report, please open a new issue
- Try to make small pull requests
- Every pull request should be covered by unit tests and should be discussed first in an issue
- Please run
make lint
before submitting a PR (you need staticcheck installed).