-2

I am trying to extract a hash value from a function output:

let receipt = do_something();
println!("{receipt:?}");
let receipt_txn_hash = receipt.transaction_hash;
println!("receipt_txn_hash: {}", receipt_txn_hash);

let tx = provider.get_transaction(receipt_txn_hash).await?;
println!("tx: {}\n", serde_json::to_string(&tx)?);

let tx_hash = if let Some(txn) = tx {
    txn.hash.to_string()
} else {
    "hash_not_found".to_owned() //H256([0; 32])
};
println!("tx.hash: {}\n", &tx_hash);

And it prints out in the terminal:

TransactionReceipt { transaction_hash: 0xd6a0e48e6a0f80ae4467193f40721da1ad53ec854a738ea57d7201619e60f3b7, ... }
receipt_txn_hash: 0xd6a0…f3b7

tx: {"hash":"0xd6a0e48e6a0f80ae4467193f40721da1ad53ec854a738ea57d7201619e60f3b7",...}
tx.hash: 0xd6a0…f3b7

Somehow Rust-Analyzer identifies this receipt has {unknown} type, but tx has Option<Transaction> type.

But still, both the receipt.transaction_hash, tx.hash have been truncated... *why?

How can I get the original complete hash value and return it as a String?

Here are the dependencies:

ethers = { version = "1.0.2", features = ["legacy", "rustls"] }
ethers-solc = { version = "1.0.2", features = ["full"] }
ethers-providers = "1.0.2"
eyre = "0.6.8"
hex = "0.4.3"
reqwest = { version = "0.11.14", default-features = false }
serde_json = "1.0.93"

[Update] Thanks to the answer below, both receipt_txn_hash and tx_hash are of the type H256.

The Display trait of H256 type is defined here: https://github.com/paritytech/parity-common/blob/223af1dc6c176e35698aed9285f44e428da0050e/fixed-hash/src/hash.rs#L217

        impl $crate::core_::fmt::Display for $name {
            fn fmt(&self, f: &mut $crate::core_::fmt::Formatter) -> $crate::core_::fmt::Result {
                $crate::core_::write!(f, "0x")?;
                for i in &self.0[0..2] {
                    $crate::core_::write!(f, "{:02x}", i)?;
                }
                $crate::core_::write!(f, "…")?;
                for i in &self.0[$n_bytes - 2..$n_bytes] {
                    $crate::core_::write!(f, "{:02x}", i)?;
                }
                Ok(())
            }
        }

From the Display trait definition above, we know Display trait is causing the truncation.

So we need to modify the Display trait(defined in our dependencies).

But we cannot modify or override the foreign trait definition due to: only traits defined in the current crate can be implemented for types defined outside of the crate. Define and implement a trait or new type instead

So we have to make our local type to modify the foreign trait!

Hence, this question is basically asking how to implement a local type(NewH256), on a foreign trait(fmt::Display)?

Russo
  • 2,186
  • 2
  • 26
  • 42
  • What are the packages involved? What are the types involved? – Masklinn Feb 15 '23 at 11:07
  • @Masklinn Please have a look at my dependencies above – Russo Feb 15 '23 at 11:14
  • Please provide a [MRE], with a `main` and `use` statements that can simply be copied, pasted and reproduces the exact output you claim it does. – Finomnis Feb 15 '23 at 15:27
  • The `do_something` function does not exist; the `provider` variable also does not exist. Both are crucial to reproduce your problem. – Finomnis Feb 15 '23 at 15:29

1 Answers1

1

Going by a quick search I'm guessing the type is a H256 from here. This has a Display implementation here which gives you your ellipses.

If you want to show the full hex, you might be best just printing the debug output:

println!("receipt_txn_hash: {:?}", receipt_txn_hash);

If that doesn't work, you can just copy the Display impl and format it however you want.

Michael
  • 507
  • 4
  • 11
  • Solved by https://stackoverflow.com/questions/25413201/how-do-i-implement-a-trait-i-dont-own-for-a-type-i-dont-own – Russo Feb 16 '23 at 07:32