Skip to content

Commit

Permalink
refactor: use shared chains
Browse files Browse the repository at this point in the history
  • Loading branch information
0xyaco committed Jul 31, 2024
1 parent 163669b commit 213aae4
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
20 changes: 18 additions & 2 deletions packages/blocknumber/src/services/blockNumberService.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Logger, supportedChains } from "@ebo-agent/shared";
import {
createPublicClient,
fallback,
Expand Down Expand Up @@ -50,9 +51,12 @@ export class BlockNumberService {
private buildBlockNumberProviders(chainRpcUrls: Map<Caip2ChainId, RpcUrl[]>) {
if (chainRpcUrls.size == 0) throw new EmptyRpcUrls();

const supportedChainIds = this.getSupportedChainIds(supportedChains);
const providers = new Map<Caip2ChainId, BlockNumberProvider>();

for (const [chainId, urls] of chainRpcUrls) {
if (!supportedChainIds.includes(chainId)) throw new UnsupportedChain(chainId);

const client = createPublicClient({
transport: fallback(urls.map((url) => http(url))),
});
Expand All @@ -67,15 +71,27 @@ export class BlockNumberService {
return providers;
}

private getSupportedChainIds(chainsConfig: typeof supportedChains) {
const namespacesChains = Object.values(chainsConfig);

return namespacesChains.reduce((acc, namespaceChains) => {
return [...acc, ...Object.values(namespaceChains.chains)];
}, [] as string[]);
}

public static buildProvider(
chainId: Caip2ChainId,
client: PublicClient<FallbackTransport<HttpTransport[]>>,
) {
const chainNamespace = Caip2.getNamespace(chainId);

switch (chainNamespace) {
case "eip155":
return new EvmBlockNumberProvider(client, DEFAULT_PROVIDER_CONFIG);
case supportedChains.evm.namespace:
return new EvmBlockNumberProvider(
client,
DEFAULT_PROVIDER_CONFIG,
Logger.getInstance(), // Should we drop this arg?
);

default:
throw new UnsupportedChain(chainId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { EvmBlockNumberProvider } from "../../src/providers/evmBlockNumberProvid
describe("EvmBlockNumberProvider", () => {
describe("getEpochBlockNumber", () => {
const searchConfig = { blocksLookback: 2n, deltaMultiplier: 2n };
const logger = Logger.getInstance();
let evmProvider: EvmBlockNumberProvider;

it("returns the first of two consecutive blocks when their timestamp contains the searched timestamp", async () => {
Expand Down
10 changes: 10 additions & 0 deletions packages/shared/src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const supportedChains = {
evm: {
namespace: "eip155",
chains: {
ethereum: "eip155:1",
polygon: "eip155:137",
arbitrum: "eip155:42161",
},
},
} as const;

0 comments on commit 213aae4

Please sign in to comment.