Package: uniswap-smart-path
Uniswap Smart PathProject InformationCode QualityOverviewWith several V2 and V3 pools, and 2 or 3 tokens per path, there may be quite a few routes to perform a swap.
And if you add the gas fees into the equation, it is not straightforward to be sure to get the best deal.The object of this library is to find the path(s), from v2 and v3 pools, to swap with the best price,
including gas fees, and to return it/them in order to be used directly with theUR codec,
along with a percentage to know how to divide the amount between them.⚠ This project is a work in progress. Not all features have been implemented yet.
For instance the gas fees is not evaluated in the current version.⚠ This tool does not replace your own due diligence to find the best price/path to swap any token.InstallationA good practice is to usePython virtual environments, here is atutorial.The library can be pip installed frompypi.orgas usual:# update pip to latest version if neededpipinstall-Upip# install the decoder from pypi.orgpipinstalluniswap-smart-pathUsageThe library exposes a class,SmartPathwith a public methodget_swap_in_path()that can be used to find the best path/price.
For performance's sake, it is asynchronous. Currently, it supports only the Ethereum blockchain.fromuniswap_smart_pathimportSmartPathsmart_path=awaitSmartPath.create(async_w3)# where async_w3 is your AsyncWeb3 instancepath=awaitsmart_path.get_swap_in_path(amount_in_wei,token0_address,token1_address)You can also create theSmartPathinstance from a rpc endpoint:fromuniswap_smart_pathimportSmartPathsmart_path=awaitSmartPath.create(rpc_endpoint=rpc_endpoint)V2 or V3 pools onlyThe factory methodSmartPath.create_v2_only()can be used to create aSmartPathinstance that will look for the best path in V2 pools only.
Currently, it supports only the Ethereum blockchain.fromuniswap_smart_pathimportSmartPathsmart_path=awaitSmartPath.create_v2_only(rpc_endpoint=rpc_endpoint)# could also use an AsyncWeb3 instance i/o rpcSame thing if you wish to look into V3 pools only, just useSmartPath.create_v3_only()Currently, it supports only the Ethereum blockchain.fromuniswap_smart_pathimportSmartPathsmart_path=awaitSmartPath.create_v3_only(rpc_endpoint=rpc_endpoint)# could also use an AsyncWeb3 instance i/o rpcCustom pools and blockchainsA custom SmartPath can be created with the factory methodSmartPath.create_custom()fromuniswap_smart_pathimportSmartPathpivot_tokens=(wbnb_address,usdt_address,usdc_address,dai_address)# BSC addressesv3_pool_fees=(100,500,2500,10000)# Pancakeswap v3 feessmart_path=awaitSmartPath.create_custom(w3,# BSC AsyncWeb3 instancepivot_tokens=pivot_tokens,v3_pool_fees=v3_pool_fees,v2_router=pancakeswapv2_address,v2_factory=pancakeswapv2_factory,v3_quoter=pancakeswapv3_quoter_address,v3_factory=pancakeswapv3_factory,)ResultExamples of output paths that you can use with theUR codecto encode a transaction.Single path({'path':('0xD533a949740bb3306d119CC777fa900bA034cd52',3000,'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2',3000,'0x9f8F72aA9304c8B593d555F12eF6589cC3A579A2'),'function':'V3_SWAP_EXACT_IN','weight':100,'estimate':128331138758276360764},)Multi pathHere, 90% of the amount should be swapped on a V3 pool and 10% on a V2 pool, all that in a single transaction using the UR codec.({'path':('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984','0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),'function':'V2_SWAP_EXACT_IN','weight':10,'estimate':32858922292711987411},{'path':('0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984',3000,'0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'),'function':'V3_SWAP_EXACT_IN','weight':90,'estimate':295000857928717844546})