From f797e8c266005676023ef1cfb5105df6c81bb790 Mon Sep 17 00:00:00 2001 From: Angel Leon Date: Mon, 22 Sep 2025 10:22:40 -0600 Subject: [PATCH] =?UTF-8?q?auctions:=20centralize=20SystemTime=E2=86=92?= =?UTF-8?q?=C2=B5s=20conversions;=20fix=20U256/u64=20handling;=20tighten?= =?UTF-8?q?=20errors?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Introduce helpers in AuctionClient to convert SystemTime to: - u128 microseconds (single source of truth) - u64 microseconds (with overflow check + clear error msg) - U256 microseconds (for event/topic filters) - Replace ad-hoc conversions with helpers in: - fetch_bids_for_deadline(): now passes U256 microseconds directly to topic3 - submit_bid(): now converts to u64 microseconds via checked path - Fix misleading error text ("seconds") → accurate "microseconds". Why: - Correctness: the ABI uses microseconds; mixing ad-hoc conversions risks ms/us mistakes. - Safety: explicit u64 fallible conversion avoids silent truncation on far-future deadlines. - Consistency/maintainability: one code path for time conversions reduces duplication and bugs. - Clarity: U256 for event topics matches on-chain types; errors now communicate the real unit. No behavior change for valid inputs; client logic only (no contract/ABI changes). --- rust-sdk/src/auctions/client.rs | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/rust-sdk/src/auctions/client.rs b/rust-sdk/src/auctions/client.rs index 859f5768..a8a4dba9 100644 --- a/rust-sdk/src/auctions/client.rs +++ b/rust-sdk/src/auctions/client.rs @@ -22,6 +22,24 @@ pub struct Bid { } impl AuctionClient { + // Convert SystemTime -> microseconds as `u128`. + fn micros_from_system_time(deadline: SystemTime) -> u128 { + Timestamp::from(deadline).as_micros() + } + + // Convert SystemTime -> microseconds as `u64`, failing with a clear message on overflow. + fn micros_u64_from_system_time(deadline: SystemTime) -> anyhow::Result { + let micros = Self::micros_from_system_time(deadline); + micros + .try_into() + .context("deadline microseconds must fit in u64") + } + + // Convert SystemTime -> microseconds as `U256` for contract topics. + fn micros_u256_from_system_time(deadline: SystemTime) -> U256 { + U256::from(Self::micros_from_system_time(deadline)) + } + pub fn new(provider: PodProvider, contract: Address) -> Self { AuctionClient { auction: AuctionInstance::new(contract, provider), @@ -61,12 +79,12 @@ impl AuctionClient { #[tracing::instrument(skip(self))] pub async fn fetch_bids_for_deadline(&self, deadline: SystemTime) -> anyhow::Result> { - let deadline_us = Timestamp::from(deadline).as_micros(); + let deadline_us = Self::micros_u256_from_system_time(deadline); let logs = self .auction .BidSubmitted_filter() - .topic3(U256::from(deadline_us)) + .topic3(deadline_us) .to_block(BlockNumberOrTag::Latest) .query() .await @@ -90,11 +108,7 @@ impl AuctionClient { bid: U256, data: Vec, ) -> anyhow::Result { - let deadline_ts = Timestamp::from(deadline); - let deadline = deadline_ts - .as_micros() - .try_into() - .context("deadline seconds must fit in u64")?; + let deadline = Self::micros_u64_from_system_time(deadline)?; let pending_tx = self .auction