Skip to content

Instantly share code, notes, and snippets.

@kmcintyre
Last active March 17, 2021 19:17
Show Gist options
  • Save kmcintyre/28ae26fa1d0bc3e0358e5646cad1f0fd to your computer and use it in GitHub Desktop.
Save kmcintyre/28ae26fa1d0bc3e0358e5646cad1f0fd to your computer and use it in GitHub Desktop.
import websocket
import json
import pprint
from web3 import Web3
from eth_abi import decode_single, decode_abi
subs = [{
'id': 1,
'method': 'eth_subscribe',
'params': ['logs', {}],
}, {
'id': 2,
'method': 'eth_subscribe',
'params': ['newHeads'],
}]
newHeadsSortedKeys = set([])
def result_pendingTransactions(res):
print('pending tx')
def result_newHeads(res):
k = ' '.join(res.keys())
if k not in newHeadsSortedKeys:
#print('newHeads:', res.keys())
newHeadsSortedKeys.add(k)
prefix = '000000000000000000000000'
recognized_addr = {
'0x52e49872153fb72f96a0e4a04356d06d5b4aacd8': 'km',
'0xfeb7e3fe76a60658221033a5e8f6c652ba8fadf1': 'dl',
'0x7ceaaddcff77ac786c893109cd8f3d3b0c875f53': 'jd',
'0x4eaaeef2de237b154fc0f28e26b2a7297f4c435e': 'dm'
}
static_addr = {
'0x0000000000000000000000000000000000000000': 'actor 0',
}
common_addr = {
'0xdccd412f0b1252819cb1fd330b93224ca42612892bb3f4f789976e6d81936496': 'burn',
'0x25f8daaa4635a7729927ba3f5b3d59cc3320aca7c32c9db4e7ca7b9574343640': 'proposal',
'0x1c411e9a96e071241c2f21f7726b17ae89e3cab4c78be50e062b03a9fffbbad1': 'signature',
'0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef': 'transfer',
'0xd78ad95fa46c994b6551d0da85fc275fe613ce37657fb8d5e3d130840159d822': 'swap',
'0x7fcf532c15f0a6db0bd6d0e038bea71d30d808c7d98cb3bf7268a95bf5081b65': 'withdrawal',
'0x8c5be1e5ebec7d5bd14f71427d1e84f3dd0314c0f7b2291e5b200ac8c7c3b925': 'approval',
'0x4c209b5fc8ad50758f13e2e1088ba56a560dff690a1c6fef26394f4c03821c4f': 'mint',
'0xe1fffcc4923d04b559f4d29a8bfc6cda04eb5b0d3c460751c2402c5c5cc9109c': 'deposit',
'0x90890809c654f11d6e72a28fa60149770a0d11ec6c92319d6ceb2bb0a4ea1a15': 'deposit 2',
'0x9e71bc8eea02a63969f509818f2dafb9254532904319f9dbda79b67bd34a5f3d': 'stake',
'0xe2403640ba68fed3a2f88b7557551d1993f84b99bb10ff833f0cf8db0c5e0486': 'reward',
'0x803c5a12f6bde629cea32e63d4b92d1b560816a6fb72e939d3c89e1cab650417': 'bridge', #chainbridge
'0x7084f5476618d8e60b11ef0d7d3f06914655adb8793e28ff7f018d4c76d505d5': 'withdraw',
'0xf279e6a1f5e320cca91135676d9cb6e44ca8a08c0b88342bcdb1144f6511b568': 'withdraw 2',
'0x5b859394fabae0c1ba88baffe67e751ab5248d2e879028b8c8d6897b0519f56a': 'unkknon 4',
'0xb0e0a660b4e50f26f0b7ce75c24655fc76cc66e3334a54ff410277229fa10bd4': 'unkknon 5',
'0xe3afa8cd238a27a8f92ebe755184336b9fb0d4c364820aa99de0c94770d73242': 'pango something',
'0x05af636b70da6819000c49f85b21fa82081c632069bb626f30932034099107d8': 'unknown 6',
'0xdbb69440df8433824a026ef190652f29929eb64b4d1d5d2a69be8afe3e6eaed8': 'unknown',
'0xc7606d21ac05cd309191543e409f0845c016120563783d70e4f41419dc0ef234': 'unknown 2',
'0x884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364': 'deposit relay ?',
'0x4ec90e965519d92681267467f775ada5bd214aa92c0dc93d90a5e880ce9ed026': 'claimed',
'0x04afd2ce457d973046bd54f5d7d36368546da08b88be1bca8ae50e32b451da17': 'multisended',
'0x6f30e1ee4d81dcc7a8a478577f65d2ed2edb120565960ac45fe7c50551c87932': 'punk bid withdrawn'
}
defi_addr = {
'0x171356c0dc284df6bb9e1dad8161b41af97f9116': 'merkle airdrop',
'': 'png airdrop',
'0xe54ca86531e17ef3616d22ca28b0d458b6c89106': 'pangolin',
'0x4e550fefbf888cb43ead73d821f646f43b1f2309': 'pango lp png-eth pool',
'0x7accc6f16bf8c0dce22371fbd914c6b5b402bf9f': 'pango lp png-usdt pool',
'0x797cbcf107519f4b279fc5db372e292cdf7e6956': 'pango lp axax-yfi pool',
'0x4ad6e309805cb477010bea9ffc650cb27c1a9504': 'pango lp png-link pool',
'0x99b06b9673fea30ba55179b1433ce909fdc28723': 'pango lp png-wbtc pool',
'0x8866077f08b076360c25f4fd7fbc959ef135474c': 'pango lp png-dai pool',
'0x6955cb85edea63f861c0be39c3d7f8921606c4dc': 'pango lp png-sushi pool',
'0x41188b4332fe68135d1524e43db98e81519d263b': 'pango lp png-uni pool',
'0xb921a3ae9ceda66fa8a74dbb0946367fb14fae34': 'pango lp png-aave pool',
'0x2061298c76cd76219b9b44439e96a75f19c61f7f': 'pango lp png-yfi pool',
'0xd7538cabbf8605bde1f4901b47b8d42c61de0367': 'pangolin lp',
'0x9ee0a4e21bd333a6bb2ab298194320b8daa26516': 'pangolin lp2',
'0x17a2e8275792b4616befb02eb9ae699aa0dcb94b': 'pangolin lp avax-link pool',
'0x1acf1583bebdca21c8025e172d8e8f2817343d65': 'pangolin lp avax-uni pool',
'0xa1c2c3b6b120cbd4cec7d2371ffd4a931a134a32': 'pangolin lp avax-link reward',
'0x18c8e1346d26824063706242adb391ddb16c293e': 'pangolin lp6',
'0x7a6131110b82dacbb5872c7d352bfe071ea6a17c': 'pangolin lp wbtc-avax pool',
'0x5f233a14e1315955f48c5750083d9a44b0df8b50': 'pangolin lp aave-avax',
'0x7a886b5b2f24ed0ec0b3c4a17b930e16d160bd17': 'pangolin lp yfi-avax',
'0xbbc7fff833d27264aac8806389e02f717a5506c9': 'pangolin lp9',
'0x53b37b9a6631c462d74d65d61e1c056ea9daa637': 'pangolin lp10',
'0xe8acf438b10a2c09f80aef3ef2858f8e758c98f9': 'pangolin lp11',
'0xd8b262c0676e13100b33590f10564b46eef652ad': 'pangolin lp12',
'0xf105fb50fc6ddd8a857bbecd296c8a630e8ca857': 'pangolin lp13',
'0x92dc558cb9f8d0473391283ead77b79b416877ca': 'pangolin lp14',
'0x7d7ecd4d370384b17dfc1b4155a8410e97841b65': 'pangolin ?',
'0x88f26b81c9cae4ea168e31bc6353f493fda29661': 'pangolin ? 2',
'0xa329d806fbc80a14415588334ae4b205813c6bb2': 'sudo su complus',
'0x78c18e6be20df11f1f41b9635f3a18b8ad82ddd1': 'complus',
'0xf0ed25fd26e0b64c86c6c78b661f2ef283e9b6ff': 'complus lp',
'0xecee953e187d9e82d57baedaaef1e56e5283f5c3': 'complus lp2',
'0xcea22ad29bfcf13fd1576d0994c134406dc58d7e': 'complus lp3',
'0x4153552cbf87686e7b092029c8e08bd1da9b4416': 'complus lp4',
'0x9309ec8333a65c58b319f4405e199463a2e5c91a': 'complus maestro',
'0x262dcfb36766c88e6a7a2953c16f8defc40c378a': 'yeti',
'0x07099b26f36fcb7e086d5a879ec1261271319829': 'yeti lp',
'0x85995d5f8ee9645ca855e92de16fa62d26398060': 'zero',
'0xfa2c38470ad0a970240cf1afd35cd04d9e994e76': 'zero 1',
'0x45c2755eefa0eb96ce15c2f6fdc48346da7f3a7e': 'zero 2',
'0x60f19487bda9c2f8336784110dc5c4d66425402d': 'zero 3',
'0x7b35150abde10f98f44ded0d02e7e942321fbbe0': 'zero lp',
'0x4cea032b4b3f59f31d6d52071258ee0d42b6cc7e': 'zero lp2',
'0x332719570155dc61bec2901a06d6b36faf02f184': 'zero lp3',
'0x17766e5dd91fdc14c24cd9847e5e93fd62a05afd': 'zero lp4',
'0x0751f9a49d921aa282257563c2041b9a0e00eb78': 'zero lp5',
'0xd3694aeb35db0d73a4d1e83ffe8f462e8202ed0f': 'zero ?',
'0xdcedf06fd33e1d7b6eb4b309f779a0e9d3172e44': 'zero ? 2',
'0x40a07f36655a0724557ca53a9e5d1b5018e9df32': 'zero ? 3',
'0x8754699cf9f32b56654f7da44ff580bdf09f3526': 'zero ? 4',
'0x1eC206a9dD85625E1940cD2B0c8e14a894D2e9ac': 'ice',
'0xb12531a2d758c7a8bf09f44fc88e646e1bf9d375': 'ice_queen',
'0x751089f1bf31b13fa0f0537ae78108088a2253bf': 'ice_avax_sushi',
'0x586554828ee99811a8ef75029351179949762c26': 'ice_avax_eth',
'0x621207093d2e65bf3ac55dd8bf0351b980a63815': 'ice_avax_png',
'0x14ec55f8b4642111a5af4f5ddc56b7be867eb6cc': 'ice_strategy',
'0x1ec206a9dd85625e1940cd2b0c8e14a894d2e9ac': 'ice_strategy 2',
'0xc9a51fb9057380494262fd291aed74317332c0a2': 'ice_avax_png 2',
}
nft_addr = {
'0x3c7b682d5da98001a9b8cbda6c647d2c63d698a4184fd1d55e2ce7b66f5d21eb': 'offer punk',
'0x8a0e37b73a0d9c82e205d4d1a3ff3d0b57ce5f4d7bccf6bac03336dc101cb7ba': 'alavanche punk',
'0x58e5d5a525e3b40bc15abaa38b5882678db1ee68befd2f60bafe3a7fd06db9e3': 'bought punk'
}
known_addr = {}
for d in [recognized_addr, static_addr, common_addr, defi_addr, nft_addr]:
for k in d:
if len(k) == 42:
known_addr[k[:2] + prefix + k[2:]] = d[k]
else:
known_addr[k] = d[k]
unknown_addr = {}
def is_known(t):
if t not in unknown_addr and t not in known_addr:
unknown_addr[t] = 0
elif t in known_addr.keys() :
return known_addr[t]
unknown_addr[t] += 1
return str(unknown_addr[t]) + '-' + t
def clean_it(k):
ans = k.split('-')[-1]
if len(ans) == 66:
return ans.replace(prefix,'')
return ans
existing_hash = None
def result_logs(res):
global existing_hash
if 'topics' not in res and 'data' not in res:
ws.close()
topics = res['topics']
data = res['data']
known = [is_known(t) for t in topics]
tx_hash = res['transactionHash']
if existing_hash != tx_hash:
print('')
print('tx:', tx_hash)
existing_hash = tx_hash
if known[0] in ['signature']:
print('\t', known[0] + ':', known, 'data length:', len(data))
elif '-' not in known[0]:
print('\t', known[0] + ':', [clean_it(k) for k in known[1:]], 'data length:', len(data))
else:
print('\t', 'topics:', len(topics), 'known:', known, 'data length:', len(data))
exit(-1)
def on_message(ws, message):
try:
msg = json.loads(message)
if 'params' in msg and 'result' in msg['params']:
result = msg['params']['result']
sub = [s for s in subs if s['subscription_id'] == msg['params']['subscription']][0]
sub['on_result'](result)
elif 'result' in msg:
print('subscribed:', msg['id'])
sub = [s for s in subs if s['id'] == msg['id']][0]
sub.update({'subscription_id': msg['result'], 'on_result': globals()['result_' + sub['params'][0]]})
remaining_subs = [s for s in subs if 'subscription_id' not in s]
if len(remaining_subs) > 0:
ws.send(json.dumps(remaining_subs[0]))
else:
print('unknown:', msg)
except Exception as e:
print('msg:', msg)
print('bad message:', e)
ws.close()
def on_open(ws):
def run(*args):
ws.send(json.dumps(subs[0]))
thread.start_new_thread(run, ())
def on_error(ws, error):
print('error:', error)
def on_close(ws):
print('closed')
try:
import thread
except ImportError:
import _thread as thread
import time
if __name__ == '__main__':
ws = websocket.WebSocketApp('ws://192.168.1.3:9650/ext/bc/C/ws',
on_open = on_open,
on_message = on_message,
on_error = on_error,
on_close = on_close
)
ws.run_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment