Skip to content

Commit

Permalink
move u256_to_hex fn to common mod and minor changes
Browse files Browse the repository at this point in the history
  • Loading branch information
borngraced committed Jan 6, 2025
1 parent 2243c9d commit 1a8f243
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 22 deletions.
3 changes: 1 addition & 2 deletions mm2src/coins/eth/wallet_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::Eip1559Ops;
use crate::{BytesJson, MarketCoinOps, TransactionErr};

use common::log::info;
use common::u256_to_hex;
use derive_more::Display;
use enum_derives::EnumFromStringify;
use ethcore_transaction::{Action, SignedTransaction};
Expand Down Expand Up @@ -56,8 +57,6 @@ pub struct WcEthTxParams<'a> {

impl<'a> WcEthTxParams<'a> {
fn prepare_wc_tx_format(&self) -> MmResult<serde_json::Value, EthWalletConnectError> {
fn u256_to_hex(value: U256) -> String { format!("0x{:x}", value) }

let mut tx_json = json!({
"nonce": u256_to_hex(self.nonce),
"from": format!("{:x}", self.my_address),
Expand Down
31 changes: 16 additions & 15 deletions mm2src/coins/tendermint/wallet_connect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,21 +192,22 @@ where
let value = Value::deserialize(deserializer)?;

match value {
Value::Object(map) => {
let mut vec = Vec::new();
for i in 0..map.len() {
if let Some(Value::Number(num)) = map.get(&i.to_string()) {
if let Some(byte) = num.as_u64() {
vec.push(byte as u8);
} else {
return Err(serde::de::Error::custom("Invalid byte value"));
}
} else {
return Err(serde::de::Error::custom("Invalid format"));
}
}
Ok(vec)
},
Value::Object(map) => map
.iter()
.enumerate()
.map(|(_, (_, value))| {
value
.as_u64()
.ok_or_else(|| serde::de::Error::custom("Invalid byte value"))
.and_then(|n| {
if n <= 255 {
Ok(n as u8)
} else {
Err(serde::de::Error::custom("Invalid byte value"))
}
})
})
.collect(),
Value::Array(arr) => arr
.into_iter()
.map(|v| {
Expand Down
5 changes: 5 additions & 0 deletions mm2src/common/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ pub mod wio;

#[cfg(target_arch = "wasm32")] pub mod wasm;

use primitive_types::U256;
#[cfg(target_arch = "wasm32")] pub use wasm::*;

use backtrace::SymbolName;
Expand Down Expand Up @@ -1155,6 +1156,10 @@ pub fn http_uri_to_ws_address(uri: http::Uri) -> String {
format!("{}{}{}{}", address_prefix, host_address, port, path)
}

/// Converts a U256 value to a lowercase hexadecimal string with "0x" prefix
#[inline]
pub fn u256_to_hex(value: U256) -> String { format!("0x{:x}", value) }

/// If 0x prefix exists in an str strip it or return the str as-is
#[macro_export]
macro_rules! str_strip_0x {
Expand Down
11 changes: 6 additions & 5 deletions mm2src/kdf_walletconnect/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,11 @@ impl WalletConnectCtx {
let (conn_live_sender, conn_live_receiver) = unbounded();
let (client, _) = Client::new_with_callback(
Handler::new("Komodefi", inbound_message_tx, conn_live_sender),
|r, h| abortable_system.weak_spawner().spawn(client_event_loop(r, h)),
|receiver, handler| {
abortable_system
.weak_spawner()
.spawn(client_event_loop(receiver, handler))
},
);

let message_id_generator = MessageIdGenerator::new();
Expand Down Expand Up @@ -330,10 +334,7 @@ impl WalletConnectCtxImpl {
pairing_topics.push(pairing_topic);
}

let all_topics = valid_topics
.into_iter()
.chain(pairing_topics.into_iter())
.collect::<Vec<_>>();
let all_topics = valid_topics.into_iter().chain(pairing_topics).collect::<Vec<_>>();

if !all_topics.is_empty() {
self.client.batch_subscribe(all_topics).await?;
Expand Down

0 comments on commit 1a8f243

Please sign in to comment.