Skip to content

Commit

Permalink
feat: receive message args
Browse files Browse the repository at this point in the history
  • Loading branch information
ibrizsabin committed Sep 19, 2024
1 parent 4d4bfbb commit 3bc2349
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 17 deletions.
53 changes: 36 additions & 17 deletions contracts/sui/intent_v1/sources/main.move
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -74,6 +74,11 @@ module intents_v1::main {
id:u128,
}

public struct Params has drop {
type_args: vector<String>,
args: vector<String>,
}

fun init(ctx: &mut TxContext) {
let admin = AdminCap {id: object::new(ctx)};
let storage = Storage {
Expand Down Expand Up @@ -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<u8>):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<String> = vector::empty();
type_args.push_back(token_type);

let mut args:vector<String> = 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 }


}




Expand Down Expand Up @@ -407,22 +441,7 @@ module intents_v1::main {
*self.orders.borrow<u128,SwapOrder>(id)
}

public fun get_type_arg(msg:vector<u8>):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){
Expand Down
20 changes: 20 additions & 0 deletions contracts/sui/intent_v1/sources/types/utils.move
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -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))
}

}

0 comments on commit 3bc2349

Please sign in to comment.