Skip to content

Commit 3ba264f

Browse files
committed
Sync with master and fix conflicts
2 parents fd4681a + 820f5d9 commit 3ba264f

File tree

9 files changed

+123
-11
lines changed

9 files changed

+123
-11
lines changed

src/adapters/hyperliquid/index.ts

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Chain } from "@defillama/sdk/build/general";
2+
import { BridgeAdapter, PartialContractEventParams } from "../../helpers/bridgeAdapter.type";
3+
import { constructTransferParams } from "../../helpers/eventParams";
4+
import { getTxDataFromEVMEventLogs } from "../../helpers/processTransactions";
5+
6+
const HYPERLIQUID_BRIDGE_ADDRESS = "0x2Df1c51E09aECF9cacB7bc98cB1742757f163dF7";
7+
8+
const erc20WithdrawalEventParams: PartialContractEventParams = constructTransferParams(
9+
HYPERLIQUID_BRIDGE_ADDRESS,
10+
false
11+
);
12+
13+
const erc20DepositEventParams: PartialContractEventParams = constructTransferParams(HYPERLIQUID_BRIDGE_ADDRESS, true);
14+
15+
const constructParams = (chain: string) => {
16+
let eventParams = [] as PartialContractEventParams[];
17+
eventParams.push(erc20WithdrawalEventParams, erc20DepositEventParams);
18+
return async (fromBlock: number, toBlock: number) =>
19+
getTxDataFromEVMEventLogs("hyperliquid", chain as Chain, fromBlock, toBlock, eventParams);
20+
};
21+
22+
const adapter: BridgeAdapter = {
23+
arbitrum: constructParams("arbitrum"),
24+
};
25+
26+
export default adapter;

src/adapters/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ import wormhole from "./wormhole";
7676
import thresholdnetwork from "./threshold-network";
7777
import zircuit from "./zircuit";
7878
import relay from "./relay";
79+
import hyperliquid from "./hyperliquid";
7980

8081
export default {
8182
polygon,
@@ -155,6 +156,7 @@ export default {
155156
thresholdnetwork,
156157
zircuit,
157158
relay,
159+
hyperliquid,
158160
} as {
159161
[bridge: string]: BridgeAdapter | AsyncBridgeAdapter;
160162
};

src/adapters/orbiter/index.ts

+15-4
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,13 @@ const eoaAddressErc = [
3636
"0x8086061cf07c03559fbb4aa58f191f9c4a5df2b2",
3737
"0x732efacd14b0355999aebb133585787921aba3a9",
3838
"0x34723b92ae9708ba33843120a86035d049da7dfa",
39-
"0x095d2918b03b2e86d68551dcf11302121fb626c9"
40-
39+
"0x095d2918b03b2e86d68551dcf11302121fb626c9",
40+
"0x3bDB03ad7363152DFBc185Ee23eBC93F0CF93fd1",
41+
"0xACc517ea627CEb71Cf25e002AdAa9761623837b9",
42+
"0x9C6750D463aD17Deec97A630aF766F0A78F95127",
43+
"0xA8bD77769c875f8490E4B49f4c02A1Dd83D21a18",
44+
"0xa383A72e000C056ccEaa9305B7B5d2D90887fbFd",
45+
"0xed01d58Fe6433A5Fe69720a0aa0Ab1d1fdB15212"
4146
];
4247

4348
const eoaAddressNative = [
@@ -53,8 +58,14 @@ const eoaAddressNative = [
5358
"0x8086061cf07c03559fbb4aa58f191f9c4a5df2b2",
5459
"0x732efacd14b0355999aebb133585787921aba3a9",
5560
"0x34723b92ae9708ba33843120a86035d049da7dfa",
56-
"0x095d2918b03b2e86d68551dcf11302121fb626c9"
57-
61+
"0x095d2918b03b2e86d68551dcf11302121fb626c9",
62+
"0x095d2918b03b2e86d68551dcf11302121fb626c9",
63+
"0x3bDB03ad7363152DFBc185Ee23eBC93F0CF93fd1",
64+
"0xACc517ea627CEb71Cf25e002AdAa9761623837b9",
65+
"0x9C6750D463aD17Deec97A630aF766F0A78F95127",
66+
"0xA8bD77769c875f8490E4B49f4c02A1Dd83D21a18",
67+
"0xa383A72e000C056ccEaa9305B7B5d2D90887fbFd",
68+
"0xed01d58Fe6433A5Fe69720a0aa0Ab1d1fdB15212"
5869
];
5970

6071
const nativeTokens: Record<string, string> = {

src/data/bridgeNetworkData.ts

+10
Original file line numberDiff line numberDiff line change
@@ -1804,4 +1804,14 @@ export default [
18041804
"Proof of Play Boss",
18051805
],
18061806
},
1807+
{
1808+
id: 80,
1809+
displayName: "Hyperliquid",
1810+
bridgeDbName: "hyperliquid",
1811+
iconLink: "icons:hyperliquid",
1812+
largeTxThreshold: 10000,
1813+
url: "https://app.hyperliquid.xyz/trade",
1814+
chains: ["Arbitrum", "Hyperliquid"],
1815+
destinationChain: "Hyperliquid",
1816+
},
18071817
] as BridgeNetwork[];

src/server/cron.ts

+9
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { runAdaptersFromTo } from "./jobs/runAdaptersFromTo";
55
import { handler as runWormhole } from "../handlers/runWormhole";
66
import { aggregateHourlyVolume } from "./jobs/aggregateHourlyVolume";
77
import { aggregateDailyVolume } from "./jobs/aggregateDailyVolume";
8+
import { warmAllCaches } from "./jobs/warmCache";
89

910
const createTimeout = (minutes: number) =>
1011
new Promise((_, reject) =>
@@ -21,6 +22,10 @@ const withTimeout = async (promise: Promise<any>, timeoutMinutes: number) => {
2122
};
2223

2324
const cron = () => {
25+
if (process.env.NO_CRON) {
26+
return;
27+
}
28+
2429
new CronJob("15,30,45 * * * *", async () => {
2530
await withTimeout(runAllAdapters(), 10);
2631
}).start();
@@ -44,6 +49,10 @@ const cron = () => {
4449
new CronJob("35 * * * *", async () => {
4550
await withTimeout(aggregateDailyVolume(), 20);
4651
}).start();
52+
53+
new CronJob("*/5 * * * *", async () => {
54+
await withTimeout(warmAllCaches(), 4);
55+
}).start();
4756
};
4857

4958
export default cron;

src/server/index.ts

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import getTransactions from "../handlers/getTransactions";
1212
import runAdapter from "../handlers/runAdapter";
1313
import getBridgeStatsOnDay from "../handlers/getBridgeStatsOnDay";
1414
import cron from "./cron";
15-
import { generateApiCacheKey, cache } from "../utils/cache";
15+
import { generateApiCacheKey, cache, registerCacheHandler, warmCache, needsWarming } from "../utils/cache";
1616

1717
dotenv.config();
1818

@@ -42,7 +42,13 @@ const lambdaToFastify = (handler: Function) => async (request: any, reply: any)
4242

4343
const cacheKey = generateApiCacheKey(event);
4444
const cachedData = cache.get(cacheKey);
45+
46+
registerCacheHandler(cacheKey, () => handler(event));
47+
4548
if (cachedData) {
49+
if (needsWarming(cacheKey)) {
50+
warmCache(cacheKey);
51+
}
4652
return reply.code(200).send(cachedData);
4753
}
4854

src/server/jobs/warmCache.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { warmCache, handlerRegistry, needsWarming } from "../../utils/cache";
2+
3+
const warmAllCaches = async () => {
4+
const cacheKeys = Array.from(handlerRegistry.keys());
5+
6+
for (const cacheKey of cacheKeys) {
7+
try {
8+
if (needsWarming(cacheKey)) {
9+
await warmCache(cacheKey);
10+
await new Promise((resolve) => setTimeout(resolve, 1000));
11+
}
12+
} catch (error) {
13+
console.error(`Failed to warm cache for key ${cacheKey}:`, error);
14+
}
15+
}
16+
};
17+
18+
export { warmAllCaches };

src/utils/cache.ts

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,20 @@
11
import { LRUCache } from "lru-cache";
22
import hash from "object-hash";
33

4-
export const cache = new LRUCache({
5-
max: 1000,
6-
ttl: 1000 * 60 * 60,
7-
});
8-
94
interface APIEvent {
105
pathParameters?: Record<string, any>;
116
queryStringParameters?: Record<string, any>;
127
body?: any;
138
}
149

10+
export const handlerRegistry = new Map<string, Function>();
11+
12+
export const cache = new LRUCache<string, any>({
13+
max: 1000,
14+
ttl: 1000 * 60 * 60,
15+
updateAgeOnGet: false,
16+
});
17+
1518
export const generateApiCacheKey = (event: APIEvent): string => {
1619
const eventToNormalize = {
1720
path: event.pathParameters || {},
@@ -27,6 +30,33 @@ export const generateApiCacheKey = (event: APIEvent): string => {
2730
}).substring(0, 16);
2831
};
2932

33+
export const CACHE_WARM_THRESHOLD = 1000 * 60 * 10;
34+
35+
export const needsWarming = (cacheKey: string): boolean => {
36+
if (!cache.has(cacheKey)) return true;
37+
38+
const ttlRemaining = cache.getRemainingTTL(cacheKey);
39+
return ttlRemaining !== undefined && ttlRemaining < CACHE_WARM_THRESHOLD;
40+
};
41+
42+
export const warmCache = async (cacheKey: string): Promise<void> => {
43+
const handler = handlerRegistry.get(cacheKey);
44+
if (!handler) {
45+
return;
46+
}
47+
try {
48+
const result = await handler();
49+
const parsedBody = JSON.parse(result.body);
50+
cache.set(cacheKey, parsedBody);
51+
} catch (error) {
52+
throw error;
53+
}
54+
};
55+
56+
export const registerCacheHandler = (cacheKey: string, handler: Function) => {
57+
handlerRegistry.set(cacheKey, handler);
58+
};
59+
3060
export const getCacheKey = (...parts: (string | undefined)[]) => parts.filter(Boolean).join(":");
3161

3262
export const DEFAULT_TTL = 600;

src/utils/runAdapterHistorical.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ const runAllAdaptersHistorical = async (startTimestamp: number, endTimestamp: nu
9898
}
9999
};
100100

101-
if (bridgeName && chain) {
101+
if (bridgeName) {
102102
fillAdapterHistorical(startTs, endTs, bridgeName, chain);
103103
} else {
104104
runAllAdaptersHistorical(startTs, endTs);

0 commit comments

Comments
 (0)