Skip to content

Commit b4bed73

Browse files
authored
Adds ability to serialise Raw Sui Swap transactions (#4247)
* Adds ability to serialise Raw Sui Swap transactions * Adds sui compile test for raw json * Addresses review comments * Use serde::as_string instead of serde_with crate * Use specified values for gas budget and gas price if provided * Addresses review comment
1 parent df1843f commit b4bed73

File tree

14 files changed

+1917
-21
lines changed

14 files changed

+1917
-21
lines changed

rust/Cargo.lock

Lines changed: 20 additions & 15 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rust/chains/tw_sui/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@ edition = "2021"
77
indexmap = "2.0"
88
move-core-types = { git = "https://github.com/move-language/move", rev = "ea70797099baea64f05194a918cebd69ed02b285", features = ["address32"] }
99
serde = { version = "1.0", features = ["derive"] }
10+
serde_json = "1.0"
1011
serde_repr = "0.1"
1112
tw_coin_entry = { path = "../../tw_coin_entry" }
1213
tw_encoding = { path = "../../tw_encoding" }
1314
tw_hash = { path = "../../tw_hash" }
1415
tw_keypair = { path = "../../tw_keypair" }
1516
tw_memory = { path = "../../tw_memory" }
16-
tw_misc = { path = "../../tw_misc" }
17+
tw_misc = { path = "../../tw_misc", features = ["serde"] }
1718
tw_proto = { path = "../../tw_proto" }

rust/chains/tw_sui/src/modules/tx_builder.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ impl<'a> TWTransactionBuilder<'a> {
5353
TransactionType::transfer_object(ref transfer_obj) => {
5454
self.transfer_object_from_proto(transfer_obj)
5555
},
56+
TransactionType::raw_json(ref raw_json) => self.raw_json_from_proto(raw_json),
5657
TransactionType::None => SigningError::err(SigningErrorType::Error_invalid_params),
5758
}?;
5859
Ok(TWTransaction::Transaction(tx_data))
@@ -182,6 +183,14 @@ impl<'a> TWTransactionBuilder<'a> {
182183
)
183184
}
184185

186+
fn raw_json_from_proto(&self, raw_json: &str) -> SigningResult<TransactionData> {
187+
TransactionBuilder::raw_json(
188+
raw_json,
189+
self.input.gas_budget,
190+
self.input.reference_gas_price,
191+
)
192+
}
193+
185194
fn signer_address(&self) -> SigningResult<SuiAddress> {
186195
if self.input.private_key.is_empty() {
187196
SuiAddress::from_str(&self.input.signer)

rust/chains/tw_sui/src/transaction/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
pub mod command;
66
pub mod programmable_transaction;
7+
pub mod raw_types;
78
pub mod sui_types;
89
pub mod transaction_builder;
910
pub mod transaction_data;
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
use move_core_types::language_storage::TypeTag;
2+
use serde::{Deserialize, Serialize};
3+
use std::str::FromStr;
4+
use tw_coin_entry::error::prelude::SigningError;
5+
use tw_memory::Data;
6+
use tw_misc::serde::as_string;
7+
8+
use crate::address::SuiAddress;
9+
10+
use super::{
11+
command::Argument,
12+
sui_types::{ObjectArg, ObjectID, SequenceNumber},
13+
};
14+
15+
#[derive(Debug, Deserialize, Serialize)]
16+
#[serde(rename_all = "camelCase")]
17+
pub struct PaymentConfig {
18+
pub object_id: String,
19+
#[serde(with = "as_string")]
20+
pub version: u64,
21+
pub digest: String,
22+
}
23+
24+
#[derive(Debug, Deserialize, Serialize)]
25+
pub struct GasConfig {
26+
#[serde(with = "as_string")]
27+
pub budget: u64,
28+
#[serde(with = "as_string")]
29+
pub price: u64,
30+
pub payment: Vec<PaymentConfig>,
31+
}
32+
33+
#[derive(Debug, Deserialize, Serialize)]
34+
pub enum InputObjectArg {
35+
#[serde(rename_all = "camelCase")]
36+
Shared {
37+
mutable: bool,
38+
#[serde(with = "as_string")]
39+
initial_shared_version: u64,
40+
object_id: String,
41+
},
42+
}
43+
44+
impl TryFrom<InputObjectArg> for ObjectArg {
45+
type Error = SigningError;
46+
47+
fn try_from(arg: InputObjectArg) -> Result<Self, Self::Error> {
48+
match arg {
49+
InputObjectArg::Shared {
50+
mutable,
51+
initial_shared_version,
52+
object_id,
53+
} => Ok(ObjectArg::SharedObject {
54+
id: ObjectID::from_str(&object_id)?,
55+
initial_shared_version: SequenceNumber(initial_shared_version),
56+
mutable,
57+
}),
58+
}
59+
}
60+
}
61+
62+
#[derive(Debug, Deserialize, Serialize)]
63+
pub enum InputArg {
64+
Pure(Data),
65+
Object(InputObjectArg),
66+
}
67+
68+
#[derive(Debug, Deserialize, Serialize)]
69+
#[serde(rename_all = "camelCase")]
70+
pub struct Input {
71+
pub kind: String,
72+
pub index: u16,
73+
#[serde(rename = "type")]
74+
pub input_type: String,
75+
pub value: InputArg,
76+
}
77+
78+
#[derive(Debug, Deserialize, Serialize)]
79+
#[serde(tag = "kind")]
80+
pub enum TransactionArg {
81+
GasCoin,
82+
Input { index: u16 },
83+
Result { index: u16 },
84+
}
85+
86+
impl From<TransactionArg> for Argument {
87+
fn from(arg: TransactionArg) -> Self {
88+
match arg {
89+
TransactionArg::GasCoin => Argument::GasCoin,
90+
TransactionArg::Input { index } => Argument::Input(index),
91+
TransactionArg::Result { index } => Argument::Result(index),
92+
}
93+
}
94+
}
95+
96+
#[derive(Debug, Deserialize, Serialize, Clone)]
97+
pub struct TypeTagWrapper(#[serde(with = "as_string")] TypeTag);
98+
99+
impl From<TypeTagWrapper> for TypeTag {
100+
fn from(value: TypeTagWrapper) -> Self {
101+
value.0
102+
}
103+
}
104+
105+
#[derive(Debug, Deserialize, Serialize)]
106+
#[serde(tag = "kind")]
107+
pub enum Transaction {
108+
SplitCoins {
109+
coin: TransactionArg,
110+
amounts: Vec<TransactionArg>,
111+
},
112+
#[serde(rename_all = "camelCase")]
113+
MoveCall {
114+
target: String,
115+
type_arguments: Vec<TypeTagWrapper>,
116+
arguments: Vec<TransactionArg>,
117+
},
118+
TransferObjects {
119+
objects: Vec<TransactionArg>,
120+
address: TransactionArg,
121+
},
122+
}
123+
124+
#[derive(Debug, Deserialize, Serialize)]
125+
#[serde(rename_all = "camelCase")]
126+
pub struct RawTransaction {
127+
pub version: u8,
128+
pub sender: SuiAddress,
129+
pub expiration: Option<u64>,
130+
pub gas_config: GasConfig,
131+
pub inputs: Vec<Input>,
132+
pub transactions: Vec<Transaction>,
133+
}

0 commit comments

Comments
 (0)