Skip to content

Commit

Permalink
Add the rest of dynamic field + object that it's needed for MVR
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Oct 15, 2024
1 parent 3be26a8 commit 43fbd40
Show file tree
Hide file tree
Showing 4 changed files with 107 additions and 16 deletions.
74 changes: 73 additions & 1 deletion crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>,
before: Option<String>,
first: Option<i32>,
last: Option<i32>,
) -> Result<Option<Page<DynamicFieldOutput>>, 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::<Vec<DynamicFieldOutput>>();

Ok(Some(Page::new(page_info, jsons)))
} else {
Ok(None)
}
} else {
Ok(None)
}
}

// ===========================================================================
// Epoch API
// ===========================================================================
Expand Down Expand Up @@ -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<u64>,
) -> Result<Option<serde_json::Value>, 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
// ===========================================================================
Expand Down Expand Up @@ -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);
}
Expand Down
38 changes: 23 additions & 15 deletions crates/sui-graphql-client/src/query_types/dynamic_fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,6 +23,27 @@ pub struct DynamicFieldsQuery {
pub object: Option<Object>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(
schema = "rpc",
graphql_type = "Query",
variables = "DynamicFieldConnectionArgs"
)]
pub struct DynamicFieldsOwnerQuery {
#[arguments(address: $address)]
pub owner: Option<ObjectOwner>,
}
#[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 {
Expand Down Expand Up @@ -70,21 +93,6 @@ pub struct DynamicFieldConnectionArgs {
pub last: Option<i32>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "MoveValue")]
pub struct MoveValue {
pub __typename: String,
pub bcs: Base64,
pub json: Option<JsonValue>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "MoveObject")]
pub struct MoveObject {
pub bcs: Option<Base64>,
pub contents: Option<MoveValue>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "DynamicFieldConnection")]
pub struct DynamicFieldConnection {
Expand Down
9 changes: 9 additions & 0 deletions crates/sui-graphql-client/src/query_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -116,8 +117,16 @@ pub struct GQLAddress {
#[cynic(schema = "rpc", graphql_type = "MoveObject")]
pub struct MoveObject {
pub bcs: Option<Base64>,
pub contents: Option<MoveValue>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "MoveValue")]
pub struct MoveValue {
pub __typename: String,
pub bcs: Base64,
pub json: Option<JsonValue>,
}
// ===========================================================================
// Utility Types
// ===========================================================================
Expand Down
2 changes: 2 additions & 0 deletions crates/sui-graphql-client/src/query_types/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

// ===========================================================================
Expand Down Expand Up @@ -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<MoveObject>,
pub bcs: Option<Base64>,
}

Expand Down

0 comments on commit 43fbd40

Please sign in to comment.