Skip to content

Commit

Permalink
WIP - cache chain info (#36)
Browse files Browse the repository at this point in the history
* cache chainInfo for supported chains

* allow chainId filtering in chain info query
  • Loading branch information
gsteenkamp89 authored Oct 4, 2024
1 parent 7e5c9f9 commit 31a0a8c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
30 changes: 30 additions & 0 deletions packages/sdk/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
Address,
Chain,
ContractFunctionExecutionError,
encodeFunctionData,
Expand Down Expand Up @@ -45,13 +46,15 @@ import {
simulateTxOnTenderly,
TenderlySimulateTxParams,
assertValidIntegratorId,
AcrossChain,
} from "./utils";
import {
AcrossApiSimulationError,
ConfigError,
SimulationError,
} from "./errors";
import {
ChainInfoMap,
ConfiguredPublicClient,
ConfiguredPublicClientMap,
ConfiguredWalletClient,
Expand Down Expand Up @@ -138,6 +141,7 @@ export class AcrossClient {

private integratorId: Hex;
private publicClients: ConfiguredPublicClientMap;
private chainInfo?: ChainInfoMap;
private walletClient?: ConfiguredWalletClient;
private apiUrl: string;
private indexerUrl: string;
Expand Down Expand Up @@ -228,6 +232,32 @@ export class AcrossClient {
return client;
}

async getSpokePoolAddress(chainId: number): Promise<Address> {
const chainInfo = await this.getChainInfo(chainId);
return chainInfo.spokePool;
}

/**
* @param chainId - number
* @returns See {@link AcrossChain}.
*/
async getChainInfo(chainId: number): Promise<AcrossChain> {
if (!this.chainInfo) {
const acrossChains = await this.getSupportedChains({
chainId: Array.from(this.publicClients.keys()),
});
// cache across chain info in memory
this.chainInfo = new Map(
acrossChains.map((acrossChain) => [acrossChain.chainId, acrossChain]),
);
}
if (this.chainInfo.has(chainId)) {
throw new Error(`Could not find chainInfo for chain with id ${chainId}`);
}

return this.chainInfo.get(chainId)!;
}

/**
* Execute a quote by:
* 1. Approving the SpokePool contract if necessary
Expand Down
3 changes: 3 additions & 0 deletions packages/sdk/src/types/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
WalletClient,
} from "viem";
import { STATUS } from "../constants";
import { AcrossChain } from "../utils/getSupportedChains";

export type Status = keyof typeof STATUS;

Expand Down Expand Up @@ -73,3 +74,5 @@ export type Deposit = {
fillTxBlock?: bigint;
actionSuccess?: boolean;
};

export type ChainInfoMap = Map<number, AcrossChain>;
5 changes: 3 additions & 2 deletions packages/sdk/src/utils/getSupportedChains.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { MAINNET_API_URL } from "../constants";
import { TokenInfo } from "../types";
import { LoggerT, fetchAcrossApi } from ".";
import { Address } from "viem";

export type ChainsQueryParams = Partial<{
inputTokenSymbol: string;
outputTokenSymbol: string;
chainId: number; // origin chainId
chainId: number | number[];
omitTokens: boolean;
}>;

Expand Down Expand Up @@ -33,7 +34,7 @@ export type AcrossChain = {
publicRpcUrl: string;
explorerUrl: string;
logoUrl: string;
spokePool: string;
spokePool: Address;
inputTokens: TokenInfo[];
outputTokens: TokenInfo[];
};
Expand Down

0 comments on commit 31a0a8c

Please sign in to comment.