• 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.

  • 相关阅读:
    yzoj P2344 斯卡布罗集市 题解
    yzoj P2350 逃离洞穴 题解
    yzoj P2349 取数 题解
    JXOI 2017 颜色 题解
    NOIP 2009 最优贸易 题解
    CH 4302 Interval GCD 题解
    CH4301 Can you answer on these queries III 题解
    Luogu2533[AHOI2012]信号塔
    Luogu3320[SDOI2015]寻宝游戏
    Luogu3187[HNOI2007]最小矩形覆盖
  • 原文地址:https://www.cnblogs.com/chenkuang/p/16363681.html
Copyright © 2020-2023  润新知