Skip to content

Commit 31a0a8c

Browse files
authored
WIP - cache chain info (#36)
* cache chainInfo for supported chains * allow chainId filtering in chain info query
1 parent 7e5c9f9 commit 31a0a8c

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

packages/sdk/src/client.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
Address,
23
Chain,
34
ContractFunctionExecutionError,
45
encodeFunctionData,
@@ -45,13 +46,15 @@ import {
4546
simulateTxOnTenderly,
4647
TenderlySimulateTxParams,
4748
assertValidIntegratorId,
49+
AcrossChain,
4850
} from "./utils";
4951
import {
5052
AcrossApiSimulationError,
5153
ConfigError,
5254
SimulationError,
5355
} from "./errors";
5456
import {
57+
ChainInfoMap,
5558
ConfiguredPublicClient,
5659
ConfiguredPublicClientMap,
5760
ConfiguredWalletClient,
@@ -138,6 +141,7 @@ export class AcrossClient {
138141

139142
private integratorId: Hex;
140143
private publicClients: ConfiguredPublicClientMap;
144+
private chainInfo?: ChainInfoMap;
141145
private walletClient?: ConfiguredWalletClient;
142146
private apiUrl: string;
143147
private indexerUrl: string;
@@ -228,6 +232,32 @@ export class AcrossClient {
228232
return client;
229233
}
230234

235+
async getSpokePoolAddress(chainId: number): Promise<Address> {
236+
const chainInfo = await this.getChainInfo(chainId);
237+
return chainInfo.spokePool;
238+
}
239+
240+
/**
241+
* @param chainId - number
242+
* @returns See {@link AcrossChain}.
243+
*/
244+
async getChainInfo(chainId: number): Promise<AcrossChain> {
245+
if (!this.chainInfo) {
246+
const acrossChains = await this.getSupportedChains({
247+
chainId: Array.from(this.publicClients.keys()),
248+
});
249+
// cache across chain info in memory
250+
this.chainInfo = new Map(
251+
acrossChains.map((acrossChain) => [acrossChain.chainId, acrossChain]),
252+
);
253+
}
254+
if (this.chainInfo.has(chainId)) {
255+
throw new Error(`Could not find chainInfo for chain with id ${chainId}`);
256+
}
257+
258+
return this.chainInfo.get(chainId)!;
259+
}
260+
231261
/**
232262
* Execute a quote by:
233263
* 1. Approving the SpokePool contract if necessary

packages/sdk/src/types/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
WalletClient,
1010
} from "viem";
1111
import { STATUS } from "../constants";
12+
import { AcrossChain } from "../utils/getSupportedChains";
1213

1314
export type Status = keyof typeof STATUS;
1415

@@ -73,3 +74,5 @@ export type Deposit = {
7374
fillTxBlock?: bigint;
7475
actionSuccess?: boolean;
7576
};
77+
78+
export type ChainInfoMap = Map<number, AcrossChain>;

packages/sdk/src/utils/getSupportedChains.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import { MAINNET_API_URL } from "../constants";
22
import { TokenInfo } from "../types";
33
import { LoggerT, fetchAcrossApi } from ".";
4+
import { Address } from "viem";
45

56
export type ChainsQueryParams = Partial<{
67
inputTokenSymbol: string;
78
outputTokenSymbol: string;
8-
chainId: number; // origin chainId
9+
chainId: number | number[];
910
omitTokens: boolean;
1011
}>;
1112

@@ -33,7 +34,7 @@ export type AcrossChain = {
3334
publicRpcUrl: string;
3435
explorerUrl: string;
3536
logoUrl: string;
36-
spokePool: string;
37+
spokePool: Address;
3738
inputTokens: TokenInfo[];
3839
outputTokens: TokenInfo[];
3940
};

0 commit comments

Comments
 (0)