A user asked me in Discord about the arb.id
variable in my last post, and that triggered a thought (“Shit, I forgot to mention the arbitrage IDs!”)
I’m working on another post to be released soon, but this is important enough that I stopped to write this one to help you use the previous framework.
Arbitrage IDs are a nice way to give a deterministic label for each path. In a previous version of the bot I was storing a pointer to the object itself, but that made blacklisting arbitrage pathways difficult.
I had been learning about keccak hashing at the time, and decided that I could use it as a way to generate unique IDs.
The technique is very simple: generate a string of all pool addresses in a particular arb path together (with the 0x prefix stripped), keccak the string, and store that as the ID.
This has the desirable property of generating a reproducible ID of a constant length from just the pool addresses.
For example, let’s consider a 2pool arb using the DAI-WETH pairs on UniswapV2 and Sushiswap. The UniswapV2 address is 0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11 and the Sushiswap LP is 0xC3D03e4F041Fd4cD388c549Ee2A29a9E5075882f.
The web3py code to generate a hash for this arb path of DAI/WETH (UniswapV2) → DAI/WETH (Sushiswap) is:
>>> import web3
>>> w3.keccak(hexstr="0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11"[2:]+"0xC3D03e4F041Fd4cD388c549Ee2A29a9E5075882f"[2:]).hex()
'0x78158f16b70a46b03d15522a962881e72fe5ba692840df41e4f3625417b5e4f5'
And the opposite path, DAI/WETH (Sushiswap) → DAI/WETH (UniswapV2) is:
>>> w3.keccak(hexstr="0xC3D03e4F041Fd4cD388c549Ee2A29a9E5075882f"[2:]+"0xA478c2975Ab1Ea89e8196811F51A7B7Ade33eB11"[2:]).hex()
'0x111ac105d964408bbebc57475bf20d560a3eca7d835779bf7eea88fafbaaa91c'
A more generalized function to hash a path with an arbitrary number of pools is:
>>> w3.keccak(hexstr="".join([pool[2:] for pool in pools]))
Where pools
is some list of 0x-prefixed hex addresses.
I’ve added this new tag to my 2pool and triangle arb parsers, which are below. If you have no idea what I’m talking about, read the lessons covering Arbitrage Pathfinding with NetworkX, Automatic Arbitrage Path Building and JSON Data Exchange.