我正在主动学习使用Web3js和Ganache来测试与区块链的交互。(所以请不要让我成为一个菜鸟)
在这里尝试#3示例时:https://www.dappuniversity.com/articles/web3-js-intro
我收到错误"TypeError: web3.eth.sendSignedTransaction不是一个函数“
当我控制台记录我的Web3元素时,sendSignedTransaction不在返回的JSON中。请帮帮忙?
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)
})
})错误:
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)发布于 2022-08-01 14:01:09
我认为新的事务对象--它没有完成。试试下面的代码☺
我用戈里和罗普斯滕进行了测试(在这两个方面都做过工作)。从技术上讲,Ropsten和Rinkeby被认为不受欢迎是这样的,所以试着使用Goerli,至少您确信它是被维护的,并且按预期工作。
我使用了钱斯塔克的S端点,您可以免费获得这些端点。
另外,这段代码是从Chainstack的文档中,从节点API参考页中提取的。有许多RPC示例(也在web3.py中)。
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);
});
})希望这对你有帮助!
https://ethereum.stackexchange.com/questions/132768
复制相似问题