config based on network

This commit is contained in:
mihaisc 2020-08-06 16:39:41 +03:00
parent 4bff015c5d
commit 1f7be93bf6
1 changed files with 9 additions and 21 deletions

View File

@ -28,7 +28,7 @@ interface OceanProviderValue {
balance: Balance balance: Balance
chainId: number | undefined chainId: number | undefined
status: ProviderStatus status: ProviderStatus
connect: (opts?: Partial<ICoreOptions>) => Promise<void> connect: (config: Config) => Promise<void>
logout: () => Promise<void> logout: () => Promise<void>
refreshBalance: () => Promise<void> refreshBalance: () => Promise<void>
} }
@ -36,12 +36,14 @@ interface OceanProviderValue {
const OceanContext = createContext(null) const OceanContext = createContext(null)
function OceanProvider({ function OceanProvider({
config, initialConfig,
web3ModalOpts, web3ModalOpts,
handleNetworkChanged,
children children
}: { }: {
config: Config initialConfig: Config
web3ModalOpts?: Partial<ICoreOptions> web3ModalOpts?: Partial<ICoreOptions>
handleNetworkChanged: (networkId: string | number)=>Promise<void>
children: any children: any
}): ReactElement { }): ReactElement {
const [web3, setWeb3] = useState<Web3 | undefined>() const [web3, setWeb3] = useState<Web3 | undefined>()
@ -51,6 +53,7 @@ function OceanProvider({
const [chainId, setChainId] = useState<number | undefined>() const [chainId, setChainId] = useState<number | undefined>()
const [account, setAccount] = useState<Account | undefined>() const [account, setAccount] = useState<Account | undefined>()
const [accountId, setAccountId] = useState<string | undefined>() const [accountId, setAccountId] = useState<string | undefined>()
const [config, setConfig] = useState<Config>(initialConfig)
const [balance, setBalance] = useState<Balance | undefined>({ const [balance, setBalance] = useState<Balance | undefined>({
eth: undefined, eth: undefined,
ocean: undefined ocean: undefined
@ -85,10 +88,12 @@ function OceanProvider({
web3Modal.cachedProvider && connect() web3Modal.cachedProvider && connect()
}, [web3Modal]) }, [web3Modal])
async function connect() { async function connect(newConfig?: Config) {
try { try {
Logger.log('Connecting ...') Logger.log('Connecting ...')
newConfig && setConfig(newConfig)
const provider = await web3Modal.connect() const provider = await web3Modal.connect()
setWeb3Provider(provider) setWeb3Provider(provider)
@ -131,28 +136,11 @@ function OceanProvider({
web3Modal.clearCachedProvider() web3Modal.clearCachedProvider()
} }
//
// Listen for provider, account & network changes
// and react to it
//
// const handleConnect = async (provider: any) => {
// Logger.debug("Handling 'connect' event with payload", provider)
// }
const handleAccountsChanged = async (accounts: string[]) => { const handleAccountsChanged = async (accounts: string[]) => {
Logger.debug("Handling 'accountsChanged' event with payload", accounts) Logger.debug("Handling 'accountsChanged' event with payload", accounts)
connect() connect()
} }
const handleNetworkChanged = async (networkId: string | number) => {
Logger.debug(
"Handling 'chainChanged' event with payload",
networkId,
status
)
connect()
}
// TODO: #68 Refetch balance periodically, or figure out some event to subscribe to // TODO: #68 Refetch balance periodically, or figure out some event to subscribe to