Skip to content

Commit

Permalink
fix cosmos messages (#158)
Browse files Browse the repository at this point in the history
  • Loading branch information
beer-1 authored Oct 31, 2024
1 parent 7d2dbd5 commit 6ebb22a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 26 deletions.
1 change: 1 addition & 0 deletions crates/e2e-move-tests/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ sha3 = { workspace = true }
bytes = { workspace = true }
bigdecimal = { workspace = true }
bech32 = { workspace = true }
base64 = { workspace = true }

initia-move-types = { workspace = true }
initia-move-vm = { workspace = true }
Expand Down
24 changes: 14 additions & 10 deletions crates/e2e-move-tests/src/tests/cosmos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use move_core_types::account_address::AccountAddress;
use move_core_types::language_storage::TypeTag;
use move_core_types::vm_status::VMStatus;

use base64::{self, Engine};
use bech32::{Bech32, Hrp};
use sha3::{Digest, Sha3_256};

Expand Down Expand Up @@ -397,7 +398,7 @@ fn test_cosmos_pay_fee() {
let timeout_fee_denom = str::from_utf8(FEE_B_SYMBOL).unwrap();
let sender_cosmos_addr =
bech32::encode::<Bech32>(Hrp::parse_unchecked("init"), &sender.into_bytes()).unwrap();
let expected_data = format!("{{\"@type\":\"/ibc.applications.fee.v1.MsgPayPacketFee\",\"fee\":{{\"ack_fee\":[{{\"amount\":\"{ack_fee_amount}\",\"denom\":\"{ack_fee_denom}\"}}],\"recv_fee\":[{{\"amount\":\"{recv_fee_amount}\",\"denom\":\"{recv_fee_denom}\"}}],\"timeout_fee\":[{{\"amount\":\"{timeout_fee_amount}\",\"denom\":\"{timeout_fee_denom}\"}}]}},\"relayers\":[],\"signer\":\"{sender_cosmos_addr}\",\"source_channel\":\"{source_channel}\",\"source_port\":\"{source_port}\"}}");
let expected_data = format!("{{\"@type\":\"/ibc.applications.fee.v1.MsgPayPacketFee\",\"fee\":{{\"ack_fee\":[{{\"amount\":\"{ack_fee_amount}\",\"denom\":\"{ack_fee_denom}\"}}],\"recv_fee\":[{{\"amount\":\"{recv_fee_amount}\",\"denom\":\"{recv_fee_denom}\"}}],\"timeout_fee\":[{{\"amount\":\"{timeout_fee_amount}\",\"denom\":\"{timeout_fee_denom}\"}}]}},\"relayers\":[],\"signer\":\"{sender_cosmos_addr}\",\"source_channel_id\":\"{source_channel}\",\"source_port_id\":\"{source_port}\"}}");

let test_pay_fee = (
sender,
Expand Down Expand Up @@ -442,12 +443,13 @@ fn test_cosmos_move_execute() {
let arg1 = vec![1, 2, 3];
let arg2 = vec![4, 5, 6];

let arg1_hex = hex::encode(arg1.clone());
let arg2_hex = hex::encode(arg2.clone());
let base64 = base64::engine::general_purpose::STANDARD;
let arg1_base64 = base64.encode(arg1.clone());
let arg2_base64 = base64.encode(arg2.clone());
let module_addr_hex = module_address.to_hex_literal();
let sender_cosmos_addr =
bech32::encode::<Bech32>(Hrp::parse_unchecked("init"), &sender.into_bytes()).unwrap();
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgExecute\",\"args\":[\"{arg1_hex}\",\"{arg2_hex}\"],\"function_name\":\"{function_name}\",\"module_address\":\"{module_addr_hex}\",\"module_name\":\"{module_name}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgExecute\",\"args\":[\"{arg1_base64}\",\"{arg2_base64}\"],\"function_name\":\"{function_name}\",\"module_address\":\"{module_addr_hex}\",\"module_name\":\"{module_name}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");

let test_move_execute = (
sender,
Expand Down Expand Up @@ -532,12 +534,13 @@ fn test_cosmos_move_script() {
let arg1 = vec![1, 2, 3];
let arg2 = vec![4, 5, 6];

let code_bytes_hex = hex::encode(code_bytes.clone());
let arg1_hex = hex::encode(arg1.clone());
let arg2_hex = hex::encode(arg2.clone());
let base64 = base64::engine::general_purpose::STANDARD;
let code_bytes_base64 = base64.encode(code_bytes.clone());
let arg1_base64 = base64.encode(arg1.clone());
let arg2_base64 = base64.encode(arg2.clone());
let sender_cosmos_addr =
bech32::encode::<Bech32>(Hrp::parse_unchecked("init"), &sender.into_bytes()).unwrap();
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgScript\",\"args\":[\"{arg1_hex}\",\"{arg2_hex}\"],\"code_bytes\":\"{code_bytes_hex}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgScript\",\"args\":[\"{arg1_base64}\",\"{arg2_base64}\"],\"code_bytes\":\"{code_bytes_base64}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");

let test_move_script = (
sender,
Expand Down Expand Up @@ -575,10 +578,11 @@ fn test_cosmos_move_script_with_json() {
let arg1 = b"\"hello\"".to_vec();
let arg2 = b"\"world\"".to_vec();

let code_bytes_hex = hex::encode(code_bytes.clone());
let base64 = base64::engine::general_purpose::STANDARD;
let code_bytes_base64 = base64.encode(code_bytes.clone());
let sender_cosmos_addr =
bech32::encode::<Bech32>(Hrp::parse_unchecked("init"), &sender.into_bytes()).unwrap();
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgScriptJSON\",\"args\":[\"\\\"hello\\\"\",\"\\\"world\\\"\"],\"code_bytes\":\"{code_bytes_hex}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");
let expected_data = format!("{{\"@type\":\"/initia.move.v1.MsgScriptJSON\",\"args\":[\"\\\"hello\\\"\",\"\\\"world\\\"\"],\"code_bytes\":\"{code_bytes_base64}\",\"sender\":\"{sender_cosmos_addr}\",\"type_args\":[\"{type_arg1}\",\"{type_arg2}\"]}}");

let test_move_script = (
sender,
Expand Down
Binary file modified precompile/binaries/minlib/cosmos.mv
Binary file not shown.
Binary file modified precompile/binaries/stdlib/cosmos.mv
Binary file not shown.
21 changes: 13 additions & 8 deletions precompile/modules/initia_stdlib/sources/cosmos.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module initia_std::cosmos {
use std::error;
use std::coin::metadata_to_denom;
use std::collection::collection_to_class_id;
use std::base64;

use initia_std::json;

Expand Down Expand Up @@ -78,7 +79,7 @@ module initia_std::cosmos {
module_name: String,
function_name: String,
type_args: vector<String>,
args: vector<vector<u8>>
args: vector<String> // base64 encoded
}

public entry fun move_execute(
Expand All @@ -89,6 +90,7 @@ module initia_std::cosmos {
type_args: vector<String>,
args: vector<vector<u8>>
) {
let args = vector::map(args, |arg| base64::to_string(arg));
stargate(
sender,
json::marshal(
Expand Down Expand Up @@ -142,9 +144,9 @@ module initia_std::cosmos {
struct ScriptRequest has copy, drop {
_type_: String,
sender: String,
code_bytes: vector<u8>,
code_bytes: String, // base64 encoded
type_args: vector<String>,
args: vector<vector<u8>>
args: vector<String> // base64 encoded
}

public entry fun move_script(
Expand All @@ -153,6 +155,8 @@ module initia_std::cosmos {
type_args: vector<String>,
args: vector<vector<u8>>
) {
let args = vector::map(args, |arg| base64::to_string(arg));
let code_bytes = base64::to_string(code_bytes);
stargate(
sender,
json::marshal(
Expand All @@ -170,7 +174,7 @@ module initia_std::cosmos {
struct ScriptJSONRequest has copy, drop {
_type_: String,
sender: String,
code_bytes: vector<u8>,
code_bytes: String,
type_args: vector<String>,
args: vector<String>
}
Expand All @@ -181,6 +185,7 @@ module initia_std::cosmos {
type_args: vector<String>,
args: vector<String>
) {
let code_bytes = base64::to_string(code_bytes);
stargate(
sender,
json::marshal(
Expand Down Expand Up @@ -356,8 +361,8 @@ module initia_std::cosmos {
struct PayFeeRequest has copy, drop {
_type_: String,
_signer_: String,
source_port: String,
source_channel: String,
source_port_id: String,
source_channel_id: String,
fee: Fee,
relayers: vector<String>
}
Expand All @@ -381,8 +386,8 @@ module initia_std::cosmos {
&PayFeeRequest {
_type_: string::utf8(b"/ibc.applications.fee.v1.MsgPayPacketFee"),
_signer_: address::to_sdk(signer::address_of(sender)),
source_port,
source_channel,
source_port_id: source_port,
source_channel_id: source_channel,
fee: Fee {
recv_fee: vector[
CosmosCoin {
Expand Down
21 changes: 13 additions & 8 deletions precompile/modules/minitia_stdlib/sources/cosmos.move
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ module minitia_std::cosmos {
use std::error;
use std::coin::metadata_to_denom;
use std::collection::collection_to_class_id;
use std::base64;

use minitia_std::json;

Expand Down Expand Up @@ -78,7 +79,7 @@ module minitia_std::cosmos {
module_name: String,
function_name: String,
type_args: vector<String>,
args: vector<vector<u8>>
args: vector<String> // base64 encoded
}

public entry fun move_execute(
Expand All @@ -89,6 +90,7 @@ module minitia_std::cosmos {
type_args: vector<String>,
args: vector<vector<u8>>
) {
let args = vector::map(args, |arg| base64::to_string(arg));
stargate(
sender,
json::marshal(
Expand Down Expand Up @@ -142,9 +144,9 @@ module minitia_std::cosmos {
struct ScriptRequest has copy, drop {
_type_: String,
sender: String,
code_bytes: vector<u8>,
code_bytes: String, // base64 encoded
type_args: vector<String>,
args: vector<vector<u8>>
args: vector<String> // base64 encoded
}

public entry fun move_script(
Expand All @@ -153,6 +155,8 @@ module minitia_std::cosmos {
type_args: vector<String>,
args: vector<vector<u8>>
) {
let args = vector::map(args, |arg| base64::to_string(arg));
let code_bytes = base64::to_string(code_bytes);
stargate(
sender,
json::marshal(
Expand All @@ -170,7 +174,7 @@ module minitia_std::cosmos {
struct ScriptJSONRequest has copy, drop {
_type_: String,
sender: String,
code_bytes: vector<u8>,
code_bytes: String,
type_args: vector<String>,
args: vector<String>
}
Expand All @@ -181,6 +185,7 @@ module minitia_std::cosmos {
type_args: vector<String>,
args: vector<String>
) {
let code_bytes = base64::to_string(code_bytes);
stargate(
sender,
json::marshal(
Expand Down Expand Up @@ -356,8 +361,8 @@ module minitia_std::cosmos {
struct PayFeeRequest has copy, drop {
_type_: String,
_signer_: String,
source_port: String,
source_channel: String,
source_port_id: String,
source_channel_id: String,
fee: Fee,
relayers: vector<String>
}
Expand All @@ -381,8 +386,8 @@ module minitia_std::cosmos {
&PayFeeRequest {
_type_: string::utf8(b"/ibc.applications.fee.v1.MsgPayPacketFee"),
_signer_: address::to_sdk(signer::address_of(sender)),
source_port,
source_channel,
source_port_id: source_port,
source_channel_id: source_channel,
fee: Fee {
recv_fee: vector[
CosmosCoin {
Expand Down

0 comments on commit 6ebb22a

Please sign in to comment.