Skip to content

Commit

Permalink
Add package query
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-mysten committed Oct 7, 2024
1 parent 112bfb3 commit e3263f4
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
56 changes: 56 additions & 0 deletions crates/sui-graphql-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use query_types::ObjectQuery;
use query_types::ObjectQueryArgs;
use query_types::ObjectsQuery;
use query_types::ObjectsQueryArgs;
use query_types::PackageQuery;
use query_types::PackageQueryArgs;
use query_types::PageInfo;
use query_types::ProtocolConfigQuery;
use query_types::ProtocolConfigs;
Expand Down Expand Up @@ -671,6 +673,44 @@ impl Client {
}
}

// ===========================================================================
// Package API
// ===========================================================================

// TODO: We should return maybe the MovePackage type, but the `data` field is not accessible.
// We might want to expose an API for getting the package or the struct depending what the
// object is.
/// Get a package's content by its address and optionally, a specific version.
pub async fn package(
&self,
address: Address,
version: Option<u64>,
) -> Result<Option<Object>, Error> {
let operation = PackageQuery::build(PackageQueryArgs { address, version });
let response = self.run_query(&operation).await?;

if let Some(errors) = response.errors {
return Err(Error::msg(format!("{:?}", errors)));
}

if let Some(data) = response.data {
let package = data
.package
.and_then(|p| p.bcs)
.map(|bcs| base64ct::Base64::decode_vec(bcs.0.as_str()))
.transpose()
.map_err(|e| Error::msg(format!("Cannot decode Base64 package bcs bytes: {e}")))?
.map(|b| bcs::from_bytes::<Object>(&b))
.transpose()
.map_err(|e| {
Error::msg(format!("Cannot decode bcs bytes into MovePackage: {e}"))
})?;
Ok(package)
} else {
Ok(None)
}
}

// ===========================================================================
// Transaction API
// ===========================================================================
Expand Down Expand Up @@ -1061,4 +1101,20 @@ mod tests {
);
}
}

#[tokio::test]
async fn test_package_query() {
let client = Client::new_testnet();
let package = client
.package(
"0x29100e79cce427714a2059aee4f858a0406fc34c7f50c93fb75c915b618feacf"
.parse()
.unwrap(),
None,
)
.await;
println!("{:?}", package);

assert!(package.is_ok());
}
}
3 changes: 3 additions & 0 deletions crates/sui-graphql-client/src/query_types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mod epoch;
mod events;
mod execute_tx;
mod object;
mod package;
mod protocol_config;
mod service_config;
mod transaction;
Expand Down Expand Up @@ -49,6 +50,8 @@ pub use object::ObjectQuery;
pub use object::ObjectQueryArgs;
pub use object::ObjectsQuery;
pub use object::ObjectsQueryArgs;
pub use package::PackageQuery;
pub use package::PackageQueryArgs;
pub use protocol_config::ProtocolConfigQuery;
pub use protocol_config::ProtocolConfigs;
pub use protocol_config::ProtocolVersionArgs;
Expand Down
25 changes: 25 additions & 0 deletions crates/sui-graphql-client/src/query_types/package.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright (c) Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use crate::query_types::schema;
use crate::query_types::Address;
use crate::query_types::Base64;

#[derive(cynic::QueryFragment, Debug)]
#[cynic(schema = "rpc", graphql_type = "Query", variables = "PackageQueryArgs")]
pub struct PackageQuery {
#[arguments(address: $address, version: $version)]
pub package: Option<MovePackage>,
}

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

#[derive(cynic::QueryVariables, Debug)]
pub struct PackageQueryArgs {
pub address: Address,
pub version: Option<u64>,
}

0 comments on commit e3263f4

Please sign in to comment.