Skip to content

Commit

Permalink
fix(adapters): build async adapters only when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
tmsrjs committed Oct 31, 2024
1 parent 569f296 commit a9a073d
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 17 deletions.
4 changes: 2 additions & 2 deletions src/adapters/hyperlane/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ const excludedChains = [
"lumia", // TODO: not available in defillama sdk providerList, can be added manually
];

async function buildAdapter(): Promise<BridgeAdapter> {
async function build(): Promise<BridgeAdapter> {
await setUp();

const adapter: BridgeAdapter = {
Expand All @@ -158,4 +158,4 @@ async function buildAdapter(): Promise<BridgeAdapter> {
return adapter;
}

export default buildAdapter();
export default { isAsync: true, build };
8 changes: 4 additions & 4 deletions src/adapters/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { BridgeAdapter } from "../helpers/bridgeAdapter.type";
import { BridgeAdapter, AsyncBridgeAdapter } from "../helpers/bridgeAdapter.type";
import polygon from "./polygon";
import synapse from "./synapse";
import hop from "./hop";
Expand Down Expand Up @@ -65,8 +65,8 @@ import memebridge from "./memebridge";
import bunnyfi from "./bunnyfi";
import minibridge from "./minibridge";
import cometbridge from "./cometbridge";
import fastbtc from "./rootstock-fastbtc-bridge"
import crowdswap from "./crowdswap"
import fastbtc from "./rootstock-fastbtc-bridge";
import crowdswap from "./crowdswap";
import mint from "./mint";
import suibridge from "./suibridge";
import retrobridge from "./retrobridge"
Expand Down Expand Up @@ -148,5 +148,5 @@ export default {
layerswap,
hyperlane,
} as {
[bridge: string]: BridgeAdapter | Promise<BridgeAdapter>;
[bridge: string]: BridgeAdapter | AsyncBridgeAdapter;
};
4 changes: 3 additions & 1 deletion src/adapters/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import adapters from "./";
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { getLlamaPrices } from "../utils/prices";
import { transformTokens } from "../helpers/tokenMappings";
import { isAsyncAdapter } from "../utils/adapter";

const logTypes = {
txHash: "string",
Expand All @@ -24,7 +25,8 @@ const adapterName = process.argv[2];
const numberOfBlocks = process.argv[3];

const testAdapter = async () => {
const adapter = await adapters[adapterName];
let adapter = adapters[adapterName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
if (!adapter) {
throw new Error(`Adapter for ${adapterName} not found, check it is exported correctly.`);
}
Expand Down
5 changes: 5 additions & 0 deletions src/helpers/bridgeAdapter.type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ export type BridgeAdapter = {
[chain: string]: (fromBlock: number, toBlock: number) => Promise<EventData[]>;
};

export type AsyncBridgeAdapter = {
isAsync: boolean;
build: () => Promise<BridgeAdapter>;
};

export type EventLogFilter = {
includeToken?: string[];
includeFrom?: string[];
Expand Down
18 changes: 13 additions & 5 deletions src/utils/adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import bridgeNetworks from "../data/bridgeNetworkData";
import adapters from "../adapters";
import { maxBlocksToQueryByChain, nonBlocksChains } from "./constants";
import { store } from "./s3";
import { BridgeAdapter } from "../helpers/bridgeAdapter.type";
import { BridgeAdapter, AsyncBridgeAdapter } from "../helpers/bridgeAdapter.type";
import { getCurrentUnixTimestamp } from "./date";
import type { RecordedBlocks } from "./types";
import { wait } from "../helpers/etherscan";
Expand Down Expand Up @@ -117,7 +117,8 @@ export const runAdapterToCurrentBlock = async (
console.warn(`[WARN] No recorded blocks data for ${bridgeDbName}. Error: ${e.message}`);
}

const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
if (!adapter) {
const errString = `Adapter for ${bridgeDbName} not found, check it is exported correctly.`;
console.error(`[ERROR] ${errString}`);
Expand Down Expand Up @@ -257,7 +258,8 @@ export const runAllAdaptersToCurrentBlock = async (

for (const bridgeNetwork of bridgeNetworks) {
const { id, bridgeDbName } = bridgeNetwork;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
if (!adapter) {
const errString = `Adapter for ${bridgeDbName} not found, check it is exported correctly.`;
await insertErrorRow({
Expand Down Expand Up @@ -311,7 +313,8 @@ export const runAllAdaptersTimestampRange = async (
) => {
for (const bridgeNetwork of bridgeNetworks) {
const { id, bridgeDbName } = bridgeNetwork;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
if (!adapter) {
const errString = `Adapter for ${bridgeDbName} not found, check it is exported correctly.`;
await insertErrorRow({
Expand Down Expand Up @@ -373,7 +376,8 @@ export const runAdapterHistorical = async (
const currentTimestamp = await getCurrentUnixTimestamp();
const bridgeNetwork = bridgeNetworks.filter((bridgeNetwork) => bridgeNetwork.id === bridgeNetworkId)[0];
const { bridgeDbName } = bridgeNetwork;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;

console.log(`[INFO] Running adapter for ${bridgeDbName} on ${chain} from ${startBlock} to ${endBlock}.`);

Expand Down Expand Up @@ -704,3 +708,7 @@ export const insertConfigEntriesForAdapter = async (
})
);
};

export function isAsyncAdapter(adapter: BridgeAdapter | AsyncBridgeAdapter): adapter is AsyncBridgeAdapter {
return (adapter as AsyncBridgeAdapter).isAsync;
}
7 changes: 5 additions & 2 deletions src/utils/aggregate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
insertOrUpdateTokenWithoutPrice,
} from "./wrappa/postgres/write";
import adapters from "../adapters";
import { isAsyncAdapter } from "../utils/adapter";
import bridgeNetworks from "../data/bridgeNetworkData";
import { importBridgeNetwork } from "../data/importBridgeNetwork";
import { defaultConfidenceThreshold } from "./constants";
Expand Down Expand Up @@ -66,7 +67,8 @@ export const runAggregateDataHistorical = async (
}

const { bridgeDbName, largeTxThreshold } = bridgeNetwork!;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;

if (!adapter) {
const errString = `Adapter for ${bridgeDbName} not found, check it is exported correctly.`;
Expand Down Expand Up @@ -125,7 +127,8 @@ export const runAggregateDataAllAdapters = async (timestamp: number, hourly: boo
.for(bridgeNetworks)
.process(async (bridgeNetwork) => {
const { bridgeDbName, largeTxThreshold } = bridgeNetwork;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
const chains = Object.keys(adapter);
const chainsPromises = Promise.all(
chains.map(async (chain) => {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/insertConfigRows.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

import { insertConfigEntriesForAdapter } from "./adapter";
import adapters from "../adapters";
import { isAsyncAdapter } from "../utils/adapter";

async function insertConfigRows(bridgeDbName: string) {
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
await insertConfigEntriesForAdapter(adapter, bridgeDbName)
}

Expand Down
4 changes: 3 additions & 1 deletion src/utils/insertRecordedBlocks.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import recordedBlocksRecord from "./recordedBlocks.json";
import adapters from "../adapters";
import { isAsyncAdapter } from "../utils/adapter";
import { lookupBlock } from "@defillama/sdk/build/util";
import { Chain } from "@defillama/sdk/build/general";
import bridgeNetworks from "../data/bridgeNetworkData";
Expand All @@ -8,7 +9,8 @@ const FileSystem = require("fs");

const insertRecordedBlocks = async (adapterName: string, startTimestamp: number, endTimestamp: number) => {
let recordedBlocks = recordedBlocksRecord as { [adapterChain: string]: { startBlock: number; endBlock: number } };
const adapter = await adapters[adapterName];
let adapter = adapters[adapterName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;
if (!adapter) {
throw new Error(`Adapter for ${adapterName} not found, check it is exported correctly.`);
}
Expand Down
4 changes: 3 additions & 1 deletion src/utils/testAdapterHistorical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import bridgeNetworkData from "../data/bridgeNetworkData";
import { wait } from "../helpers/etherscan";
import { maxBlocksToQueryByChain, nonBlocksChains } from "./constants";
import adapters from "../adapters";
import { isAsyncAdapter } from "../utils/adapter";
import { getCurrentUnixTimestamp } from "./date";
import { getBlockByTimestamp } from "./blocks";
const retry = require("async-retry");
Expand All @@ -22,7 +23,8 @@ export const runAdapterHistorical = async (
const currentTimestamp = await getCurrentUnixTimestamp();
const bridgeNetwork = bridgeNetworkData.filter((bridgeNetwork) => bridgeNetwork.id === bridgeNetworkId)[0];
const { bridgeDbName } = bridgeNetwork;
const adapter = await adapters[bridgeDbName];
let adapter = adapters[bridgeDbName];
adapter = isAsyncAdapter(adapter) ? await adapter.build() : adapter;

if (!adapter) {
const errString = `Adapter for ${bridgeDbName} not found, check it is exported correctly.`;
Expand Down

0 comments on commit a9a073d

Please sign in to comment.