Skip to content

Commit 13c515a

Browse files
committed
Refactor: Replace Vec<u8> with PoolId and add Bech32 (de)serialization support
1 parent 0f551f9 commit 13c515a

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

common/src/serialization.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use anyhow::anyhow;
44
use bech32::{Bech32, Hrp};
55
use serde::{ser::SerializeMap, Deserialize, Serializer};
66
use serde_with::{ser::SerializeAsWrap, DeserializeAs, SerializeAs};
7+
use crate::PoolId;
78

89
pub struct SerializeMapAs<KAs, VAs>(std::marker::PhantomData<(KAs, VAs)>);
910

@@ -60,7 +61,31 @@ impl HrpPrefix for AddrPrefix {
6061
// Generic Bech32 converter with HRP parameter
6162
pub struct DisplayFromBech32<PREFIX: HrpPrefix>(PhantomData<PREFIX>);
6263

63-
// Serialization implementation
64+
// PoolID serialization implementation
65+
impl SerializeAs<PoolId> for DisplayFromBech32<PoolPrefix>
66+
{
67+
fn serialize_as<S>(source: &PoolId, serializer: S) -> Result<S::Ok, S::Error>
68+
where
69+
S: Serializer,
70+
{
71+
let bech32_string = source.to_bech32().map_err(serde::ser::Error::custom)?;
72+
serializer.serialize_str(&bech32_string)
73+
}
74+
}
75+
76+
// PoolID deserialization implementation
77+
impl<'de> DeserializeAs<'de, PoolId> for DisplayFromBech32<PoolPrefix>
78+
{
79+
fn deserialize_as<D>(deserializer: D) -> Result<PoolId, D::Error>
80+
where
81+
D: serde::de::Deserializer<'de>,
82+
{
83+
let s = String::deserialize(deserializer)?;
84+
PoolId::from_bech32(&s).map_err(serde::de::Error::custom)
85+
}
86+
}
87+
88+
// Vec<u8> serialization implementation
6489
impl<PREFIX> SerializeAs<Vec<u8>> for DisplayFromBech32<PREFIX>
6590
where
6691
PREFIX: HrpPrefix,

modules/rest_blockfrost/src/handlers/epochs.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ pub async fn handle_epoch_total_stakes_blockfrost(
494494
.to_string()
495495
.map_err(|e| anyhow::anyhow!("Failed to convert stake address to string: {e}"))?;
496496
Ok(SPDDByEpochItemRest {
497-
pool_id: pool_id.to_vec(),
497+
pool_id,
498498
stake_address,
499499
amount,
500500
})

modules/rest_blockfrost/src/types.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use acropolis_common::{
77
rest_helper::ToCheckedF64,
88
serialization::{DisplayFromBech32, PoolPrefix},
99
AssetAddressEntry, AssetMetadataStandard, AssetMintRecord, KeyHash, PolicyAsset,
10-
PoolEpochState, PoolUpdateAction, Relay, TxHash, Vote, VrfKeyHash,
10+
PoolEpochState, PoolId, PoolUpdateAction, Relay, TxHash, Vote, VrfKeyHash,
1111
};
1212
use anyhow::Result;
1313
use num_traits::ToPrimitive;
@@ -63,7 +63,7 @@ pub struct BlockInfoREST(pub BlockInfo);
6363
pub struct SPDDByEpochItemRest {
6464
pub stake_address: String,
6565
#[serde_as(as = "DisplayFromBech32<PoolPrefix>")]
66-
pub pool_id: Vec<u8>,
66+
pub pool_id: PoolId,
6767
#[serde_as(as = "DisplayFromStr")]
6868
pub amount: u64,
6969
}

0 commit comments

Comments
 (0)