Skip to content

Commit

Permalink
improve(monitor): Read ETH address from CONTRACT_ADDRESSES (#882)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicholaspai committed Aug 14, 2023
1 parent d35c4a4 commit 79533e3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 18 deletions.
11 changes: 2 additions & 9 deletions src/clients/BalanceAllocator.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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<BigNumber> {
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);
}
Expand Down
26 changes: 22 additions & 4 deletions src/monitor/Monitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import {
ethers,
etherscanLink,
etherscanLinks,
getEthAddressForChain,
getGasPrice,
getNativeTokenSymbol,
getNetworkName,
Expand All @@ -29,7 +30,6 @@ import {
toBN,
toWei,
winston,
ZERO_ADDRESS,
} from "../utils";

import { MonitorClients, updateMonitorClients } from "./MonitorClientHelper";
Expand Down Expand Up @@ -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(
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand All @@ -939,7 +956,8 @@ export class Monitor {
private async _getDecimals(decimalrequests: { chainId: number; token: string }[]): Promise<number[]> {
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]) {
Expand Down
8 changes: 4 additions & 4 deletions src/monitor/MonitorConfig.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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),
};
}
);
Expand Down Expand Up @@ -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),
Expand Down
7 changes: 6 additions & 1 deletion src/utils/TokenUtils.ts
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -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;
};

0 comments on commit 79533e3

Please sign in to comment.