From dcf62122dede374d7547309d2deff37e8515d7a5 Mon Sep 17 00:00:00 2001 From: Danil Kovtonyuk Date: Fri, 22 Oct 2021 00:45:02 +1000 Subject: [PATCH] fix: provider --- src/config/configuration.ts | 2 +- src/constants/variables.ts | 4 ++++ src/modules/api/api.service.ts | 8 ++++---- src/services/provider.service.ts | 20 ++++++++++++++------ 4 files changed, 23 insertions(+), 11 deletions(-) diff --git a/src/config/configuration.ts b/src/config/configuration.ts index 6b60f1b..123aec1 100644 --- a/src/config/configuration.ts +++ b/src/config/configuration.ts @@ -8,7 +8,7 @@ export const baseConfig = () => ({ base: { version, port: process.env.PORT, - chainId: process.env.CHAIN_ID, + chainId: Number(process.env.CHAIN_ID), serviceFee: { transfer: toWei(process.env.TRANSFER_SERVICE_FEE).toString(), withdrawal: Number(process.env.WITHDRAWAL_SERVICE_FEE), diff --git a/src/constants/variables.ts b/src/constants/variables.ts index e28cb33..b7322f4 100644 --- a/src/constants/variables.ts +++ b/src/constants/variables.ts @@ -3,18 +3,22 @@ import { ChainId } from '@/types'; const NETWORKS_INFO: { [chainId in ChainId] } = { [ChainId.MAINNET]: { + symbol: 'ETH', gasLimit: BigNumber.from(1500000), minimumBalance: '0.5', }, [ChainId.GOERLI]: { + symbol: 'gETH', gasLimit: BigNumber.from(1500000), minimumBalance: '0.5', }, [ChainId.OPTIMISM]: { + symbol: 'ETH', gasLimit: '', minimumBalance: '0.5', }, [ChainId.XDAI]: { + symbol: 'xDAI', gasLimit: BigNumber.from(2000000), minimumBalance: '0.5', }, diff --git a/src/modules/api/api.service.ts b/src/modules/api/api.service.ts index 14b5a7a..0848c11 100644 --- a/src/modules/api/api.service.ts +++ b/src/modules/api/api.service.ts @@ -3,9 +3,9 @@ import { v4 as uuid } from 'uuid'; import { InjectQueue } from '@nestjs/bull'; import { Injectable } from '@nestjs/common'; -import { jobStatus } from '@/constants'; import { ProviderService } from '@/services'; import { ConfigService } from '@nestjs/config'; +import { jobStatus, NETWORKS_INFO } from '@/constants'; import { Transaction } from '@/types'; @@ -25,9 +25,9 @@ class ApiService { return { health, version, + chainId, serviceFee, rewardAddress, - chainId: Number(chainId), }; } @@ -59,11 +59,11 @@ class ApiService { private async healthCheck(): Promise { const status = await this.providerService.checkSenderBalance(); - const minimumBalance = this.configService.get('base.minimumBalance'); + const { chainId, minimumBalance } = this.configService.get('base'); return { status, - error: status ? '' : `Not enough balance, less than ${minimumBalance} ETH`, + error: status ? '' : `Not enough balance, less than ${minimumBalance} ${NETWORKS_INFO[chainId].symbol}`, }; } } diff --git a/src/services/provider.service.ts b/src/services/provider.service.ts index 315e211..acabb77 100644 --- a/src/services/provider.service.ts +++ b/src/services/provider.service.ts @@ -9,23 +9,31 @@ import { TornadoPool__factory as TornadoPool, OffchainOracle__factory as Offchai @Injectable() export class ProviderService { private readonly chainId: number; - public provider: ethers.providers.JsonRpcProvider; + private readonly providers: Map = new Map(); constructor(private configService: ConfigService) { this.chainId = this.configService.get('base.chainId'); - this.provider = new ethers.providers.JsonRpcProvider(RPC_LIST[this.chainId]); } - getProviderWithSigner() { - return ethers.providers.getDefaultProvider(RPC_LIST[this.chainId]); + get provider() { + return this.getProvider(this.chainId); + } + + getProvider(chainId: ChainId) { + if (!this.providers.has(chainId)) { + this.providers.set(chainId, new ethers.providers.StaticJsonRpcProvider(RPC_LIST[chainId], chainId)); + } + + // eslint-disable-next-line @typescript-eslint/no-non-null-assertion + return this.providers.get(chainId)!; } getTornadoPool() { - return TornadoPool.connect(CONTRACT_NETWORKS[this.chainId], this.getProviderWithSigner()); + return TornadoPool.connect(CONTRACT_NETWORKS[this.chainId], this.provider); } getOffChainOracle() { - const provider = ethers.providers.getDefaultProvider(RPC_LIST[ChainId.MAINNET]); + const provider = this.getProvider(ChainId.MAINNET); return OffchainOracle.connect(OFF_CHAIN_ORACLE, provider); }