在 firebase 云函数中使用 web3 连接到 infura 失败

问题描述 投票:0回答:1

我对 Firebase 云功能有疑问。调用我的函数时,与 infura 的连接失败。如here所述,我已经升级到 Blaze 计划,但它没有改变。 这是我的代码:

const Web3 = require('web3')

exports.getInstructorOnBlock = functions.database.ref('/services/{serviceId}').onUpdate((snapshot, context) => {

    if (typeof web3 !== 'undefined') {
        web3 = new Web3(web3.currentProvider);
    } else {
        web3 = new Web3(new Web3.providers.HttpProvider("https://rinkeby.infura.io/MyKey"));
    }

    var abi             = [{"constant":true,"inputs":[{"name":"","type":"uint256"}],"name":"instructorAccts","outputs":[{"name":"","type":"address"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":false,"name":"fName","type":"string"},{"indexed":false,"name":"lName","type":"string"},{"indexed":false,"name":"age","type":"uint256"}],"name":"instructorInfo","type":"event"},{"constant":false,"inputs":[{"name":"_address","type":"address"},{"name":"_age","type":"uint256"},{"name":"_fName","type":"string"},{"name":"_lName","type":"string"}],"name":"setInstructor","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"getInstructors","outputs":[{"name":"","type":"address[]"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_address","type":"address"}],"name":"getInstructor","outputs":[{"name":"","type":"uint256"},{"name":"","type":"string"},{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"countInstructors","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"}];
    var address         = "address of the contract";
    var MyContract      = web3.eth.contract(abi);
    var SmartContract   = MyContract.at(address);

    web3.eth.defaultAccount = web3.eth.accounts[0];

    SmartContract.getInstructor(web3.eth.defaultAccount,"address", (err, res) => {
        console.log("+++ res +++ " + res);
       if (err) {
        console.log("getInstructor fail" + err);
       }
   });

   return 0
});

我也试过重新安装 npm 包。这是我的 package.json

{
  "name": "functions",
  "description": "Cloud Functions for Firebase",
  "scripts": {
    "lint": "eslint .",
    "serve": "firebase serve --only functions",
    "shell": "firebase functions:shell",
    "start": "npm run shell",
    "deploy": "firebase deploy --only functions",
    "logs": "firebase functions:log"
  },
  "dependencies": {
    "ansicolor": "^1.1.72",
    "axios": "^0.18.0",
    "dotenv": "^6.0.0",
    "eth": "^4.0.1",
    "ethereumjs-tx": "^1.3.6",
    "ethereumjs-util": "^5.2.0",
    "express": "^4.16.3",
    "firebase-admin": "^5.12.1",
    "firebase-functions": "^1.0.4",
    "npm": "^6.1.0",
    "ololog": "^1.1.103",
    "personal": "^1.0.1",
    "web3": "github:ethereum/web3.js"
  },
  "devDependencies": {
    "eslint": "^4.12.0",
    "eslint-plugin-promise": "^3.8.0"
  },
  "private": true
}

以及 firebase 返回的错误:

Error: CONNECTION ERROR: Couldn't connect to node https://rinkeby.infura.io/key.
at Object.InvalidConnection (/user_code/node_modules/web3/lib/web3/errors.js:31:16)
at HttpProvider.send (/user_code/node_modules/web3/lib/web3/httpprovider.js:93:18)
at RequestManager.send (/user_code/node_modules/web3/lib/web3/requestmanager.js:58:32)
at Eth.get [as accounts] (/user_code/node_modules/web3/lib/web3/property.js:107:62)
at exports.getInstructorOnBlock.functions.database.ref.onUpdate (/user_code/index.js:112:39)
at Object.<anonymous> (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:112:27)
at next (native)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:28:71
at __awaiter (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:24:12)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:82:36

有人知道我的问题吗?

请注意,我可以通过 HTML 页面上的相同 infura/rinkeby 访问我的智能合约“getInstructor”功能。

javascript firebase google-cloud-functions smartcontracts web3js
1个回答
0
投票

将 OP 的解决方案从评论迁移到答案:

您必须将定价计划更改为随用随付(“blaze”)。免费的“Spark”计划是有限的。
Gyo22019 年 5 月 1 日 11:49

© www.soinside.com 2019 - 2024. All rights reserved.