-
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.
Merge pull request #21 from pnetwork-association/pnetwork_router_meta…
…data_event
- Loading branch information
Showing
8 changed files
with
127 additions
and
4 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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,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); | ||
} | ||
} |
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
1 change: 1 addition & 0 deletions
1
common/ethereum/src/eth_contracts/test_utils/ptokens-router-metadata-event-1.json
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 @@ | ||
{"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"} |
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