Skip to content

Commit

Permalink
refactor(AdapterManager): define base chain adapters programmatically (
Browse files Browse the repository at this point in the history
…across-protocol#1967)

Signed-off-by: bennett <[email protected]>
Co-authored-by: Paul <[email protected]>
  • Loading branch information
bmzig and pxrl authored Jan 6, 2025
1 parent 6bba9d0 commit 38ef563
Show file tree
Hide file tree
Showing 7 changed files with 23 additions and 248 deletions.
68 changes: 0 additions & 68 deletions src/adapter/AdapterManager.ts

This file was deleted.

179 changes: 17 additions & 162 deletions src/clients/bridges/AdapterManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import {
import { InventoryConfig, OutstandingTransfers } from "../../interfaces";
import { BigNumber, isDefined, winston, Signer, getL2TokenAddresses, TransactionResponse, assert } from "../../utils";
import { SpokePoolClient, HubPoolClient } from "../";
import { ArbitrumAdapter, PolygonAdapter, ZKSyncAdapter, LineaAdapter, ScrollAdapter } from "./";
import { CHAIN_IDs, TOKEN_SYMBOLS_MAP } from "@across-protocol/constants";

import { BaseChainAdapter } from "../../adapter";
import { EthereumAdapter } from "./EthereumAdapter";

export class AdapterManager {
public adapters: { [chainId: number]: BaseChainAdapter } = {};
Expand Down Expand Up @@ -46,172 +43,37 @@ export class AdapterManager {
);
};

const {
OPTIMISM,
ARBITRUM,
POLYGON,
ZK_SYNC,
BASE,
MODE,
LINEA,
LISK,
BLAST,
REDSTONE,
SCROLL,
ZORA,
ALEPH_ZERO,
INK,
} = CHAIN_IDs;
const hubChainId = hubPoolClient.chainId;
const l1Signer = spokePoolClients[hubChainId].spokePool.signer;
const constructBridges = (chainId: number) => {
if (chainId === hubChainId) {
return {};
} // Special case for the EthereumAdapter
return Object.fromEntries(
SUPPORTED_TOKENS[chainId].map((symbol) => {
SUPPORTED_TOKENS[chainId]?.map((symbol) => {
const l2Signer = spokePoolClients[chainId].spokePool.signer;
const l1Token = TOKEN_SYMBOLS_MAP[symbol].addresses[hubChainId];
const bridgeConstructor = CUSTOM_BRIDGE[chainId]?.[l1Token] ?? CANONICAL_BRIDGE[chainId];
const bridge = new bridgeConstructor(chainId, hubChainId, l1Signer, l2Signer, l1Token);
return [l1Token, bridge];
})
}) ?? []
);
};
if (this.spokePoolClients[OPTIMISM] !== undefined) {
this.adapters[OPTIMISM] = new BaseChainAdapter(
spokePoolClients,
OPTIMISM,
hubChainId,
filterMonitoredAddresses(OPTIMISM),
logger,
SUPPORTED_TOKENS[OPTIMISM],
constructBridges(OPTIMISM),
DEFAULT_GAS_MULTIPLIER[OPTIMISM] ?? 1
);
}
if (this.spokePoolClients[POLYGON] !== undefined) {
this.adapters[POLYGON] = new PolygonAdapter(logger, spokePoolClients, filterMonitoredAddresses(POLYGON));
}
if (this.spokePoolClients[ARBITRUM] !== undefined) {
this.adapters[ARBITRUM] = new ArbitrumAdapter(logger, spokePoolClients, filterMonitoredAddresses(ARBITRUM));
}
if (this.spokePoolClients[ZK_SYNC] !== undefined) {
this.adapters[ZK_SYNC] = new ZKSyncAdapter(logger, spokePoolClients, filterMonitoredAddresses(ZK_SYNC));
}
if (this.spokePoolClients[BASE] !== undefined) {
this.adapters[BASE] = new BaseChainAdapter(
spokePoolClients,
BASE,
hubChainId,
filterMonitoredAddresses(BASE),
logger,
SUPPORTED_TOKENS[BASE],
constructBridges(BASE),
DEFAULT_GAS_MULTIPLIER[BASE] ?? 1
);
}
if (this.spokePoolClients[LINEA] !== undefined) {
this.adapters[LINEA] = new LineaAdapter(logger, spokePoolClients, filterMonitoredAddresses(LINEA));
}
if (this.spokePoolClients[MODE] !== undefined) {
this.adapters[MODE] = new BaseChainAdapter(
spokePoolClients,
MODE,
hubChainId,
filterMonitoredAddresses(MODE),
logger,
SUPPORTED_TOKENS[MODE],
constructBridges(MODE),
DEFAULT_GAS_MULTIPLIER[MODE] ?? 1
);
}
if (this.spokePoolClients[REDSTONE] !== undefined) {
this.adapters[REDSTONE] = new BaseChainAdapter(
spokePoolClients,
REDSTONE,
hubChainId,
filterMonitoredAddresses(REDSTONE),
logger,
SUPPORTED_TOKENS[REDSTONE],
constructBridges(REDSTONE),
DEFAULT_GAS_MULTIPLIER[REDSTONE] ?? 1
);
}
if (this.spokePoolClients[LISK] !== undefined) {
this.adapters[LISK] = new BaseChainAdapter(
spokePoolClients,
LISK,
hubChainId,
filterMonitoredAddresses(LISK),
logger,
SUPPORTED_TOKENS[LISK],
constructBridges(LISK),
DEFAULT_GAS_MULTIPLIER[LISK] ?? 1
);
}
if (this.spokePoolClients[BLAST] !== undefined) {
this.adapters[BLAST] = new BaseChainAdapter(
spokePoolClients,
BLAST,
hubChainId,
filterMonitoredAddresses(BLAST),
logger,
SUPPORTED_TOKENS[BLAST],
constructBridges(BLAST),
DEFAULT_GAS_MULTIPLIER[BLAST] ?? 1
);
}
if (this.spokePoolClients[SCROLL] !== undefined) {
this.adapters[SCROLL] = new ScrollAdapter(logger, spokePoolClients, filterMonitoredAddresses(SCROLL));
}
if (this.spokePoolClients[CHAIN_IDs.WORLD_CHAIN] !== undefined) {
this.adapters[CHAIN_IDs.WORLD_CHAIN] = new BaseChainAdapter(
spokePoolClients,
CHAIN_IDs.WORLD_CHAIN,
hubChainId,
filterMonitoredAddresses(CHAIN_IDs.WORLD_CHAIN),
logger,
SUPPORTED_TOKENS[CHAIN_IDs.WORLD_CHAIN],
constructBridges(CHAIN_IDs.WORLD_CHAIN),
DEFAULT_GAS_MULTIPLIER[CHAIN_IDs.WORLD_CHAIN] ?? 1
);
}
if (this.spokePoolClients[ZORA] !== undefined) {
this.adapters[ZORA] = new BaseChainAdapter(
spokePoolClients,
ZORA,
hubChainId,
filterMonitoredAddresses(ZORA),
logger,
SUPPORTED_TOKENS[ZORA],
constructBridges(ZORA),
DEFAULT_GAS_MULTIPLIER[ZORA] ?? 1
);
}
if (this.spokePoolClients[ALEPH_ZERO] !== undefined) {
this.adapters[ALEPH_ZERO] = new BaseChainAdapter(
spokePoolClients,
ALEPH_ZERO,
hubChainId,
filterMonitoredAddresses(ALEPH_ZERO),
logger,
SUPPORTED_TOKENS[ALEPH_ZERO],
constructBridges(ALEPH_ZERO),
DEFAULT_GAS_MULTIPLIER[ALEPH_ZERO] ?? 1
);
}

if (this.spokePoolClients[INK] !== undefined) {
this.adapters[INK] = new BaseChainAdapter(
Object.keys(this.spokePoolClients).map((_chainId) => {
const chainId = Number(_chainId);
assert(chainId.toString() === _chainId);
// Instantiate a generic adapter and supply all network-specific configurations.
this.adapters[chainId] = new BaseChainAdapter(
spokePoolClients,
INK,
chainId,
hubChainId,
filterMonitoredAddresses(INK),
filterMonitoredAddresses(chainId),
logger,
SUPPORTED_TOKENS[INK],
constructBridges(INK),
DEFAULT_GAS_MULTIPLIER[INK] ?? 1
SUPPORTED_TOKENS[chainId] ?? [],
constructBridges(chainId),
DEFAULT_GAS_MULTIPLIER[chainId] ?? 1
);
}

});
logger.debug({
at: "AdapterManager#constructor",
message: "Initialized AdapterManager",
Expand Down Expand Up @@ -273,14 +135,7 @@ export class AdapterManager {
wrapThreshold.gte(wrapTarget),
`wrapEtherThreshold ${wrapThreshold.toString()} must be >= wrapEtherTarget ${wrapTarget.toString()}`
);
if (chainId === CHAIN_IDs.MAINNET) {
// For mainnet, construct one-off adapter to wrap ETH, because Ethereum is typically not a chain
// that we have an adapter for.
const ethAdapter = new EthereumAdapter(this.logger, this.spokePoolClients);
await ethAdapter.wrapEthIfAboveThreshold(wrapThreshold, wrapTarget, simMode);
} else {
await this.adapters[chainId].wrapEthIfAboveThreshold(wrapThreshold, wrapTarget, simMode);
}
await this.adapters[chainId].wrapEthIfAboveThreshold(wrapThreshold, wrapTarget, simMode);
}
);
}
Expand Down
7 changes: 2 additions & 5 deletions src/monitor/MonitorClientHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
constructSpokePoolClientsWithLookback,
} from "../common";
import { SpokePoolClientsByChain } from "../interfaces";
import { AdapterManager, CrossChainTransferClient } from "../clients/bridges";

export interface MonitorClients extends Clients {
bundleDataClient: BundleDataClient;
Expand All @@ -18,9 +19,6 @@ export interface MonitorClients extends Clients {
tokenTransferClient: TokenTransferClient;
}

import { GenericAdapterManager } from "../adapter/AdapterManager";
import { AdapterManager, CrossChainTransferClient } from "../clients/bridges";

export async function constructMonitorClients(
config: MonitorConfig,
logger: winston.Logger,
Expand Down Expand Up @@ -56,8 +54,7 @@ export async function constructMonitorClients(

// Cross-chain transfers will originate from the HubPool's address and target SpokePool addresses, so
// track both.
const adapterManagerConstructor = config.useGenericAdapter ? GenericAdapterManager : AdapterManager;
const adapterManager = new adapterManagerConstructor(logger, spokePoolClients, hubPoolClient, [
const adapterManager = new AdapterManager(logger, spokePoolClients, hubPoolClient, [
signerAddr,
hubPoolClient.hubPool.address,
...spokePoolAddresses,
Expand Down
3 changes: 0 additions & 3 deletions src/monitor/MonitorConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ export class MonitorConfig extends CommonConfig {
REFILL_BALANCES,
REFILL_BALANCES_ENABLED,
STUCK_REBALANCES_ENABLED,
MONITOR_USE_GENERIC_ADAPTER,
REPORT_SPOKE_POOL_BALANCES,
MONITORED_SPOKE_POOL_CHAINS,
MONITORED_TOKEN_SYMBOLS,
Expand All @@ -83,8 +82,6 @@ export class MonitorConfig extends CommonConfig {
spokePoolBalanceReportEnabled: REPORT_SPOKE_POOL_BALANCES === "true",
};

this.useGenericAdapter = MONITOR_USE_GENERIC_ADAPTER === "true";

// Used to monitor activities not from whitelisted data workers or relayers.
this.whitelistedDataworkers = parseAddressesOptional(WHITELISTED_DATA_WORKERS);
this.whitelistedRelayers = parseAddressesOptional(WHITELISTED_RELAYERS);
Expand Down
5 changes: 1 addition & 4 deletions src/relayer/RelayerClientHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import {
import { SpokePoolClientsByChain } from "../interfaces";
import { getBlockForTimestamp, getCurrentTime, getProvider, getRedisCache, Signer, SpokePool } from "../utils";
import { RelayerConfig } from "./RelayerConfig";

import { GenericAdapterManager } from "../adapter/AdapterManager";
import { AdapterManager, CrossChainTransferClient } from "../clients/bridges";

export interface RelayerClients extends Clients {
Expand Down Expand Up @@ -143,8 +141,7 @@ export async function constructRelayerClients(
await profitClient.update();

const monitoredAddresses = [signerAddr];
const adapterManagerConstructor = config.useGenericAdapter ? GenericAdapterManager : AdapterManager;
const adapterManager = new adapterManagerConstructor(
const adapterManager = new AdapterManager(
logger,
spokePoolClients,
hubPoolClient,
Expand Down
3 changes: 0 additions & 3 deletions src/relayer/RelayerConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,14 +89,11 @@ export class RelayerConfig extends CommonConfig {
RELAYER_IGNORE_LIMITS,
RELAYER_EXTERNAL_INDEXER,
RELAYER_TRY_MULTICALL_CHAINS,
RELAYER_USE_GENERIC_ADAPTER,
RELAYER_LOGGING_INTERVAL = "30",
RELAYER_MAINTENANCE_INTERVAL = "60",
} = env;
super(env);

this.useGenericAdapter = RELAYER_USE_GENERIC_ADAPTER === "true";

// External indexing is dependent on looping mode being configured.
this.externalIndexer = this.pollingDelay > 0 && RELAYER_EXTERNAL_INDEXER === "true";

Expand Down
6 changes: 3 additions & 3 deletions test/generic-adapters/AdapterManager.SendTokensCrossChain.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as zksync from "zksync-ethers";
import { SpokePoolClient } from "../../src/clients";
import { GenericAdapterManager } from "../../src/adapter/AdapterManager"; // Tested
import { AdapterManager } from "../../src/clients/bridges";
import { CONTRACT_ADDRESSES, chainIdsToCctpDomains } from "../../src/common";
import {
bnToHex,
Expand Down Expand Up @@ -30,7 +30,7 @@ const mockSpokePoolClients: {
[chainId: number]: SpokePoolClient;
} = {};
let relayer: SignerWithAddress, owner: SignerWithAddress, spyLogger: winston.Logger, amountToSend: BigNumber;
let adapterManager: AdapterManager; // tested
let adapterManager: AdapterManager;

// Atomic depositor
let l1AtomicDepositor: FakeContract;
Expand Down Expand Up @@ -78,7 +78,7 @@ describe("AdapterManager: Send tokens cross-chain", async function () {
const { hubPool } = await hubPoolFixture();
hubPoolClient = new MockHubPoolClient(spyLogger, hubPool, configStoreClient);
await seedMocks();
adapterManager = new GenericAdapterManager(spyLogger, mockSpokePoolClients, hubPoolClient, [relayer.address]);
adapterManager = new AdapterManager(spyLogger, mockSpokePoolClients, hubPoolClient, [relayer.address]);

await constructChainSpecificFakes();

Expand Down

0 comments on commit 38ef563

Please sign in to comment.