diff --git a/crates/sui-graphql-client/src/lib.rs b/crates/sui-graphql-client/src/lib.rs index 4c30a61e8..1d89e3a7f 100644 --- a/crates/sui-graphql-client/src/lib.rs +++ b/crates/sui-graphql-client/src/lib.rs @@ -23,6 +23,7 @@ use query_types::DryRunQuery; use query_types::DynamicFieldArgs; use query_types::DynamicFieldConnectionArgs; use query_types::DynamicFieldQuery; +use query_types::DynamicFieldsOwnerQuery; use query_types::DynamicFieldsQuery; use query_types::DynamicObjectFieldQuery; use query_types::EpochSummaryArgs; @@ -604,6 +605,49 @@ impl Client { } } + /// Get a page of dynamic fields for this address, assuming the dynamic fields are on wrapped + /// objects. + /// + /// This returns [`Page`] of [`serde_json::Value`]s, representing the value field of the + /// dynamic field as a JSON value. + pub async fn dynamic_fields_on_wrapped_objects( + &self, + address: Address, + after: Option, + before: Option, + first: Option, + last: Option, + ) -> Result>, Error> { + let operation = DynamicFieldsOwnerQuery::build(DynamicFieldConnectionArgs { + address, + after, + before, + first, + last, + }); + let response = self.run_query(&operation).await?; + if let Some(errors) = response.errors { + return Err(Error::msg(format!("{:?}", errors))); + } + + if let Some(dfs) = response.data { + if let Some(owner) = dfs.owner { + let page_info = owner.dynamic_fields.page_info; + let nodes = owner.dynamic_fields.nodes; + let jsons = nodes + .into_iter() + .map(|df| df.into()) + .collect::>(); + + Ok(Some(Page::new(page_info, jsons))) + } else { + Ok(None) + } + } else { + Ok(None) + } + } + // =========================================================================== // Epoch API // =========================================================================== @@ -818,6 +862,34 @@ impl Client { } } + /// Return the contents JSON of an object that is a Move object. + /// + /// If the object does not exist (e.g., due to prunning), this will return `Ok(None)`. + /// Similarly, if this is not an object but an address, it will return `Ok(None)`. + pub async fn object_move_contents( + &self, + address: Address, + version: Option, + ) -> Result, Error> { + let operation = ObjectQuery::build(ObjectQueryArgs { address, version }); + + let response = self.run_query(&operation).await?; + + if let Some(errors) = response.errors { + return Err(Error::msg(format!("{:?}", errors))); + } + + if let Some(object) = response.data { + Ok(object + .object + .and_then(|o| o.as_move_object) + .and_then(|o| o.contents) + .and_then(|mv| mv.json)) + } else { + Ok(None) + } + } + // =========================================================================== // Dry Run API // =========================================================================== @@ -1268,7 +1340,7 @@ mod tests { let mut num_coins = 0; while let Some(result) = stream.next().await { assert!(result.is_ok()); - num_coins += 1; + num_coins = 1; } assert!(num_coins > 0); } diff --git a/crates/sui-graphql-client/src/query_types/dynamic_fields.rs b/crates/sui-graphql-client/src/query_types/dynamic_fields.rs index 857c25750..a321c02ef 100644 --- a/crates/sui-graphql-client/src/query_types/dynamic_fields.rs +++ b/crates/sui-graphql-client/src/query_types/dynamic_fields.rs @@ -7,6 +7,8 @@ use crate::query_types::schema; use crate::query_types::Address; use crate::query_types::Base64; use crate::query_types::JsonValue; +use crate::query_types::MoveObject; +use crate::query_types::MoveValue; use crate::query_types::PageInfo; use crate::DynamicFieldOutput; @@ -21,6 +23,27 @@ pub struct DynamicFieldsQuery { pub object: Option, } +#[derive(cynic::QueryFragment, Debug)] +#[cynic( + schema = "rpc", + graphql_type = "Query", + variables = "DynamicFieldConnectionArgs" +)] +pub struct DynamicFieldsOwnerQuery { + #[arguments(address: $address)] + pub owner: Option, +} +#[derive(cynic::QueryFragment, Debug)] +#[cynic( + schema = "rpc", + graphql_type = "Owner", + variables = "DynamicFieldConnectionArgs" +)] +pub struct ObjectOwner { + #[arguments(after: $after, before: $before, first: $first, last: $last)] + pub dynamic_fields: DynamicFieldConnection, +} + #[derive(cynic::QueryFragment, Debug)] #[cynic(schema = "rpc", graphql_type = "Query", variables = "DynamicFieldArgs")] pub struct DynamicFieldQuery { @@ -70,21 +93,6 @@ pub struct DynamicFieldConnectionArgs { pub last: Option, } -#[derive(cynic::QueryFragment, Debug)] -#[cynic(schema = "rpc", graphql_type = "MoveValue")] -pub struct MoveValue { - pub __typename: String, - pub bcs: Base64, - pub json: Option, -} - -#[derive(cynic::QueryFragment, Debug)] -#[cynic(schema = "rpc", graphql_type = "MoveObject")] -pub struct MoveObject { - pub bcs: Option, - pub contents: Option, -} - #[derive(cynic::QueryFragment, Debug)] #[cynic(schema = "rpc", graphql_type = "DynamicFieldConnection")] pub struct DynamicFieldConnection { diff --git a/crates/sui-graphql-client/src/query_types/mod.rs b/crates/sui-graphql-client/src/query_types/mod.rs index e0aff3ec2..67162cb68 100644 --- a/crates/sui-graphql-client/src/query_types/mod.rs +++ b/crates/sui-graphql-client/src/query_types/mod.rs @@ -41,6 +41,7 @@ pub use dynamic_fields::DynamicFieldArgs; pub use dynamic_fields::DynamicFieldConnectionArgs; pub use dynamic_fields::DynamicFieldName; pub use dynamic_fields::DynamicFieldQuery; +pub use dynamic_fields::DynamicFieldsOwnerQuery; pub use dynamic_fields::DynamicFieldsQuery; pub use dynamic_fields::DynamicObjectFieldQuery; pub use epoch::Epoch; @@ -116,8 +117,16 @@ pub struct GQLAddress { #[cynic(schema = "rpc", graphql_type = "MoveObject")] pub struct MoveObject { pub bcs: Option, + pub contents: Option, } +#[derive(cynic::QueryFragment, Debug)] +#[cynic(schema = "rpc", graphql_type = "MoveValue")] +pub struct MoveValue { + pub __typename: String, + pub bcs: Base64, + pub json: Option, +} // =========================================================================== // Utility Types // =========================================================================== diff --git a/crates/sui-graphql-client/src/query_types/object.rs b/crates/sui-graphql-client/src/query_types/object.rs index 10ea0e29b..bc10c6be3 100644 --- a/crates/sui-graphql-client/src/query_types/object.rs +++ b/crates/sui-graphql-client/src/query_types/object.rs @@ -4,6 +4,7 @@ use crate::query_types::schema; use crate::query_types::Address; use crate::query_types::Base64; +use crate::query_types::MoveObject; use crate::query_types::PageInfo; // =========================================================================== @@ -50,6 +51,7 @@ pub struct ObjectsQueryArgs<'a> { #[derive(cynic::QueryFragment, Debug)] #[cynic(schema = "rpc", graphql_type = "Object")] pub struct Object { + pub as_move_object: Option, pub bcs: Option, }