发送原始交易以太坊infura nodejs npm [英] Send Raw Transaction Ethereum infura nodejs npm

查看:65
本文介绍了发送原始交易以太坊infura nodejs npm的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm currently trying to implement an ethereum Node Connection to my Typescript/ Node Project.

我连接到"Infura"节点服务器,需要在本地对我的交易进行签名. 好吧,反正.我正在使用npm软件包"ethereumjs-tx"签署我的交易,一切看起来都很好. 当我使用来自web3的"sendRawTransaction"时,我的响应是一个tx-id,这意味着我的交易应该已经在区块链中准备好了.嗯...不是

I'm connection to the "Infura" node server where i need to sign my transaction locally. Well, anyway. I'm signing my transaction using the npm package "ethereumjs-tx" and everything looks great. When i'm using "sendRawTransaction" from web3 my response is an tx-id which means my transaction should be allready in the Blockchain. Well... it isn't

我的签名交易功能如下.

My sign Transaction Function is below.

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");

    }

"signTransactionLocally"中使用的函数:

Used Functions in "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);
    }

运行我的代码如下:

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);
        });

因为sendRawTransaction会在控制台日志中显示: [节点]交易完成=> 0xc1520ebfe0a225e6971e81953221c60ac1bfcd528e2cc17080b3f9b357003e34

since sendRawTransaction results in console log: [Node] transaction Done=>0xc1520ebfe0a225e6971e81953221c60ac1bfcd528e2cc17080b3f9b357003e34

一切都应该没事.

有人遇到过同样的问题吗? 我希望有人能帮助我.祝你有美好的一天!

Has anybody had the same problem? i Hope that someone could help me. Have a nice day!

推荐答案

处理了无数次这些问题之后;我很确定您发送的 nonce 太高了.

After dealing with these issues countless times; i'm pretty sure you are sending a nonce too high.

在这种情况下,该节点仍将向您返回交易哈希,但您的交易将保留在节点队列中,并且不会进入内存池或传播到其他节点, UNTIL ,您可以填补现时的空白.

In these cases, the node will still return you a transaction hash, but your transaction will remain in the nodes queue and not enter the memory pool or be propagated to other nodes, UNTIL, you fill in the nonce gaps.

您可以尝试以下方法:

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

  • Use getTransactionCount(address, 'pending') - to include the txs that are int nodes queue & memory pool . But this method unreliable and won't deal with concurrent requests as the node needs time to assess the correct amount at any given time.

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

Keep your own counter, without relying on the node (unless you detect some serious errors).

对于更严重的项目,请使用锁来将计数器/每个地址保留在数据库级别,以处理并发,确保为每个请求给出正确的随机数.

For more serious projects, persist your counter / per address at the level of the database with locks to handle concurrency, making sure you give out correct nonces to each request.

欢呼

这篇关于发送原始交易以太坊infura nodejs npm的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆