Skip to content

Commit

Permalink
rest: enable resolving of literals (#19857)
Browse files Browse the repository at this point in the history
Improve the format of `UnresolvedTransaction` as well as introduce the
ability to resolve un-serialized pure argument literals.

Example of JSON payload of an `UnresolvedTransaction`:
```
{
  "inputs": [
    {
      "object_id": "0x2d7f57570815c43eb485be9018caabd11ac863e9d49b1d9e33b3f4ac40cadc72"
    },
    {
      "value": 1
    },
    {
      "value": "0xc6fc0e38458632b1dd1d60b3a833865268a0faebe36864c71fb9805bd5a116cf"
    }
  ],
  "commands": [
    {
      "command": "split_coins",
      "coin": {
        "input": 0
      },
      "amounts": [
        {
          "input": 1
        },
        {
          "input": 1
        }
      ]
    },
    {
      "command": "transfer_objects",
      "objects": [
        {
          "result": [
            0,
            1
          ]
        },
        {
          "result": [
            0,
            0
          ]
        }
      ],
      "address": {
        "input": 2
      }
    }
  ],
  "sender": "0xff69fdb72bfc6ff5a337ff01c650fb0ce72447105ff050c2039c6b5b267b04a7"
}
```

which is resolved into the following `Transaction`:

```
{
  "version": "1",
  "kind": {
    "kind": "programmable_transaction",
    "inputs": [
      {
        "type": "immutable_or_owned",
        "object_id": "0x03517c0699f36a1df2e93b6c18db815d8f247a853465aec9cc48f9ceae4561ca",
        "version": "1",
        "digest": "7WyoNoiZQmTj75viHKYhA48tCSJ5CFqA6HtzJ55hehxP"
      },
      {
        "type": "pure",
        "value": "AQAAAAAAAAA="
      },
      {
        "type": "pure",
        "value": "JtcBTFgpW/n7HipqY6oz4bka0J8PyUPlSQbjR5lCq0Y="
      }
    ],
    "commands": [
      {
        "command": "split_coins",
        "coin": {
          "input": 0
        },
        "amounts": [
          {
            "input": 1
          },
          {
            "input": 1
          }
        ]
      },
      {
        "command": "transfer_objects",
        "objects": [
          {
            "result": [
              0,
              1
            ]
          },
          {
            "result": [
              0,
              0
            ]
          }
        ],
        "address": {
          "input": 2
        }
      }
    ]
  },
  "sender": "0xb73663359e72a36122aaf3f08629fa684b667e0fe6e356b119c623c7c9459888",
  "gas_payment": {
    "objects": [
      {
        "object_id": "0x94b1bef12a8db7b60fa89ad9bc2966d661a3a1002d921ada981e700648470304",
        "version": "1",
        "digest": "9kcUt38E4i8g5DartpUdBxW9m5n1u8AaJLyintWiddd6"
      },
      {
        "object_id": "0xacc757731db589ef093130e0d6c839e809f9673a51be92667ecbcd486db73995",
        "version": "1",
        "digest": "2U3xxN1G9vf4raCGUHz6AejqVMWJCkEBmsbLgqwae5be"
      },
      {
        "object_id": "0xd0891f6c419f3dd1a531e70779979f3c7aa91d13ae9125ffbae05f3960ee4249",
        "version": "1",
        "digest": "DkJRVUKfwV9pZ1NYEydvKwGpJei7YDDRemfRahruBDsQ"
      },
      {
        "object_id": "0xde8bdc786f18e7d1b9d2ac975acd640952fd3c75303e4f35d0657f90ab7e947e",
        "version": "1",
        "digest": "8RJuNzFawuVbFz6zSH1GMeJuwiHfT2ZzfHKHdr6LrJrU"
      }
    ],
    "owner": "0xb73663359e72a36122aaf3f08629fa684b667e0fe6e356b119c623c7c9459888",
    "price": "1000",
    "budget": "5952000"
  },
  "expiration": null
}
```
  • Loading branch information
bmwill authored Oct 16, 2024
1 parent 6cc663c commit 2224cf3
Show file tree
Hide file tree
Showing 8 changed files with 1,422 additions and 171 deletions.
29 changes: 15 additions & 14 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 @@ -593,7 +593,8 @@ anemo-cli = { git = "https://github.com/mystenlabs/anemo.git", rev = "e609f7697e
anemo-tower = { git = "https://github.com/mystenlabs/anemo.git", rev = "e609f7697ed6169bf0760882a0b6c032a57e4f3b" }

# core-types with json format for REST api
sui-sdk-types = { version = "0.0.1", features = ["hash", "serde", "schemars"] }
# sui-sdk-types = { version = "0.0.1", features = ["hash", "serde", "schemars"] }
sui-sdk-types = { git = "https://github.com/MystenLabs/sui-rust-sdk.git", rev = "31bd9da32a8057edc45da8f5e6a8f25b83919b93", features = ["hash", "serde", "schemars"] }

### Workspace Members ###
anemo-benchmark = { path = "crates/anemo-benchmark" }
Expand Down
147 changes: 113 additions & 34 deletions crates/sui-e2e-tests/tests/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ use sui_sdk_types::types::Command;
use sui_sdk_types::types::TransactionExpiration;
use sui_sdk_types::types::UnresolvedGasPayment;
use sui_sdk_types::types::UnresolvedInputArgument;
use sui_sdk_types::types::UnresolvedObjectReference;
use sui_sdk_types::types::UnresolvedProgrammableTransaction;
use sui_sdk_types::types::UnresolvedTransaction;
use sui_sdk_types::types::UnresolvedValue;
use sui_test_transaction_builder::make_transfer_sui_transaction;
use sui_types::base_types::SuiAddress;
use sui_types::effects::TransactionEffectsAPI;
Expand Down Expand Up @@ -80,13 +80,13 @@ async fn resolve_transaction_simple_transfer() {
let unresolved_transaction = UnresolvedTransaction {
ptb: UnresolvedProgrammableTransaction {
inputs: vec![
UnresolvedInputArgument::ImmutableOrOwned(UnresolvedObjectReference {
object_id: obj_to_send.into(),
version: None,
digest: None,
}),
UnresolvedInputArgument::Pure {
value: bcs::to_bytes(&recipient).unwrap(),
UnresolvedInputArgument {
object_id: Some(obj_to_send.into()),
..Default::default()
},
UnresolvedInputArgument {
value: Some(UnresolvedValue::String(recipient.to_string())),
..Default::default()
},
],
commands: vec![Command::TransferObjects(
Expand Down Expand Up @@ -147,13 +147,13 @@ async fn resolve_transaction_transfer_with_sponsor() {
let unresolved_transaction = UnresolvedTransaction {
ptb: UnresolvedProgrammableTransaction {
inputs: vec![
UnresolvedInputArgument::ImmutableOrOwned(UnresolvedObjectReference {
object_id: obj_to_send.into(),
version: None,
digest: None,
}),
UnresolvedInputArgument::Pure {
value: bcs::to_bytes(&recipient).unwrap(),
UnresolvedInputArgument {
object_id: Some(obj_to_send.into()),
..Default::default()
},
UnresolvedInputArgument {
value: Some(UnresolvedValue::String(recipient.to_string())),
..Default::default()
},
],
commands: vec![Command::TransferObjects(
Expand Down Expand Up @@ -230,10 +230,9 @@ async fn resolve_transaction_borrowed_shared_object() {

let unresolved_transaction = UnresolvedTransaction {
ptb: UnresolvedProgrammableTransaction {
inputs: vec![UnresolvedInputArgument::Shared {
object_id: "0x6".parse().unwrap(),
initial_shared_version: None,
mutable: None,
inputs: vec![UnresolvedInputArgument {
object_id: Some("0x6".parse().unwrap()),
..Default::default()
}],
commands: vec![Command::MoveCall(sui_sdk_types::types::MoveCall {
package: "0x2".parse().unwrap(),
Expand Down Expand Up @@ -299,18 +298,17 @@ async fn resolve_transaction_mutable_shared_object() {
let unresolved_transaction = UnresolvedTransaction {
ptb: UnresolvedProgrammableTransaction {
inputs: vec![
UnresolvedInputArgument::Shared {
object_id: "0x5".parse().unwrap(),
initial_shared_version: None,
mutable: None,
UnresolvedInputArgument {
object_id: Some("0x5".parse().unwrap()),
..Default::default()
},
UnresolvedInputArgument {
object_id: Some(obj_to_stake.into()),
..Default::default()
},
UnresolvedInputArgument::ImmutableOrOwned(UnresolvedObjectReference {
object_id: obj_to_stake.into(),
version: None,
digest: None,
}),
UnresolvedInputArgument::Pure {
value: bcs::to_bytes(&validator_address).unwrap(),
UnresolvedInputArgument {
value: Some(UnresolvedValue::String(validator_address.to_string())),
..Default::default()
},
],
commands: vec![Command::MoveCall(sui_sdk_types::types::MoveCall {
Expand Down Expand Up @@ -366,10 +364,9 @@ async fn resolve_transaction_insufficient_gas() {
// Test the case where we don't have enough coins/gas for the required budget
let unresolved_transaction = UnresolvedTransaction {
ptb: UnresolvedProgrammableTransaction {
inputs: vec![UnresolvedInputArgument::Shared {
object_id: "0x6".parse().unwrap(),
initial_shared_version: None,
mutable: None,
inputs: vec![UnresolvedInputArgument {
object_id: Some("0x6".parse().unwrap()),
..Default::default()
}],
commands: vec![Command::MoveCall(sui_sdk_types::types::MoveCall {
package: "0x2".parse().unwrap(),
Expand Down Expand Up @@ -402,3 +399,85 @@ fn assert_contains(haystack: &str, needle: &str) {
panic!("{haystack:?} does not contain {needle:?}");
}
}

#[sim_test]
async fn resolve_transaction_with_raw_json() {
let test_cluster = TestClusterBuilder::new().build().await;

let client = Client::new(test_cluster.rpc_url());
let recipient = SuiAddress::random_for_testing_only();

let (sender, mut gas) = test_cluster.wallet.get_one_account().await.unwrap();
gas.sort_by_key(|object_ref| object_ref.0);
let obj_to_send = gas.first().unwrap().0;

let unresolved_transaction = serde_json::json!({
"inputs": [
{
"object_id": obj_to_send
},
{
"value": 1
},
{
"value": recipient
}
],

"commands": [
{
"command": "split_coins",
"coin": { "input": 0 },
"amounts": [
{
"input": 1,
},
{
"input": 1,
}
]
},
{
"command": "transfer_objects",
"objects": [
{ "result": [0, 1] },
{ "result": [0, 0] }
],
"address": { "input": 2 }
}
],

"sender": sender
});

let resolved = client
.inner()
.resolve_transaction_with_parameters(
&serde_json::from_value(unresolved_transaction).unwrap(),
&ResolveTransactionQueryParameters {
simulate: true,
..Default::default()
},
)
.await
.unwrap()
.into_inner();

let signed_transaction = test_cluster
.wallet
.sign_transaction(&resolved.transaction.try_into().unwrap());
let effects = client
.execute_transaction(
&ExecuteTransactionQueryParameters::default(),
&signed_transaction,
)
.await
.unwrap()
.effects;

assert!(effects.status().is_ok(), "{:?}", effects.status());
assert_eq!(
resolved.simulation.unwrap().effects,
effects.try_into().unwrap()
);
}
1 change: 1 addition & 0 deletions crates/sui-rest-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ sui-types.workspace = true
mysten-network.workspace = true
sui-protocol-config.workspace = true
move-binary-format.workspace = true
move-core-types.workspace = true

[dev-dependencies]
diffy = "0.3"
Loading

0 comments on commit 2224cf3

Please sign in to comment.