Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
75 changes: 74 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,77 @@ integrationtest:
## mock: generates mocks for unit tests
mock:
@echo "Generating mocks for unit tests..."
@mockery --dir=internal/core/domain --name=SwapParser --structname=MockSwapParser --filename=swap.go --output=internal/core/domain/mocks
@mockery --dir=internal/core/domain --name=SwapParser --structname=MockSwapParser --filename=swap.go --output=internal/core/domain/mocks

######## PG_DB ########
## pg: starts postgres db inside docker container
pg:
@echo "Starting postgres container..."
@docker run --name tdexd-pg -p 5432:5432 -e POSTGRES_USER=root -e POSTGRES_PASSWORD=secret -e POSTGRES_DB=tdexd -d postgres

## droppg: stop and remove postgres container
droppg:
@echo "Stopping and removing postgres container..."
@docker stop tdexd-pg
@docker rm tdexd-pg

## createdb: create db inside docker container
createdb:
@echo "Creating db..."
@docker exec tdexd-pg createdb --username=root --owner=root tdexd

## createtestdb: create test db inside docker container
createtestdb:
@echo "Creating test db..."
@docker exec tdexd-pg createdb --username=root --owner=root tdexd-test

## recreatedb: drop and create main and test db
recreatedb: dropdb createdb

## pgcreatetestdb: starts docker container and creates test db, used in CI
pgcreatetestdb: pg sleep createtestdb
@echo "Starting postgres container with test db..."

## dropdb: drops db inside docker container
dropdb:
@echo "Dropping db..."
@docker exec tdexd-pg dropdb tdexd

## droptestdb: drops test db inside docker container
droptestdb:
@echo "Dropping test db..."
@docker exec tdexd-pg dropdb tdexd-test

## mig_file: creates pg migration file(eg. make FILE=init mig_file)
mig_file:
@echo "creating migration file..."
@migrate create -ext sql -dir ./internal/infrastructure/storage/db/pg/migration $(FILE)

## mig_up_test: creates test db schema
mig_up_test:
@echo "creating test db schema..."
@echo "creating db schema..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd-test?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration up

## mig_up: creates db schema
mig_up:
@echo "creating db schema..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration up

## mig_down_test: apply down migration on test db
mig_down_test:
@echo "migration down on test db..."
@migrate -database "postgres://root:secret@localhost:5432/tdexd-test?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration down

## mig_down: apply down migration without prompt
mig_down:
@echo "migration down..."
@"yes" | migrate -database "postgres://root:secret@localhost:5432/tdexd?sslmode=disable" -path ./internal/infrastructure/storage/db/pg/migration down

## vet_db: check if mig_up and mig_down are ok
vet_db: recreatedb mig_up mig_down
@echo "vet db migration scripts..."

sleep:
@echo "sleeping for 3 seconds..."
@sleep 3
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
DROP TABLE IF EXISTS fee;

DROP TABLE IF EXISTS swap;

DROP TABLE IF EXISTS transaction_asset_amount;

DROP TABLE IF EXISTS transaction;

DROP TABLE IF EXISTS trade;

DROP TABLE IF EXISTS market;
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
CREATE TABLE market (
name VARCHAR(50) NOT NULL PRIMARY KEY,
Copy link
Contributor

Choose a reason for hiding this comment

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

This implies that in other layers (domain, interface) we MUST validate the name specified by the user and check that length is <= 50.

base_asset VARCHAR(255) NOT NULL,
quote_asset VARCHAR(255) NOT NULL,
base_asset_precision INTEGER NOT NULL DEFAULT 8,
quote_asset_precision INTEGER NOT NULL DEFAULT 8,
tradeable BOOLEAN NOT NULL DEFAULT FALSE,
Copy link
Contributor

@altafan altafan May 4, 2023

Choose a reason for hiding this comment

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

This is not really a typo, but the field at domain level is called Tradable, so let's stick with it. Also, we can remove defaults.

Suggested change
tradeable BOOLEAN NOT NULL DEFAULT FALSE,
tradable BOOLEAN NOT NULL,

strategy_type INTEGER NOT NULL DEFAULT 0,
base_price INTEGER NOT NULL DEFAULT 0,
quote_price INTEGER NOT NULL DEFAULT 0,
UNIQUE (base_asset, quote_asset)
);

CREATE TABLE fee (
id SERIAL PRIMARY KEY,
base_asset_fee BIGINT NOT NULL,
quote_asset_fee BIGINT NOT NULL,
type VARCHAR(10) NOT NULL CHECK (type IN ('percentage', 'fixed')),
fk_market_name VARCHAR(50) NOT NULL,
FOREIGN KEY (fk_market_name) REFERENCES market (name)
);

CREATE TABLE trade (
id VARCHAR(50) PRIMARY KEY,
type INTEGER NOT NULL,
fee_asset VARCHAR(255) NOT NULL,
fee_amount BIGINT NOT NULL,
trader_pubkey BYTEA NOT NULL,
status_code INTEGER NOT NULL DEFAULT 0,
status_failed BOOLEAN NOT NULL DEFAULT FALSE,
pset_base64 TEXT NOT NULL,
tx_id VARCHAR(255) NOT NULL,
tx_hex TEXT NOT NULL,
expiry_time BIGINT NOT NULL,
settlement_time BIGINT NOT NULL,
fk_market_name VARCHAR(50) NOT NULL,
FOREIGN KEY (fk_market_name) REFERENCES market (name)
);

CREATE TABLE swap (
id TEXT PRIMARY KEY,
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe better?

Suggested change
id TEXT PRIMARY KEY,
id VARCHAR(255) PRIMARY KEY,

message BYTEA NOT NULL,
timestamp BIGINT NOT NULL,
type VARCHAR(10) NOT NULL CHECK (type IN ('request', 'accept', 'complete', 'fail')),
fk_trade_id VARCHAR(50) NOT NULL,
FOREIGN KEY (fk_trade_id) REFERENCES trade (id)
);

CREATE TABLE transaction (
id SERIAL PRIMARY KEY,
type VARCHAR(10) NOT NULL CHECK (type IN ('withdrawal', 'deposit')),
account_name TEXT NOT NULL,
tx_id TEXT NOT NULL,
timestamp BIGINT NOT NULL
);

CREATE TABLE transaction_asset_amount (
id SERIAL PRIMARY KEY,
transaction_id INTEGER NOT NULL,
asset TEXT NOT NULL,
amount BIGINT NOT NULL,
FOREIGN KEY (transaction_id) REFERENCES transaction (id)
);