NodeJS的Lambda Web3模块初始化错误

时间:2018-12-09 12:15:01

标签: node.js lambda web3

我最近在创建具有稳定性的测试投票dapp时遇到问题, 在本地,一切都可以在Web3上正常运行,但是当我将其上传到Lambda时,它给了我这个错误:

  

模块初始化错误

我已经尝试过了,原因是:

  

const Web3 = require(“ web3”);

因为代码的其他部分都没有给我同样的错误。

有什么办法可以解决这个问题?

代码:

const Web3 = require("web3");
const path = require("path")
const cjson = require("cjson")
// contract details
const provider = "-"
const contractAddress = "-"
const privateKey = new Buffer.from("-", "hex")
const defaultAccount = "-"
const etherscanLink = "https://rinkeby.etherscan.io/tx/"
// initiate the web3
const web3 = new Web3(provider)

// functions
const voteForCandidate = (bytes32) => {
  const voted = getContract().methods.voteForCandidate(bytes32);
  return sendSignTransaction(voted);
}

const totalVotesFor = (bytes32) => {
  return new Promise((resolve, reject) => {
    const call = getContract().methods.totalVotesFor(bytes32).call().then((val) => {
      resolve(val);
    })
  });
}

const isValid = (bytes32) => {
  return new Promise((resolve, reject) => {
    const call = getContract().methods.validCandidate(bytes32).call().then((val) => {
      resolve(val);
    })
  });
}

// initiate the contract with null value
let contract = null;

// convert Wei to Eth
const convertWeiToEth = (stringValue) => {
  if (typeof stringValue != "string") {
    stringValue = String(stringValue);
  }
  return web3.utils.fromWei(stringValue, "ether");
}

// Initiate the Contract
const getContract = () => {
  if (contract === null) {
    const abi = cjson.load(path.resolve("abi.json"));
    let c = new web3.eth.Contract(abi, contractAddress)
    contract = c.clone();
    console.log("Contract Initiated successfully!")
  }
  return contract;
}

const sendSignTransaction = async (rawTrans) => {
  // Initiate values required by the dataTrans
  if (rawTrans) {
    let txCount = await web3.eth.getTransactionCount(defaultAccount) // needed for nonce
    let abiTrans = rawTrans.encodeABI() // encoded contract method 

    let gas = await rawTrans.estimateGas()
    let gasPrice = await web3.eth.getGasPrice()
    gasPrice = Number(gasPrice)
    gasPrice = gasPrice * 2
    let gasLimit = gas * 4
    // Initiate the transaction data
    let dataTrans = {
      nonce: web3.utils.toHex(txCount),
      gasLimit: web3.utils.toHex(gasLimit),
      gasPrice: web3.utils.toHex(gasPrice),
      to: contractAddress,
      data: abiTrans
    }

    // sign transaction
    let tx = new TX(dataTrans)
    tx.sign(privateKey)
    // after signing send the transaction
    return await sendSigned(tx)
  } else {
    throw new console.error("Encoded raw transaction was not given.");
  }

}

const sendSigned = async (tx) => {
  return new Promise(function(resolve, reject) {
    // send the signed transaction
    web3.eth.sendSignedTransaction("0x" + tx.serialize().toString("hex"))
      .once("transactionHash", function(hash) {
        let result = {
          "status": "sent",
          "url": etherscanLink + hash,
          "message": "click the given url to verify status of transaction"
        }
        // respond with the result
        resolve(result)
      })
      .then(out => {
        console.log(out)
      })
      .catch(err => {
        // respond with error
        reject(err)
      })
  })
}

module.exports = {
  vote: voteForCandidate,
  getVotes: totalVotesFor,
  valid: isValid,
  web3: web3
}

0 个答案:

没有答案