-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
trouble getting SQLPad data except for this one query
- Loading branch information
1 parent
d2566cd
commit a9a1fe9
Showing
1 changed file
with
309 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,309 @@ | ||
{ | ||
"cells": [ | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 1, | ||
"id": "7e3246da-b5f4-45e1-9aad-56772bff032b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"from matplotlib import pyplot as plt\n", | ||
"import sys\n", | ||
"import requests\n", | ||
"import base64\n", | ||
"import json\n", | ||
"from pprint import pprint\n", | ||
"from dotenv import load_dotenv\n", | ||
"import os\n", | ||
"\n", | ||
"sys.path.append('../../..')\n", | ||
"from hydradx.model.processing import query_sqlPad" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 2, | ||
"id": "490cc015-4ad5-4a94-870e-97b7171e088b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# query = (\n", | ||
"# f\"with fees as (\"\n", | ||
"# f\"select \"\n", | ||
"# f\" timestamp,\"\n", | ||
"# f\" operation,\"\n", | ||
"# f\" asset_fee_rate,\"\n", | ||
"# f\" protocol_fee_rate,\"\n", | ||
"# f\" amount,\"\n", | ||
"# f\" asset_fee\"\n", | ||
"# f\"from\"\n", | ||
"# f\"(select \"\n", | ||
"# f\" timestamp,\"\n", | ||
"# f\" block,\"\n", | ||
"# f\" who,\"\n", | ||
"# f\" operation,\"\n", | ||
"# f\" (select symbol from token_metadata where id = asset_in limit 1) as asset_in,\"\n", | ||
"# f\" (select symbol from token_metadata where id = asset_out limit 1) as asset_out,\"\n", | ||
"# f\" asset_fee / amount * 100 as asset_fee_rate,\"\n", | ||
"# f\" protocol_fee / amount * 100 as protocol_fee_rate,\"\n", | ||
"# f\" amount,\"\n", | ||
"# f\" amount_in,\"\n", | ||
"# f\" asset_fee\"\n", | ||
"# f\"from \"\n", | ||
"# f\"(select \"\n", | ||
"# f\" timestamp,\"\n", | ||
"# f\" block.height as block,\"\n", | ||
"# f\" args->>'who' as who,\"\n", | ||
"# f\" name as operation,\"\n", | ||
"# f\" (args->>'assetIn')::integer as asset_in, \"\n", | ||
"# f\" (args->>'assetOut')::integer as asset_out,\"\n", | ||
"# f\" (args->>'assetFeeAmount')::numeric as asset_fee,\"\n", | ||
"# f\" (args->>'protocolFeeAmount')::numeric as protocol_fee,\"\n", | ||
"# f\" (args->>'amountOut')::numeric as amount,\"\n", | ||
"# f\" (args->>'amountIn')::numeric as amount_in\"\n", | ||
"# f\"from event \"\n", | ||
"# f\"inner join block on block_id = block.id\"\n", | ||
"# f\"where name like 'Omnipool.%Executed'\"\n", | ||
"# f\"order by block_id asc) as trades) as normalized_trades\"\n", | ||
"# f\"where asset_out = 'DOT' \"\n", | ||
"# f\")\"\n", | ||
"# f\"select * from fees WHERE \"\"timestamp\"\" BETWEEN '2024-01-03T14:43:30.767Z' AND '2024-01-10T14:43:30.767Z'\"\n", | ||
"# )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 3, | ||
"id": "ea395dc7-4f7b-4021-a850-a7ffcc04f77f", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# query = (\n", | ||
"# f\"WITH pair_price AS (select \"\n", | ||
"# f\" timestamp,\"\n", | ||
"# f\" amount_in / amount_out AS price\"\n", | ||
"# f\"FROM normalized_trades\"\n", | ||
"# f\"WHERE asset_in = 'DAI' AND asset_out = 'HDX' \"\n", | ||
"# f\" AND \"\"timestamp\"\" BETWEEN '2023-07-14T19:28:59.673Z' AND '2023-07-21T19:28:59.673Z' \"\n", | ||
"# f\"UNION ALL\"\n", | ||
"# f\"SELECT \"\n", | ||
"# f\" timestamp,\"\n", | ||
"# f\" amount_out / amount_in AS price\"\n", | ||
"# f\"FROM normalized_trades\"\n", | ||
"# f\"WHERE asset_in = 'HDX' AND asset_out = 'DAI' \"\n", | ||
"# f\" AND \"\"timestamp\"\" BETWEEN '2023-07-14T19:28:59.673Z' AND '2023-07-21T19:28:59.673Z' \"\n", | ||
"# f\"ORDER BY timestamp)\"\n", | ||
"# f\"SELECT\"\n", | ||
"# f\" timestamp AS \"\"time\"\",\"\n", | ||
"# f\" price\"\n", | ||
"# f\"FROM pair_price\"\n", | ||
"# f\"ORDER BY 1\"\n", | ||
"# f\"LIMIT 1000\"\n", | ||
"# )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 4, | ||
"id": "2ac68a6d-0943-41e5-9bd4-65deb399bd5d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"query = (\n", | ||
" f\"WITH normalized_trades AS (\"\n", | ||
" f\" SELECT\" \n", | ||
" f\" timestamp,\"\n", | ||
" f\" block,\"\n", | ||
" f\" who,\"\n", | ||
" f\" operation,\"\n", | ||
" f\" (SELECT symbol FROM token_metadata WHERE id = asset_in LIMIT 1) AS asset_in,\"\n", | ||
" f\" (SELECT symbol FROM token_metadata WHERE id = asset_out LIMIT 1) AS asset_out,\"\n", | ||
" f\" amount_in / POWER(10, (SELECT decimals FROM token_metadata WHERE id = asset_in LIMIT 1)) AS amount_in,\"\n", | ||
" f\" amount_out / POWER(10, (SELECT decimals FROM token_metadata WHERE id = asset_out LIMIT 1)) AS amount_out\"\n", | ||
" f\" FROM (\"\n", | ||
" f\" SELECT\" \n", | ||
" f\" timestamp,\"\n", | ||
" f\" block.height AS block,\"\n", | ||
" f\" args->>'who' AS who,\"\n", | ||
" f\" name AS operation,\"\n", | ||
" f\" (args->>'assetIn')::integer AS asset_in,\" \n", | ||
" f\" (args->>'assetOut')::integer AS asset_out,\"\n", | ||
" f\" (args->>'amountIn')::numeric AS amount_in,\" \n", | ||
" f\" (args->>'amountOut')::numeric AS amount_out\"\n", | ||
" f\" FROM event\" \n", | ||
" f\" INNER JOIN block ON block_id = block.id\"\n", | ||
" f\" WHERE name LIKE 'Omnipool.%Executed'\"\n", | ||
" f\" AND \"\"timestamp\"\" BETWEEN '2024-01-01T00:00:00.000Z' AND '2024-01-29T00:00:00.000Z'\" \n", | ||
" f\" ) AS trades\"\n", | ||
" f\"),\"\n", | ||
" f\"trades AS (\"\n", | ||
" f\" SELECT\" \n", | ||
" f\" timestamp,\"\n", | ||
" f\" amount_in / amount_out AS price,\"\n", | ||
" f\" amount_in as amount,\"\n", | ||
" f\" 'buy HDX' as direction\"\n", | ||
" f\" FROM normalized_trades\"\n", | ||
" f\" WHERE asset_in = 'USDT' AND asset_out = 'HDX'\" \n", | ||
" f\" UNION ALL\"\n", | ||
" f\" SELECT\" \n", | ||
" f\" timestamp,\"\n", | ||
" f\" amount_out / amount_in AS price,\"\n", | ||
" f\" amount_out as amount,\"\n", | ||
" f\" 'sell HDX' as direction\"\n", | ||
" f\" FROM normalized_trades\"\n", | ||
" f\" WHERE asset_in = 'HDX' AND asset_out = 'USDT'\"\n", | ||
" f\")\"\n", | ||
" f\"SELECT\"\n", | ||
" f\" timestamp AS \"\"time\"\",\"\n", | ||
" f\" price,\"\n", | ||
" f\" amount,\"\n", | ||
" f\" direction \"\n", | ||
" f\"FROM trades \"\n", | ||
" f\"ORDER BY 1; \"\n", | ||
")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 5, | ||
"id": "b5fc56bf-e2a5-4ec6-b410-3a3e8ac9ff92", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# query = (\n", | ||
"# f\"select sum(amt) from\"\n", | ||
"# f\"(\"\n", | ||
"# f\" SELECT *, CAST(args->>'tradeRewards' as bigint) as amt from event\"\n", | ||
"# f\" where event.name = 'Referrals.Claimed'\"\n", | ||
"# f\" and event.args->>'who' = '0x6d6f646c7374616b696e67230000000000000000000000000000000000000000' -- staking\"\n", | ||
"# f\") tempTable\"\n", | ||
"# )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": 6, | ||
"id": "6a492444-5c4e-4271-9077-2f00148bb47a", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"# query = (\n", | ||
"# f\"select data.extrinsic_id, data.call_id, data.call_name, data.event_name, data.call_success,\"\n", | ||
"# f\" data.event_args->>'asset' as asset,\"\n", | ||
"# f\" data.event_args->>'price' as price,\"\n", | ||
"# f\" data.event_args->>'amount' as amount,\"\n", | ||
"# f\" data.event_args->>'shares' as shares,\"\n", | ||
"# f\" data.event_args->>'positionId' as position_id,\"\n", | ||
"# f\" data.event_args->>'owner' as who,\"\n", | ||
"# f\" fees.fee, fees.tip\"\n", | ||
"# f\"from\"\n", | ||
"# f\"(select call.extrinsic_id, call.name as call_name, call.args as call_args, call.success as call_success, event.call_id,\"\n", | ||
"# f\"event.name as event_name, event.args as event_args\"\n", | ||
"# f\"from event inner join call\"\n", | ||
"# f\"on call.id = event.call_id\"\n", | ||
"# f\"and call.name = 'Omnipool.add_liquidity'\"\n", | ||
"# f\") as data\"\n", | ||
"# f\"inner join\"\n", | ||
"# f\"(select fee, tip, id from extrinsic) as fees\"\n", | ||
"# f\"on data.extrinsic_id = fees.id\"\n", | ||
"# f\"where event_name like 'Omnipool.PositionCreated'\"\n", | ||
"# f\"and cast(SUBSTR(data.extrinsic_id, 4, 7) as integer) > 3000000\"\n", | ||
"# f\"order by extrinsic_id\"\n", | ||
"# f\"LIMIT 100\"\n", | ||
"# )" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "7d2c023c-47a9-4fe4-ad1e-4057d8b450c2", | ||
"metadata": {}, | ||
"outputs": [ | ||
{ | ||
"name": "stdout", | ||
"output_type": "stream", | ||
"text": [ | ||
"waiting for query to finish...\n" | ||
] | ||
} | ||
], | ||
"source": [ | ||
"data = await query_sqlPad(query)\n", | ||
"print(\"done\")" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "8d23e70b-1472-4c26-91fd-57658f4fa07b", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"plt.figure(figsize=(20, 5))\n", | ||
"plt.plot([line[1] for line in data])\n", | ||
"plt.title('HDX/USDT price over time')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "0ee70e61-a945-4c5d-9593-09f88541fbd0", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"buy_sell_ratio = 0\n", | ||
"ratio_trend = []\n", | ||
"smooth_factor = 20\n", | ||
"decay_factor = 1 / smooth_factor\n", | ||
"for line in data:\n", | ||
" if line[3].startswith('buy'):\n", | ||
" buy_sell_ratio = buy_sell_ratio * (1 - decay_factor) + decay_factor\n", | ||
" else:\n", | ||
" buy_sell_ratio = buy_sell_ratio * (1 - decay_factor) - decay_factor\n", | ||
" ratio_trend.append(buy_sell_ratio)\n", | ||
"plt.figure(figsize=(20, 5))\n", | ||
"plt.plot(ratio_trend)\n", | ||
"plt.title('HDX/USDT buy/sell ratio over the same time period')" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "858ae068-b091-49bf-ab81-75a420ec8e1d", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [ | ||
"print(query)" | ||
] | ||
}, | ||
{ | ||
"cell_type": "code", | ||
"execution_count": null, | ||
"id": "1f7a8c2a-c01f-4300-a85e-9e800efb2501", | ||
"metadata": {}, | ||
"outputs": [], | ||
"source": [] | ||
} | ||
], | ||
"metadata": { | ||
"kernelspec": { | ||
"display_name": "Python 3 (ipykernel)", | ||
"language": "python", | ||
"name": "python3" | ||
}, | ||
"language_info": { | ||
"codemirror_mode": { | ||
"name": "ipython", | ||
"version": 3 | ||
}, | ||
"file_extension": ".py", | ||
"mimetype": "text/x-python", | ||
"name": "python", | ||
"nbconvert_exporter": "python", | ||
"pygments_lexer": "ipython3", | ||
"version": "3.9.7" | ||
} | ||
}, | ||
"nbformat": 4, | ||
"nbformat_minor": 5 | ||
} |