From 1413a9754d3999e39548137dd9a0e40a1e807b4a Mon Sep 17 00:00:00 2001 From: spsjvc Date: Thu, 21 Nov 2024 14:16:13 +0100 Subject: [PATCH] fix: use custom transport url for ethers provider (#226) --- src/ethers-compat/publicClientToProvider.ts | 5 ++- .../publicClientToProvider.unit.test.ts | 33 +++++++++++++++++++ 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 src/ethers-compat/publicClientToProvider.unit.test.ts diff --git a/src/ethers-compat/publicClientToProvider.ts b/src/ethers-compat/publicClientToProvider.ts index 6f6dfd8b..a0b1cc2d 100644 --- a/src/ethers-compat/publicClientToProvider.ts +++ b/src/ethers-compat/publicClientToProvider.ts @@ -17,5 +17,8 @@ export function publicClientToProvider( ensAddress: chain.contracts?.ensRegistry?.address, }; - return new providers.StaticJsonRpcProvider(chain.rpcUrls.default.http[0], network); + const transportUrl = publicClient.transport.url as string | undefined; + const url = transportUrl ?? chain.rpcUrls.default.http[0]; + + return new providers.StaticJsonRpcProvider(url, network); } diff --git a/src/ethers-compat/publicClientToProvider.unit.test.ts b/src/ethers-compat/publicClientToProvider.unit.test.ts new file mode 100644 index 00000000..9702d27d --- /dev/null +++ b/src/ethers-compat/publicClientToProvider.unit.test.ts @@ -0,0 +1,33 @@ +import { it, expect } from 'vitest'; +import { createPublicClient, http } from 'viem'; +import { arbitrumSepolia } from 'viem/chains'; + +import { publicClientToProvider } from './publicClientToProvider'; + +it(`successfully converts PublicClient to Provider`, () => { + const publicClient = createPublicClient({ + chain: arbitrumSepolia, + transport: http(), + }); + + const provider = publicClientToProvider(publicClient); + + expect(provider.network.chainId).toEqual(publicClient.chain.id); + expect(provider.network.name).toEqual(publicClient.chain.name); + + expect(provider.connection.url).toEqual('https://sepolia-rollup.arbitrum.io/rpc'); +}); + +it(`successfully converts PublicClient to Provider (custom Transport)`, () => { + const publicClient = createPublicClient({ + chain: arbitrumSepolia, + transport: http('https://arbitrum-sepolia.gateway.tenderly.co'), + }); + + const provider = publicClientToProvider(publicClient); + + expect(provider.network.chainId).toEqual(publicClient.chain.id); + expect(provider.network.name).toEqual(publicClient.chain.name); + + expect(provider.connection.url).toEqual('https://arbitrum-sepolia.gateway.tenderly.co'); +});