-
Notifications
You must be signed in to change notification settings - Fork 2
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
josojo
wants to merge
1
commit into
main
Choose a base branch
from
avgGasImprovement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,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; |
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,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=[], | ||
) | ||
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()) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.