router_wasm_bindings/
types.rs

1use crate::Bytes;
2use cosmwasm_std::{Coin, StdError, StdResult, Uint128};
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
7pub struct RequestMetaData {
8    pub dest_gas_limit: u64,
9    pub dest_gas_price: u64,
10    pub ack_gas_limit: u64,
11    pub ack_gas_price: u64,
12    pub relayer_fee: Uint128,
13    pub ack_type: AckType,
14    pub is_read_call: bool,
15    pub asm_address: String,
16}
17
18impl RequestMetaData {
19    pub fn get_abi_encoded_bytes(&self) -> Bytes {
20        let mut bytes: Bytes = vec![];
21        bytes.append(&mut self.dest_gas_limit.to_be_bytes().to_vec());
22        bytes.append(&mut self.dest_gas_price.to_be_bytes().to_vec());
23        bytes.append(&mut self.ack_gas_limit.to_be_bytes().to_vec());
24        bytes.append(&mut self.ack_gas_price.to_be_bytes().to_vec());
25        bytes.append(&mut self.relayer_fee.u128().to_be_bytes().to_vec());
26        bytes.append(&mut self.ack_type.get_ack_code().to_be_bytes().to_vec());
27        if self.is_read_call {
28            bytes.append(&mut vec![1]);
29        } else {
30            bytes.append(&mut vec![0]);
31        }
32        bytes.append(&mut self.asm_address.as_bytes().to_vec());
33        return bytes;
34    }
35
36    pub fn from_abi_encoded_bytes(bytes: Bytes) -> StdResult<Self> {
37        if bytes.len() < 50 {
38            return Err(StdError::generic_err(
39                "MetaData length should not be less than 50 bytes",
40            ));
41        }
42        let mut le_bytes: [u8; 8] = [0; 8];
43        le_bytes.copy_from_slice(&bytes[0..8]);
44        let dest_gas_limit: u64 = u64::from_be_bytes(le_bytes);
45
46        le_bytes.copy_from_slice(&bytes[8..16]);
47        let dest_gas_price: u64 = u64::from_be_bytes(le_bytes);
48
49        le_bytes.copy_from_slice(&bytes[16..24]);
50        let ack_gas_limit: u64 = u64::from_be_bytes(le_bytes);
51
52        le_bytes.copy_from_slice(&bytes[24..32]);
53        let ack_gas_price: u64 = u64::from_be_bytes(le_bytes);
54
55        let mut le_bytes: [u8; 16] = [0; 16];
56        le_bytes.copy_from_slice(&bytes[32..48]);
57        let relayer_fee: Uint128 = Uint128::from(u128::from_be_bytes(le_bytes));
58
59        let le_bytes: [u8; 1] = [bytes[48]];
60        let ack_type: AckType = AckType::get_ack_type(u8::from_be_bytes(le_bytes) as u64)?;
61
62        let is_read_call: bool = if bytes[49] == 0 { false } else { true };
63        let asm_bytes_vec: Bytes = bytes[50..bytes.len()].to_vec();
64        let asm_address: String = String::from_utf8(asm_bytes_vec)?;
65        Ok(Self {
66            dest_gas_limit,
67            dest_gas_price,
68            ack_gas_limit,
69            ack_gas_price,
70            relayer_fee,
71            ack_type,
72            is_read_call,
73            asm_address,
74        })
75    }
76}
77
78#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
79pub enum ChainType {
80    ChainTypeNone,
81    ChainTypeRouter,
82    ChainTypeEvm,
83    ChainTypeCosmos,
84    ChainTypePolkadot,
85    ChainTypeSolana,
86    ChainTypeNear,
87    ChainTypeTron,
88    ChainTypeStarknet,
89    ChainTypeBitcoin,
90    ChainTypeSui,
91    ChainTypeAlephZero,
92    ChainTypeCasper,
93    ChainTypeAptos,
94}
95
96impl ChainType {
97    pub fn get_chain_code(&self) -> u64 {
98        match self {
99            ChainType::ChainTypeNone => 0,
100            ChainType::ChainTypeRouter => 1,
101            ChainType::ChainTypeEvm => 2,
102            ChainType::ChainTypeCosmos => 3,
103            ChainType::ChainTypePolkadot => 4,
104            ChainType::ChainTypeSolana => 5,
105            ChainType::ChainTypeNear => 6,
106            ChainType::ChainTypeTron => 7,
107            ChainType::ChainTypeStarknet => 8,
108            ChainType::ChainTypeBitcoin => 9,
109            ChainType::ChainTypeSui => 10,
110            ChainType::ChainTypeAlephZero => 11,
111            ChainType::ChainTypeCasper => 12,
112            ChainType::ChainTypeAptos => 13,
113        }
114    }
115}
116
117#[derive(Serialize, Deserialize, Clone, PartialEq, JsonSchema, Debug)]
118pub enum AckType {
119    NoAck,
120    AckOnSuccess,
121    AckOnError,
122    AckOnBoth,
123}
124
125impl AckType {
126    pub fn get_ack_code(&self) -> u8 {
127        match self {
128            AckType::NoAck => 0,
129            AckType::AckOnSuccess => 1,
130            AckType::AckOnError => 2,
131            AckType::AckOnBoth => 3,
132        }
133    }
134
135    pub fn get_ack_type(ack_type: u64) -> StdResult<AckType> {
136        match ack_type {
137            0 => Ok(AckType::NoAck),
138            1 => Ok(AckType::AckOnSuccess),
139            2 => Ok(AckType::AckOnError),
140            3 => Ok(AckType::AckOnBoth),
141            _ => StdResult::Err(StdError::not_found("Invalid AckType")),
142        }
143    }
144}
145
146#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema)]
147#[serde(rename_all = "snake_case")]
148pub struct CrosschainRequestResponse {
149    pub request_identifier: u64,
150    pub fee_deducted: Coin,
151}
152
153pub const INBOUND_OUTBOUND_MAPPING_EVENT_NAME: &str = "inbound_outbound_mapping_event";
154
155pub const NATIVE_DENOM: &str = "route";
156
157pub const EVM_ADDRESS_LENGTH: usize = 20;
158
159#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Default)]
160#[serde(rename_all = "snake_case")]
161pub struct GasPriceResponse {
162    pub gas_price: u64,
163    pub decimals: u64,
164}
165
166#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, JsonSchema, Default)]
167#[serde(rename_all = "snake_case")]
168pub struct TokenPriceResponse {
169    pub token_price: Uint128,
170    pub token_decimal: u64,
171}