发送原始交易 Ethereum infura nodejs npm

Ste*_*mer 5 node.js blockchain ethereum web3js

我目前正在尝试实现到我的 Typescript/Node 项目的以太坊节点连接。

我正在连接到我需要在本地签署我的交易的“Infura”节点服务器。好吧,无论如何。我正在使用 npm 包“ethereumjs-tx”签署我的交易,一切看起来都很棒。当我使用来自 web3 的“sendRawTransaction”时,我的响应是一个 tx-id,这意味着我的交易应该已经在区块链中了。嗯……不是

我的标志交易功能如下。

private signTransactionLocally(amountInWei: number, to: string, privateKey: string = <PRIVATE_KEY>, wallet: string = <MY_WALLET>) {
        const pKeyBuffer = Buffer.from(privateKey, "hex");

        const txParams = {
            nonce: this.getNonce(true,wallet),
            //gas: this.getGasPrice(true),
            gasLimit: this.getGasLimit2(true),
            to: to,
            value: amountInWei,
            data: '0x000000000000000000000000000000000000000000000000000000000000000000000000',
            chainId: "0x1"
        };

        // console.log(JSON.stringify(txParams));
        const tx = new this.ethereumTx(txParams);
        tx.sign(pKeyBuffer);
        return tx.serialize().toString("hex");

    }
Run Code Online (Sandbox Code Playgroud)

在“signTransactionLocally”中使用的函数:

    private getGasLimit2(hex: boolean = false) {
        const latestGasLimit = this.web3.eth.getBlock("latest").gasLimit;
        return hex ? this.toHex(latestGasLimit) : latestGasLimit;
    }
    
        private getNonce(hex:boolean = false, wallet: string = "0x60a22659E0939a061a7C9288265357f5d26Cf98a") {
        return hex ? this.toHex(this.eth().getTransactionCount(wallet)) : this.eth().getTransactionCount(wallet);
    }
Run Code Online (Sandbox Code Playgroud)

运行我的代码如下所示:

this.dumpInformations();
const signedTransaction = this.signTransactionLocally(this.toHex((this.getMaxAmountToSend(false, "0x60a22659E0939a061a7C9288265357f5d26Cf98a") / 3 )), "0x38bc48f1d19fdf7c8094a4e40334250ce1c1dc66" );
        console.log(signedTransaction);
        
this.web3.eth.sendRawTransaction("0x" + signedTransaction, function(err: any, res: any) {
            if (err)
                console.log(err);
            else
                console.log("transaction Done=>" + res);
        });
Run Code Online (Sandbox Code Playgroud)

由于 sendRawTransaction 结果在控制台日志中:[节点] 交易完成=>0xc1520ebfe0a225e6971e81953221c60ac1bfcd528e2cc17080b3f9b357003e34

一切都应该没问题。

有没有人有同样的问题?我希望有人可以帮助我。祝你今天过得愉快!

eha*_*noc 5

在处理这些问题无数次之后;我很确定你发送的随机数太高了。

在这些情况下,节点仍将返回一个交易哈希,但您的交易将保留在节点队列中,不会进入内存池或传播到其他节点,直到您填充随机数间隙。

您可以尝试以下操作:

  • 使用 getTransactionCount(address, 'pending') - 包含 int 节点队列和内存池的交易。但这种方法不可靠,并且无法处理并发请求,因为节点需要时间在任何给定时间评估正确的数量。

  • 保留自己的计数器,而不依赖节点(除非您检测到一些严重错误)。

  • 对于更严肃的项目,请在数据库级别保留您的计数器/每个地址,并使用锁来处理并发性,确保为每个请求提供正确的随机数。

干杯