This repository has been archived by the owner on Feb 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* adds custom events * adds react-toastify to sandbox * InitialState -> WalletProvider * adds more information to event * updates event with title and type * moved event listener to events hook * renames back to useListeners * updates provider listener to update when account/chain is changed * remove tech debt * code cleanup * updates README
- Loading branch information
Showing
21 changed files
with
303 additions
and
149 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { useRef, useEffect } from "react" | ||
import { toast } from "react-toastify" | ||
import { PROVIDER_EVENT, WalletProviderEvent } from '@decent-org/wallet-provider'; | ||
|
||
export function useEvents() { | ||
const isMountedRef = useRef(false) | ||
|
||
useEffect(() => { | ||
const providerEvent = (event: CustomEventInit<WalletProviderEvent>) => { | ||
toast[event.detail!.type](event.detail!.message) | ||
} | ||
if(isMountedRef.current) { | ||
window.addEventListener(PROVIDER_EVENT, providerEvent) | ||
} | ||
isMountedRef.current = true | ||
return () => { | ||
window.removeEventListener(PROVIDER_EVENT, providerEvent) | ||
} | ||
}, []) | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,15 @@ | ||
import { InitialState } from '../types'; | ||
import { WalletProvider } from '../types'; | ||
|
||
export enum Web3ProviderActions { | ||
CONNECT = 'CONNECT_WALLET', | ||
DISCONNECT = 'DISCONNECT', | ||
} | ||
|
||
export type ActionTypes = { | ||
type: Web3ProviderActions.CONNECT; | ||
payload: InitialState; | ||
}; | ||
export type ActionTypes = | ||
| { | ||
type: Web3ProviderActions.CONNECT; | ||
payload: WalletProvider; | ||
} | ||
| { | ||
type: Web3ProviderActions.DISCONNECT; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export const PROVIDER_EVENT = 'provider-event'; | ||
export const CHAIN_CHANGED = 'chainChanged'; | ||
export const ACCOUNT_CHANGED = 'accountsChanged'; | ||
export const UNSUPPORTED_CHAIN_IDS = 'UNSUPPORTED_CHAIN_IDS'; | ||
export const CONNECT = 'connect'; | ||
export const DISCONNECT = 'disconnect'; | ||
export const UNSUPPORTED_CHAINS_IDS_MESSAGE = 'Switch to a supported chain:'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './events'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import { | ||
PROVIDER_EVENT, | ||
UNSUPPORTED_CHAINS_IDS_MESSAGE, | ||
UNSUPPORTED_CHAIN_IDS, | ||
} from '../constants'; | ||
import type { WalletProviderEvent } from '../types'; | ||
|
||
export function emitUnsupportedChainEvent(event: string, supportedChainIds: string) { | ||
const unsupportChainIdEvent = new CustomEvent<WalletProviderEvent>(PROVIDER_EVENT, { | ||
detail: { | ||
type: 'warn', | ||
title: UNSUPPORTED_CHAIN_IDS, | ||
message: `${UNSUPPORTED_CHAINS_IDS_MESSAGE}: ${supportedChainIds}`, | ||
}, | ||
}); | ||
window.dispatchEvent(unsupportChainIdEvent); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,2 @@ | ||
import { ethers, getDefaultProvider } from 'ethers'; | ||
import { DWPConfig, InitialState, ProviderApiKeys } from '../types/index'; | ||
|
||
export const getProviderInfo = async (_provider: any, config: DWPConfig) => { | ||
const provider = new ethers.providers.Web3Provider(_provider); | ||
const network = await provider.getNetwork(); | ||
const signer = provider.getSigner(); | ||
const local = config.localChainId && network.chainId === parseInt(config.localChainId, 10); | ||
const account = (await signer.getAddress()) || null; | ||
return { | ||
account: account, | ||
signerOrProvider: signer, | ||
provider: provider, | ||
connectionType: !account ? 'fallback' : 'injected provider', | ||
network: local ? 'localhost' : network.name, | ||
chainId: network.chainId, | ||
}; | ||
}; | ||
|
||
export const getLocalProvider = (config: DWPConfig): Promise<InitialState> => { | ||
const localProvider = new ethers.providers.JsonRpcProvider(config.localProviderURL); | ||
return new Promise<InitialState>((resolve, reject) => { | ||
localProvider | ||
.detectNetwork() | ||
.then(network => { | ||
resolve({ | ||
account: null, | ||
provider: localProvider, | ||
signerOrProvider: localProvider, | ||
connectionType: 'local provider', | ||
network: 'localhost', | ||
chainId: network.chainId, | ||
}); | ||
}) | ||
.catch(reject); | ||
}); | ||
}; | ||
|
||
export const getFallbackProvider = (config: DWPConfig): InitialState => { | ||
const providerApiKeys: ProviderApiKeys = {}; | ||
if (config.providerKeys.infura) providerApiKeys.infura = config.providerKeys.infura; | ||
if (config.providerKeys.alchemy) providerApiKeys.alchemy = config.providerKeys.alchemy; | ||
if (config.providerKeys.etherscan) providerApiKeys.etherscan = config.providerKeys.etherscan; | ||
|
||
const network = ethers.providers.getNetwork(parseInt(config.fallbackChainId || '0', 10)); | ||
const defaultProvider = getDefaultProvider(network, providerApiKeys); | ||
|
||
return { | ||
account: null, | ||
provider: defaultProvider, | ||
signerOrProvider: defaultProvider, | ||
connectionType: 'readonly provider', | ||
network: defaultProvider.network.name, | ||
chainId: defaultProvider.network.chainId, | ||
}; | ||
}; | ||
export * from './events'; | ||
export * from './providers'; |
Oops, something went wrong.