我正在尝试通过Infura节点使用web3在我的智能合约(Ropsten)上调用函数。我已经在Metmask中创建了一个测试帐户,并导出了帐户地址和私钥。详细信息看起来正确,但是出现错误{"code":-32000,"message":"invalid sender"}
。我猜这是交易签名的问题吗?
这是我的代码
const Web3 = require('web3');
const Tx = require('ethereumjs-tx').Transaction;
const fs = require('fs');
const pk = Buffer.from('PRIVATE KEY FROM METAMASK', 'hex')
const sourceAccount = 'ACCOUNT ADDRESS FROM METAMASK'
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/API_KEY"));
const consumerAddress = '0xc36577aa0171f649be6bd7205148ed83c07198ee';
web3.eth.defaultAccount = sourceAccount;
//Get Consumer contract instance
const consumerAbi = JSON.parse(fs.readFileSync('rental-contract-abi.json', 'utf8'));
let consumerContract = new web3.eth.Contract(consumerAbi, consumerAddress);
const myData = consumerContract.methods.checkDate("e716efba3b404da98e28faaa2939c0fd","2019-06-04","AU-NSW").encodeABI();
web3.eth.getTransactionCount(sourceAccount, (err, txCount) => {
// Build the transaction
const txObject = {
nonce: web3.utils.toHex(txCount),
to: consumerAddress,
from: sourceAccount,
chainId: 3,
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(pk);
const feeCost = tx.getUpfrontCost()
console.log('Total Amount of ETH needed:' + web3.utils.fromWei(feeCost.toString(), 'ether'))
console.log('---Serialized TX----')
console.log(tx.serialize().toString('hex'))
console.log('--------------------')
const serializedTx = tx.serialize();
const raw = '0x' + serializedTx.toString('hex');
// Broadcast the transaction
const transaction = web3.eth.sendSignedTransaction(raw, (err, tx) => {
console.log(tx);
console.log(err);
});
});
答案 0 :(得分:0)
很难调试,请您尝试以下代码(tx签名对我来说很好):
const Web3 = require('web3');
const fs = require('fs');
const web3 = new Web3(new Web3.providers.HttpProvider("https://ropsten.infura.io/v3/API_KEY"));
const consumerAbi = JSON.parse(fs.readFileSync('rental-contract-abi.json', 'utf8'));
let consumerContract = new web3.eth.Contract(consumerAbi, consumerAddress);
const myData = consumerContract.methods.checkDate("e716efba3b404da98e28faaa2939c0fd","2019-06-04","AU-NSW").encodeABI();
const from = 'ACCOUNT ADDRESS FROM METAMASK'
const privateKey = 'PRIVATE KEY FROM METAMASK'
const to = '0xc36577aa0171f649be6bd7205148ed83c07198ee'
const tx = {
from,
to,
value: '0',
gasPrice: web3.utils.toWei('6', 'gwei'),
gas: 2100000,
chainId: 3,
nonce: await web3.eth.getTransactionCount(from,'pending'),
data: myData
}
const signed = await web3.eth.accounts.signTransaction(tx, privateKey)
const rawTx = signed.rawTransaction
const sendRawTx = rawTx =>
new Promise((resolve, reject) =>
web3.eth
.sendSignedTransaction(rawTx)
.on('transactionHash', resolve)
.on('error', reject)
)
sendRawTx(rawTx).then(hash => console.log({hash}))
如果遇到错误,则可能是myData
变量出错了。尝试发送没有任何数据的TX,以确保
答案 1 :(得分:0)
您需要在签署交易时添加网络信息。请参阅最新的web3文档。将签名交易代码更改为:
connection.query(sql, function (error, results) {
if (error) throw error;
example(results);
});