简体   繁体   English

使用 web3js 或 nodejs 中类似的东西从事务 Id 获取原始事务

[英]Get raw transaction from transaction Id using web3js or something similar in nodejs

So i will give an example let's say i have the transaction id https://etherscan.io/tx/0xafef64d0d03db9f13c6c3f8aec5902167ea680bd0ffa0268d89a426d624b2ae1所以我举个例子,假设我有交易 ID https://etherscan.io/tx/0xafef64d0d03db9f13c6c3f8aec5902167ea680bd0ffa0268d89a426d624b2ae1

In etherscan i can use their menu to see the raw transaction which will be在 etherscan 中,我可以使用他们的菜单来查看原始交易

0xf8a915850516aab3ad82be7c947d1afa7b718fb893db30a3abc0cfc608aacfebb080b844095ea7b300000000000000000000000022b1cbb8d98a01a3b71d034bb899775a76eb1cc2ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff26a003daa35e6aa4a7cd439133411a390ff0796420d5cb39e9e276db75b01218ed41a028605f36c601527bd435b36da5403ee972fc2fb2c8f70959199bcdba0d0e8c77

I can't get this with etherscan api or geth rpc, i have found eth_getRawTransaction but it give just 0x for transfer transaction is there anyway i can find the raw transaction using nodejs code我无法通过 etherscan api 或 geth rpc 获得此信息,我找到了 eth_getRawTransaction 但它只为传输交易提供 0x 无论如何我可以使用 nodejs 代码找到原始交易

thank you谢谢你

I think i need to rlp encode the transaction or something like that, i'm lost我想我需要对交易进行 rlp 编码或类似的东西,我迷路了

A raw transaction is an object.原始交易是一个对象。 This hex string represents a serialized transaction object.这个十六进制字符串表示一个序列化的交易对象。

Example code below.下面的示例代码。

Note that ethers requires you to parse out the signature and other params of an already mined/validated transaction, and pass the signature as a second argument.请注意, ethers要求您解析出已经挖掘/验证的交易的签名和其他参数,并将签名作为第二个参数传递。 There's probably a more generic way of doing that, I couldn't think of any right now.可能有一种更通用的方法可以做到这一点,我现在想不出任何方法。

const tx = await provider.getTransaction("0xafef64d0d03db9f13c6c3f8aec5902167ea680bd0ffa0268d89a426d624b2ae1");

const unsignedTx = {
    to: tx.to,
    nonce: tx.nonce,
    gasLimit: tx.gasLimit,
    gasPrice: tx.gasPrice,
    data: tx.data,
    value: tx.value,
    chainId: tx.chainId
};
const signature = {
    v: tx.v,
    r: tx.r,
    s: tx.s
}

const serialized = ethers.utils.serializeTransaction(unsignedTx, signature);
console.log(serialized);

Docs:文档:

Thank you for your answers for the ones looking if you are hosting your own geth node, it doesn't index all the transactions感谢您对那些寻找您是否托管自己的 geth 节点的人的回答,它不会索引所有交易

you need to run geth with --txlookuplimit 0您需要使用 --txlookuplimit 0 运行 geth

then the rpc call eth_getRawTransactionByHash will do the job my mistake然后 rpc 调用 eth_getRawTransactionByHash 将完成我的错误

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM