官方文檔:
https://docs.infura.io/infura/tutorials/ethereum/call-a-contract
由於開發時,官方文檔未更新,所以用另外的方法來實現。
1.0 install @ethereumjs/tx
npm install @ethereumjs/tx
2.0 convect object to bytes
async function getContractRaw(txCount, data) {
var gasPrice = await web3.eth.getGasPrice();
curGasPrice = gasPrice;
var txParams = {
nonce: web3.utils.toHex(parseInt(txCount)),
gasLimit: web3.utils.toHex(60000*4),
gasPrice: web3.utils.toHex(gasPrice),
to: contractAddress,
value: '0x00',
data: data,
};
var tx = new EthereumTx.Transaction(txParams, {chain: network});
tx.sign(_privateKey)
var serializedTx = tx.serialize();
return '0x' + serializedTx.toString('hex');
}
Also, encrypt your private key in bytes.
3.0 call ERC721 funcs using sendSignedTransaction
var raw = await getContractRaw(txCount, data);
web3.eth.sendSignedTransaction(raw)
.on('transactionHash', function(_hash){})
.on('receipt', function(receipt){})
.on('confirmation', function(confirmationNumber, receipt){
res.jsonp({result: {transactionHash: hash, effectiveGasPrice: curGasPrice}});
})
.on('error', console.error);
sendSignedTransaction() allows you to call ERC721 funcs that requires gas fee.
浙公网安备 33010602011771号