Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Draft code] average gas improvements #13

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions queries/num-trades-per-batch.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
SELECT
num_trades,
CONCAT('0x', ENCODE(tx_hash, 'hex')) as txHash
FROM gnosis_protocol_v2."batches" b
where block_time > now() - interval '5 hour'
ORDER BY block_time DESC;
63 changes: 63 additions & 0 deletions src/gas_saved_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os

import click
import pandas as pd
from dotenv import load_dotenv
from web3 import Web3

from src.db.pg_client import pg_engine

from duneapi.api import DuneAPI
from duneapi.types import DuneQuery, Network
from duneapi.util import open_query


def fetch_trades_per_batch(dune: DuneAPI):
query = DuneQuery.from_environment(
raw_sql=open_query("./queries/num-trades-per-batch.sql"),
name="num-trades-per-batch",
network=Network.MAINNET,
parameters=[],
Comment on lines +18 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI - these fields are all Optional now with defaults essentially as you have specified here (empty parameters and mainnet and empty string for name). No need to change here, just thought you might like to know about these recently added conveniences.

)
results = dune.fetch(query)
return results


def get_percentage_gas_used_of_estimate(batch_tx_hash):
db_engine = pg_engine()
GAS_QUOTES_QUERY = f"""
SELECT orders.id::bytea, order_quotes.gas_amount, order_quotes.gas_price
FROM
solver_competitions,
jsonb_to_recordset(solver_competitions.json->'solutions'->-1->'orders') AS orders(id text)
LEFT JOIN order_quotes ON order_quotes.order_uid = ('\\' || LTRIM(orders.id::text, '0'))::bytea
WHERE
solver_competitions.tx_hash = '\\{batch_tx_hash[1:]}'
"""
df_quotes = pd.read_sql(GAS_QUOTES_QUERY, db_engine)
# subtract settlement_overhead from price estimation
# Ref: https://github.com/cowprotocol/services/blob/fd5f7cf47a6afdff89b310b60b869dfc577ac7a7/crates/shared/src/price_estimation/gas.rs#L37
df_quotes["gas_amount"] = df_quotes["gas_amount"].apply(lambda x: x - 106391)
load_dotenv()
w3 = Web3(
Web3.HTTPProvider(f"https://mainnet.infura.io/v3/{os.environ['INFURA_KEY']}")
)
tx = w3.eth.get_transaction_receipt(batch_tx_hash)
if df_quotes['gas_amount'].sum() == 0:
return -1
print(batch_tx_hash)
print(tx.gasUsed/df_quotes['gas_amount'].sum())
return tx.gasUsed/df_quotes['gas_amount'].sum()



if __name__ == "__main__":
dune_conn = DuneAPI.new_from_environment()
df = fetch_trades_per_batch(dune_conn)
data = {'tx_hash': [], 'num_trades': [], 'gas_percentage': []}
df_result = pd.DataFrame(data)
for i in df:
new_row = {'tx_hash':i['txhash'], 'num_trades':i['num_trades'], 'gas_percentage':get_percentage_gas_used_of_estimate(i['txhash'])}
df_result = df_result.append(new_row, ignore_index=True)

print(df_result[df_result.gas_percentage > 0].groupby('num_trades')['gas_percentage'].mean())