From 5d40c87c07f18117f89de61470b498c9e0fdf6a1 Mon Sep 17 00:00:00 2001 From: Matthias Kretschmann Date: Tue, 17 Dec 2019 13:24:46 +0100 Subject: [PATCH] provide global loadOcean method --- .vscode/settings.json | 3 -- client/src/context/UserProvider.tsx | 50 ++++++++++++++++++++++++----- client/src/ocean.ts | 30 +++-------------- 3 files changed, 46 insertions(+), 37 deletions(-) delete mode 100644 .vscode/settings.json diff --git a/.vscode/settings.json b/.vscode/settings.json deleted file mode 100644 index 55712c1..0000000 --- a/.vscode/settings.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "typescript.tsdk": "node_modules/typescript/lib" -} \ No newline at end of file diff --git a/client/src/context/UserProvider.tsx b/client/src/context/UserProvider.tsx index c72306d..3eb2350 100644 --- a/client/src/context/UserProvider.tsx +++ b/client/src/context/UserProvider.tsx @@ -1,15 +1,23 @@ import React, { PureComponent } from 'react' import Web3 from 'web3' -import { Ocean, Account } from '@oceanprotocol/squid' +import { Ocean, Account, Config } from '@oceanprotocol/squid' import { User } from '.' import { provideOcean, requestFromFaucet, FaucetResponse } from '../ocean' -import { nodeUri } from '../config' import MarketProvider from './MarketProvider' import { MetamaskProvider } from './MetamaskProvider' import { BurnerWalletProvider } from './BurnerWalletProvider' import { NetworkSwitcher } from '../components/molecules/NetworkSwitcher' +import { + aquariusUri, + brizoUri, + brizoAddress, + nodeUri, + secretStoreUri, + verbose +} from '../config' + const POLL_ACCOUNTS = 1000 // every 1s const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min const DEFAULT_WEB3 = new Web3(new Web3.providers.HttpProvider(nodeUri)) // default web3 @@ -31,9 +39,19 @@ interface UserProviderState { loginMetamask(): Promise loginBurnerWallet(): Promise logoutBurnerWallet(): Promise + loadOcean: (config: Config) => Promise message: string } +const oceanConfig = { + nodeUri, + aquariusUri, + brizoUri, + brizoAddress, + secretStoreUri, + verbose +} + export default class UserProvider extends PureComponent<{}, UserProviderState> { private loginMetamask = async () => { const metamaskProvider = new MetamaskProvider() @@ -46,7 +64,10 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> { web3 }, () => { - this.loadOcean() + this.loadOcean({ + web3Provider: this.state.web3, + ...oceanConfig + }) } ) } @@ -62,7 +83,10 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> { web3 }, () => { - this.loadOcean() + this.loadOcean({ + web3Provider: this.state.web3, + ...oceanConfig + }) } ) } @@ -89,6 +113,7 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> { loginMetamask: () => this.loginMetamask(), loginBurnerWallet: () => this.loginBurnerWallet(), logoutBurnerWallet: () => this.logoutBurnerWallet(), + loadOcean: (config: Config) => this.loadOcean(config), message: 'Connecting to Ocean...' } @@ -120,13 +145,19 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> { web3: DEFAULT_WEB3 }, () => { - this.loadOcean() + this.loadOcean({ + web3Provider: this.state.web3, + ...oceanConfig + }) } ) } - private loadOcean = async () => { - const { ocean } = await provideOcean(this.state.web3) + private loadOcean = async (config: Config) => { + const { ocean } = await provideOcean({ + web3Provider: this.state.web3, + ...config + }) this.setState({ ocean, isLoading: false }, () => { this.initNetworkPoll() this.initAccountsPoll() @@ -152,7 +183,10 @@ export default class UserProvider extends PureComponent<{}, UserProviderState> { web3 }, () => { - this.loadOcean() + this.loadOcean({ + web3Provider: this.state.web3, + ...oceanConfig + }) } ) } else { diff --git a/client/src/ocean.ts b/client/src/ocean.ts index d26b4d2..432bf48 100644 --- a/client/src/ocean.ts +++ b/client/src/ocean.ts @@ -1,30 +1,8 @@ -import { Ocean, Logger } from '@oceanprotocol/squid' -import Web3 from 'web3' -import { User } from './context' +import { Ocean, Logger, Config } from '@oceanprotocol/squid' +import { faucetUri } from './config' -import { - aquariusUri, - brizoUri, - brizoAddress, - faucetUri, - nodeUri, - secretStoreUri, - verbose -} from './config' - - - -export async function provideOcean(web3Provider: Web3) { - const config = { - web3Provider, - nodeUri, - aquariusUri, - brizoUri, - brizoAddress, - secretStoreUri, - verbose - } - const ocean: any = await Ocean.getInstance(config) +export async function provideOcean(config: Config) { + const ocean: Ocean = await Ocean.getInstance(config) return { ocean } }