Skip to content

Commit

Permalink
Merge pull request #50 from cowprotocol/imbalances-schema
Browse files Browse the repository at this point in the history
Fixed inconsistency in table schema
  • Loading branch information
harisang authored Aug 29, 2024
2 parents a6eef14 + d5cc283 commit a897914
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 27 deletions.
35 changes: 12 additions & 23 deletions src/helpers/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ def execute_and_commit(self, query: str, params: dict):
connection.rollback()
raise

def record_exists(self, tx_hash: bytes, token_address: bytes) -> bool:
"""
Function checks if an entry of (tx_hash, token) already exists in the token imbalances table.
"""
query = read_sql_file("src/sql/select_record_exists.sql")
result = self.execute_query(
query, {"tx_hash": tx_hash, "token_address": token_address}
)
return result.fetchone() is not None

def write_token_imbalances(
self,
tx_hash: str,
Expand All @@ -59,19 +49,18 @@ def write_token_imbalances(
tx_hash_bytes = bytes.fromhex(tx_hash[2:])
token_address_bytes = bytes.fromhex(token_address[2:])

if not self.record_exists(tx_hash_bytes, token_address_bytes):
query = read_sql_file("src/sql/insert_raw_token_imbalances.sql")
self.execute_and_commit(
query,
{
"auction_id": auction_id,
"chain_name": self.chain_name,
"block_number": block_number,
"tx_hash": tx_hash_bytes,
"token_address": token_address_bytes,
"imbalance": imbalance,
},
)
query = read_sql_file("src/sql/insert_raw_token_imbalances.sql")
self.execute_and_commit(
query,
{
"auction_id": auction_id,
"chain_name": self.chain_name,
"block_number": block_number,
"tx_hash": tx_hash_bytes,
"token_address": token_address_bytes,
"imbalance": imbalance,
},
)

def write_prices(
self,
Expand Down
3 changes: 2 additions & 1 deletion src/sql/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ CREATE TABLE raw_token_imbalances (
block_number BIGINT NOT NULL,
tx_hash BYTEA NOT NULL,
token_address BYTEA NOT NULL,
imbalance NUMERIC(78,0)
imbalance NUMERIC(78,0),
PRIMARY KEY (tx_hash, token_address)
);

-- Table: slippage_prices (for storing per unit token prices in ETH)
Expand Down
3 changes: 0 additions & 3 deletions src/sql/select_record_exists.sql

This file was deleted.

0 comments on commit a897914

Please sign in to comment.