Skip to content

Commit

Permalink
fix: use custom transport url for ethers provider
Browse files Browse the repository at this point in the history
  • Loading branch information
spsjvc committed Nov 21, 2024
1 parent 729facd commit 84f97a6
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/ethers-compat/publicClientToProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@ export function publicClientToProvider<TChain extends Chain | undefined>(
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);
}
33 changes: 33 additions & 0 deletions src/ethers-compat/publicClientToProvider.unit.test.ts
Original file line number Diff line number Diff line change
@@ -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');
});

0 comments on commit 84f97a6

Please sign in to comment.