Skip to content

Commit

Permalink
Address comments part 2
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Mar 27, 2024
1 parent 8640ed8 commit 174a929
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 41 deletions.
2 changes: 1 addition & 1 deletion Tiltfile
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ local_resource(

local_resource(
"auction-server",
serve_cmd="source ../tilt-resources.env; cargo run -- run --relayer-private-key $RELAYER_PRIVATE_KEY",
serve_cmd="source ../tilt-resources.env; source ./.env; cargo run -- run --database-url $DATABASE_URL --relayer-private-key $RELAYER_PRIVATE_KEY",
serve_dir="auction-server",
resource_deps=["create-configs"],
readiness_probe=probe(period_secs=5, http_get=http_get_action(port=9000)),
Expand Down

This file was deleted.

This file was deleted.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 12 additions & 2 deletions auction-server/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ Each blockchain is configured in `config.yaml`.

This package uses Cargo for building and dependency management.
Simply run `cargo build` and `cargo test` to build and test the project.
We use `sqlx` for database operations, so you need to have a PostgreSQL server running locally.
Check the Migration section for more information on how to setup the database.

## Local Development

Expand All @@ -25,12 +27,16 @@ This command will start the webservice on `localhost:9000`.

You can check the documentation of the webservice by visiting `localhost:9000/docs`.

## Migrations
## DB & Migrations

sqlx checks the database schema at compile time, so you need to have the database schema up-to-date
before building the project. You can create a `.env` file similar
to the `.env.example` file and set `DATABASE_URL` to the URL of your PostgreSQL database. This file
will be picked up by sqlx-cli and cargo scripts when running the checks.

Install sqlx-cli by running `cargo install sqlx-cli`. Then, run the following command to apply migrations:

```bash
export DATABASE_URL=postgres://postgres@localhost/postgres
sqlx migrate run
```

Expand All @@ -39,3 +45,7 @@ We use revertible migrations to manage the database schema. You can create a new
```bash
sqlx migrate add -r <migration-name>
```

Since we don't have a running db instance on CI, we use `cargo sqlx prepare` to generate the necessary
info offline. This command will update the `.sqlx` folder.
You need to commit the changes to this folder when adding or changing the queries.
4 changes: 2 additions & 2 deletions auction-server/migrations/20240320162754_init.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ CREATE TABLE opportunity
creation_time TIMESTAMP NOT NULL,
permission_key BYTEA NOT NULL,
chain_id TEXT NOT NULL,
target_contract BYTEA NOT NULL,
target_call_value NUMERIC(80, 0) NOT NULL,
target_contract BYTEA NOT NULL CHECK (LENGTH(permission_key) = 20),
target_call_value NUMERIC(78, 0) NOT NULL,
target_calldata BYTEA NOT NULL,
sell_tokens JSONB NOT NULL,
buy_tokens JSONB NOT NULL,
Expand Down
1 change: 1 addition & 0 deletions auction-server/migrations/20240326063340_bids.down.sql
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
DROP TABLE bid;
DROP TYPE bid_status;
8 changes: 5 additions & 3 deletions auction-server/migrations/20240326063340_bids.up.sql
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
CREATE TYPE bid_status AS ENUM ('pending', 'lost', 'submitted');

CREATE TABLE bid
(
id UUID PRIMARY KEY,
creation_time TIMESTAMP NOT NULL,
permission_key BYTEA NOT NULL,
chain_id TEXT NOT NULL,
target_contract BYTEA NOT NULL,
target_contract BYTEA NOT NULL CHECK (LENGTH(permission_key) = 20),
target_calldata BYTEA NOT NULL,
bid_amount NUMERIC(80, 0) NOT NULL,
status TEXT NOT NULL, -- pending, lost, submitted
bid_amount NUMERIC(78, 0) NOT NULL,
status bid_status NOT NULL,
auction_id UUID, -- TODO: should be linked to the auction table in the future
removal_time TIMESTAMP -- TODO: should be removed and read from the auction table in the future
);
4 changes: 2 additions & 2 deletions auction-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl Store {
&bid.target_contract.to_fixed_bytes(),
bid.target_calldata.to_vec(),
BigDecimal::from_str(&bid.bid_amount.to_string()).unwrap(),
bid.status.status_name(),
bid.status.status_name() as _,
)
.execute(&self.db)
.await.map_err(|e| {
Expand All @@ -301,7 +301,7 @@ impl Store {
let now = OffsetDateTime::now_utc();
sqlx::query!(
"UPDATE bid SET status = $1, removal_time = $2 WHERE id = $3 AND removal_time IS NULL",
update.bid_status.status_name(),
update.bid_status.status_name() as _,
PrimitiveDateTime::new(now.date(), now.time()),
update.id
)
Expand Down

0 comments on commit 174a929

Please sign in to comment.