Skip to content

feat: ethers skipFetchSetup support #2085

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Dec 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions .changeset/great-experts-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
"@thirdweb-dev/sdk": patch
---

Fix error when fetching data on `vite-node` environment by setting a global variable `TW_SKIP_FETCH_SETUP` to `true`. Fixes https://github.com/thirdweb-dev/js/issues/2002

Setting this flag sets `skipFetchSetup` to `true` in [ethers ConnectionInfo](https://docs.ethers.org/v5/api/utils/web/#ConnectionInfo)

```ts
// set this global variable
globalThis.TW_SKIP_FETCH_SETUP = true;

// use the thirdweb sdk...
```
12 changes: 12 additions & 0 deletions packages/sdk/src/evm/constants/urls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,16 @@ export function getProviderFromRpcUrl(
if (existingProvider) {
return existingProvider;
}

// TODO: remove below `skipFetchSetup` logic when ethers.js v6 support arrives
let _skipFetchSetup = false;
if (
typeof globalThis !== "undefined" &&
"TW_SKIP_FETCH_SETUP" in globalThis &&
typeof (globalThis as any).TW_SKIP_FETCH_SETUP === "boolean"
) {
_skipFetchSetup = (globalThis as any).TW_SKIP_FETCH_SETUP as boolean;
}

// Otherwise, create a new provider on the specific network
const newProvider = chainId
Expand All @@ -292,13 +302,15 @@ export function getProviderFromRpcUrl(
{
url: rpcUrl,
headers,
skipFetchSetup: _skipFetchSetup,
},
chainId,
)
: // Otherwise fall back to the built in json rpc batch provider
new providers.JsonRpcBatchProvider({
url: rpcUrl,
headers,
skipFetchSetup: _skipFetchSetup,
});

// Save the provider in our cache
Expand Down