首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >web3.eth.sendSignedTransaction不是一个函数

web3.eth.sendSignedTransaction不是一个函数
EN

Ethereum用户
提问于 2022-07-31 22:16:37
回答 1查看 418关注 0票数 2

我正在主动学习使用Web3js和Ganache来测试与区块链的交互。(所以请不要让我成为一个菜鸟)

在这里尝试#3示例时:https://www.dappuniversity.com/articles/web3-js-intro

我收到错误"TypeError: web3.eth.sendSignedTransaction不是一个函数“

当我控制台记录我的Web3元素时,sendSignedTransaction不在返回的JSON中。请帮帮忙?

代码语言:javascript
复制
var Tx = require("ethereumjs-tx").Transaction

const URL = "https://ropsten.infura.io/v3/{myInfuraID}" //Ropsten test network URL 

const Web3 = require('web3')

const web3 = new Web3(new Web3.providers.HttpProvider(URL)) 

console.log(web3) //sendSignedTransaction is not in response

const account1 = "{myGanacheAccount1}" //Ganache account 1
const account2 = "{myGanacheAccount2}" //Ganache account 2

const address1_keys = "{myGanacheAccountPrivateKey}"
const privateKey1 = Buffer.from(address1_keys, 'hex')

web3.eth.getTransactionCount(account1, (err, txCount) => {
    const txObject = {
        nonce: web3.toHex(txCount), //I also don't know why web3.utils.toHex(txCount) throws error
        to: account2,
        value: web3.toHex(web3.toWei('1', 'ether')),
        gasLimit: web3.toHex(21000),
        gasPrice: web3.toHex(web3.toWei('10', 'gwei'))
    }

    const tx = new Tx(txObject)
    tx.sign(privateKey1)

    const serializedTransaction = tx.serialize()
    const raw = '0x' + serializedTransaction.toString('hex')

    web3.eth.sendSignedTransaction(raw, (err, txHash) => { //throws error here
        console.log('txHash: ', txHash)
    })
})

错误:

代码语言:javascript
复制
TypeError: web3.eth.sendSignedTransaction is not a function
    at Object.callback (C:\Users\Me\Documents\NFTs\Web3_js\testing.js:48:14)
    at C:\Users\Me\node_modules\web3\lib\web3\method.js:142:25
    at C:\Users\Me\node_modules\web3\lib\web3\requestmanager.js:89:9
    at XMLHttpRequest.request.onreadystatechange (C:\Users\Me\node_modules\web3\lib\web3\httpprovider.js:129:7)
    at XMLHttpRequestEventTarget.dispatchEvent (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (C:\Users\Me\nFode_modules\xhr2-cookies\dist\xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpResponseEnd (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request.js:318:14)
    at IncomingMessage.<anonymous> (C:\Users\Me\node_modules\xhr2-cookies\dist\xml-http-request.js:289:61)
    at IncomingMessage.emit (node:events:532:35)
at endReadableNT (node:internal/streams/readable:1346:12)
EN

回答 1

Ethereum用户

发布于 2022-08-01 14:01:09

我认为新的事务对象--它没有完成。试试下面的代码☺

我用戈里罗普斯滕进行了测试(在这两个方面都做过工作)。从技术上讲,Ropsten和Rinkeby被认为不受欢迎是这样的,所以试着使用Goerli,至少您确信它是被维护的,并且按预期工作。

我使用了钱斯塔克的S端点,您可以免费获得这些端点。

另外,这段代码是从Chainstack的文档中,从节点API参考页中提取的。有许多RPC示例(也在web3.py中)。

代码语言:javascript
复制
var Web3 = require('web3');
var node_URL = 'CHAINSTACK_NODE_URL';
var web3 = new Web3(node_URL);
var Tx = require('ethereumjs-tx').Transaction;

// Logic of this code:
    // Set the addresses and private key to sign the transaction
    // Build transaction 
    // Sign and send the transaction 

// Addresses and private key
const sender = "SENDER_ADDRESS";
const receiver = "RECEIVER_ADDRESS";
const private_key = Buffer.from('PRIVATE_KEY', "hex"); 

// Build the transaction
web3.eth.getTransactionCount(sender, (err, transactionCount) => {
    const transaction_Object = {
        to: receiver,
        gasPrice: web3.utils.toHex(web3.utils.toWei("20", "gwei")),
        gasLimit: web3.utils.toHex(21000),
        nonce: web3.utils.toHex(transactionCount),
        value: web3.utils.toHex(web3.utils.toWei("0.5", "ether")),
    };

    // Signing the transaction 

    // create a new transaction object to sign 
    const tx = new Tx(transaction_Object, {
        chain: "ropsten"  // Adapt this to the network to use. 
    });

    // sign the transaction using the private key  
    tx.sign(private_key);

    //   Send signed transaction to the blockchain 
    const sTx = tx.serialize();
    const rawTransaction = "0x" + sTx.toString("hex");

    web3.eth.sendSignedTransaction(rawTransaction, (err, hash) => {
        console.log("TxHash:" + hash);
        console.log(err);
    });
})

希望这对你有帮助!

票数 4
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/132768

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档