From bf0b141f8288ab94eb419631e2985d45bf3f8efe Mon Sep 17 00:00:00 2001 From: Bogdan Crisan Date: Mon, 16 Oct 2023 21:32:03 +0200 Subject: [PATCH] sanitize address before insertion into the db --- .../src/processors/econia_processor.rs | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/rust/processor/src/processors/econia_processor.rs b/rust/processor/src/processors/econia_processor.rs index 06517330b..4b46f941e 100644 --- a/rust/processor/src/processors/econia_processor.rs +++ b/rust/processor/src/processors/econia_processor.rs @@ -33,6 +33,16 @@ use std::{collections::HashMap, fmt::Debug, str::FromStr}; pub const NAME: &str = "econia_processor"; +pub fn strip_hex_number(hex: String) -> anyhow::Result { + let (start, end) = hex.split_at(2); + + if start != "0x" { + Err(anyhow!("Invalid hex provided.")) + } else { + Ok(format!("0x{}", end.trim_start_matches("0"))) + } +} + #[derive(Clone, Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] pub struct EconiaProcessorConfig { @@ -328,8 +338,8 @@ fn event_data_to_fill_event( event_idx: BigDecimal, time: DateTime, ) -> anyhow::Result { - let emit_address = event.account_address.to_string(); - let maker_address = opt_value_to_string(event.data.get("maker"))?; + let emit_address = strip_hex_number(event.account_address.to_string())?; + let maker_address = strip_hex_number(opt_value_to_string(event.data.get("maker"))?)?; let maker_custodian_id = opt_value_to_big_decimal(event.data.get("maker_custodian_id"))?; let maker_order_id = opt_value_to_big_decimal(event.data.get("maker_order_id"))?; let maker_side = opt_value_to_bool(event.data.get("maker_side"))?; @@ -338,7 +348,7 @@ fn event_data_to_fill_event( let sequence_number_for_trade = opt_value_to_big_decimal(event.data.get("sequence_number_for_trade"))?; let size = opt_value_to_big_decimal(event.data.get("size"))?; - let taker_address = opt_value_to_string(event.data.get("taker"))?; + let taker_address = strip_hex_number(opt_value_to_string(event.data.get("taker"))?)?; let taker_custodian_id = opt_value_to_big_decimal(event.data.get("taker_custodian_id"))?; let taker_order_id = opt_value_to_big_decimal(event.data.get("taker_order_id"))?; let taker_quote_fees_paid = opt_value_to_big_decimal(event.data.get("taker_quote_fees_paid"))?; @@ -401,7 +411,7 @@ fn event_data_to_recognized_market_event( if let Some(base_type) = type_data.get("base_type") { ( None, - Some(opt_value_to_string(base_type.get("account_address"))?), + Some(strip_hex_number(opt_value_to_string(base_type.get("account_address"))?)?), Some(opt_value_to_string(base_type.get("module_name"))?), Some(opt_value_to_string(base_type.get("struct_name"))?), ) @@ -424,7 +434,7 @@ fn event_data_to_recognized_market_event( let (quote_account_address, quote_module_name_hex, quote_struct_name_hex) = if let Some(quote_type) = type_data.get("quote_type") { ( - opt_value_to_string(quote_type.get("account_address"))?, + strip_hex_number(opt_value_to_string(quote_type.get("account_address"))?)?, opt_value_to_string(quote_type.get("module_name"))?, opt_value_to_string(quote_type.get("struct_name"))?, ) @@ -471,7 +481,7 @@ fn event_data_to_market_registration_event( if let Some(base_type) = event.data.get("base_type") { ( None, - Some(opt_value_to_string(base_type.get("account_address"))?), + Some(strip_hex_number(opt_value_to_string(base_type.get("account_address"))?)?), Some(opt_value_to_string(base_type.get("module_name"))?), Some(opt_value_to_string(base_type.get("struct_name"))?), ) @@ -494,7 +504,7 @@ fn event_data_to_market_registration_event( let (quote_account_address, quote_module_name_hex, quote_struct_name_hex) = if let Some(quote_type) = event.data.get("quote_type") { ( - opt_value_to_string(quote_type.get("account_address"))?, + strip_hex_number(opt_value_to_string(quote_type.get("account_address"))?)?, opt_value_to_string(quote_type.get("module_name"))?, opt_value_to_string(quote_type.get("struct_name"))?, ) @@ -782,7 +792,7 @@ impl ProcessorTrait for EconiaTransactionProcessor { .expect("Failed to parse MarketAccounts"); let map_field = data.get("map").expect("No map field"); market_account_handles.push(MarketAccountHandle { - user: resource.address.clone(), + user: strip_hex_number(resource.address.clone())?, handle: opt_value_to_string(map_field.get("handle"))?, creation_time: time, })