Skip to content
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

Sqlx #37

Merged
merged 19 commits into from
Mar 28, 2024
Merged

Sqlx #37

merged 19 commits into from
Mar 28, 2024

Conversation

m30m
Copy link
Collaborator

@m30m m30m commented Mar 26, 2024

  • sqlx doesn't support nightly, so I switched to the stable toolchain

auction-server/.prettierignore Outdated Show resolved Hide resolved
creation_time TIMESTAMP NOT NULL,
permission_key BYTEA NOT NULL,
chain_id TEXT NOT NULL,
target_contract BYTEA NOT NULL,
Copy link
Contributor

Choose a reason for hiding this comment

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

may be worth defining our own custom types here and using for the type fields, here Address (i.e. length 20 bytes)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Ideally, I could add new domains but sqlx wasn't playing nice with these custom types so I added checks per column

target_contract BYTEA NOT NULL,
target_calldata BYTEA NOT NULL,
bid_amount NUMERIC(80, 0) NOT NULL,
status TEXT NOT NULL, -- pending, lost, submitted
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I made it an enum but it seems that it's not playing so nice on the rust side:
launchbadge/sqlx#1004 (comment)

Submitted(H256),
/// The bid lost the auction
Lost,
}

impl BidStatus {
pub fn status_name(&self) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe just implement From<String> and that'll give you the Into?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I didn't want to do that since one variant has more info to be displayed but we don't need that in our sql table. It's not ideal if you log the BidStatus and just see the Submitted variant without the actual hash.

pub event_sender: broadcast::Sender<UpdateEvent>,
pub struct Store {
pub chains: HashMap<ChainId, ChainStore>,
pub bids: RwLock<HashMap<BidId, SimulatedBid>>,
Copy link
Contributor

Choose a reason for hiding this comment

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

may make sense to create a separate BidStore object, since any changes to this object struct in the future could then be localized to that class and its methods

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

will do so when it gets more complicated

auction-server/src/state.rs Outdated Show resolved Hide resolved
/// database url for persistent storage
#[arg(long = "database-url")]
#[arg(env = "DATABASE_URL")]
pub database_url: String,
Copy link
Contributor

Choose a reason for hiding this comment

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

need to update this in tilt file as well

auction-server/src/opportunity_adapter.rs Show resolved Hide resolved
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:
Copy link
Contributor

Choose a reason for hiding this comment

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

mind adding a note on which directory to run this from, just for clarity

update: BidStatusWithId,
) -> anyhow::Result<()> {
if update.bid_status == BidStatus::Pending {
return Err(anyhow::anyhow!("Cannot finalize a pending bid"));
Copy link
Contributor

Choose a reason for hiding this comment

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

this error msg is a bit awkward, maybe just "Bid is still pending"

@@ -290,22 +311,25 @@ impl Store {
Ok(())
}

pub async fn set_bid_status_and_broadcast(
pub async fn finalize_bid_status_and_broadcast(
Copy link
Contributor

Choose a reason for hiding this comment

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

a clearer function name might be broadcast_bid_status_and_remove since that is less ambiguous as to what finalize means. but i'm fine with current name if you prefer it

sqlx::query!(
"UPDATE bid SET status = $1 WHERE id = $2",
update.bid_status.status_name(),
"UPDATE bid SET status = $1, removal_time = $2 WHERE id = $3 AND removal_time IS NULL",
Copy link
Contributor

Choose a reason for hiding this comment

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

finalization_time or status_time?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

This field will be removed soon, when we add the auction table. Let's keep it as is for now.

@m30m m30m merged commit 95ba434 into main Mar 28, 2024
1 check passed
@m30m m30m deleted the sqlx branch March 28, 2024 12:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants