Skip to content

Commit

Permalink
WIP - oracle fee modeling
Browse files Browse the repository at this point in the history
  • Loading branch information
poliwop committed Feb 7, 2025
1 parent 9a5a111 commit 21b9ac1
Showing 1 changed file with 55 additions and 64 deletions.
119 changes: 55 additions & 64 deletions hydradx/notebooks/Omnipool/oracle_data_exploration.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"metadata": {
"collapsed": true,
"ExecuteTime": {
"end_time": "2025-02-06T21:04:58.156591Z",
"start_time": "2025-02-06T21:04:58.143361Z"
"end_time": "2025-02-07T20:43:12.654184Z",
"start_time": "2025-02-07T20:43:12.627926Z"
}
},
"source": [
Expand Down Expand Up @@ -160,55 +160,59 @@
"\n",
"# Define the log data\n",
"log_data = {\n",
" \"data\": \"0x0000000000000000000000000000000000000000000000000000048c27395000\",\n",
" \"data\": \"0x00000000000000000000000000000000000000000000000000000000000000600000000000000000000000000000000000000000000000000000053126afd5c6000000000000000000000000000000000000000000000000000000006690e3f70000000000000000000000000000000000000000000000000000000000000008574254432f555344000000000000000000000000000000000000000000000000\",\n",
" \"topics\": [\n",
" \"0x3115d1449a7b732c986cba18244e897a450f61e1bb8d589cd2e69e6c8924f9f7\",\n",
" \"0x0000000000000000000000000000000000000000000000000000000100000005\",\n",
" \"0x0000000000000000000000001232508adcaf57c6e78a850f9d715e3694b52000\",\n",
" \"0x0000000000000000000000001232508adcaf57c6e78a850f9d715e3694b52000\"\n",
" \"0xa7fc99ed7617309ee23f63ae90196a1e490d362e6f6a547a59bc809ee2291782\"\n",
" ],\n",
" \"address\": \"0x1b02e051683b5cfac5929c25e84adb26ecf87b38\"\n",
" \"address\": \"0xdee629af973ebf5bf261ace12ffd1900ac715f5e\"\n",
" }\n",
"\n",
"\n",
"\n"
],
"outputs": [],
"execution_count": 48
"execution_count": 22
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-06T21:04:58.206748Z",
"start_time": "2025-02-06T21:04:58.200278Z"
"end_time": "2025-02-07T20:43:12.687930Z",
"start_time": "2025-02-07T20:43:12.680793Z"
}
},
"cell_type": "code",
"source": [
"def decode_log_with_abi(log_data, contract_abi):\n",
"from web3 import Web3\n",
"\n",
"def decode_log_with_abi(log_data: dict, contract_abi: list):\n",
" w3 = Web3()\n",
"\n",
" # Extract event signature (first topic)\n",
" event_signature_hex = log_data[\"topics\"][0]\n",
" # Normalize log's event signature hash\n",
" raw_log_hash = log_data[\"topics\"][0].lower()\n",
" log_event_hash = raw_log_hash[2:] if raw_log_hash.startswith(\"0x\") else raw_log_hash\n",
"\n",
" # Find matching event in ABI\n",
" # Find matching event with normalized comparison\n",
" matching_event = None\n",
" for item in contract_abi:\n",
" if item[\"type\"] == \"event\":\n",
" # Generate event signature hash\n",
" args_types = [inp[\"type\"] for inp in item[\"inputs\"]]\n",
" event_signature = f\"{item['name']}({','.join(args_types)})\"\n",
" computed_hash = w3.keccak(text=event_signature).hex()\n",
" if item.get(\"type\") == \"event\":\n",
" # Build event signature\n",
" inputs = [inp[\"type\"] for inp in item[\"inputs\"]]\n",
" signature = f\"{item['name']}({','.join(inputs)})\"\n",
"\n",
" # Compute and normalize our hash\n",
" computed_raw = w3.keccak(text=signature).hex().lower()\n",
" computed_hash = computed_raw[2:] if computed_raw.startswith(\"0x\") else computed_raw\n",
"\n",
" if computed_hash == event_signature_hex:\n",
" if computed_hash == log_event_hash:\n",
" matching_event = item\n",
" break\n",
"\n",
" if not matching_event:\n",
" available_events = [e[\"name\"] for e in contract_abi if e.get(\"type\") == \"event\"]\n",
" print(f\"No matching event found. Available events: {available_events}\")\n",
" matching_event = \"OracleUpdate\"\n",
" # raise ValueError(f\"No matching event found. Available events: {available_events}\")\n",
" available = [e[\"name\"] for e in contract_abi if e.get(\"type\") == \"event\"]\n",
" raise ValueError(\n",
" f\"No event matches normalized hash {log_event_hash}\\n\"\n",
" f\"Available events: {available}\"\n",
" )\n",
"\n",
" # Create dummy log entry with required fields\n",
" log_entry = {\n",
Expand All @@ -217,39 +221,31 @@
" \"logIndex\": 0,\n",
" \"transactionIndex\": 0,\n",
" \"transactionHash\": b'\\x00' * 32,\n",
" \"address\": log_data[\"address\"],\n",
" \"address\": None,\n",
" \"blockHash\": b'\\x00' * 32,\n",
" \"blockNumber\": 0\n",
" }\n",
"\n",
" # Decode using the matched event\n",
" event_abi = next(\n",
" item for item in abi\n",
" if item.get(\"type\") == \"event\"\n",
" and item.get(\"name\") == \"OracleUpdate\"\n",
" )\n",
" print(event_abi)\n",
" contract = w3.eth.contract(abi=[event_abi])\n",
" contract_event = contract.events.OracleUpdate()\n",
" contract = w3.eth.contract(abi=[matching_event])\n",
" event_class = getattr(contract.events, matching_event[\"name\"])\n",
" decoded = event_class().process_log(log_entry)\n",
"\n",
" # event_class = getattr(contract.events, matching_event[\"name\"])\n",
" decoded_log = contract_event().process_log(log_entry)\n",
" print(decoded_log)\n",
" # return {\n",
" # \"event_name\": matching_event[\"name\"],\n",
" # \"args\": dict(decoded.args),\n",
" # \"raw_log\": decoded\n",
" # }"
" return {\n",
" \"event_name\": matching_event[\"name\"],\n",
" \"args\": dict(decoded[\"args\"]),\n",
" \"raw_log\": decoded\n",
" }"
],
"id": "5bb7520c6016c4c7",
"outputs": [],
"execution_count": 49
"execution_count": 23
},
{
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-06T21:04:58.297515Z",
"start_time": "2025-02-06T21:04:58.264942Z"
"end_time": "2025-02-07T20:43:12.739162Z",
"start_time": "2025-02-07T20:43:12.733905Z"
}
},
"cell_type": "code",
Expand All @@ -268,32 +264,27 @@
"name": "stdout",
"output_type": "stream",
"text": [
"No matching event found. Available events: ['OracleUpdate', 'UpdaterAddressChange']\n",
"{'anonymous': False, 'inputs': [{'indexed': False, 'internalType': 'string', 'name': 'key', 'type': 'string'}, {'indexed': False, 'internalType': 'uint128', 'name': 'value', 'type': 'uint128'}, {'indexed': False, 'internalType': 'uint128', 'name': 'timestamp', 'type': 'uint128'}], 'name': 'OracleUpdate', 'type': 'event'}\n",
"{'args': {'key': 'WBTC/USD', 'value': 5708660594118, 'timestamp': 1720771575}, 'event': 'OracleUpdate', 'logIndex': 0, 'transactionIndex': 0, 'transactionHash': b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00', 'address': '0xdee629af973ebf5bf261ace12ffd1900ac715f5e', 'blockHash': b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00', 'blockNumber': 0}\n"
]
},
{
"ename": "TypeError",
"evalue": "'NoneType' object is not subscriptable",
"output_type": "error",
"traceback": [
"\u001B[0;31m---------------------------------------------------------------------------\u001B[0m",
"\u001B[0;31mTypeError\u001B[0m Traceback (most recent call last)",
"Input \u001B[0;32mIn [50]\u001B[0m, in \u001B[0;36m<cell line: 3>\u001B[0;34m()\u001B[0m\n\u001B[1;32m 1\u001B[0m \u001B[38;5;66;03m# Usage\u001B[39;00m\n\u001B[1;32m 2\u001B[0m decoded \u001B[38;5;241m=\u001B[39m decode_log_with_abi(log_data, abi)\n\u001B[0;32m----> 3\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124mDecoded \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mdecoded[\u001B[38;5;124m'\u001B[39m\u001B[38;5;124mevent_name\u001B[39m\u001B[38;5;124m'\u001B[39m]\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m:\u001B[39m\u001B[38;5;124m\"\u001B[39m)\n\u001B[1;32m 4\u001B[0m \u001B[38;5;28;01mfor\u001B[39;00m arg, value \u001B[38;5;129;01min\u001B[39;00m decoded[\u001B[38;5;124m\"\u001B[39m\u001B[38;5;124margs\u001B[39m\u001B[38;5;124m\"\u001B[39m]\u001B[38;5;241m.\u001B[39mitems():\n\u001B[1;32m 5\u001B[0m \u001B[38;5;28mprint\u001B[39m(\u001B[38;5;124mf\u001B[39m\u001B[38;5;124m\"\u001B[39m\u001B[38;5;132;01m{\u001B[39;00marg\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m: \u001B[39m\u001B[38;5;132;01m{\u001B[39;00mvalue\u001B[38;5;132;01m}\u001B[39;00m\u001B[38;5;124m\"\u001B[39m)\n",
"\u001B[0;31mTypeError\u001B[0m: 'NoneType' object is not subscriptable"
"Decoded OracleUpdate:\n",
"key: WBTC/USD\n",
"value: 5708660594118\n",
"timestamp: 1720771575\n"
]
}
],
"execution_count": 50
"execution_count": 24
},
{
"metadata": {},
"metadata": {
"ExecuteTime": {
"end_time": "2025-02-07T20:43:12.786374Z",
"start_time": "2025-02-07T20:43:12.783887Z"
}
},
"cell_type": "code",
"outputs": [],
"execution_count": null,
"source": "",
"id": "d45ac1bcc978e3c7"
"id": "d45ac1bcc978e3c7",
"outputs": [],
"execution_count": null
}
],
"metadata": {
Expand Down

0 comments on commit 21b9ac1

Please sign in to comment.