Skip to content

Commit

Permalink
Merge pull request #74 from cowprotocol/add_new_database
Browse files Browse the repository at this point in the history
Add database setup
  • Loading branch information
harisang authored Oct 10, 2024
2 parents da9ff65 + 567fd0b commit 21727e4
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Dockerfile.test_db
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM postgres
ENV POSTGRES_PASSWORD=postgres
ENV POSTGRES_DB=mainnet
COPY ./database/01_table_creation.sql /docker-entrypoint-initdb.d/
23 changes: 23 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
DOCKER_IMAGE_NAME=test_db_image
DOCKER_CONTAINER_NAME=test_db_container
DB_PORT=5432

install:
pip install -r requirements.txt

imbalances:
python -m src.imbalances_script

daemon:
python -m src.daemon

test_db:
docker build -t $(DOCKER_IMAGE_NAME) -f Dockerfile.test_db .
docker run -d --name $(DOCKER_CONTAINER_NAME) -p $(DB_PORT):$(DB_PORT) $(DOCKER_IMAGE_NAME)

stop_test_db:
docker stop $(DOCKER_CONTAINER_NAME) || true
docker rm $(DOCKER_CONTAINER_NAME) || true
docker rmi $(DOCKER_IMAGE_NAME) || true

.PHONY: install imbalances daemon test_db run_test_db stop_test_db clean
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,10 @@ python -m src.imbalances_script
python -m src.daemon
```

**To run the basic test in the `tests/` folder:**
using pytest, simply run the command: `pytest`
## Tests

To build and start a local database for testing use the command
```sh
docker build -t test_db_image -f Dockerfile.test_db .
docker run -d --name test_db_container -p 5432:5432 test_db_image
```
27 changes: 27 additions & 0 deletions database/01_table_creation.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
CREATE TABLE transaction_timestamps (
tx_hash bytea PRIMARY KEY,
time timestamp NOT NULL
);

CREATE TABLE transaction_tokens (
tx_hash bytea NOT NULL,
token_address bytea NOT NULL,

PRIMARY KEY (tx_hash, token_address)
);

CREATE TYPE PriceSource AS ENUM ('coingecko', 'moralis', 'dune', 'native');

CREATE TABLE prices (
token_address bytea NOT NULL,
time timestamp NOT NULL,
price numeric(60, 18) NOT NULL,
source PriceSource NOT NULL,

PRIMARY KEY (token_address, time, source)
);

CREATE TABLE token_decimals (
token_address bytea PRIMARY KEY,
decimals int NOT NULL
);

0 comments on commit 21727e4

Please sign in to comment.