diff --git a/src/config.ts b/src/config.ts index 9a8b733..4ea12f5 100644 --- a/src/config.ts +++ b/src/config.ts @@ -1,13 +1,9 @@ import { jobType } from './types'; -import tornConfig from 'torn-token'; -import { EnsResolver } from './modules'; -import { getProvider } from './modules/contracts'; - +import tornConfig, { availableIds } from 'torn-token'; require('dotenv').config(); - -export const netId = Number(process.env.NET_ID) || 1; +export const netId = Number(process.env.NET_ID || 1); export const redisUrl = process.env.REDIS_URL || 'redis://127.0.0.1:6379'; export const httpRpcUrl = process.env.HTTP_RPC_URL; export const wsRpcUrl = process.env.WS_RPC_URL; @@ -21,7 +17,7 @@ export const instances = tornConfig.instances; export const torn = tornConfig; export const port = process.env.APP_PORT || 8000; export const tornadoServiceFee = Number(process.env.REGULAR_TORNADO_WITHDRAW_FEE); -export const miningServiceFee = Number(process.env.MINING_SERVICE_FEE); +// export const miningServiceFee = Number(process.env.MINING_SERVICE_FEE); export const rewardAccount = process.env.REWARD_ACCOUNT; export const governanceAddress = '0x5efda50f22d34F262c29268506C5Fa42cB56A1Ce'; export const tornadoGoerliProxy = '0x454d870a72e29d5E5697f635128D18077BD04C60'; @@ -33,19 +29,8 @@ export const gasLimits = { }; export const minimumBalance = '1000000000000000000'; export const baseFeeReserve = Number(process.env.BASE_FEE_RESERVE_PERCENTAGE); - -let tornadoProxyAddress: string; -let tornadoMiningAddress: string; - -export const initConfig = async () => { - const provider = getProvider(); - const resolver = new EnsResolver(provider); - if (netId === 5) { - tornadoProxyAddress = tornadoGoerliProxy; - } else { - tornadoProxyAddress = await resolver.resolve(torn.tornadoRouter.address); - } - tornadoMiningAddress = await resolver.resolve(torn.miningV2.address); +export const tornToken = { + tokenAddress: '0x77777FeDdddFfC19Ff86DB637967013e6C6A116C', + symbol: 'TORN', + decimals: 18, }; - -export { tornadoProxyAddress, tornadoMiningAddress }; diff --git a/src/services/ConfigService.ts b/src/services/ConfigService.ts index 80dda3d..02305d0 100644 --- a/src/services/ConfigService.ts +++ b/src/services/ConfigService.ts @@ -1,42 +1,85 @@ -import { netId, torn, tornadoGoerliProxy } from '../config'; +import { instances, netId, torn, tornadoGoerliProxy, tornToken } from '../config'; import { Token } from '../types'; -import { getProvider } from '../modules/contracts'; +import { getProvider, getTornadoProxyContract, getTornadoProxyLightContract } from '../modules/contracts'; import { EnsResolver } from '../modules'; +import { ProxyLightABI, TornadoProxyABI } from '../../contracts'; +import { availableIds, netIds } from '../../../torn-token'; +import { getAddress } from 'ethers/lib/utils'; + +const resolver = new EnsResolver(getProvider()); -const tornToken = { - tokenAddress: '0x77777FeDdddFfC19Ff86DB637967013e6C6A116C', - symbol: 'TORN', - decimals: 18, -}; export class ConfigService { - get proxyAddress(): string { - if (!this._proxyAddress) { - this.init().then(() => - this._proxyAddress); + static instance: ConfigService; + netId: availableIds; + netIdKey: netIds; + tokens: Token[]; + private _proxyAddress: string; + private _proxyContract: TornadoProxyABI | ProxyLightABI; + addressMap = new Map(); + isLightMode: boolean; + + constructor() { + this.netId = netId; + this.netIdKey = `netId${this.netId}`; + this.isLightMode = ![1, 5].includes(netId); + + for (const [currency, { instanceAddress, symbol, decimals }] of Object.entries(instances[this.netIdKey])) { + Object.entries(instanceAddress).forEach(([amount, address]) => + this.addressMap.set(getAddress(address), { + currency, + amount, + symbol, + decimals, + }), + ); } + } + + get proxyContract(): TornadoProxyABI | ProxyLightABI { + return this._proxyContract; + } + + get proxyAddress(): string { return this._proxyAddress; } - tokens: Token[]; - private _proxyAddress: string; - - constructor() { - this.tokens = [tornToken, ...Object.values(torn.instances.netId1)].map(el => ({ - address: el.tokenAddress, - ...el, - })).filter(e => e.address); + async init() { + if (this.isLightMode) { + this._proxyAddress = await resolver.resolve(torn.tornadoProxyLight.address); + this._proxyContract = getTornadoProxyLightContract(); + } else { + if (this.netIdKey === 'netId1') { + this._proxyAddress = await resolver.resolve(torn.tornadoRouter.address); + } else { + this._proxyAddress = tornadoGoerliProxy; + } + this._proxyContract = getTornadoProxyContract(); + this.tokens = [tornToken, ...Object.values(torn.instances['netId1'])] + .map(el => ({ + address: getAddress(el.tokenAddress), + ...el, + })).filter(e => e.address); + } } - async init() { - const provider = getProvider(); - const resolver = new EnsResolver(provider); - if (netId === 5) { - this._proxyAddress = tornadoGoerliProxy; - } else { - this._proxyAddress = await resolver.resolve(torn.tornadoRouter.address); + getInstance(address: string) { + return this.addressMap.get(getAddress(address)); + } + + public static getServiceInstance(): ConfigService { + if (!ConfigService.instance) { + ConfigService.instance = new ConfigService(); } + return ConfigService.instance; } } -export default new ConfigService(); +type InstanceProps = { + currency: string, + amount: string, + symbol: string, + decimals: number, +} + +export default ConfigService.getServiceInstance();