Skip to content

Commit ec1101d

Browse files
stefan-mystenDaughterOfMars
authored andcommitted
sui-graphql-client: fix return of bcs and typetag in DynamicFieldOutput (iotaledger#46)
1 parent fbaa407 commit ec1101d

File tree

2 files changed

+38
-15
lines changed

2 files changed

+38
-15
lines changed

crates/sui-graphql-client/src/lib.rs

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,12 @@ pub struct DynamicFieldName {
107107
pub json: Option<serde_json::Value>,
108108
}
109109

110-
type Typename = String;
111-
112110
#[derive(Debug)]
113111
pub struct DynamicFieldOutput {
114112
/// The name of the dynamic field
115113
pub name: DynamicFieldName,
116114
/// The dynamic field value typename and bcs
117-
pub value: Option<(Typename, Vec<u8>)>,
115+
pub value: Option<(TypeTag, Vec<u8>)>,
118116
/// The json representation of the dynamic field value object
119117
pub value_as_json: Option<serde_json::Value>,
120118
}
@@ -191,19 +189,41 @@ impl From<BcsName> for NameValue {
191189
}
192190

193191
impl DynamicFieldOutput {
194-
pub fn deserialize<T: DeserializeOwned>(
192+
/// Deserialize the name of the dynamic field into the specified type.
193+
pub fn deserialize_name<T: DeserializeOwned>(
195194
&self,
196-
expected_type: TypeTag,
195+
expected_type: &TypeTag,
197196
) -> Result<T, anyhow::Error> {
198197
assert_eq!(
199-
expected_type, self.name.type_,
198+
expected_type, &self.name.type_,
200199
"Expected type {}, but got {}",
201-
expected_type, self.name.type_
200+
expected_type, &self.name.type_
202201
);
203202

204203
let bcs = &self.name.bcs;
205204
bcs::from_bytes::<T>(bcs).map_err(|_| anyhow!("Cannot decode BCS bytes"))
206205
}
206+
207+
/// Deserialize the value of the dynamic field into the specified type.
208+
pub fn deserialize_value<T: DeserializeOwned>(
209+
&self,
210+
expected_type: &TypeTag,
211+
) -> Result<T, anyhow::Error> {
212+
let typetag = self.value.as_ref().map(|(typename, _)| typename);
213+
assert_eq!(
214+
Some(&expected_type),
215+
typetag.as_ref(),
216+
"Expected type {}, but got {:?}",
217+
expected_type,
218+
typetag
219+
);
220+
221+
if let Some((_, bcs)) = &self.value {
222+
bcs::from_bytes::<T>(bcs).map_err(|_| anyhow!("Cannot decode BCS bytes"))
223+
} else {
224+
Err(anyhow!("No value found"))
225+
}
226+
}
207227
}
208228

209229
/// The GraphQL client for interacting with the Sui blockchain.

crates/sui-graphql-client/src/query_types/dynamic_fields.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -135,15 +135,18 @@ impl DynamicFieldValue {
135135
}
136136

137137
/// Return the typename and bcs of this dynamic field value.
138-
pub fn type_bcs(&self) -> Option<(String, Vec<u8>)> {
138+
pub fn type_bcs(&self) -> Option<(TypeTag, Vec<u8>)> {
139139
match self {
140-
DynamicFieldValue::MoveObject(mo) => mo
141-
.contents
142-
.as_ref()
143-
.map(|o| (o.type_.repr.clone(), o.bcs.0.clone().into())),
144-
DynamicFieldValue::MoveValue(mv) => {
145-
Some((mv.type_.repr.clone(), mv.bcs.0.clone().into()))
146-
}
140+
DynamicFieldValue::MoveObject(mo) => mo.contents.as_ref().map(|o| {
141+
(
142+
TypeTag::from_str(&o.type_.repr.clone()).expect("Invalid TypeTag"),
143+
base64ct::Base64::decode_vec(&o.bcs.0).expect("Invalid Base64"),
144+
)
145+
}),
146+
DynamicFieldValue::MoveValue(mv) => Some((
147+
TypeTag::from_str(&mv.type_.repr.clone()).expect("Invalid TypeTag"),
148+
base64ct::Base64::decode_vec(&mv.bcs.0).expect("Invalid Base64"),
149+
)),
147150
_ => None,
148151
}
149152
}

0 commit comments

Comments
 (0)