Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Web3 doesnt work with Cloud Functions #3332

Closed
Gabriel1590 opened this issue Jan 23, 2020 · 1 comment
Closed

Web3 doesnt work with Cloud Functions #3332

Gabriel1590 opened this issue Jan 23, 2020 · 1 comment

Comments

@Gabriel1590
Copy link

I´m trying to use a Cloud Function to send a transaction everytime that is new data in the datastore, first I try to obtain the GasPrice but everytime I connect to infura with web3 in the function gives me this error:

Error: Invalid JSON RPC response: ""

this is my code:

import * as functions from 'firebase-functions';
import admin = require('firebase-admin');
const Web3 = require('web3');

export const transaction = functions.database
.ref('/transacciones/{transaccionesId}')
.onCreate(async (snapshot) => {
    
    const url = 'https://mainnet.infura.io/v3/INFURAAPIKEY'
    const web3 = new Web3(url);

    console.log(web3) 
/* Web3 {
  currentProvider: [Getter/Setter],
  _requestManager: 
   RequestManager {
     provider: 
      HttpProvider {
        host: 'https://mainnet.infura.io/v3/INFURAAPIKEY',
        httpsAgent: [Object],
        withCredentials: false,
        timeout: 0,
        headers: undefined,
        connected: false },
     providers: 
      { WebsocketProvider: [Function: WebsocketProvider],
        HttpProvider: [Function: HttpProvider],
        IpcProvider: [Function: IpcProvider] },
     subscriptions: {} },
  givenProvider: null,
  providers: 
   { WebsocketProvider: [Function: WebsocketProvider],
     HttpProvider: [Function: HttpProvider],
     IpcProvider: [Function: IpcProvider] },
  _provider: 
   HttpProvider {
     host: 'https://mainnet.infura.io/v3/INFURAAPIKEY',
     httpsAgent: 
      Agent {
        domain: [Object],
        _events: [Object],
        _eventsCount: 1,
        _maxListeners: undefined,
        defaultPort: 443,
        protocol: 'https:',
        options: [Object],
        requests: {},
        sockets: {},
        freeSockets: {},
        keepAliveMsecs: 1000,
        keepAlive: true,
        maxSockets: Infinity,
        maxFreeSockets: 256,
        maxCachedSessions: 100,
        _sessionCache: [Object] },
     withCredentials: false,
     timeout: 0,
     headers: undefined,
     connected: false },
  setProvider: [Function],
  BatchRequest: [Function: bound Batch],
  extend: 
   { [Function: ex]
     formatters: 
      { inputDefaultBlockNumberFormatter: [Function: inputDefaultBlockNumberFormatter],
        inputBlockNumberFormatter: [Function: inputBlockNumberFormatter],
        inputCallFormatter: [Function: inputCallFormatter],
        inputTransactionFormatter: [Function: inputTransactionFormatter],
        inputAddressFormatter: [Function: inputAddressFormatter],
        inputPostFormatter: [Function: inputPostFormatter],
        inputLogFormatter: [Function: inputLogFormatter],
        inputSignFormatter: [Function: inputSignFormatter],
        outputBigNumberFormatter: [Function: outputBigNumberFormatter],
        outputTransactionFormatter: [Function: outputTransactionFormatter],
        outputTransactionReceiptFormatter: [Function: outputTransactionReceiptFormatter],
        outputBlockFormatter: [Function: outputBlockFormatter],
        outputLogFormatter: [Function: outputLogFormatter],
        outputPostFormatter: [Function: outputPostFormatter],
        outputSyncingFormatter: [Function: outputSyncingFormatter] },
     utils: 
      { _fireError: [Function: _fireError],
        _jsonInterfaceMethodToString: [Function: _jsonInterfaceMethodToString],
        _flattenTypes: [Function: _flattenTypes],
        randomHex: [Function: randomHex],
        _: [Object],
        BN: [Object],
        isBN: [Function: isBN],
        isBigNumber: [Function: isBigNumber],
        isHex: [Function: isHex],
        isHexStrict: [Function: isHexStrict],
        sha3: [Object],
        keccak256: [Object],
        soliditySha3: [Function: soliditySha3],
        isAddress: [Function: isAddress],
        checkAddressChecksum: [Function: checkAddressChecksum],
        toChecksumAddress: [Function: toChecksumAddress],
        toHex: [Function: toHex],
        toBN: [Function: toBN],
        bytesToHex: [Function: bytesToHex],
        hexToBytes: [Function: hexToBytes],
        hexToNumberString: [Function: hexToNumberString],
        hexToNumber: [Function: hexToNumber],
        toDecimal: [Function: hexToNumber],
        numberToHex: [Function: numberToHex],
        fromDecimal: [Function: numberToHex],
        hexToUtf8: [Function: hexToUtf8],
        hexToString: [Function: hexToUtf8],
        toUtf8: [Function: hexToUtf8],
        utf8ToHex: [Function: utf8ToHex],
        stringToHex: [Function: utf8ToHex],
        fromUtf8: [Function: utf8ToHex],
        hexToAscii: [Function: hexToAscii],
        toAscii: [Function: hexToAscii],
        asciiToHex: [Function: asciiToHex],
        fromAscii: [Function: asciiToHex],
        unitMap: [Object],
        toWei: [Function: toWei],
        fromWei: [Function: fromWei],
        padLeft: [Function: leftPad],
        leftPad: [Function: leftPad],
        padRight: [Function: rightPad],
        rightPad: [Function: rightPad],
        toTwosComplement: [Function: toTwosComplement] },
     Method: [Function: Method] },
  version: '1.2.4',
  utils: 
   { _fireError: [Function: _fireError],
     _jsonInterfaceMethodToString: [Function: _jsonInterfaceMethodToString],
     _flattenTypes: [Function: _flattenTypes],
     randomHex: [Function: randomHex],
     _: 
      { [Function: _]
        _: [Circular],
        VERSION: '1.9.1',
        iteratee: [Function: builtinIteratee],
        forEach: [Function],
        each: [Function],
        collect: [Function],
        map: [Function],
        inject: [Function],
        foldl: [Function],
        reduce: [Function],
        foldr: [Function],
        reduce 
*/

    const gasPrice = await web3.eth.getGasPrice()

    console.log({gasPrice})
 /* Error: Invalid JSON RPC response: ""
    at Object.InvalidResponse (/srv/node_modules/web3-core-helpers/src/errors.js:42:16)
    at XMLHttpRequest.request.onreadystatechange (/srv/node_modules/web3-providers-http/src/index.js:98:32)
    at XMLHttpRequestEventTarget.dispatchEvent (/srv/node_modules/xhr2-cookies/dist/xml-http-request-event-target.js:34:22)
    at XMLHttpRequest._setReadyState (/srv/node_modules/xhr2-cookies/dist/xml-http-request.js:208:14)
    at XMLHttpRequest._onHttpRequestError (/srv/node_modules/xhr2-cookies/dist/xml-http-request.js:349:14)
    at ClientRequest.<anonymous> (/srv/node_modules/xhr2-cookies/dist/xml-http-request.js:252:61)
    at emitOne (events.js:116:13)
    at ClientRequest.emit (events.js:211:7)
    at TLSSocket.socketErrorListener (_http_client.js:401:9)
    at emitOne (events.js:116:13)
    at TLSSocket.emit (events.js:211:7)
    at emitErrorNT (internal/streams/destroy.js:73:8)
    at _combinedTickCallback (internal/process/next_tick.js:139:11)
    at process._tickDomainCallback (internal/process/next_tick.js:219:9)
*/

});

I have already updated my account to Blaze and it doesnt work either

Any solution?

@adambrooks107
Copy link

Did you end up solving the issue? I am having the same problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants