Skip to content

Commit

Permalink
fix: evm tx hash. (#266)
Browse files Browse the repository at this point in the history
  • Loading branch information
cowbeer authored Jun 26, 2024
1 parent 3e2caf9 commit 8dbfffa
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 4 deletions.
21 changes: 19 additions & 2 deletions explorer/src/service/v2/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::AppState;
use axum::extract::{Query, State};
use axum::Json;
use module::schema::TransactionResponse;
use scanner::types::FindoraEVMTxWrap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sqlx::Row;
Expand Down Expand Up @@ -80,9 +81,17 @@ pub async fn get_txs(
let result: Value = row.try_get("result").map_err(internal_error)?;
let value: Value = row.try_get("value").map_err(internal_error)?;

let evm_tx_hash = if ty == 1 {
let evm_tx: FindoraEVMTxWrap = serde_json::from_value(value.clone()).unwrap();
let hash = evm_tx.hash();
format!("{hash:?}")
} else {
"".to_string()
};

txs.push(TransactionResponse {
tx_hash,
evm_tx_hash: "".to_string(),
evm_tx_hash,
block_hash,
height,
timestamp,
Expand Down Expand Up @@ -134,9 +143,17 @@ pub async fn get_tx_by_hash(
let result: Value = row.try_get("result").map_err(internal_error)?;
let value: Value = row.try_get("value").map_err(internal_error)?;

let evm_tx_hash = if ty == 1 {
let evm_tx: FindoraEVMTxWrap = serde_json::from_value(value.clone()).unwrap();
let hash = evm_tx.hash();
format!("{hash:?}")
} else {
"".to_string()
};

let tx = TransactionResponse {
tx_hash,
evm_tx_hash: "".to_string(),
evm_tx_hash,
block_hash,
height,
timestamp,
Expand Down
2 changes: 1 addition & 1 deletion scanner/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ pub mod error;
pub mod rpc;
pub mod scanner;
pub mod tx;
mod types;
pub mod types;
mod util;

pub use error::{Error, Result};
Expand Down
26 changes: 25 additions & 1 deletion scanner/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use ethereum::{LegacyTransaction, TransactionAction, TransactionSignature};
use ethereum_types::U256;
use ethereum_types::{H256, U256};
use rlp::{Encodable, RlpStream};
use serde::{Deserialize, Serialize};
use serde_json::Value;
use sha3::{Digest, Keccak256};

#[allow(dead_code)]
pub enum FindoraTxType {
Expand Down Expand Up @@ -64,6 +65,29 @@ pub struct FindoraEVMTx {
pub struct FindoraEVMTxWrap {
pub function: EthereumWrap,
}

impl FindoraEVMTxWrap {
pub fn hash(&self) -> H256 {
let tx = FindoraEVMTx {
function: Ethereum {
ethereum: Transact {
transact: LegacyTransaction {
nonce: self.function.ethereum.transact.nonce,
gas_price: self.function.ethereum.transact.gas_price,
gas_limit: self.function.ethereum.transact.gas_limit,
action: self.function.ethereum.transact.action,
value: self.function.ethereum.transact.value,
input: self.function.ethereum.transact.input.clone(),
signature: self.function.ethereum.transact.signature.clone(),
},
},
},
};

H256::from_slice(Keccak256::digest(&rlp::encode(&tx)).as_slice())
}
}

impl Encodable for FindoraEVMTx {
fn rlp_append(&self, s: &mut RlpStream) {
self.function.ethereum.transact.rlp_append(s)
Expand Down

0 comments on commit 8dbfffa

Please sign in to comment.