From 03b925ca7c694faee355c141e40bf6b339257903 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emil=20Holm=20Gj=C3=B8rup?= Date: Thu, 9 Jan 2025 08:38:24 +0100 Subject: [PATCH] Make BakerId use the Long scalar --- backend-rust/Cargo.toml | 2 +- backend-rust/src/graphql_api.rs | 4 +--- backend-rust/src/graphql_api/baker.rs | 12 ++++++++---- backend-rust/src/graphql_api/block.rs | 4 ++-- backend-rust/src/scalar_types.rs | 20 ++++++++++++++++++-- 5 files changed, 30 insertions(+), 12 deletions(-) diff --git a/backend-rust/Cargo.toml b/backend-rust/Cargo.toml index 1a23f3ee..9240053b 100644 --- a/backend-rust/Cargo.toml +++ b/backend-rust/Cargo.toml @@ -18,7 +18,7 @@ ciborium = "0.2" chrono = { version = "0.4", features = ["serde"] } clap = { version = "4.5", features = ["derive", "env", "cargo"] } concordium-rust-sdk = { path = "./concordium-rust-sdk" } -derive_more = { version = "1.0.0", features = ["display", "from", "from_str"] } +derive_more = { version = "1.0.0", features = ["display", "from", "from_str", "into"] } dotenvy = "0.15" futures = "0.3" hex = "0.4" diff --git a/backend-rust/src/graphql_api.rs b/backend-rust/src/graphql_api.rs index c240ee90..3aca7f47 100644 --- a/backend-rust/src/graphql_api.rs +++ b/backend-rust/src/graphql_api.rs @@ -927,7 +927,6 @@ impl AccountReleaseScheduleItem { async fn amount(&self) -> Amount { self.amount } } -#[derive(sqlx::FromRow)] struct Account { // release_schedule: AccountReleaseSchedule, index: i64, @@ -935,7 +934,6 @@ struct Account { /// Only `None` for genesis accounts. transaction_index: Option, /// The address of the account in Base58Check. - #[sqlx(try_from = "String")] address: AccountAddress, /// The total amount of CCD hold by the account. amount: Amount, @@ -996,7 +994,7 @@ impl Account { staked_amount: self.delegated_stake, delegation_target: if let Some(target) = self.delegated_target_baker_id { DelegationTarget::BakerDelegationTarget(BakerDelegationTarget { - baker_id: target, + baker_id: target.into(), }) } else { DelegationTarget::PassiveDelegationTarget(PassiveDelegationTarget { diff --git a/backend-rust/src/graphql_api/baker.rs b/backend-rust/src/graphql_api/baker.rs index b35b43ab..7fd18197 100644 --- a/backend-rust/src/graphql_api/baker.rs +++ b/backend-rust/src/graphql_api/baker.rs @@ -18,8 +18,12 @@ impl QueryBaker { Baker::query_by_id(get_pool(ctx)?, id).await } - async fn baker_by_baker_id<'a>(&self, ctx: &Context<'a>, id: BakerId) -> ApiResult { - Baker::query_by_id(get_pool(ctx)?, id).await + async fn baker_by_baker_id<'a>( + &self, + ctx: &Context<'a>, + baker_id: BakerId, + ) -> ApiResult { + Baker::query_by_id(get_pool(ctx)?, baker_id).await } #[allow(clippy::too_many_arguments)] @@ -83,7 +87,7 @@ impl Baker { finalization_commission FROM bakers WHERE id=$1 "#, - baker_id + i64::from(baker_id) ) .fetch_optional(pool) .await? @@ -131,7 +135,7 @@ impl Baker { } async fn account<'a>(&self, ctx: &Context<'a>) -> ApiResult { - Account::query_by_index(get_pool(ctx)?, self.id).await?.ok_or(ApiError::NotFound) + Account::query_by_index(get_pool(ctx)?, i64::from(self.id)).await?.ok_or(ApiError::NotFound) } // transactions("Returns the first _n_ elements from the list." first: Int diff --git a/backend-rust/src/graphql_api/block.rs b/backend-rust/src/graphql_api/block.rs index e3d4ebcc..8ba640f8 100644 --- a/backend-rust/src/graphql_api/block.rs +++ b/backend-rust/src/graphql_api/block.rs @@ -106,7 +106,7 @@ pub struct Block { /// being finalized. finalization_time: Option, // finalized_by: Option, - baker_id: Option, + baker_id: Option, total_amount: Amount, // total_staked: Amount, } @@ -165,7 +165,7 @@ impl Block { async fn block_height(&self) -> &BlockHeight { &self.height } - async fn baker_id(&self) -> Option { self.baker_id } + async fn baker_id(&self) -> Option { self.baker_id.map(BakerId::from) } async fn total_amount(&self) -> &Amount { &self.total_amount } diff --git a/backend-rust/src/scalar_types.rs b/backend-rust/src/scalar_types.rs index 780da7a0..c387bbab 100644 --- a/backend-rust/src/scalar_types.rs +++ b/backend-rust/src/scalar_types.rs @@ -4,7 +4,7 @@ use async_graphql::{scalar, InputValueError, InputValueResult, Scalar, ScalarTyp pub type Amount = i64; // TODO: should be UnsignedLong in graphQL pub type Energy = i64; // TODO: should be UnsignedLong in graphQL pub type DateTime = chrono::DateTime; // TODO check format matches. -pub type BakerId = i64; +pub type BakerId = Long; pub type BlockHeight = i64; pub type BlockHash = String; pub type TransactionHash = String; @@ -27,6 +27,7 @@ pub type MetadataUrl = String; )] #[repr(transparent)] #[serde(transparent)] +#[display("{_0}")] pub struct UnsignedLong(pub u64); #[Scalar] impl ScalarType for UnsignedLong { @@ -52,7 +53,17 @@ impl TryFrom for UnsignedLong { /// The `Long` scalar type represents non-fractional signed whole 64-bit numeric /// values. Long can represent values between -(2^63) and 2^63 - 1. -#[derive(serde::Serialize, serde::Deserialize, derive_more::From)] +#[derive( + Debug, + serde::Serialize, + serde::Deserialize, + Clone, + Copy, + derive_more::From, + derive_more::FromStr, + derive_more::Into, + derive_more::Display, +)] #[repr(transparent)] #[serde(transparent)] pub struct Long(pub i64); @@ -71,6 +82,11 @@ impl ScalarType for Long { fn to_value(&self) -> Value { Value::Number(self.0.into()) } } +impl TryFrom for Long { + type Error = std::num::TryFromIntError; + + fn try_from(value: u64) -> Result { Ok(Self(value.try_into()?)) } +} #[derive(serde::Serialize, serde::Deserialize, derive_more::From)] #[repr(transparent)]