Skip to content

Commit

Permalink
Merge pull request #207 from 1Hive/fix-fallback-httpprovider-when-wro…
Browse files Browse the repository at this point in the history
…ng-network

Reload on network change + Fallaback to http provider when wrong chainId
  • Loading branch information
Corantin authored Apr 6, 2022
2 parents a8a8c24 + e5ce5cf commit 95fe6f4
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 7 deletions.
4 changes: 2 additions & 2 deletions packages/react-app/src/networks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const networks = {
governAddress: '0xa0F5e6759d49063040eAB18c1B0E684C45a4B4cA',
governQueueAddress: '0x19B918802eA9C71c500Ca481917F383f3992cDB0',
celesteAddress: '0xdd58ebed3c36460939285a92807f90e3d3a26789',
httpProvider: 'https://rinkeby.infura.io/v3/',
httpProvider: 'https://rinkeby.infura.io/v3',
defaultToken: TOKENS.HoneyTest,
nativeToken: TOKENS.Ether,
isTestNetwork: true,
Expand All @@ -49,7 +49,7 @@ export const networks = {
defaultEthNode: 'https://xdai.poanetwork.dev/',
questFactory: HardhatDeployement[100]?.xdai.contracts.QuestFactory.address,
govern: '', // TODO : When govern will be on xDai
httpProvider: 'https://xdai.poanetwork.dev/',
httpProvider: 'https://xdai.poanetwork.dev',
defaultToken: TOKENS.Honey,
nativeToken: TOKENS.xDAI,
stableToken: TOKENS.xDAI,
Expand Down
17 changes: 12 additions & 5 deletions packages/react-app/src/utils/web3.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import { wrapError } from './errors.util';
import { Logger } from './logger';

const DEFAULT_LOCAL_CHAIN = 'private';
const ethOrWeb = (window as any).ethereum ?? (window as any).web3?.currentProvider;
ethOrWeb?.on('chainChanged', (_chainId: string) => window.location.reload());

export function getWeb3(): Web3 {
let ethereum: any = null;
Expand Down Expand Up @@ -158,13 +160,18 @@ export function fromBigNumber(bigNumber: BigNumber | string, decimals: number |
}

export function getDefaultProvider() {
const { httpProvider } = getNetwork();
let ethOrWeb = (window as any).ethereum ?? (window as any).web3;
if (!ethOrWeb) {
ethOrWeb = new Web3.providers.HttpProvider(`${httpProvider}/${env('INFURA_API_KEY')}`);
const { httpProvider, chainId: expectedChainId } = getNetwork();
let provider = ethOrWeb;
if (!provider || +provider.chainId !== +expectedChainId) {
const infuraId = env('INFURA_API_KEY');
if (infuraId) {
provider = new Web3.providers.HttpProvider(`${httpProvider}/${infuraId}`);
} else {
throw new Error(`No http provider key provided in env`);
}
}

return ethOrWeb && new ethersUtil.providers.Web3Provider(ethOrWeb);
return provider && new ethersUtil.providers.Web3Provider(provider);
}

// Re-export some web3-utils functions
Expand Down
1 change: 1 addition & 0 deletions packages/react-app/template.env
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
REACT_APP_INFURA_API_KEY=[INFURA_ID]

1 comment on commit 95fe6f4

@vercel
Copy link

@vercel vercel bot commented on 95fe6f4 Apr 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

quests – ./

quests-1hive.vercel.app
quests.vercel.app
quests-git-main-1hive.vercel.app

Please sign in to comment.