在调用Solidity合同时显示错误,该合同已在Ropsten-Infura中部署。我正在使用Web3(@0.19.1)来调用合同。
任何人都面临同一问题?
我猜想您直接连接到Infura,这不支持eth_sendTransaction
。(为了支持它,它需要了解您的私钥,但这是一个共享的公共节点。)
您需要亲自签署交易,然后通过eth_sendRawTransaction
发送或使用可以在浏览器中保存MetAmask之类的私钥的提供商。
这是智能合约功能执行的示例。众所周知,不可能在以太坊网络上进行交易而无需费用,因此您可以按合同帐户委派费用:
const rawTx =
{
nonce: _hex_nonce,
from: MainAccountAddress,
to: contractAddress,
gasPrice: _hex_gasPrice,
gasLimit: _hex_gasLimit,
gas: _hex_Gas,
value: '0x0',
data: contract.methods.transfer(toAddress, _hex_value).encodeABI()
};
const tx = new Tx(rawTx, { 'chain': 'ropsten' });
tx.sign(privateKey);
var serializedTx = '0x' + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(serializedTx.toString('hex'), function (err, hash) {
if (err) {
reject(err);
}
else {
resolve(hash);
}
})
以下是通过合同主帐户执行委托书的智能合约的完整来源:
async function TransferERC20Token(toAddress, value) {
return new Promise(function (resolve, reject) {
try {
web3.eth.getBlock("latest", false, (error, result) => {
var _gasLimit = result.gasLimit;
let contract = new web3.eth.Contract(contractABI, contractAddress);
contract.methods.decimals().call().then(function (result) {
try {
var decimals = result;
let amount = parseFloat(value) * Math.pow(10, decimals);
web3.eth.getGasPrice(function (error, result) {
var _gasPrice = result;
try {
const Tx = require('ethereumjs-tx').Transaction;
const privateKey = Buffer.from(MainAccountPrivateKey, 'hex')
var _hex_gasLimit = web3.utils.toHex((_gasLimit + 1000000).toString());
var _hex_gasPrice = web3.utils.toHex(_gasPrice.toString());
var _hex_value = web3.utils.toHex(amount.toString());
var _hex_Gas = web3.utils.toHex('60000');
web3.eth.getTransactionCount(MainAccountAddress).then(
nonce => {
var _hex_nonce = web3.utils.toHex(nonce);
const rawTx =
{
nonce: _hex_nonce,
from: MainAccountAddress,
to: contractAddress,
gasPrice: _hex_gasPrice,
gasLimit: _hex_gasLimit,
gas: _hex_Gas,
value: '0x0',
data: contract.methods.transfer(toAddress, _hex_value).encodeABI()
};
const tx = new Tx(rawTx, { 'chain': 'ropsten' });
tx.sign(privateKey);
var serializedTx = '0x' + tx.serialize().toString('hex');
web3.eth.sendSignedTransaction(serializedTx.toString('hex'), function (err, hash) {
if (err) {
reject(err);
}
else {
resolve(hash);
}
})
});
} catch (error) {
reject(error);
}
});
} catch (error) {
reject(error);
}
});
});
} catch (error) {
reject(error);
}
})
}
我通过将我的ganache cli指向Infura fork并使用脚本中的本地Ganache网络地址解决了这个问题。
已经由 @user94559指向,如果您直接连接到infura。
您需要在之前签署交易,这就是我使用Web3 1.0.0进行的方式。
我从metAmask使用了Web3-Provider-engine:
getWalletEthTxSubprovider() {
return new HookedWalletEthTxSubprovider({
getAccounts: callback => {
callback(null, [this.web3.eth.defaultAccount]);
},
getPrivateKey: (address, callback) => {
if (address.toLowerCase() === this.web3.eth.defaultAccount.toLowerCase()) {
return callback(
null,
Buffer.from(
this.web3.eth.accounts.wallet[address].privateKey.replace('0x', ''),
'hex'
)
);
}
return callback(new Error('not private key supplied for that account'));
}
});
}
您可以在此处看到完整的代码