fix: env rpc

This commit is contained in:
Danil Kovtonyuk 2021-12-10 00:58:05 +10:00
parent c95249ca4c
commit 709f9d3944
10 changed files with 26 additions and 32 deletions

View File

@ -11,6 +11,10 @@ REDIS_URL=redis://redis/0
# REDIS_URL=localhost
CHAIN_ID=100
# RPC_URL=https://rpc.xdaichain.com/tornado
# ORACLE_RPC_URL should always point to the mainnet
# ORACLE_RPC_URL=https://mainnet.infura.io
REWARD_ADDRESS=

View File

@ -1,6 +1,9 @@
import { Wallet } from 'ethers';
import { ChainId } from '@/types';
import { toWei } from '@/utilities';
import { NETWORKS_INFO } from '@/constants';
import { NETWORKS_INFO, RPC_LIST } from '@/constants';
import { version } from '../../package.json';
@ -13,6 +16,8 @@ export const baseConfig = () => ({
transfer: toWei(process.env.TRANSFER_SERVICE_FEE).toString(),
withdrawal: Number(process.env.WITHDRAWAL_SERVICE_FEE),
},
rpcUrl: process.env.RPC_URL || RPC_LIST[process.env.CHAIN_ID],
oracleRpcUrl: process.env.ORACLE_RPC_URL || RPC_LIST[ChainId.MAINNET],
rewardAddress: process.env.REWARD_ADDRESS,
address: new Wallet(process.env.PRIVATE_KEY).address,
gasLimit: NETWORKS_INFO[process.env.CHAIN_ID].gasLimit,

View File

@ -3,7 +3,7 @@ import { RPC_LIST } from '@/constants';
export default registerAs('txManager', () => ({
privateKey: process.env.PRIVATE_KEY,
rpcUrl: RPC_LIST[process.env.CHAIN_ID],
rpcUrl: process.env.RPC_URL || RPC_LIST[process.env.CHAIN_ID],
config: {
THROW_ON_REVERT: false,
CONFIRMATIONS: process.env.CONFIRMATIONS,

View File

@ -6,9 +6,7 @@ export const CONTRACT_NETWORKS: { [chainId in ChainId]: string } = {
};
export const RPC_LIST: { [chainId in ChainId]: string } = {
[ChainId.MAINNET]: 'https://mainnet.infura.io/v3/eb6a84e726614079948e0b1efce5baa5',
[ChainId.GOERLI]: 'https://eth-goerli.alchemyapi.io/v2/hlSj0EqPUuLGyyTExs6UqFKnXDrc_eOh',
[ChainId.OPTIMISM]: 'https://optimism-kovan.infura.io/v3/8f786b96d16046b78e0287fa61c6fcf8',
[ChainId.MAINNET]: 'https://api.mycryptoapi.com/eth',
[ChainId.XDAI]: 'https://rpc.xdaichain.com/tornado',
};

View File

@ -2,21 +2,6 @@ import { BigNumber } from 'ethers';
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),

View File

@ -7,8 +7,8 @@ import { ConfigService } from '@nestjs/config';
import { InjectQueue, Process, Processor, OnQueueActive, OnQueueCompleted, OnQueueFailed } from '@nestjs/bull';
import { Transaction } from '@/types';
import { CONTRACT_ERRORS, jobStatus } from '@/constants';
import { getToIntegerMultiplier, toWei } from '@/utilities';
import { numbers, CONTRACT_ERRORS, jobStatus } from '@/constants';
import { GasPriceService, ProviderService, OffchainPriceService } from '@/services';
import txMangerConfig from '@/config/txManager.config';

View File

@ -5,7 +5,6 @@ import { BigNumber } from 'ethers';
import { GasPriceOracle } from 'gas-price-oracle';
import { toWei } from '@/utilities';
import { RPC_LIST } from '@/constants';
const bump = (gas: BigNumber, percent: number) => gas.mul(percent).div(100).toHexString();
const gweiToWei = (value: number) => toWei(String(value), 'gwei');
@ -20,15 +19,17 @@ const percentBump = {
@Injectable()
export class GasPriceService {
private readonly chainId: number;
private readonly rpcUrl: string;
constructor(private configService: ConfigService) {
this.chainId = this.configService.get<number>('base.chainId');
this.rpcUrl = this.configService.get('base.rpcUrl');
}
async getGasPrice() {
const instance = new GasPriceOracle({
chainId: this.chainId,
defaultRpc: RPC_LIST[this.chainId],
defaultRpc: this.rpcUrl,
});
const result = await instance.gasPrices();

View File

@ -11,9 +11,11 @@ import { toWei } from '@/utilities';
@Injectable()
export class OffchainPriceService {
private readonly chainId: number;
private readonly rpcUrl: string;
constructor(private configService: ConfigService, private providerService: ProviderService) {
this.chainId = ChainId.MAINNET;
this.rpcUrl = this.configService.get('base.oracleRpcUrl');
}
async getDaiEthPrice() {

View File

@ -3,25 +3,27 @@ import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { ChainId } from '@/types';
import { CONTRACT_NETWORKS, OFF_CHAIN_ORACLE, RPC_LIST } from '@/constants';
import { CONTRACT_NETWORKS, OFF_CHAIN_ORACLE } from '@/constants';
import { TornadoPool__factory as TornadoPool, OffchainOracle__factory as OffchainOracle } from '@/artifacts';
@Injectable()
export class ProviderService {
private readonly chainId: number;
private readonly rpcUrl: string;
private readonly providers: Map<ChainId, ethers.providers.StaticJsonRpcProvider> = new Map();
constructor(private configService: ConfigService) {
this.chainId = this.configService.get<number>('base.chainId');
this.rpcUrl = this.configService.get('base.rpcUrl');
}
get provider() {
return this.getProvider(this.chainId);
return this.getProvider(this.chainId, this.rpcUrl);
}
getProvider(chainId: ChainId) {
getProvider(chainId: ChainId, rpcUrl: string) {
if (!this.providers.has(chainId)) {
this.providers.set(chainId, new ethers.providers.StaticJsonRpcProvider(RPC_LIST[chainId], chainId));
this.providers.set(chainId, new ethers.providers.StaticJsonRpcProvider(rpcUrl, chainId));
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
@ -33,7 +35,8 @@ export class ProviderService {
}
getOffChainOracle() {
const provider = this.getProvider(ChainId.MAINNET);
const oracleRpcUrl = this.configService.get('base.oracleRpcUrl');
const provider = this.getProvider(ChainId.MAINNET, oracleRpcUrl);
return OffchainOracle.connect(OFF_CHAIN_ORACLE, provider);
}

View File

@ -2,14 +2,10 @@ import { BigNumberish } from 'ethers';
import { BytesLike } from '@ethersproject/bytes';
const MAINNET_CHAIN_ID = 1;
const GOERLI_CHAIN_ID = 5;
const OPTIMISM_CHAIN_ID = 69;
const XDAI_CHAIN_ID = 100;
export enum ChainId {
MAINNET = MAINNET_CHAIN_ID,
GOERLI = GOERLI_CHAIN_ID,
OPTIMISM = OPTIMISM_CHAIN_ID,
XDAI = XDAI_CHAIN_ID,
}