Skip to content

Commit

Permalink
Refactor sui-client for Sui Rust SDK and Rust SDK example project (My…
Browse files Browse the repository at this point in the history
…stenLabs#3102)

* refactor and polish up sui-client

* tic tac toe cli wip

* add code comments

* some refactoring and re-exporting sui deps in sui-client

* docs

* move sdk example to crate folder

* fix toml

* add workspace hack

* address PR commits
rename sui-client to sui-sdk
renamed SuiRpcClient to SuiClient as we might support embedded client in the future
removed client apis, implementing the functions in SuiClient directly instead.

* rename sui-client to sui-sdk

* Move examples to sui-sdk/examples
added event subscription
added SDK docs and in doc examples

* minor fix

* fix typo
  • Loading branch information
patrickkuo authored Jul 15, 2022
1 parent 10eff9f commit be7f0e0
Show file tree
Hide file tree
Showing 46 changed files with 1,188 additions and 274 deletions.
48 changes: 39 additions & 9 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ members = [
"crates/sui-gateway",
"crates/sui-json",
"crates/sui-json-rpc",
"crates/sui-json-rpc-api",
"crates/sui-json-rpc-types",
"crates/sui-network",
"crates/sui-node",
"crates/sui-open-rpc",
"crates/sui-open-rpc-macros",
"crates/sui-quorum-driver",
"crates/sui-sdk",
"crates/sui-storage",
"crates/sui-swarm",
"crates/sui-tool",
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-cluster-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ telemetry-subscribers = { git = "https://github.com/MystenLabs/mysten-infra", re

sui-faucet = { path = "../sui-faucet" }
sui = { path = "../sui" }
sui-json-rpc-api = { path = "../sui-json-rpc-api" }
sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-types = { path = "../sui-types" }
sui-sdk = { path = "../sui-sdk" }
sui-json = { path = "../sui-json" }
sui-config = { path = "../sui-config" }
workspace-hack = { path = "../workspace-hack"}
4 changes: 2 additions & 2 deletions crates/sui-cluster-test/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use sui::config::{Config, GatewayType, SuiClientConfig};
use sui_config::SUI_KEYSTORE_FILENAME;
use sui_faucet::FaucetResponse;
use sui_json::SuiJsonValue;
use sui_json_rpc_api::keystore::KeystoreType;
use sui_json_rpc_api::rpc_types::{GetObjectDataResponse, SuiExecutionStatus, TransactionResponse};
use sui_json_rpc_types::{GetObjectDataResponse, SuiExecutionStatus, TransactionResponse};
use sui_sdk::crypto::KeystoreType;
use sui_types::{
base_types::{encode_bytes_hex, ObjectID, SuiAddress},
crypto::get_key_pair,
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ sui-types = { path = "../sui-types" }
sui-storage = { path = "../sui-storage" }
sui-config = { path = "../sui-config" }
sui-json = { path = "../sui-json" }
sui-json-rpc-api = { path = "../sui-json-rpc-api" }
sui-json-rpc-types = { path = "../sui-json-rpc-types" }

move-binary-format = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f" }
move-bytecode-utils = { git = "https://github.com/move-language/move", rev = "7733658048a8bc80e9ba415b8c99aed9234eaa5f" }
Expand Down
43 changes: 3 additions & 40 deletions crates/sui-core/src/event_handler.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
// Copyright (c) 2022, Mysten Labs, Inc.
// SPDX-License-Identifier: Apache-2.0

use std::collections::BTreeMap;
use std::sync::Arc;

use move_bytecode_utils::module_cache::SyncModuleCache;
use serde_json::Value;
use sui_json_rpc_api::rpc_types::{SuiMoveStruct, SuiMoveValue};
use sui_json_rpc_types::SuiMoveStruct;
use tokio_stream::Stream;
use tracing::{debug, error, trace};

Expand Down Expand Up @@ -93,8 +91,8 @@ impl EventHandler {
let move_struct =
Event::move_event_to_move_struct(type_, contents, &self.module_cache)?;
// Convert into `SuiMoveStruct` which is a mirror of MoveStruct but with additional type supports, (e.g. ascii::String).
let sui_move_struct = move_struct.into();
Some(to_json_value(sui_move_struct).map_err(|e| {
let sui_move_struct = SuiMoveStruct::from(move_struct);
Some(sui_move_struct.to_json_value().map_err(|e| {
SuiError::ObjectSerializationError {
error: e.to_string(),
}
Expand All @@ -116,38 +114,3 @@ impl EventHandler {
self.event_streamer.subscribe(filter)
}
}

fn to_json_value(move_struct: SuiMoveStruct) -> Result<Value, serde_json::Error> {
// Unwrap MoveStructs
let unwrapped = match move_struct {
SuiMoveStruct::Runtime(values) => {
let values = values
.into_iter()
.map(|value| match value {
SuiMoveValue::Struct(move_struct) => to_json_value(move_struct),
SuiMoveValue::Vector(values) => to_json_value(SuiMoveStruct::Runtime(values)),
_ => serde_json::to_value(&value),
})
.collect::<Result<Vec<_>, _>>()?;
serde_json::to_value(&values)
}
// We only care about values here, assuming struct type information is known at the client side.
SuiMoveStruct::WithTypes { type_: _, fields } | SuiMoveStruct::WithFields(fields) => {
let fields = fields
.into_iter()
.map(|(key, value)| {
let value = match value {
SuiMoveValue::Struct(move_struct) => to_json_value(move_struct),
SuiMoveValue::Vector(values) => {
to_json_value(SuiMoveStruct::Runtime(values))
}
_ => serde_json::to_value(&value),
};
value.map(|value| (key, value))
})
.collect::<Result<BTreeMap<_, _>, _>>()?;
serde_json::to_value(&fields)
}
}?;
serde_json::to_value(&unwrapped)
}
2 changes: 1 addition & 1 deletion crates/sui-core/src/gateway_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ use crate::{
authority_client::AuthorityAPI, query_helpers::QueryHelpers,
};
use sui_json::{resolve_move_function_args, SuiJsonCallArg, SuiJsonValue};
use sui_json_rpc_api::rpc_types::{
use sui_json_rpc_types::{
GetObjectDataResponse, GetRawObjectDataResponse, MergeCoinResponse, MoveCallParams,
PublishResponse, RPCTransactionRequestParams, SplitCoinResponse, SuiMoveObject, SuiObject,
SuiObjectInfo, SuiTransactionEffects, SuiTypeTag, TransactionEffectsResponse,
Expand Down
11 changes: 6 additions & 5 deletions crates/sui-core/src/unit_tests/event_handler_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use move_core_types::{
use serde::Deserialize;
use serde::Serialize;
use serde_json::json;
use sui_json_rpc_types::SuiMoveStruct;

use crate::event_handler::to_json_value;
use sui_types::base_types::{ObjectID, SequenceNumber};
use sui_types::gas_coin::GasCoin;
use sui_types::SUI_FRAMEWORK_ADDRESS;
Expand All @@ -33,10 +33,11 @@ fn test_to_json_value() {
],
};
let event_bytes = bcs::to_bytes(&move_event).unwrap();
let sui_move_struct = MoveStruct::simple_deserialize(&event_bytes, &TestEvent::layout())
.unwrap()
.into();
let json_value = to_json_value(sui_move_struct).unwrap();
let sui_move_struct: SuiMoveStruct =
MoveStruct::simple_deserialize(&event_bytes, &TestEvent::layout())
.unwrap()
.into();
let json_value = sui_move_struct.to_json_value().unwrap();

assert_eq!(
Some(&json!(1000000)),
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-faucet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ tower-http = { version = "0.3.4", features = ["cors"] }
http = { version = "0.2.8" }

sui = { path = "../sui" }
sui-json-rpc-api = { path = "../sui-json-rpc-api" }
sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-types = { path = "../sui-types" }
sui-config = { path = "../sui-config" }
telemetry-subscribers = { git = "https://github.com/MystenLabs/mysten-infra", rev = "40dae350a699f59f2e296152e02c78765db34e68" }
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-faucet/src/faucet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use crate::FaucetError;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use sui_json_rpc_api::rpc_types::SuiParsedObject;
use sui_json_rpc_types::SuiParsedObject;
use sui_types::{
base_types::{ObjectID, SuiAddress},
gas_coin::GasCoin,
Expand Down
2 changes: 1 addition & 1 deletion crates/sui-faucet/src/faucet/simple_faucet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
use anyhow::anyhow;
use async_trait::async_trait;
use sui::client_commands::WalletContext;
use sui_json_rpc_api::rpc_types::{SuiExecutionStatus, SuiParsedObject};
use sui_json_rpc_types::{SuiExecutionStatus, SuiParsedObject};
use sui_types::{
base_types::{ObjectID, SuiAddress},
gas_coin::GasCoin,
Expand Down
3 changes: 2 additions & 1 deletion crates/sui-gateway/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ sui-config = { path = "../sui-config" }
sui-types = { path = "../sui-types" }
sui-json = { path = "../sui-json" }
sui-json-rpc = { path = "../sui-json-rpc" }
sui-json-rpc-api = { path = "../sui-json-rpc-api" }
sui-json-rpc-types= { path = "../sui-json-rpc-types" }
sui-sdk = { path = "../sui-sdk" }
sui-node = { path = "../sui-node" }

mysten-network = { git = "https://github.com/MystenLabs/mysten-infra", rev = "40dae350a699f59f2e296152e02c78765db34e68" }
Expand Down
Loading

0 comments on commit be7f0e0

Please sign in to comment.