Skip to content

Commit

Permalink
[tto-sdks] Add Rust SDK support for receiving arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tzakian committed Jul 20, 2023
1 parent 1518b1e commit ffca330
Show file tree
Hide file tree
Showing 6 changed files with 471 additions and 10 deletions.
12 changes: 5 additions & 7 deletions crates/sui-json-rpc-types/src/sui_transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1536,12 +1536,12 @@ impl SuiCallArg {
mutable,
}),
CallArg::Object(ObjectArg::Receiving((object_id, version, digest))) => {
SuiCallArg::Object(SuiObjectArg::Receiving {
SuiCallArg::Object(SuiObjectArg::Receiving {
object_id,
version,
digest,
})
}
})
}
})
}

Expand All @@ -1555,10 +1555,8 @@ impl SuiCallArg {
pub fn object(&self) -> Option<&ObjectID> {
match self {
SuiCallArg::Object(SuiObjectArg::SharedObject { object_id, .. })
| SuiCallArg::Object(SuiObjectArg::ImmOrOwnedObject { object_id, .. })
| SuiCallArg::Object(SuiObjectArg::Receiving{ object_id, ..}) => {
Some(object_id)
}
| SuiCallArg::Object(SuiObjectArg::ImmOrOwnedObject { object_id, .. })
| SuiCallArg::Object(SuiObjectArg::Receiving { object_id, .. }) => Some(object_id),
_ => None,
}
}
Expand Down
22 changes: 22 additions & 0 deletions crates/sui-json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use sui_types::base_types::{
};
use sui_types::id::{ID, RESOLVED_SUI_ID};
use sui_types::move_package::MovePackage;
use sui_types::transfer::RESOLVED_RECEIVING_STRUCT;
use sui_types::MOVE_STDLIB_ADDRESS;

const HEX_PREFIX: &str = "0x";
Expand Down Expand Up @@ -818,6 +819,27 @@ fn resolve_call_arg(
}
}

pub fn is_receiving_argument(view: &BinaryIndexedView, arg_type: &SignatureToken) -> bool {
let mut current_token = Some(arg_type);
while let Some(sig_tok) = current_token {
match sig_tok {
// Progress down into references to determine if the underlying type is a receiving
// type or not.
SignatureToken::Reference(inner) | SignatureToken::MutableReference(inner) => {
current_token = Some(inner);
}
SignatureToken::StructInstantiation(sidx, targs) => {
return resolve_struct(view, *sidx) == RESOLVED_RECEIVING_STRUCT
&& targs.len() == 1;
}
_ => {
return false;
}
}
}
false
}

fn resolve_call_args(
view: &BinaryIndexedView,
type_args: &[TypeTag],
Expand Down
23 changes: 20 additions & 3 deletions crates/sui-transaction-builder/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ use std::sync::Arc;
use anyhow::{anyhow, bail, ensure, Ok};
use async_trait::async_trait;
use futures::future::join_all;
use move_binary_format::binary_views::BinaryIndexedView;
use move_binary_format::file_format::SignatureToken;
use move_binary_format::file_format_common::VERSION_MAX;
use move_core_types::identifier::Identifier;
use move_core_types::language_storage::{StructTag, TypeTag};

use sui_json::{resolve_move_function_args, ResolvedCallArg, SuiJsonValue};
use sui_json::{is_receiving_argument, resolve_move_function_args, ResolvedCallArg, SuiJsonValue};
use sui_json_rpc_types::{
RPCTransactionRequestParams, SuiData, SuiObjectDataOptions, SuiObjectResponse, SuiRawData,
SuiTypeTag,
Expand Down Expand Up @@ -325,6 +327,8 @@ impl TransactionBuilder {
id: ObjectID,
objects: &mut BTreeMap<ObjectID, Object>,
is_mutable_ref: bool,
view: &BinaryIndexedView<'_>,
arg_type: &SignatureToken,
) -> Result<ObjectArg, anyhow::Error> {
let response = self
.0
Expand All @@ -335,6 +339,9 @@ impl TransactionBuilder {
let obj_ref = obj.compute_object_reference();
let owner = obj.owner;
objects.insert(id, obj);
if is_receiving_argument(view, arg_type) {
return Ok(ObjectArg::Receiving(obj_ref));
}
Ok(match owner {
Owner::Shared {
initial_shared_version,
Expand Down Expand Up @@ -385,6 +392,8 @@ impl TransactionBuilder {

let mut args = Vec::new();
let mut objects = BTreeMap::new();
let module = package.deserialize_module(module, VERSION_MAX, true)?;
let view = BinaryIndexedView::Module(&module);
for (arg, expected_type) in json_args_and_tokens {
args.push(match arg {
ResolvedCallArg::Pure(p) => builder.input(CallArg::Pure(p)),
Expand All @@ -394,6 +403,8 @@ impl TransactionBuilder {
id,
&mut objects,
matches!(expected_type, SignatureToken::MutableReference(_)),
&view,
&expected_type,
)
.await?,
)),
Expand All @@ -402,8 +413,14 @@ impl TransactionBuilder {
let mut object_ids = vec![];
for id in v {
object_ids.push(
self.get_object_arg(id, &mut objects, /* is_mutable_ref */ false)
.await?,
self.get_object_arg(
id,
&mut objects,
/* is_mutable_ref */ false,
&view,
&expected_type,
)
.await?,
)
}
builder.make_obj_vec(object_ids)
Expand Down
Loading

0 comments on commit ffca330

Please sign in to comment.