Skip to content

Commit

Permalink
sanitize address before insertion into the db
Browse files Browse the repository at this point in the history
  • Loading branch information
CRBl69 committed Oct 16, 2023
1 parent 0912208 commit bf0b141
Showing 1 changed file with 18 additions and 8 deletions.
26 changes: 18 additions & 8 deletions rust/processor/src/processors/econia_processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
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 {
Expand Down Expand Up @@ -328,8 +338,8 @@ fn event_data_to_fill_event(
event_idx: BigDecimal,
time: DateTime<Utc>,
) -> anyhow::Result<FillEvent> {
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"))?;
Expand All @@ -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"))?;
Expand Down Expand Up @@ -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"))?),
)
Expand All @@ -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"))?,
)
Expand Down Expand Up @@ -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"))?),
)
Expand All @@ -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"))?,
)
Expand Down Expand Up @@ -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,
})
Expand Down

0 comments on commit bf0b141

Please sign in to comment.