bguiz
bguiz

Reputation: 28587

How can I use a transaction hash from Hashscan in a JSON-RPC query, on Hedera?

I'm looking at a transaction on Hashscan

Its transaction hash is:
0x89eb7e219df6f8b3b3406c8a3698d5b484a4945059af643861c878e41ffc161b2589cc82ff40b4392ceb53856c5252c1 ... which does not work with the eth_getTransactionByHash RPC:

TXHASH=0x89eb7e219df6f8b3b3406c8a3698d5b484a4945059af643861c878e41ffc161b2589cc82ff40b4392ceb53856c5252c1

curl -s -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","id":"2","method":"eth_getTransactionByHash","params":["'"${TXHASH}"'"]}' \
    http://localhost:7546 | jq

The error is:

{
    "jsonrpc": "2.0",
    "id": "2",
    "error": {
        "code": -32602,
        "name": "Invalid parameter",
        "message": "[Request ID: 5fda4d4a-3160-4f8f-8ed3-51ad64dada46] Invalid parameter 0: Expected 0x prefixed string representing the hash (32 bytes) of a transaction, value: 0x89eb7e219df6f8b3b3406c8a3698d5b484a4945059af643861c878e41ffc161b2589cc82ff40b4392ceb53856c5252c1"
}

How can I get a transaction hash that can be used in RPCs?

Upvotes: 3

Views: 387

Answers (1)

Defi Girl
Defi Girl

Reputation: 336

  • In Hedera, transaction hashes are 48 bytes, which are represented as 96 hexits
    • 96 hexadecimal digits
    • 98 characters total length when represented as a string, due to the 0x prefix
  • In the EVM, transaction hashes are 32 bytes, which are represented as 64 hexits
    • 64 hexadecimal digits
    • 66 characters total length when represented as a string, due to the 0x prefix ​ You need to convert from the Hedera transaction hash format to the EVM transaction hash format, in order to use it in RPC endpoints. ​ To do so, simply truncate to the required length. You can do this in the shell with TXHASHEVM=${TXHASH:0:66}, and then using TXHASHEVM as the 1st parameter of the eth_getTransactionByHash RPC request. ​

Example ​ Request: ​

TXHASH=0x89eb7e219df6f8b3b3406c8a3698d5b484a4945059af643861c878e41ffc161b2589cc82ff40b4392ceb53856c5252c1
​
TXHASHEVM=${TXHASH:0:66}
​
curl -s -X POST \
    -H 'Content-Type: application/json' \
    -d '{"jsonrpc":"2.0","id":"2","method":"eth_getTransactionByHash","params":["'"${TXHASHEVM}"'"]}' \
    http://localhost:7546 | jq ".result"

​ Response: ​

{
  "blockHash": "0x84e4e82415f707eeb24fa9514500aaae55dbeb43d63a4e0e4bbd827ada9dcdd5",
  "blockNumber": "0x452953",
  "chainId": null,
  "from": "0x3633d26fb458e6723ac9f8c2d5659b04eb666283",
  "gas": "0xea60",
  "gasPrice": null,
  "hash": "0x89eb7e219df6f8b3b3406c8a3698d5b484a4945059af643861c878e41ffc161b",
  "input": "0x368b87720000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000b6e6577206d657373616765000000000000000000000000000000000000000000",
  "maxPriorityFeePerGas": null,
  "maxFeePerGas": null,
  "nonce": "0x0",
  "r": null,
  "s": null,
  "to": "0x0000000000000000000000000000000000469c13",
  "transactionIndex": "0x24",
  "type": null,
  "v": "0x0",
  "value": "0x0"
}

Upvotes: 5

Related Questions