Skip to content

Commit

Permalink
Merge pull request #365 from Concordium/baker-id-long
Browse files Browse the repository at this point in the history
Make BakerId use the Long scalar
  • Loading branch information
limemloh authored Jan 9, 2025
2 parents eccb6c9 + 03b925c commit bb26eae
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 12 deletions.
2 changes: 1 addition & 1 deletion backend-rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
4 changes: 1 addition & 3 deletions backend-rust/src/graphql_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1002,15 +1002,13 @@ impl AccountReleaseScheduleItem {
async fn amount(&self) -> Amount { self.amount }
}

#[derive(sqlx::FromRow)]
struct Account {
// release_schedule: AccountReleaseSchedule,
index: i64,
/// Index of the transaction creating this account.
/// Only `None` for genesis accounts.
transaction_index: Option<i64>,
/// The address of the account in Base58Check.
#[sqlx(try_from = "String")]
address: AccountAddress,
/// The total amount of CCD hold by the account.
amount: Amount,
Expand Down Expand Up @@ -1071,7 +1069,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 {
Expand Down
12 changes: 8 additions & 4 deletions backend-rust/src/graphql_api/baker.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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> {
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> {
Baker::query_by_id(get_pool(ctx)?, baker_id).await
}

#[allow(clippy::too_many_arguments)]
Expand Down Expand Up @@ -83,7 +87,7 @@ impl Baker {
finalization_commission
FROM bakers WHERE id=$1
"#,
baker_id
i64::from(baker_id)
)
.fetch_optional(pool)
.await?
Expand Down Expand Up @@ -131,7 +135,7 @@ impl Baker {
}

async fn account<'a>(&self, ctx: &Context<'a>) -> ApiResult<Account> {
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
Expand Down
4 changes: 2 additions & 2 deletions backend-rust/src/graphql_api/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ pub struct Block {
/// being finalized.
finalization_time: Option<i32>,
// finalized_by: Option<BlockHeight>,
baker_id: Option<BakerId>,
baker_id: Option<i64>,
total_amount: Amount,
// total_staked: Amount,
}
Expand Down Expand Up @@ -165,7 +165,7 @@ impl Block {

async fn block_height(&self) -> &BlockHeight { &self.height }

async fn baker_id(&self) -> Option<BakerId> { self.baker_id }
async fn baker_id(&self) -> Option<BakerId> { self.baker_id.map(BakerId::from) }

async fn total_amount(&self) -> &Amount { &self.total_amount }

Expand Down
20 changes: 18 additions & 2 deletions backend-rust/src/scalar_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<chrono::Utc>; // TODO check format matches.
pub type BakerId = i64;
pub type BakerId = Long;
pub type BlockHeight = i64;
pub type BlockHash = String;
pub type TransactionHash = String;
Expand All @@ -27,6 +27,7 @@ pub type MetadataUrl = String;
)]
#[repr(transparent)]
#[serde(transparent)]
#[display("{_0}")]
pub struct UnsignedLong(pub u64);
#[Scalar]
impl ScalarType for UnsignedLong {
Expand All @@ -52,7 +53,17 @@ impl TryFrom<i64> 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);
Expand All @@ -71,6 +82,11 @@ impl ScalarType for Long {

fn to_value(&self) -> Value { Value::Number(self.0.into()) }
}
impl TryFrom<u64> for Long {
type Error = std::num::TryFromIntError;

fn try_from(value: u64) -> Result<Self, Self::Error> { Ok(Self(value.try_into()?)) }
}

#[derive(serde::Serialize, serde::Deserialize, derive_more::From)]
#[repr(transparent)]
Expand Down

0 comments on commit bb26eae

Please sign in to comment.