Skip to content

Commit

Permalink
[Cosmos]: Add Terra and standard WASM tests
Browse files Browse the repository at this point in the history
  • Loading branch information
satoshiotomakan committed Nov 13, 2023
1 parent e31495a commit 2ebc7bb
Show file tree
Hide file tree
Showing 5 changed files with 262 additions and 26 deletions.
12 changes: 6 additions & 6 deletions rust/tw_cosmos_sdk/src/modules/tx_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ where

let execute_payload = WasmExecutePayload::Transfer {
amount: U256::from_big_endian_slice(&transfer.amount)?,
recipient: Address::from_str_with_coin(coin, &transfer.recipient_address)?,
recipient: transfer.recipient_address.to_string(),
};

let msg = TerraExecuteContractMessage {
Expand All @@ -380,7 +380,7 @@ where

let execute_payload = WasmExecutePayload::Send {
amount: U256::from_big_endian_slice(&send.amount)?,
contract: Address::from_str_with_coin(coin, &send.contract_address)?,
contract: send.recipient_contract_address.to_string(),
msg: send.msg.to_string(),
};

Expand Down Expand Up @@ -410,7 +410,7 @@ where
let msg = TerraExecuteContractMessage {
sender: Address::from_str_with_coin(coin, &generic.sender_address)?,
contract: Address::from_str_with_coin(coin, &generic.contract_address)?,
execute_msg: ExecuteMsg::RegularString(generic.execute_msg.to_string()),
execute_msg: ExecuteMsg::String(generic.execute_msg.to_string()),
coins,
};
Ok(msg.into_boxed())
Expand All @@ -426,7 +426,7 @@ where

let transfer_payload = WasmExecutePayload::Transfer {
amount: U256::from_big_endian_slice(&transfer.amount)?,
recipient: Address::from_str_with_coin(coin, &transfer.recipient_address)?,
recipient: transfer.recipient_address.to_string(),
};

let msg = WasmExecuteContractMessage {
Expand All @@ -449,7 +449,7 @@ where

let execute_payload = WasmExecutePayload::Send {
amount: U256::from_big_endian_slice(&send.amount)?,
contract: Address::from_str_with_coin(coin, &send.contract_address)?,
contract: send.recipient_contract_address.to_string(),
msg: send.msg.to_string(),
};

Expand Down Expand Up @@ -478,7 +478,7 @@ where
let msg = WasmExecuteContractMessage {
sender: Address::from_str_with_coin(coin, &generic.sender_address)?,
contract: Address::from_str_with_coin(coin, &generic.contract_address)?,
msg: ExecuteMsg::RegularString(generic.execute_msg.to_string()),
msg: ExecuteMsg::String(generic.execute_msg.to_string()),
coins,
};
Ok(msg.into_boxed())
Expand Down
15 changes: 13 additions & 2 deletions rust/tw_cosmos_sdk/src/transaction/message/terra_wasm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,10 @@ use crate::address::CosmosAddress;
use crate::modules::serializer::protobuf_serializer::build_coin;
use crate::proto::terra;
use crate::transaction::message::wasm_message::ExecuteMsg;
use crate::transaction::message::{message_to_json, CosmosMessage, JsonMessage, ProtobufMessage};
use crate::transaction::message::{CosmosMessage, JsonMessage, ProtobufMessage};
use crate::transaction::Coin;
use serde::Serialize;
use serde_json::json;
use tw_coin_entry::error::SigningResult;
use tw_proto::to_any;

Expand All @@ -36,7 +37,17 @@ impl<Address: CosmosAddress> CosmosMessage for TerraExecuteContractMessage<Addre
}

fn to_json(&self) -> SigningResult<JsonMessage> {
// Don't use `message_to_json` because we need to try to convert [`ExecuteMsg::String`] to [`ExecuteMsg::Json`] if possible.
let value = json!({
"coins": self.coins,
"contract": self.contract,
"execute_msg": self.execute_msg.try_to_json(),
"sender": self.sender,
});
// TODO custom_msg_type
message_to_json("wasm/MsgExecuteContract", self)
Ok(JsonMessage {
msg_type: "wasm/MsgExecuteContract".to_string(),
value,
})
}
}
38 changes: 29 additions & 9 deletions rust/tw_cosmos_sdk/src/transaction/message/wasm_message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,35 @@
use crate::address::CosmosAddress;
use crate::modules::serializer::protobuf_serializer::build_coin;
use crate::proto::cosmwasm;
use crate::transaction::message::{message_to_json, CosmosMessage, JsonMessage, ProtobufMessage};
use crate::transaction::message::{CosmosMessage, JsonMessage, ProtobufMessage};
use crate::transaction::Coin;
use serde::Serialize;
use serde_json::Value as Json;
use serde_json::{json, Value as Json};
use tw_coin_entry::error::{SigningError, SigningErrorType, SigningResult};
use tw_memory::Data;
use tw_number::U256;
use tw_proto::to_any;

#[derive(Serialize)]
#[derive(Clone, Serialize)]
#[serde(untagged)]
pub enum ExecuteMsg {
/// Either a regular string or a stringified JSON object.
RegularString(String),
String(String),
/// JSON object with a type.
Json(Json),
}

impl ExecuteMsg {
/// Tries to convert [`ExecuteMsg::String`] to [`ExecuteMsg::Json`], otherwise returns the same object.
pub fn try_to_json(&self) -> ExecuteMsg {
if let ExecuteMsg::String(s) = self {
if let Ok(json) = serde_json::from_str(&s) {
return ExecuteMsg::Json(json);
}
}
self.clone()
}

pub fn json<Payload: Serialize>(payload: Payload) -> SigningResult<ExecuteMsg> {
let payload = serde_json::to_value(payload)
.map_err(|_| SigningError(SigningErrorType::Error_internal))?;
Expand All @@ -34,7 +44,7 @@ impl ExecuteMsg {

pub fn to_bytes(&self) -> Data {
match self {
ExecuteMsg::RegularString(ref s) => s.as_bytes().to_vec(),
ExecuteMsg::String(ref s) => s.as_bytes().to_vec(),
ExecuteMsg::Json(ref j) => j.to_string().as_bytes().to_vec(),
}
}
Expand All @@ -61,24 +71,34 @@ impl<Address: CosmosAddress> CosmosMessage for WasmExecuteContractMessage<Addres
}

fn to_json(&self) -> SigningResult<JsonMessage> {
// Don't use `message_to_json` because we need to try to convert [`ExecuteMsg::String`] to [`ExecuteMsg::Json`] if possible.
let value = json!({
"coins": self.coins,
"contract": self.contract,
"msg": self.msg.try_to_json(),
"sender": self.sender,
});
// TODO custom_msg_type
message_to_json("wasm/MsgExecuteContract", self)
Ok(JsonMessage {
msg_type: "wasm/MsgExecuteContract".to_string(),
value,
})
}
}

#[derive(Serialize)]
pub enum WasmExecutePayload<Address: CosmosAddress> {
pub enum WasmExecutePayload {
#[serde(rename = "transfer")]
Transfer {
#[serde(serialize_with = "U256::as_decimal_str")]
amount: U256,
recipient: Address,
recipient: String,
},
#[serde(rename = "send")]
Send {
#[serde(serialize_with = "U256::as_decimal_str")]
amount: U256,
contract: Address,
contract: String,
msg: String,
},
}
Loading

0 comments on commit 2ebc7bb

Please sign in to comment.