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

274 lines
7.7 KiB
TypeScript
Raw Normal View History

2019-01-23 11:15:27 +01:00
import React, { Component } from 'react'
2019-01-30 17:33:56 +01:00
import Web3 from 'web3'
import { BrowserRouter as Router } from 'react-router-dom'
2019-02-27 15:39:41 +01:00
import { Logger } from '@oceanprotocol/squid'
2019-03-25 18:03:49 +01:00
import Header from './components/organisms/Header'
import Footer from './components/organisms/Footer'
2019-02-13 12:39:04 +01:00
import Spinner from './components/atoms/Spinner'
2019-01-30 17:33:56 +01:00
import { User } from './context/User'
import { provideOcean } from './ocean'
2019-01-30 17:33:56 +01:00
import Routes from './Routes'
2019-01-23 13:03:41 +01:00
import './styles/global.scss'
import styles from './App.module.scss'
2019-01-23 13:03:41 +01:00
2019-02-20 15:00:42 +01:00
import {
nodeHost,
nodePort,
nodeScheme,
faucetHost,
faucetPort,
faucetScheme
2019-04-04 11:30:31 +02:00
} from './config/config'
2019-01-30 17:33:56 +01:00
2019-04-09 21:51:22 +02:00
const POLL_ACCOUNTS = 1000 // every 1s
const POLL_NETWORK = POLL_ACCOUNTS * 60 // every 1 min
declare global {
interface Window {
2019-02-08 12:04:19 +01:00
web3: Web3
ethereum: any
}
}
2019-02-05 17:05:28 +01:00
interface AppState {
2019-02-05 16:00:22 +01:00
isLogged: boolean
isLoading: boolean
isWeb3: boolean
2019-04-05 16:07:46 +02:00
isNile: boolean
account: string
2019-03-04 16:06:52 +01:00
balance: {
eth: number
ocn: number
}
2019-03-04 11:58:52 +01:00
network: string
web3: Web3
2019-04-06 17:55:24 +02:00
ocean: any
requestFromFaucet(): void
message: string
2019-01-30 17:33:56 +01:00
}
2019-02-05 17:05:28 +01:00
class App extends Component<{}, AppState> {
private accountsInterval: any = null
private networkInterval: any = null
2019-02-05 17:05:28 +01:00
2019-02-20 15:00:42 +01:00
private requestFromFaucet = async () => {
try {
const url = `${faucetScheme}://${faucetHost}:${faucetPort}/faucet`
const response = await fetch(url, {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
address: this.state.account,
agent: 'commons'
})
})
return response.json()
} catch (error) {
Logger.log('requestFromFaucet', error)
2019-02-20 15:00:42 +01:00
}
}
2019-02-05 17:05:28 +01:00
public state = {
isLogged: false,
isLoading: true,
isWeb3: false,
2019-04-05 16:07:46 +02:00
isNile: false,
2019-03-04 16:06:52 +01:00
balance: {
eth: 0,
ocn: 0
},
2019-03-04 11:58:52 +01:00
network: '',
web3: new Web3(
new Web3.providers.HttpProvider(
`${nodeScheme}://${nodeHost}:${nodePort}`
)
),
account: '',
2019-04-06 17:55:24 +02:00
ocean: {} as any,
requestFromFaucet: this.requestFromFaucet,
message: 'Connecting to Ocean...'
2019-01-30 17:33:56 +01:00
}
public async componentDidMount() {
2019-04-09 21:51:22 +02:00
await this.bootstrap()
2019-04-12 18:00:44 +02:00
this.initAccountsPoll()
this.initNetworkPoll()
2019-01-30 17:33:56 +01:00
}
private bootstrap = async () => {
2019-04-06 17:55:24 +02:00
try {
//
// Start with Web3 detection
//
this.setState({ message: 'Setting up Web3...' })
// Modern dapp browsers
if (window.ethereum) {
window.web3 = new Web3(window.ethereum)
this.setState({ isWeb3: true })
}
// Legacy dapp browsers
else if (window.web3) {
window.web3 = new Web3(window.web3.currentProvider)
this.setState({ isWeb3: true })
}
// Non-dapp browsers
else {
this.setState({ isWeb3: false })
}
2019-04-12 15:43:20 +02:00
// Modern & legacy dapp browsers
if (this.state.isWeb3) {
2019-04-12 15:43:20 +02:00
//
// Detecting network with window.web3
//
let isNile
await window.web3.eth.net.getId((err, netId) => {
2019-04-12 15:43:20 +02:00
if (err) return
isNile = netId === 8995
2019-04-12 18:00:44 +02:00
const network = isNile ? 'Nile' : netId.toString()
if (
isNile !== this.state.isNile ||
network !== this.state.network
) {
this.setState({ isNile, network })
}
2019-04-12 15:43:20 +02:00
})
if (!isNile) {
window.web3 = this.state.web3
2019-04-12 15:43:20 +02:00
}
//
// Provide the Ocean
//
this.setState({ message: 'Connecting to Ocean...' })
const { ocean } = await provideOcean(window.web3)
2019-04-12 15:50:20 +02:00
this.setState({ ocean, isLoading: false })
2019-04-12 15:43:20 +02:00
2019-04-12 18:00:44 +02:00
// Set proper network names now that we have Ocean
this.fetchNetwork()
2019-04-12 18:00:44 +02:00
// Get accounts
this.fetchAccounts()
}
// Non-dapp browsers
else {
this.setState({ message: 'Connecting to Ocean...' })
2019-04-06 17:55:24 +02:00
const { ocean } = await provideOcean(this.state.web3)
this.setState({ ocean, isLoading: false })
this.fetchNetwork()
2019-01-30 17:33:56 +01:00
}
} catch (e) {
2019-04-06 17:55:24 +02:00
// error in bootstrap process
// show error connecting to ocean
Logger.log('web3 error', e)
2019-04-12 15:43:20 +02:00
this.setState({ isLoading: false })
}
2019-01-30 17:33:56 +01:00
}
2019-04-09 21:51:22 +02:00
private initAccountsPoll() {
if (!this.accountsInterval) {
2019-04-09 21:51:22 +02:00
this.accountsInterval = setInterval(
this.fetchAccounts,
POLL_ACCOUNTS
)
}
}
private initNetworkPoll() {
if (!this.networkInterval) {
2019-04-09 21:51:22 +02:00
this.networkInterval = setInterval(this.fetchNetwork, POLL_NETWORK)
}
}
private fetchAccounts = async () => {
const { ocean, isWeb3, isLogged, isNile } = this.state
if (isWeb3) {
// Modern dapp browsers
if (window.ethereum) {
if (!isLogged && isNile) {
try {
await window.ethereum.enable()
} catch (error) {
// User denied account access...
this.accountsInterval = null
return
}
}
}
2019-04-09 21:51:22 +02:00
const accounts = await ocean.accounts.list()
if (accounts.length > 0) {
const account = accounts[0].getId()
if (account !== this.state.account) {
2019-04-12 18:00:44 +02:00
this.setState({ account, isLogged: true })
}
const balance = await accounts[0].getBalance()
if (
balance.eth !== this.state.balance.eth ||
balance.ocn !== this.state.balance.ocn
) {
this.setState({ balance })
2019-04-09 21:51:22 +02:00
}
} else {
isLogged !== false &&
2019-04-09 21:51:22 +02:00
this.setState({ isLogged: false, account: '' })
}
}
}
private fetchNetwork = async () => {
const { ocean, isWeb3 } = this.state
2019-04-09 21:51:22 +02:00
if (isWeb3) {
2019-04-09 21:51:22 +02:00
const network = await ocean.keeper.getNetworkName()
const isNile = network === 'Nile'
network !== this.state.network && this.setState({ isNile, network })
}
}
public render() {
return (
<div className={styles.app}>
<User.Provider value={this.state}>
<Router>
<>
<Header />
2019-02-08 14:06:55 +01:00
2019-02-13 12:39:04 +01:00
<main className={styles.main}>
{this.state.isLoading ? (
2019-02-13 12:52:27 +01:00
<div className={styles.loader}>
<Spinner message={this.state.message} />
2019-02-13 12:52:27 +01:00
</div>
2019-02-13 12:39:04 +01:00
) : (
<Routes />
2019-02-13 12:39:04 +01:00
)}
</main>
2019-02-08 14:06:55 +01:00
<Footer />
</>
</Router>
</User.Provider>
</div>
)
}
}
2019-01-23 11:15:27 +01:00
export default App