diff --git a/src/clients/BalanceAllocator.ts b/src/clients/BalanceAllocator.ts index 50200bdd3..d86bf1c27 100644 --- a/src/clients/BalanceAllocator.ts +++ b/src/clients/BalanceAllocator.ts @@ -1,5 +1,4 @@ -import { CONTRACT_ADDRESSES } from "../common/ContractAddresses"; -import { BigNumber, ERC20, ethers, ZERO_ADDRESS, min } from "../utils"; +import { BigNumber, ERC20, ethers, min, getEthAddressForChain } from "../utils"; // This type is used to map used and current balances of different users. export interface BalanceMap { @@ -142,15 +141,9 @@ export class BalanceAllocator { this.balances = {}; } - isEthAddress(chainId: number, tokenAddress: string): boolean { - // If there is an ETH address defined in CONTRACT_ADDRESSES, use it, otherwise assume ETH address is the zero - // address. - return (CONTRACT_ADDRESSES[chainId]?.eth?.address ?? ZERO_ADDRESS) === tokenAddress; - } - // This method is primarily here to be overriden for testing purposes. protected async _queryBalance(chainId: number, token: string, holder: string): Promise { - return this.isEthAddress(chainId, token) + return getEthAddressForChain(chainId).toLowerCase() === token.toLowerCase() ? await this.providers[chainId].getBalance(holder) : await ERC20.connect(token, this.providers[chainId]).balanceOf(holder); } diff --git a/src/monitor/Monitor.ts b/src/monitor/Monitor.ts index 335be231b..1fbcfddb9 100644 --- a/src/monitor/Monitor.ts +++ b/src/monitor/Monitor.ts @@ -21,6 +21,7 @@ import { ethers, etherscanLink, etherscanLinks, + getEthAddressForChain, getGasPrice, getNativeTokenSymbol, getNetworkName, @@ -29,7 +30,6 @@ import { toBN, toWei, winston, - ZERO_ADDRESS, } from "../utils"; import { MonitorClients, updateMonitorClients } from "./MonitorClientHelper"; @@ -316,6 +316,21 @@ export class Monitor { const { monitoredBalances } = this.monitorConfig; const balances = await this._getBalances(monitoredBalances); const decimalValues = await this._getDecimals(monitoredBalances); + + this.logger.debug({ + at: "Monitor#checkBalances", + message: "Checking balances", + currentBalances: monitoredBalances.map(({ chainId, token, account, warnThreshold, errorThreshold }, i) => { + return { + chainId, + token, + account, + currentBalance: balances[i].toString(), + warnThreshold: ethers.utils.parseUnits(warnThreshold.toString(), decimalValues[i]), + errorThreshold: ethers.utils.parseUnits(errorThreshold.toString(), decimalValues[i]), + }; + }), + }); const alerts = ( await Promise.all( monitoredBalances.map( @@ -334,8 +349,9 @@ export class Monitor { trippedThreshold = { level: "error", threshold: errorThreshold }; } if (trippedThreshold !== null) { + const ethAddressForChain = getEthAddressForChain(chainId); const symbol = - token === ZERO_ADDRESS + token === ethAddressForChain ? getNativeTokenSymbol(chainId) : await new Contract( token, @@ -913,8 +929,9 @@ export class Monitor { if (this.balanceCache[chainId]?.[token]?.[account]) { return this.balanceCache[chainId][token][account]; } + const ethAddressForChain = getEthAddressForChain(chainId); const balance = - token === ZERO_ADDRESS + token === ethAddressForChain ? await this.clients.spokePoolClients[chainId].spokePool.provider.getBalance(account) : // Use the latest block number the SpokePoolClient is aware of to query balances. // This prevents double counting when there are very recent refund leaf executions that the SpokePoolClients @@ -939,7 +956,8 @@ export class Monitor { private async _getDecimals(decimalrequests: { chainId: number; token: string }[]): Promise { return await Promise.all( decimalrequests.map(async ({ chainId, token }) => { - if (token === ZERO_ADDRESS) { + const ethAddressForChain = getEthAddressForChain(chainId); + if (token === ethAddressForChain) { return 18; } // Assume all EVM chains have 18 decimal native tokens. if (this.decimals[chainId]?.[token]) { diff --git a/src/monitor/MonitorConfig.ts b/src/monitor/MonitorConfig.ts index 6b0992c7e..3152be4e7 100644 --- a/src/monitor/MonitorConfig.ts +++ b/src/monitor/MonitorConfig.ts @@ -1,5 +1,5 @@ import { CommonConfig, ProcessEnv } from "../common"; -import { ethers, ZERO_ADDRESS } from "../utils"; +import { ethers, getEthAddressForChain } from "../utils"; // Set modes to true that you want to enable in the AcrossMonitor bot. export interface BotModes { @@ -102,7 +102,7 @@ export class MonitorConfig extends CommonConfig { // Optional fields that will set to defaults: isHubPool: Boolean(isHubPool), // Fields that are always set to defaults: - token: ZERO_ADDRESS, + token: getEthAddressForChain(chainId), }; } ); @@ -150,9 +150,9 @@ export class MonitorConfig extends CommonConfig { parsedWarnThreshold = Number(warnThreshold); } - const isNativeToken = !token || token === "0x0" || token === ZERO_ADDRESS; + const isNativeToken = !token || token === "0x0" || token === getEthAddressForChain(chainId); return { - token: isNativeToken ? ZERO_ADDRESS : token, + token: isNativeToken ? getEthAddressForChain(chainId) : token, errorThreshold: parsedErrorThreshold, warnThreshold: parsedWarnThreshold, account: ethers.utils.getAddress(account), diff --git a/src/utils/TokenUtils.ts b/src/utils/TokenUtils.ts index dba23b674..dc4a79694 100644 --- a/src/utils/TokenUtils.ts +++ b/src/utils/TokenUtils.ts @@ -1,5 +1,6 @@ import { constants, utils } from "@across-protocol/sdk-v2"; -const { TOKEN_SYMBOLS_MAP, CHAIN_IDs } = constants; +import { CONTRACT_ADDRESSES } from "../common"; +const { TOKEN_SYMBOLS_MAP, CHAIN_IDs, ZERO_ADDRESS } = constants; export const { fetchTokenInfo } = utils; @@ -8,3 +9,7 @@ export const getL2TokenAddresses = (l1TokenAddress: string): { [chainId: number] return details.addresses[CHAIN_IDs.MAINNET] === l1TokenAddress; })?.addresses; }; + +export const getEthAddressForChain = (chainId: number): string => { + return CONTRACT_ADDRESSES[chainId]?.eth?.address ?? ZERO_ADDRESS; +};