eth_sendTransaction 没有 exist/is 不可用
eth_sendTransaction does not exist/is not available
我目前正在使用 ERC721PresetMinterPauserAutoId for a smart contract and the Web3.js library in the Node.js backend server. When I try to call the mint function using this Web3 API:
var myContract = new web3.eth.Contract(ERC721PresetMinterPauserAutoIdABI, ERC721PresetMinterPauserAutoIdContractAddress, {
from: from,
gasPrice: gasPrice
});
let result;
try {
result = await myContract.methods.mint(receipientAddress).send();
res.status(201).send(result)
} catch (error) {
res.status(201).send(error)
}
我收到以下错误:
Returned error: The method eth_sendTransaction does not exist/is not available
我正在通过 Infura 网关与 Rinkeby 区块链通信,根据这个 post,Infura 仅支持 eth_sendRawTransaction
,不支持 eth_sendTransaction
。
我能够使用 signed transaction:
成功发送以太币
const gasPrice = await web3.eth.getGasPrice()
const txCount = await web3.eth.getTransactionCount(from, 'pending')
var rawTx = {
nonce: txCount,
gasPrice:"0x" + gasPrice,
gasLimit: '0x200000',
to: to,
value: "0x1000000000000000",
data: "0x",
chainId: 4
};
var privateKey = new Buffer.from(pk, "hex")
var tx = new Tx(rawTx, {"chain": "rinkeby"});
tx.sign(privateKey);
var serializedTx = tx.serialize();
const signedTx = await web3.eth.sendSignedTransaction("0x" + serializedTx.toString("hex"));
但是,我无法使用原始交易在智能合约上调用 mint
方法。我试过:
await myContract.methods.mint(receipientAddress).sendSignedTransaction("0x" + serializedTx.toString("hex"));
或
await myContract.methods.mint(receipientAddress).sendRawTransaction("0x" + serializedTx.toString("hex"));
但是,我仍然收到错误消息 eth_sendTransaction does not exist/is not available
。
更新
在@MikkoOhtamaa 的建议下,我尝试使用 Truffle 的库对交易进行签名:
const HDWalletProvider = require("@truffle/hdwallet-provider");
const privateKeys = process.env.PRIVATE_KEYS || ""
const walletAPIUrl = `https://rinkeby.infura.io/v3/${process.env.INFURA_API_KEY}`
const provider = new HDWalletProvider(
privateKeys.split(','),
walletAPIUrl
);
const web3 = new Web3API(provider)
安装一个 Web3.js 在本地签署交易的中间件层,而不是向 Infura 发送 JSON-RPC 方法。
其中一个解决方案是 Truffle HDWallet。
我目前正在使用 ERC721PresetMinterPauserAutoId for a smart contract and the Web3.js library in the Node.js backend server. When I try to call the mint function using this Web3 API:
var myContract = new web3.eth.Contract(ERC721PresetMinterPauserAutoIdABI, ERC721PresetMinterPauserAutoIdContractAddress, {
from: from,
gasPrice: gasPrice
});
let result;
try {
result = await myContract.methods.mint(receipientAddress).send();
res.status(201).send(result)
} catch (error) {
res.status(201).send(error)
}
我收到以下错误:
Returned error: The method eth_sendTransaction does not exist/is not available
我正在通过 Infura 网关与 Rinkeby 区块链通信,根据这个 post,Infura 仅支持 eth_sendRawTransaction
,不支持 eth_sendTransaction
。
我能够使用 signed transaction:
成功发送以太币 const gasPrice = await web3.eth.getGasPrice()
const txCount = await web3.eth.getTransactionCount(from, 'pending')
var rawTx = {
nonce: txCount,
gasPrice:"0x" + gasPrice,
gasLimit: '0x200000',
to: to,
value: "0x1000000000000000",
data: "0x",
chainId: 4
};
var privateKey = new Buffer.from(pk, "hex")
var tx = new Tx(rawTx, {"chain": "rinkeby"});
tx.sign(privateKey);
var serializedTx = tx.serialize();
const signedTx = await web3.eth.sendSignedTransaction("0x" + serializedTx.toString("hex"));
但是,我无法使用原始交易在智能合约上调用 mint
方法。我试过:
await myContract.methods.mint(receipientAddress).sendSignedTransaction("0x" + serializedTx.toString("hex"));
或
await myContract.methods.mint(receipientAddress).sendRawTransaction("0x" + serializedTx.toString("hex"));
但是,我仍然收到错误消息 eth_sendTransaction does not exist/is not available
。
更新
在@MikkoOhtamaa 的建议下,我尝试使用 Truffle 的库对交易进行签名:
const HDWalletProvider = require("@truffle/hdwallet-provider");
const privateKeys = process.env.PRIVATE_KEYS || ""
const walletAPIUrl = `https://rinkeby.infura.io/v3/${process.env.INFURA_API_KEY}`
const provider = new HDWalletProvider(
privateKeys.split(','),
walletAPIUrl
);
const web3 = new Web3API(provider)
安装一个 Web3.js 在本地签署交易的中间件层,而不是向 Infura 发送 JSON-RPC 方法。
其中一个解决方案是 Truffle HDWallet。