error: Invalid JSON RPC response: "" when trying to transfer testnet token

error: Invalid JSON RPC response: "" when trying to transfer testnet token

Manage alerts

Loading saved threads...

0xMukesh · External communityPost link
External question — Ethereum Stack Exchange Author: 0xMukesh Original post: https://ethereum.stackexchange.com/questions/127031 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. I am trying to transfer testnet tokens using Node.js, Web3.js, Ethers, and Alchemy but I am receiving Invalid JSON RPC response: "" whenever I deploy my code to Railway . Is this an issue of Railway or with my code? If with my code then what's the issue? import { Request, Response } from 'express'; import { ethers } from 'ethers'; import dotenv from 'dotenv'; import Web3 from 'web3'; import constants from '../data/constants'; import { chainId, txUrl, apiKeys, httpsUrls, amount, ethersSupportedNetworkNames, } from '../data/networks'; dotenv.config(); const privateKey = process.env.PRIVATE_KEY; const wallet = new ethers.Wallet(privateKey as string); const token = async (req: Request, res: Response) => { const XHR = require('xhr2-cookies').XMLHttpRequest; XHR.prototype._onHttpRequestError = function ( request: { abort: () => void }, error: any ) { if (this._request !== request) { return; } console.log(error, 'request'); this._setError(); request.abort(); this._setReadyState(XHR.DONE); this._dispatchProgress('error'); this._dispatchProgress('loadend'); }; const address = wallet.address; const networkApiKey = apiKeys.get(String(req.query.network)); const networkHttpsUrl = httpsUrls.get(String(req.query.network)); var web3 = new Web3( new Web3.providers.HttpProvider(networkHttpsUrl as string) ); const httpsProvider = new ethers.providers.AlchemyProvider( ethersSupportedNetworkNames.get(req.query.network as string), networkApiKey ); let nonce = await httpsProvider.getTransactionCount(address, 'latest'); let feeData = await httpsProvider.getFeeData(); const balance = web3.utils.fromWei( await web3.eth.getBalance(constants['fromAddress']), 'ether' ); if (web3.utils.isAddress(String(req.query.address!)) === false) { res.json({ error: 'Invalid receiver address', invalidAddress: true, }); } else { if (balance < amount?.get(req.query.network as string)!) { res.json({ error: 'Insufficient funds', insufficientFunds: true, }); } else { const tx = { type: 2, nonce: nonce, to: req.query.address, maxPriorityFeePerGas: feeData['maxPriorityFeePerGas'], maxFeePerGas: feeData['maxFeePerGas'], value: ethers.utils.parseEther( amount.get(req.query.network as string) as string ), gasLimit: 30000, chainId: chainId.get(String(req.query.network)), }; const signedTx = await wallet.signTransaction(tx as any); const txHash = ethers.utils.keccak256(signedTx); console.log('Precomputed txHash:', txHash); httpsProvider.sendTransaction(signedTx).then(console.log); res.json({ txLink: `${txUrl.get(String(req.query.network))}/${txHash}`, }); } } }; export default token; I tried the solution by the Infura community https://community.infura.io/t/invalid-json-rpc-response-error/1281 but still, it doesn't solve my issue. Here is the entire error: The code of other files which I have imported into the main file: data/constants.ts const constants = { fromAddress: '0x894b6579524c61b56B3aB5624740464EaDF5237E', }; export default constants; data/networks.ts import dotenv from 'dotenv'; dotenv.config(); const chainId = new Map([ ['mumbai', 80001], ['rinkeby', 4], ]); const txUrl = new Map([ ['mumbai', 'https://mumbai.polygonscan.com/tx'], ['rinkeby', 'https://rinkeby.etherscan.io/tx'], ]); const apiKeys = new Map([ ['mumbai', process.env.ALCHEMY_API_KEY_MUMBAI], ['rinkeby', process.env.ALCHEMY_API_KEY_RINKEBY], ]); const httpsUrls = new Map([ [ 'mumbai', 'https://polygon-mumbai.g.alchemy.com/v2/' + process.env.ALCHEMY_API_KEY_MUMBAI, ], [ 'rinkeby', 'https://eth-rinkeby.alchemyapi.io/v2/' + process.env.ALCHEMY_API_KEY_RINKEBY, ], ]); const ethersSupportedNetworkNames = new Map([ ['mumbai', 'maticmum'], ['rinkeby', 'rinkeby'], ]); const amount = new Map([ ['mumbai', '1'], ['rinkeby', '0.1'], ]); export { chainId, txUrl, apiKeys, httpsUrls, amount, ethersSupportedNetworkNames, };
Quote
Report
aakash4dev · External communityPost link
External answer — Ethereum Stack Exchange Author: aakash4dev Original post: https://ethereum.stackexchange.com/a/127283 License: CC BY-SA 4.0 — https://creativecommons.org/licenses/by-sa/4.0/ Adaptation: HTML converted to plain text; contact email addresses removed. put2 more await on your codes sir const balance = await web3.utils.fromWei( await web3.eth.getBalance(constants['fromAddress']), 'ether' ); const txHash = await ethers.utils.keccak256(signedTx); does it work now?
Quote
Report

Post Reply

Checking account access…