Skip to content

Commit 324d1d0

Browse files
committed
feat: enhance defi-wrapper with health monitoring commands and dev-tools integration
1 parent e17edd5 commit 324d1d0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+2930
-267
lines changed

configs/deployed.ts

Lines changed: 64 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as process from 'node:process';
33
import path from 'path';
44
import { zeroAddress, Address, Chain } from 'viem';
55

6-
import { getValueByPath, validateConfig } from 'utils';
6+
import { getValueByPath, logError, logInfo, validateConfig } from 'utils';
77
import { Config } from 'types';
88

99
import { envs } from './envs.js';
@@ -52,20 +52,79 @@ export const getDeployed = () => {
5252
return deployedJSON;
5353
};
5454

55-
export const getChainId = () => {
55+
let chainIdCache: number | undefined;
56+
const getRpcChainId = async (elURL: string) => {
57+
if (chainIdCache) {
58+
return chainIdCache;
59+
}
60+
61+
try {
62+
const rpcChainIdResponse = await fetch(elURL, {
63+
method: 'POST',
64+
headers: {
65+
'Content-Type': 'application/json',
66+
},
67+
body: JSON.stringify({
68+
jsonrpc: '2.0',
69+
id: 1,
70+
method: 'eth_chainId',
71+
params: [],
72+
}),
73+
});
74+
75+
if (!rpcChainIdResponse?.ok) {
76+
throw new Error(
77+
`RPC request failed: ${rpcChainIdResponse.status} ${rpcChainIdResponse.statusText}`,
78+
);
79+
}
80+
81+
const rpcChainIdData = await rpcChainIdResponse.json();
82+
if (rpcChainIdData?.error) {
83+
throw new Error(
84+
`RPC error: ${rpcChainIdData.error.message || JSON.stringify(rpcChainIdData.error)}`,
85+
);
86+
}
87+
88+
const rpcChainId = parseInt(rpcChainIdData.result, 16);
89+
chainIdCache = rpcChainId;
90+
91+
return rpcChainId;
92+
} catch (error) {
93+
if (!chainIdCache) {
94+
logError(
95+
'Filed to get RPC chainId. Please check if the EL_URL environment variable is correct or try to use another EL.',
96+
);
97+
logInfo('Continue work without RPC');
98+
}
99+
100+
return chainIdCache;
101+
}
102+
};
103+
104+
export const getChainId = async () => {
56105
const config = getConfig();
106+
const elURL = getElUrl();
57107
const deployed = getDeployed();
58108
const chainId = config.CHAIN_ID;
109+
const rpcChainId = await getRpcChainId(elURL);
59110

60111
if (chainId !== deployed.chainId) {
61-
throw new Error('ChainId in env and deployed file mismatch');
112+
throw new Error(
113+
`ChainId in env and deployed file mismatch. ENV: ${chainId} DEPLOYED: ${deployed.chainId}`,
114+
);
115+
}
116+
117+
if (rpcChainId && chainId !== rpcChainId) {
118+
throw new Error(
119+
`ChainId in env and RPC chainId mismatch. ENV: ${chainId} RPC: ${rpcChainId}`,
120+
);
62121
}
63122

64123
return chainId;
65124
};
66125

67-
export const getChain = (): Chain => {
68-
const chainId = getChainId();
126+
export const getChain = async (): Promise<Chain> => {
127+
const chainId = await getChainId();
69128
const chain = SUPPORTED_CHAINS_LIST.find((chain) => chain.id === chainId);
70129

71130
if (!chain) {

contracts/dashboard-impl.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,23 @@ import {
88
import { DashboardAbi } from 'abi';
99
import { getChain, getDashboardImplAddress, getElUrl } from 'configs';
1010

11-
export const getDashboardImplContract = (): GetContractReturnType<
12-
typeof DashboardAbi,
13-
PublicClient
11+
export const getDashboardImplContract = async (): Promise<
12+
GetContractReturnType<typeof DashboardAbi, PublicClient>
1413
> => {
1514
const elUrl = getElUrl();
15+
const chain = await getChain();
1616
const address = getDashboardImplAddress();
1717

1818
return getContract({
1919
address,
2020
abi: DashboardAbi,
2121
client: createPublicClient({
22-
chain: getChain(),
22+
chain,
2323
transport: http(elUrl),
2424
}),
2525
});
2626
};
2727

28-
export type DashboardImplContract = ReturnType<typeof getDashboardImplContract>;
28+
export type DashboardImplContract = Awaited<
29+
ReturnType<typeof getDashboardImplContract>
30+
>;

contracts/dashboard.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@ import {
77
import { DashboardAbi } from 'abi';
88
import { getPublicClient } from 'providers';
99

10-
export const getDashboardContract = (
10+
export const getDashboardContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof DashboardAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof DashboardAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: DashboardAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

20-
export type DashboardContract = ReturnType<typeof getDashboardContract>;
22+
export type DashboardContract = Awaited<
23+
ReturnType<typeof getDashboardContract>
24+
>;

contracts/defi-wrapper/distributor.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
import { DistributorAbi } from 'abi/defi-wrapper/index.js';
88
import { getPublicClient } from 'providers';
99

10-
export const getDistributorContract = (
10+
export const getDistributorContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof DistributorAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof DistributorAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: DistributorAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

contracts/defi-wrapper/factory.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
import { FactoryAbi } from 'abi/defi-wrapper/index.js';
88
import { getPublicClient } from 'providers';
99

10-
export const getFactoryContract = (
10+
export const getFactoryContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof FactoryAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof FactoryAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: FactoryAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

contracts/defi-wrapper/stv-pool.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,16 @@ import {
77
import { StvPoolAbi } from 'abi/defi-wrapper/index.js';
88
import { getPublicClient } from 'providers';
99

10-
export const getStvPoolContract = (
10+
export const getStvPoolContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof StvPoolAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof StvPoolAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: StvPoolAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

20-
export type StvPoolContract = ReturnType<typeof getStvPoolContract>;
22+
export type StvPoolContract = Awaited<ReturnType<typeof getStvPoolContract>>;

contracts/defi-wrapper/stv-steth-pool.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
import { StvStETHPoolAbi } from 'abi/defi-wrapper/index.js';
88
import { getPublicClient } from 'providers';
99

10-
export const getStvStethPoolContract = (
10+
export const getStvStethPoolContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof StvStETHPoolAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof StvStETHPoolAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: StvStETHPoolAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

contracts/defi-wrapper/withdrawal-queue.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,15 @@ import {
77
import { WithdrawalQueueAbi } from 'abi/defi-wrapper/index.js';
88
import { getPublicClient } from 'providers';
99

10-
export const getWithdrawalQueueContract = (
10+
export const getWithdrawalQueueContract = async (
1111
address: Address,
12-
): GetContractReturnType<typeof WithdrawalQueueAbi, WalletClient> => {
12+
): Promise<GetContractReturnType<typeof WithdrawalQueueAbi, WalletClient>> => {
13+
const publicClient = await getPublicClient();
14+
1315
return getContract({
1416
address: address,
1517
abi: WithdrawalQueueAbi,
16-
client: getPublicClient(),
18+
client: publicClient,
1719
});
1820
};
1921

contracts/lazy-oracle.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,15 @@ export const getLazyOracleContract = async (): Promise<
1414
GetContractReturnType<typeof LazyOracleAbi, WalletClient>
1515
> => {
1616
const elUrl = getElUrl();
17-
const locator = getLocatorContract();
17+
const chain = await getChain();
18+
const locator = await getLocatorContract();
1819
const address = await locator.read.lazyOracle();
1920

2021
return getContract({
2122
address,
2223
abi: LazyOracleAbi,
2324
client: createPublicClient({
24-
chain: getChain(),
25+
chain,
2526
transport: http(elUrl),
2627
}),
2728
});

contracts/locator.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,25 @@
1-
import { getContract, createPublicClient, http } from 'viem';
1+
import {
2+
getContract,
3+
createPublicClient,
4+
http,
5+
GetContractReturnType,
6+
PublicClient,
7+
} from 'viem';
28
import { LidoLocatorAbi } from 'abi';
39
import { getChain, getLocatorAddress, getElUrl } from 'configs';
410

5-
export const getLocatorContract = () => {
11+
export const getLocatorContract = async (): Promise<
12+
GetContractReturnType<typeof LidoLocatorAbi, PublicClient>
13+
> => {
614
const elUrl = getElUrl();
15+
const chain = await getChain();
716
const address = getLocatorAddress();
817

918
return getContract({
1019
address,
1120
abi: LidoLocatorAbi,
1221
client: createPublicClient({
13-
chain: getChain(),
22+
chain,
1423
transport: http(elUrl),
1524
}),
1625
});

0 commit comments

Comments
 (0)