Skip to content

Commit

Permalink
extend transports for public clients (#44)
Browse files Browse the repository at this point in the history
* extend transports for public clients

* add test
  • Loading branch information
gsteenkamp89 authored Oct 8, 2024
1 parent 01b35b8 commit a086a2f
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 16 deletions.
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
"clean": "rm -rf .turbo && rm -rf node_modules && rm -rf dist",
"type-check": "tsc",
"check-exports": "attw --pack . --ignore-rules=cjs-resolves-to-esm",
"test": "vitest run",
"test": "vitest run --reporter=verbose",
"ci": "pnpm run build && pnpm run check-exports pnpm npm run lint && pnpm run test",
"typedoc": "typedoc --out docs src/index.ts"
},
Expand Down
14 changes: 10 additions & 4 deletions packages/sdk/src/utils/configurePublicClients.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,31 @@
import { Chain, createPublicClient, http } from "viem";
import { Chain, createPublicClient, http, Transport, webSocket } from "viem";
import { ConfiguredPublicClientMap } from "../types";

// creates a mapping chainId => publicClient
export function configurePublicClients(
chains: Chain[],
pollingInterval: number, // milliseconds
pollingInterval?: number, // milliseconds
rpcUrls?: {
[key: number]: string;
},
transports?: {
[key: number]: Transport;
},
): ConfiguredPublicClientMap {
return new Map(
chains.map((chain) => {
// get custom rpc if one is specified, or use default
const rpcUrl = rpcUrls?.[chain.id];
const customTransport = transports?.[chain.id];
const transport =
customTransport ??
(rpcUrl?.startsWith("wss") ? webSocket(rpcUrl) : http(rpcUrl));
return [
chain.id,
createPublicClient({
chain,
pollingInterval,
key: chain.id.toString(),
transport: http(rpcUrl),
transport,
batch: {
multicall: true,
},
Expand Down
8 changes: 8 additions & 0 deletions packages/sdk/test/utils/_utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { AcrossClient } from "../../src/client";
import { hardhat } from "viem/chains";

export const testSDK = AcrossClient.create({
useTestnet: true,
logLevel: "WARN",
chains: [hardhat],
});
46 changes: 46 additions & 0 deletions packages/sdk/test/utils/configurePublicClients.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { arbitrum, mainnet, optimism, polygon } from "viem/chains";
import { describe, expect, test } from "vitest";
import { configurePublicClients } from "../../src";

const chains = [...[mainnet, optimism, polygon, arbitrum]];

const webSocketArgs: Parameters<typeof configurePublicClients> = [
chains,
undefined,
{
[mainnet.id]: "wss://eth-mainnet.g.alchemy.com/v2/123abc123abc",
[optimism.id]: "wss://opt-mainnet.g.alchemy.com/v2/123abc123abc",
[polygon.id]: "wss://polygon-mainnet.g.alchemy.com/v2/123abc123abc",
[arbitrum.id]: "wss://arb-mainnet.g.alchemy.com/v2/123abc123abc",
},
];

const httpArgs: Parameters<typeof configurePublicClients> = [
chains,
undefined,
{
[mainnet.id]: "https://eth-mainnet.g.alchemy.com/v2/123abc123abc",
[optimism.id]: "https://opt-mainnet.g.alchemy.com/v2/123abc123abc",
[polygon.id]: "https://polygon-mainnet.g.alchemy.com/v2/123abc123abc",
[arbitrum.id]: "https://arb-mainnet.g.alchemy.com/v2/123abc123abc",
},
];

const webSocketClientsMap = configurePublicClients(...webSocketArgs);
const httpClientsMap = configurePublicClients(...httpArgs);

describe("Initializes Clients correctly", () => {
test("Resolves web socket url as expected", () => {
const mainnetTransport = webSocketClientsMap.get(mainnet.id);
expect(mainnetTransport).to.not.be.undefined;
const transportType = mainnetTransport?.transport.type;
expect(transportType).toBe("webSocket");
});

test("Resolves http url as expected", () => {
const mainnetTransport = httpClientsMap.get(mainnet.id);
expect(mainnetTransport).to.not.be.undefined;
const transportType = mainnetTransport?.transport.type;
expect(transportType).toBe("http");
});
});
15 changes: 4 additions & 11 deletions packages/sdk/test/utils/logger.test.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
import { expect, test, vi } from "vitest";
import { AcrossClient } from "../../src/client";
import { hardhat } from "viem/chains";

const client = AcrossClient.create({
useTestnet: true,
logLevel: "WARN",
chains: [hardhat],
});
import { testSDK } from "./_utils";

test("Higher severity is logged", () => {
const consoleLogSpy = vi.spyOn(console, "log");
client.logger.error("Should be logged");
testSDK.logger.error("Should be logged");

expect(consoleLogSpy).toHaveBeenCalled();
});

test("Equal severity is logged", () => {
const consoleLogSpy = vi.spyOn(console, "log");
client.logger.warn("Should be logged");
testSDK.logger.warn("Should be logged");

expect(consoleLogSpy).toHaveBeenCalled();
});

test("Lower severity is not logged", () => {
const consoleLogSpy = vi.spyOn(console, "log");
client.logger.debug("Should not be logged");
testSDK.logger.debug("Should not be logged");

expect(consoleLogSpy).not.toHaveBeenCalled();
});

0 comments on commit a086a2f

Please sign in to comment.