调用 web3.js,连接 infura 节点进行合约转账

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了调用 web3.js,连接 infura 节点进行合约转账相关的知识,希望对你有一定的参考价值。

参考技术A 利用 web3,调用 erc 20 token 合约,连接 infura 节点,进行转账

批量转账合约,参考 这里 的实现,部分源码如下:

调用 token 合约,授权批量合约转账

调用批量转账合约,进行转账

发送原始交易 Ethereum infura nodejs npm

【中文标题】发送原始交易 Ethereum infura nodejs npm【英文标题】:Send Raw Transaction Ethereum infura nodejs npm 【发布时间】:2017-11-29 12:51:09 【问题描述】:

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

我正在连接到“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");

    

“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

一切都应该没问题。

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

【问题讨论】:

我也遇到过类似的问题,但在我的情况下,我了解到它仍然可以生成 Tx id,但如果 nonce 错误,它实际上不会处理。 【参考方案1】:

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

在这些情况下,节点仍然会返回一个交易哈希,但你的交易会留在节点队列中,不会进入内存池或传播到其他节点,UNTIL ,你填补随机数的空白。

您可以尝试以下方法:

使用 getTransactionCount(address, 'pending') - 包括 int nodes queue & memory pool 的 txs。但是这种方法不可靠,并且不会处理并发请求,因为节点需要时间来评估任何给定时间的正确数量。

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

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

干杯

【讨论】:

以上是关于调用 web3.js,连接 infura 节点进行合约转账的主要内容,如果未能解决你的问题,请参考以下文章

web3.js 中的连接超时

Web3.js查询以太币和代币余额以及转账

Web3.js查询以太币和代币余额以及转账

发送原始交易 Ethereum infura nodejs npm

如何将我的交易部署到 eth 网络?我将我的系统终端与 infura 主网一起使用,但它似乎不起作用

Solidity知识点集 — Keccak256与事件(二)