Skip to content

Commit

Permalink
[4/n][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 Sep 26, 2023
1 parent 7ebccba commit 77b7863
Show file tree
Hide file tree
Showing 5 changed files with 458 additions and 3 deletions.
17 changes: 17 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,22 @@ fn resolve_call_arg(
}
}

pub fn is_receiving_argument(view: &BinaryIndexedView, arg_type: &SignatureToken) -> bool {
use SignatureToken as ST;

// Progress down into references to determine if the underlying type is a receiving
// type or not.
let mut token = arg_type;
while let ST::Reference(inner) | ST::MutableReference(inner) = token {
token = inner;
}

matches!(
token,
ST::StructInstantiation(idx, targs) if resolve_struct(view, *idx) == RESOLVED_RECEIVING_STRUCT && targs.len() == 1
)
}

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 77b7863

Please sign in to comment.