Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(ethereum): <- add PTokenRouter Metadata event parser to that #21

Merged
merged 5 commits into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion common/ethereum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ license = "MIT"
publish = false
edition = "2021"
name = "ethereum"
version = "6.13.0"
version = "6.14.0"
readme = "README.md"
rust-version = "1.56"
keywords = ["defi", "crypto"]
Expand Down
2 changes: 1 addition & 1 deletion common/ethereum/src/eth_contracts/erc777_token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ impl Erc777RedeemEvent {
}?,
destination_chain_id: match tokens[4] {
EthAbiToken::FixedBytes(ref bytes) => Ok(Some(MetadataChainId::from_bytes(bytes)?)),
_ => Err(Self::get_err_msg("origin_chain_id")),
_ => Err(Self::get_err_msg("destination_chain_id")),
ubordignon marked this conversation as resolved.
Show resolved Hide resolved
}?,
})
})
Expand Down
2 changes: 2 additions & 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 Expand Up @@ -35,6 +36,7 @@ pub use self::{
ERC_777_REDEEM_EVENT_TOPIC_WITHOUT_USER_DATA,
ERC_777_REDEEM_EVENT_TOPIC_WITH_USER_DATA,
},
ptokens_router::PTokensRouterMetadataEvent,
weth::{ToWethDepositEvent, WethDepositEvent, WethDepositEvents},
};

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

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

use crate::{EthLog, EthLogExt, EthReceipt};

crate::make_topics!(
PTOKENS_ROUTER_METADATA_EVENT_TOPIC => "41954c3bf6e497b17fc12f429900878df830619bbcccb5f61aedc91e6ccc9e64",
);

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

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

fn try_from(log: &EthLog) -> 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[3] {
EthAbiToken::FixedBytes(ref bytes) => Ok(MetadataChainId::from_bytes(bytes)?),
_ => Err(get_err_msg("destination_chain_id")),
}?,
destination_address: match tokens[4] {
EthAbiToken::String(ref string) => Ok(string.to_string()),
_ => Err(get_err_msg("destination_address")),
}?,
})
}
}

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

fn try_from(receipt: &EthReceipt) -> Result<Self, Self::Error> {
info!("decoding `PTokensRouterMetadataEvent` from receipt...");

match receipt
.logs
.iter()
.find(|log| log.contains_topic(&PTOKENS_ROUTER_METADATA_EVENT_TOPIC))
{
Some(log) => Self::try_from(log),
None => Err("no matching log found".to_string())?,
}
}
}

#[cfg(test)]
mod tests {
use std::str::FromStr;

use super::*;
use crate::eth_contracts::test_utils::get_ptokens_router_metadata_event_receipt;

#[test]
fn should_decode_metadata_event_from_receipt() {
let expected_event = PTokensRouterMetadataEvent {
user_data: vec![192, 255, 238],
origin_chain_id: MetadataChainId::from_str("0x00f34368").unwrap(),
origin_address: "0xfEDFe2616EB3661CB8FEd2782F5F0cC91D59DCaC".to_string(),
destination_chain_id: MetadataChainId::from_str("0xffffffff").unwrap(),
destination_address: "0xedB86cd455ef3ca43f0e227e00469C3bDFA40628".to_string(),
};

let receipt = get_ptokens_router_metadata_event_receipt().unwrap();
let event = PTokensRouterMetadataEvent::try_from(&receipt).unwrap();

assert_eq!(expected_event, event);
}
}
11 changes: 10 additions & 1 deletion common/ethereum/src/eth_contracts/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{fs::read_to_string, path::Path, str::FromStr};

use common::{dictionaries::eth_evm::EthEvmTokenDictionary, types::Result};

use crate::{EthLog, EthPrivateKey, EthSubmissionMaterial};
use crate::{EthLog, EthPrivateKey, EthReceipt, EthSubmissionMaterial};

fn get_sample_submission_material_string_n(chain_type: &str, n: usize) -> Result<String> {
let path = format!(
Expand Down Expand Up @@ -54,6 +54,15 @@ pub fn get_sample_submission_material_with_weth_deposit() -> EthSubmissionMateri
EthSubmissionMaterial::from_str(&get_sample_submission_material_string_n("eth", 2).unwrap()).unwrap()
}

const PTOKENS_ROUTER_METADATA_EVENT_RECEIPT: &str = "src/eth_contracts/test_utils/ptokens-router-metadata-event-1.json";

pub fn get_ptokens_router_metadata_event_receipt() -> Result<EthReceipt> {
match Path::new(PTOKENS_ROUTER_METADATA_EVENT_RECEIPT).exists() {
true => EthReceipt::from_str(&read_to_string(PTOKENS_ROUTER_METADATA_EVENT_RECEIPT)?),
false => Err(format!("✘ Cannot find {} file!", PTOKENS_ROUTER_METADATA_EVENT_RECEIPT).into()),
}
ubordignon marked this conversation as resolved.
Show resolved Hide resolved
}

mod tests {
use super::*;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"to":"0x5eb3Bc0a489C5A8288765d2336659EbCA68FCd00","from":"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266","contractAddress":"0x0000000000000000000000000000000000000000","transactionIndex":0,"gasUsed":104467,"logsBloom":"0x0000000000000000000000000000000000000000000000000000000000008000000000010000000000000000000000000000000000000000000000000000004000000000000000000000000a000000000000000000000000000000001000000000000000020000000000040000000800000000480400000000000010000000000000000000000000000000000000000000000000000000000000000000000000000000000100000000200000000000000000000000000080000004000000000000000002002000000000000002400000000000000000000000002000000020480000100000400000000000020000040000000400000000000000000000000000","blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b","transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","logs":[{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x9d4454B023096f34B160D6B654540c56A1F81688","topics":["0x06b541ddaa720db2b10a4d0cdac39b8d360425fc073085fac19bc82614677987","0x0000000000000000000000005eb3bc0a489c5a8288765d2336659ebca68fcd00","0x0000000000000000000000005eb3bc0a489c5a8288765d2336659ebca68fcd00","0x0000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf"],"data":"0x0000000000000000000000000000000000000000000000000000000000000539000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000002c000000000000000000000000000000000000000000000000000000000000002400300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000010000f34368000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000140ffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001a0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000002200000000000000000000000000000000000000000000000000000000000000003c0ffee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30786645444665323631364542333636314342384645643237383246354630634339314435394443614300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30786564423836636434353565663363613433663065323237653030343639433362444641343036323800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","logIndex":0,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"},{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x9d4454B023096f34B160D6B654540c56A1F81688","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000005eb3bc0a489c5a8288765d2336659ebca68fcd00","0x0000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf"],"data":"0x0000000000000000000000000000000000000000000000000000000000000539","logIndex":1,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"},{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x8f86403A4DE0BB5791fa46B8e795C547942fE4Cf","topics":["0x41954c3bf6e497b17fc12f429900878df830619bbcccb5f61aedc91e6ccc9e64"],"data":"0x00000000000000000000000000000000000000000000000000000000000000a000f343680000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000e0ffffffff0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001400000000000000000000000000000000000000000000000000000000000000003c0ffee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30786645444665323631364542333636314342384645643237383246354630634339314435394443614300000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30786564423836636434353565663363613433663065323237653030343639433362444641343036323800000000000000000000000000000000000000000000","logIndex":2,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"},{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x9d4454B023096f34B160D6B654540c56A1F81688","topics":["0xa78a9be3a7b862d26933ad85fb11d80ef66b8f972d7cbba06621d583943a4098","0x0000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf","0x0000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf"],"data":"0x0000000000000000000000000000000000000000000000000000000000000539000000000000000000000000000000000000000000000000000000000000006000000000000000000000000000000000000000000000000000000000000000a00000000000000000000000000000000000000000000000000000000000000003c0ffee00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000","logIndex":3,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"},{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x9d4454B023096f34B160D6B654540c56A1F81688","topics":["0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef","0x0000000000000000000000008f86403a4de0bb5791fa46b8e795c547942fe4cf","0x0000000000000000000000000000000000000000000000000000000000000000"],"data":"0x0000000000000000000000000000000000000000000000000000000000000539","logIndex":4,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"},{"transactionIndex":0,"blockNumber":53,"transactionHash":"0x3a32d6740a23fece5ffbda75dd25939699e122b831c5c0ac0e7a446f672fe499","address":"0x9d4454B023096f34B160D6B654540c56A1F81688","topics":["0xf3107bfe806a84514d43c6ab063c9ef95dcbe72df4921dc64293124e996c5819"],"data":"0x0000000000000000000000000000000000000000000000000000000000000539000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000000c0ffffffff000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000003c0ffee0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002a30786564423836636434353565663363613433663065323237653030343639433362444641343036323800000000000000000000000000000000000000000000","logIndex":5,"blockHash":"0x7b1146e228063ccd403eae906bd15483784ba1f9cf56fc0c96af85b3d890417b"}],"blockNumber":53,"cumulativeGasUsed":104467,"status":true,"type":"0x2"}
1 change: 1 addition & 0 deletions common/ethereum/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ pub use self::{
Erc20VaultPegInEventParams,
Erc777BurnEvent,
Erc777RedeemEvent,
PTokensRouterMetadataEvent,
SupportedTopics,
ToErc20TokenTransferEvent,
ToWethDepositEvent,
Expand Down
Loading