@@ -107,14 +107,12 @@ pub struct DynamicFieldName {
107107 pub json : Option < serde_json:: Value > ,
108108}
109109
110- type Typename = String ;
111-
112110#[ derive( Debug ) ]
113111pub 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
193191impl 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.
0 commit comments