From 54c35f54d482a006d9ec8027fa7cef2a9a45a00b Mon Sep 17 00:00:00 2001 From: YaroShkvorets Date: Thu, 23 Mar 2023 18:49:05 -0400 Subject: [PATCH] Fix types --- pomelo/src/abi.rs | 82 +++++++++++++++++++++++++-------------------- pomelo/src/sinks.rs | 4 +-- 2 files changed, 47 insertions(+), 39 deletions(-) diff --git a/pomelo/src/abi.rs b/pomelo/src/abi.rs index 7f9b378..4ba858a 100644 --- a/pomelo/src/abi.rs +++ b/pomelo/src/abi.rs @@ -1,13 +1,30 @@ -// use antelope::Asset; use serde::{Deserialize, Serialize}; -macro_rules! impl_from_str { +type Name = String; +type Double = String; +type Timestamp = String; +type Checksum = String; +type SymbolCode = String; +type Asset = String; +type Uint64 = u64; +type Uint16 = u16; + + +#[derive(Serialize, Deserialize, Debug)] +#[serde(deny_unknown_fields)] +pub struct ExtendedQuantity { + pub quantity: Name, + pub contract: Name, +} + + +macro_rules! impl_try_from_str { ($type:ty) => { - impl From<&str> for $type { + impl TryFrom<&str> for $type { + type Error = serde_json::Error; #[inline] - #[must_use] - fn from(str: &str) -> Self { - serde_json::from_str(str).unwrap() + fn try_from(str: &str) -> Result { + serde_json::from_str(str) } } }; @@ -16,43 +33,34 @@ macro_rules! impl_from_str { #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct GrantsRow { - pub id: String, - pub r#type: String, - pub author_user_id: String, - pub funding_account: String, - pub accepted_tokens: Vec, - pub status: String, - pub created_at: String, - pub updated_at: String, -} -impl_from_str!(GrantsRow); - - - -#[derive(Serialize, Deserialize, Debug)] -#[serde(deny_unknown_fields)] -pub struct ExtendedQuantity { - pub quantity: String, - pub contract: String, + pub id: Name, + pub r#type: Name, + pub author_user_id: Name, + pub funding_account: Name, + pub accepted_tokens: Vec, + pub status: Name, + pub created_at: Timestamp, + pub updated_at: Timestamp, } +impl_try_from_str!(GrantsRow); #[derive(Serialize, Deserialize, Debug)] #[serde(deny_unknown_fields)] pub struct TransfersRow { - pub transfer_id: u64, - pub from: String, - pub to: String, + pub transfer_id: Uint64, + pub from: Name, + pub to: Name, pub ext_quantity: ExtendedQuantity, - pub fee: String, + pub fee: Asset, pub memo: String, - pub user_id: String, - pub season_id: u16, - pub round_id: u16, - pub project_type: String, - pub project_id: String, - pub value: String, - pub trx_id: String, - pub created_at: String, + pub user_id: Name, + pub season_id: Uint16, + pub round_id: Uint16, + pub project_type: Name, + pub project_id: Name, + pub value: Double, + pub trx_id: Checksum, + pub created_at: Timestamp, } -impl_from_str!(TransfersRow); +impl_try_from_str!(TransfersRow); diff --git a/pomelo/src/sinks.rs b/pomelo/src/sinks.rs index 443adc6..0776b0d 100644 --- a/pomelo/src/sinks.rs +++ b/pomelo/src/sinks.rs @@ -16,7 +16,7 @@ pub fn entity_out( block: Block) -> Result { if db_op.code == "app.pomelo" { match db_op.table_name.as_str() { "grants" => { - let grant = abi::GrantsRow::from(db_op.new_data_json.as_str()); + let grant = abi::GrantsRow::try_from(db_op.new_data_json.as_str()).unwrap(); let op = from_dbop_to_entityop(&db_op.operation()); log::info!("Op: {:?}, Grant={:?}", op, grant); entity_changes.push_change("Grant", &grant.id, 1, op) @@ -29,7 +29,7 @@ pub fn entity_out( block: Block) -> Result { .change("trx_id", trx.id.clone()); } "transfers" => { - let transfer = abi::TransfersRow::from(db_op.new_data_json.as_str()); + let transfer = abi::TransfersRow::try_from(db_op.new_data_json.as_str()).unwrap(); let op = from_dbop_to_entityop(&db_op.operation()); let transfer_id = format!("{}-{}", transfer.round_id, transfer.transfer_id); log::info!("Op: {:?}, Transfer={:?}", op, transfer);