Go-Ethereum
方法 eth_sendTransaction 不存在/在 infura 上不可用
我試圖在 infura 節點上使用 web3.js 將交易發送到簡單的歡迎契約。在這樣做時,我收到了這個錯誤。
throw errors.InvalidResponse(result); ^ Error: The method eth_sendTransaction does not exist/is not available
在網際網路上對這個錯誤進行了大量調查後,我得出結論 infura 不支持這種方法,我們必須離線簽署交易並將原始交易發送到節點。我已經在做同樣的事情了。但不斷收到此錯誤,請幫助我修復此錯誤。這是我的交易簽名程式碼:
const contractFunction = contract.greet({from:web3.eth.defaultAccount},"hello all devs"); const functionAbi = contractFunction.encodeABI(); let estimatedGas; let nonce; console.log("Getting gas estimate"); contractFunction.estimateGas({from: account}).then((gasAmount) => { estimatedGas = gasAmount.toString(16); console.log("Estimated gas: " + estimatedGas); web3.eth.getTransactionCount(account).then(_nonce => { nonce = _nonce.toString(16); console.log("Nonce: " + nonce); const txParams = { gasPrice: '0x09184e72a000', gasLimit: 3000000, to: contract_Address, data: functionAbi, from: account, nonce: '0x' + nonce }; const tx = new Tx(txParams); signed_tx = web3.eth.account.signTransaction(tx, privateKey) tx_hash= web3.eth.sendRawTransaction(signed_tx.rawTransaction) tx_receipt = web3.eth.waitForTransactionReceipt(tx_hash) //tx.sign(privateKey); console.log(tx_receipt)
伙計(這是關於同一件事的第三個問題),如果您對此有疑問,我已經更改了一些方法,請寫下來,不要打開新問題:)
這是新程式碼
var Tx = require('ethereumjs-tx'); const Web3 = require('web3'); const provider = new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/......"); const web3 = new Web3(provider); const account1 = '.......'; // Your account address 1 //const account2 = '' // Your account address 2 web3.eth.defaultAccount = account1; const privateKey1 = Buffer.from('.......', 'hex'); const abi = [{"constant":false,"inputs":[{"name":"_greeting","type":"string"}],"name":"greet","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getGreeting","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}]; const contract_Address = "0xcbe74e21b070a979b9d6426b11e876d4cb618daf"; const myContract = new web3.eth.Contract(abi, contract_Address); const myData = myContract.methods.greet( "hello blockchain devs").encodeABI(); web3.eth.getTransactionCount(account1, (err, txCount) => { // Build the transaction const txObject = { nonce: web3.utils.toHex(txCount), to: contract_Address, value: web3.utils.toHex(web3.utils.toWei('0', 'ether')), gasLimit: web3.utils.toHex(2100000), gasPrice: web3.utils.toHex(web3.utils.toWei('6', 'gwei')), data: myData } // Sign the transaction const tx = new Tx(txObject); tx.sign(privateKey1); const serializedTx = tx.serialize(); const raw = '0x' + serializedTx.toString('hex'); // Broadcast the transaction const transaction = web3.eth.sendSignedTransaction(raw, (err, tx) => { console.log(tx) }); });
這就是它有效的證明