From 3bc2349e3a663d46899f18dceb8458d3348f722c Mon Sep 17 00:00:00 2001 From: ibrizsabin Date: Thu, 19 Sep 2024 15:33:28 +0545 Subject: [PATCH] feat: receive message args --- contracts/sui/intent_v1/sources/main.move | 53 +++++++++++++------ .../sui/intent_v1/sources/types/utils.move | 20 +++++++ 2 files changed, 56 insertions(+), 17 deletions(-) diff --git a/contracts/sui/intent_v1/sources/main.move b/contracts/sui/intent_v1/sources/main.move index bb7443e6..20c82a5f 100644 --- a/contracts/sui/intent_v1/sources/main.move +++ b/contracts/sui/intent_v1/sources/main.move @@ -17,7 +17,7 @@ module intents_v1::main { use sui::address::{Self as suiaddress}; use intents_v1::cluster_connection::{Self, ConnectionState}; use sui::hex::{Self}; - use intents_v1::utils::{id_to_hex_string}; + use intents_v1::utils::{id_to_hex_string,Self}; const FILL: u8 = 1; // Constant for Fill message type @@ -74,6 +74,11 @@ module intents_v1::main { id:u128, } + public struct Params has drop { + type_args: vector, + args: vector, + } + fun init(ctx: &mut TxContext) { let admin = AdminCap {id: object::new(ctx)}; let storage = Storage { @@ -378,6 +383,35 @@ module intents_v1::main { self.get_connection_state_mut().set_relayer(relayer); } + entry fun get_receive_msg_args(self:&Storage,msg:vector):Params{ + let msg= order_message::decode(&msg); + let bytes= if (msg.get_type() == FILL) { + let fill = order_fill::decode(&msg.get_message()); + fill.get_order_bytes() + }else if (msg.get_type() == CANCEL) { + let cancel = order_cancel::decode(&msg.get_message()); + cancel.get_order_bytes() + } else { + abort EInvalidMsgType + }; + + let order=swap_order::decode(&bytes); + let token_type=order.get_token(); + + let mut type_args:vector = vector::empty(); + type_args.push_back(token_type); + + let mut args:vector = vector::empty(); + args.push_back(utils::id_to_hex_string(&self.get_id())); + args.push_back(b"srcNid".to_string()); + args.push_back(b"conn_sn".to_string()); + args.push_back(b"msg".to_string()); + + Params { type_args, args } + + + } + @@ -407,22 +441,7 @@ module intents_v1::main { *self.orders.borrow(id) } - public fun get_type_arg(msg:vector):String{ - let msg= order_message::decode(&msg); - let bytes= if (msg.get_type() == FILL) { - let fill = order_fill::decode(&msg.get_message()); - fill.get_order_bytes() - }else if (msg.get_type() == CANCEL) { - let cancel = order_cancel::decode(&msg.get_message()); - cancel.get_order_bytes() - } else { - abort EInvalidMsgType - }; - - let order=swap_order::decode(&bytes); - return order.get_token() - - } + #[test_only] public fun test_init(ctx:&mut TxContext){ diff --git a/contracts/sui/intent_v1/sources/types/utils.move b/contracts/sui/intent_v1/sources/types/utils.move index 64722db4..fc015837 100644 --- a/contracts/sui/intent_v1/sources/types/utils.move +++ b/contracts/sui/intent_v1/sources/types/utils.move @@ -1,6 +1,7 @@ module intents_v1::utils { use std::string::{Self,String}; use sui::hex::{Self}; + use sui::bcs::{Self}; public fun id_to_hex_string(id:&ID): String { let bytes = object::id_to_bytes(id); @@ -22,4 +23,23 @@ module intents_v1::utils { }; sui_addr } + + public fun address_to_hex_string(address:&address): String { + let bytes = bcs::to_bytes(address); + let hex_bytes = hex::encode(bytes); + let mut prefix = string::utf8(b"0x"); + prefix.append(string::utf8(hex_bytes)); + prefix + } + + public fun address_from_hex_string(str: &String): address { + let mut modified_str = str; + if(string::length(str) == 66 ){ + modified_str = &str.substring(2, 66); + }; + let bytes = modified_str.bytes(); + let hex_bytes = hex::decode(*bytes); + bcs::peel_address(&mut bcs::new(hex_bytes)) + } + } \ No newline at end of file