1
0
mirror of https://github.com/oceanprotocol/commons.git synced 2023-03-15 18:03:00 +01:00

provide global loadOcean method

This commit is contained in:
Matthias Kretschmann 2019-12-17 13:24:46 +01:00
parent 125df58464
commit 5d40c87c07
Signed by: m
GPG Key ID: 606EEEF3C479A91F
3 changed files with 46 additions and 37 deletions

View File

@ -1,3 +0,0 @@
{
"typescript.tsdk": "node_modules/typescript/lib"
}

View File

@ -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<any>
loginBurnerWallet(): Promise<any>
logoutBurnerWallet(): Promise<any>
loadOcean: (config: Config) => Promise<Ocean | void>
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 {

View File

@ -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 }
}