Skip to content

Commit

Permalink
feat(ethereum): <- add PTokensRouterMetadataEvent with EthLog parser
Browse files Browse the repository at this point in the history
  • Loading branch information
ubordignon committed Feb 19, 2024
1 parent 0d731c6 commit 064cad8
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 0 deletions.
1 change: 1 addition & 0 deletions common/ethereum/src/eth_contracts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ mod erc20_token;
mod erc20_vault;
mod erc777_proxy;
mod erc777_token;
mod ptokens_router;
mod weth;

pub use self::{
Expand Down
63 changes: 63 additions & 0 deletions common/ethereum/src/eth_contracts/ptokens_router.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
use std::convert::TryFrom;

use common::{types::Bytes, AppError};
use common_metadata::{MetadataChainId, METADATA_CHAIN_ID_NUMBER_OF_BYTES};
use derive_more::Constructor;
use ethabi::{decode as eth_abi_decode, ParamType as EthAbiParamType, Token as EthAbiToken};

use crate::{EthLog, EthLogExt};

#[derive(Debug, Clone, Constructor, Eq, PartialEq, Default)]
pub struct PTokensRouterMetadataEvent {
pub user_data: Bytes,
pub origin_chain_id: MetadataChainId,
pub origin_address: String,
pub destination_chain_id: MetadataChainId,
pub destination_address: String,
}

impl TryFrom<&EthLog> for PTokensRouterMetadataEvent {
type Error = AppError;

fn try_from(log: &EthLog) -> std::result::Result<Self, Self::Error> {
info!("decoding `PTokensRouterMetadataEvent` from log...");

fn get_err_msg(field: &str) -> String {
format!("Error getting `{}` from `PTokensRouterMetadataEvent`!", field)
}

let tokens = eth_abi_decode(
&[
EthAbiParamType::Bytes,
EthAbiParamType::FixedBytes(METADATA_CHAIN_ID_NUMBER_OF_BYTES),
EthAbiParamType::String,
EthAbiParamType::FixedBytes(METADATA_CHAIN_ID_NUMBER_OF_BYTES),
EthAbiParamType::String,
],
&log.get_data(),
)?;

Ok(Self {
user_data: match tokens[0] {
EthAbiToken::Bytes(ref bytes) => Ok(bytes.to_vec()),
_ => Err(get_err_msg("user_data")),
}?,
origin_chain_id: match tokens[1] {
EthAbiToken::FixedBytes(ref bytes) => Ok(MetadataChainId::from_bytes(bytes)?),
_ => Err(get_err_msg("origin_chain_id")),
}?,
origin_address: match tokens[2] {
EthAbiToken::String(ref string) => Ok(string.to_string()),
_ => Err(get_err_msg("origin_address")),
}?,
destination_chain_id: match tokens[1] {
EthAbiToken::FixedBytes(ref bytes) => Ok(MetadataChainId::from_bytes(bytes)?),
_ => Err(get_err_msg("destination_chain_id")),
}?,
destination_address: match tokens[2] {
EthAbiToken::String(ref string) => Ok(string.to_string()),
_ => Err(get_err_msg("destination_address")),
}?,
})
}
}

0 comments on commit 064cad8

Please sign in to comment.