-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ethereum): <- add PTokensRouterMetadataEvent with EthLog parser
- Loading branch information
1 parent
0d731c6
commit 064cad8
Showing
2 changed files
with
64 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")), | ||
}?, | ||
}) | ||
} | ||
} |