其他分享
首页 > 其他分享> > infura調用需gasfee的ERC721合約功能

infura調用需gasfee的ERC721合約功能

作者:互联网

官方文檔:

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.

 

标签:function,tx,ERC721,gasfee,web3,調用,var,data,gasPrice
来源: https://www.cnblogs.com/chenkuang/p/16363681.html