Kev*_*vvv 6 ethereum solidity web3js
我目前正在使用ERC721PresetMinterPauserAutoId作为智能合约,并在 Node.js 后端服务器中使用 Web3.js 库。当我尝试使用此Web3 API调用mint函数时:
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)
}
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
返回错误:方法 eth_sendTransaction 不存在/不可用
我通过 Infura 网关与 Rinkeby 区块链进行通信,根据这篇文章,Infura 仅支持eth_sendRawTransaction,不支持eth_sendTransaction。
我能够使用签名交易成功发送以太币:
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"));
Run Code Online (Sandbox Code Playgroud)
但是,我无法使用mint原始交易调用智能合约上的方法。我试过了:
await myContract.methods.mint(receipientAddress).sendSignedTransaction("0x" + serializedTx.toString("hex"));
Run Code Online (Sandbox Code Playgroud)
或者
await myContract.methods.mint(receipientAddress).sendRawTransaction("0x" + serializedTx.toString("hex"));
Run Code Online (Sandbox Code Playgroud)
但是,我仍然收到错误消息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)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3615 次 |
| 最近记录: |