Skip to content

Commit

Permalink
Add new rpc endpoint to get typed object info
Browse files Browse the repository at this point in the history
  • Loading branch information
666lcz committed Apr 19, 2022
1 parent 9ca0158 commit 1f91097
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 20 deletions.
7 changes: 3 additions & 4 deletions sui/src/rest_gateway/responses.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use serde_with::serde_as;

use sui_types::base_types::{ObjectDigest, ObjectID, ObjectRef, SequenceNumber};
use sui_types::crypto::SignableBytes;
use sui_types::error::SuiError;
use sui_types::messages::TransactionData;
use sui_types::object::ObjectRead;

Expand Down Expand Up @@ -159,16 +160,14 @@ pub enum GetObjectInfoResponse {
}

impl TryFrom<ObjectRead> for GetObjectInfoResponse {
type Error = HttpError;
type Error = SuiError;

fn try_from(obj: ObjectRead) -> Result<Self, Self::Error> {
match obj {
ObjectRead::Exists(object_ref, object, layout) => {
Ok(Self::Exists(ObjectExistsResponse {
object_ref: object_ref.into(),
object: object.to_json(&layout).map_err(|e| {
custom_http_error(StatusCode::INTERNAL_SERVER_ERROR, e.to_string())
})?,
object: object.to_json(&layout)?,
}))
}
ObjectRead::NotExists(object_id) => Ok(Self::NotExists(ObjectNotExistsResponse {
Expand Down
24 changes: 10 additions & 14 deletions sui/src/rest_server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ use sui::rest_gateway::requests::{
SplitCoinRequest, SyncRequest, TransferTransactionRequest,
};
use sui::rest_gateway::responses::{
custom_http_error, GetObjectInfoResponse, HttpResponseOk, JsonResponse, NamedObjectRef,
ObjectResponse, ObjectSchemaResponse, TransactionBytes,
custom_http_error, HttpResponseOk, JsonResponse, NamedObjectRef, ObjectResponse,
ObjectSchemaResponse, TransactionBytes,
};
use sui::{sui_config_dir, SUI_GATEWAY_CONFIG};
use sui_core::gateway_state::gateway_responses::TransactionResponse;
Expand Down Expand Up @@ -265,24 +265,20 @@ async fn object_schema(
async fn object_info(
ctx: Arc<RequestContext<ServerContext>>,
query: Query<GetObjectInfoRequest>,
) -> Result<HttpResponseOk<GetObjectInfoResponse>, HttpError> {
) -> Result<HttpResponseOk<JsonResponse<ObjectRead>>, HttpError> {
let gateway = ctx.context().gateway.lock().await;

let object_info_params = query.into_inner();
let object_id = ObjectID::try_from(object_info_params.object_id)
.map_err(|error| custom_http_error(StatusCode::BAD_REQUEST, format!("{error}")))?;

let object: GetObjectInfoResponse = gateway
.get_object_info(object_id)
.await
.map_err(|error| {
custom_http_error(
StatusCode::NOT_FOUND,
format!("Error while getting object info: {:?}", error),
)
})?
.try_into()?;
Ok(HttpResponseOk(object))
let object_read = gateway.get_object_info(object_id).await.map_err(|error| {
custom_http_error(
StatusCode::NOT_FOUND,
format!("Error while getting object info: {:?}", error),
)
})?;
Ok(HttpResponseOk(JsonResponse(object_read)))
}

/// Transfer object from one address to another. Gas will be paid using the gas
Expand Down
19 changes: 17 additions & 2 deletions sui/src/rpc_gateway.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ use sui_types::object::ObjectRead;

use crate::config::PersistedConfig;
use crate::gateway::GatewayConfig;
use crate::rest_gateway::responses::GetObjectInfoResponse;
use crate::rest_gateway::responses::{NamedObjectRef, ObjectResponse};

#[rpc(server, client, namespace = "sui")]
pub trait RpcGateway {
#[method(name = "getObjectInfo")]
async fn get_object_info(&self, object_id: ObjectID) -> RpcResult<ObjectRead>;
#[method(name = "getObjectTypedInfo")]
async fn get_object_typed_info(&self, object_id: ObjectID) -> RpcResult<GetObjectInfoResponse>;

#[method(name = "transferCoin")]
async fn transfer_coin(
Expand Down Expand Up @@ -118,6 +119,11 @@ pub trait RpcGateway {

#[method(name = "getTransaction")]
async fn get_transaction(&self, digest: TransactionDigest) -> RpcResult<CertifiedTransaction>;

/// Low level API to get object info. Client Applications should prefer to use
/// `get_object_typed_info` instead.
#[method(name = "getObjectInfoRaw")]
async fn get_object_info(&self, object_id: ObjectID) -> RpcResult<ObjectRead>;
}

pub struct RpcGatewayImpl {
Expand Down Expand Up @@ -248,6 +254,15 @@ impl RpcGatewayServer for RpcGatewayImpl {
Ok(self.gateway.get_object_info(object_id).await?)
}

async fn get_object_typed_info(&self, object_id: ObjectID) -> RpcResult<GetObjectInfoResponse> {
Ok(self
.gateway
.get_object_info(object_id)
.await?
.try_into()
.map_err(|e| anyhow!("{}", e))?)
}

async fn execute_transaction(
&self,
signed_tx: SignedTransaction,
Expand Down

0 comments on commit 1f91097

Please sign in to comment.