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'
|
2019-02-08 13:29:29 +01:00
|
|
|
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'
|
2019-02-01 13:01:36 +01:00
|
|
|
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'
|
2019-02-08 13:29:29 +01:00
|
|
|
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
|
|
|
|
|
2019-02-08 11:44:02 +01:00
|
|
|
declare global {
|
|
|
|
interface Window {
|
2019-02-08 12:04:19 +01:00
|
|
|
web3: Web3
|
2019-02-08 11:44:02 +01:00
|
|
|
ethereum: any
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
interface AppState {
|
2019-02-05 16:00:22 +01:00
|
|
|
isLogged: boolean
|
2019-02-08 11:44:02 +01:00
|
|
|
isLoading: boolean
|
2019-02-11 16:53:55 +01:00
|
|
|
isWeb3: boolean
|
2019-04-05 16:07:46 +02:00
|
|
|
isNile: boolean
|
2019-02-12 16:23:45 +01:00
|
|
|
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
|
2019-02-08 11:44:02 +01:00
|
|
|
web3: Web3
|
2019-04-06 17:55:24 +02:00
|
|
|
ocean: any
|
2019-02-05 16:00:22 +01:00
|
|
|
startLogin: () => void
|
2019-04-03 18:01:00 +02:00
|
|
|
message: string
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
class App extends Component<{}, AppState> {
|
2019-04-09 21:51:22 +02:00
|
|
|
private accountsInterval: any
|
|
|
|
private networkInterval: any
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
public startLogin = (event?: any) => {
|
|
|
|
if (event) {
|
|
|
|
event.preventDefault()
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
2019-02-05 17:05:28 +01:00
|
|
|
this.startLoginProcess()
|
|
|
|
}
|
|
|
|
|
2019-02-20 15:00:42 +01:00
|
|
|
private requestFromFaucet = async () => {
|
|
|
|
if (this.state.account !== '') {
|
|
|
|
try {
|
2019-04-01 17:15:59 +02:00
|
|
|
const response = await fetch(
|
2019-02-20 15:00:42 +01:00
|
|
|
`${faucetScheme}://${faucetHost}:${faucetPort}/faucet`,
|
|
|
|
{
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
Accept: 'application/json',
|
|
|
|
'Content-Type': 'application/json'
|
|
|
|
},
|
|
|
|
body: JSON.stringify({
|
|
|
|
address: this.state.account,
|
2019-04-03 14:40:31 +02:00
|
|
|
agent: 'commons'
|
2019-02-20 15:00:42 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
2019-04-01 17:15:59 +02:00
|
|
|
|
|
|
|
return response.json()
|
2019-02-20 15:00:42 +01:00
|
|
|
} catch (error) {
|
2019-03-04 11:58:52 +01:00
|
|
|
Logger.log('requestFromFaucet', error)
|
2019-02-20 15:00:42 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no account found
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
public state = {
|
|
|
|
isLogged: false,
|
2019-02-08 11:44:02 +01:00
|
|
|
isLoading: true,
|
2019-02-11 16:53:55 +01:00
|
|
|
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: '',
|
2019-02-08 11:44:02 +01:00
|
|
|
web3: new Web3(
|
|
|
|
new Web3.providers.HttpProvider(
|
|
|
|
`${nodeScheme}://${nodeHost}:${nodePort}`
|
|
|
|
)
|
|
|
|
),
|
2019-02-12 16:23:45 +01:00
|
|
|
account: '',
|
2019-04-06 17:55:24 +02:00
|
|
|
ocean: {} as any,
|
2019-02-20 15:00:42 +01:00
|
|
|
startLogin: this.startLogin,
|
2019-04-03 18:01:00 +02:00
|
|
|
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 15:43:20 +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 {
|
|
|
|
if (window.web3) {
|
2019-04-12 15:43:20 +02:00
|
|
|
let web3provider = new Web3(window.web3.currentProvider)
|
|
|
|
this.setState({
|
|
|
|
isWeb3: true,
|
|
|
|
message: 'Setting up Web3...'
|
|
|
|
})
|
|
|
|
|
|
|
|
//
|
|
|
|
// Detecting network with window.web3
|
|
|
|
//
|
|
|
|
let isNile
|
|
|
|
|
|
|
|
await web3provider.eth.net.getId((err, netId) => {
|
|
|
|
if (err) return
|
|
|
|
|
|
|
|
isNile = netId === 8995
|
|
|
|
|
|
|
|
this.setState({ isNile, network: netId.toString() })
|
|
|
|
})
|
|
|
|
|
|
|
|
if (!isNile) {
|
|
|
|
web3provider = this.state.web3
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Provide the Ocean
|
|
|
|
//
|
|
|
|
this.setState({ message: 'Connecting to Ocean...' })
|
|
|
|
|
|
|
|
const { ocean } = await provideOcean(this.state.web3)
|
|
|
|
|
|
|
|
// Get accounts
|
2019-04-06 17:55:24 +02:00
|
|
|
const accounts = await ocean.accounts.list()
|
2019-04-12 15:43:20 +02:00
|
|
|
|
2019-01-30 17:33:56 +01:00
|
|
|
if (accounts.length > 0) {
|
2019-04-06 17:55:24 +02:00
|
|
|
const balance = await accounts[0].getBalance()
|
2019-02-08 12:04:19 +01:00
|
|
|
this.setState({
|
2019-01-30 17:33:56 +01:00
|
|
|
isLogged: true,
|
2019-04-06 17:55:24 +02:00
|
|
|
ocean,
|
|
|
|
balance,
|
|
|
|
account: accounts[0].getId(),
|
2019-04-08 10:44:03 +02:00
|
|
|
isLoading: false
|
2019-04-06 17:55:24 +02:00
|
|
|
})
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
2019-04-06 17:55:24 +02:00
|
|
|
} else {
|
2019-04-12 15:43:20 +02:00
|
|
|
//
|
|
|
|
// No Web3 browser
|
|
|
|
//
|
2019-04-06 17:55:24 +02:00
|
|
|
const { ocean } = await provideOcean(this.state.web3)
|
|
|
|
const network = await ocean.keeper.getNetworkName()
|
|
|
|
const isNile = network === 'Nile'
|
|
|
|
this.setState({
|
|
|
|
isNile,
|
|
|
|
ocean,
|
|
|
|
network,
|
|
|
|
isLoading: false
|
|
|
|
})
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
2019-02-11 16:53:55 +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-02-11 16:53:55 +01:00
|
|
|
}
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
2019-02-08 13:29:29 +01:00
|
|
|
|
2019-04-09 21:51:22 +02:00
|
|
|
private initAccountsPoll() {
|
|
|
|
if (!this.accountsInterval) {
|
|
|
|
this.accountsInterval = setInterval(
|
|
|
|
this.fetchAccounts,
|
|
|
|
POLL_ACCOUNTS
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private initNetworkPoll() {
|
|
|
|
if (!this.networkInterval) {
|
|
|
|
this.networkInterval = setInterval(this.fetchNetwork, POLL_NETWORK)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fetchAccounts = async () => {
|
|
|
|
const { web3 } = window
|
|
|
|
const { ocean } = this.state
|
|
|
|
|
|
|
|
if (web3) {
|
|
|
|
const accounts = await ocean.accounts.list()
|
|
|
|
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
const account = accounts[0].getId()
|
|
|
|
|
|
|
|
if (account !== this.state.account) {
|
|
|
|
const balance = await accounts[0].getBalance()
|
|
|
|
this.setState({ account, balance, isLogged: true })
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.state.isLogged !== false &&
|
|
|
|
this.setState({ isLogged: false, account: '' })
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
this.state.isWeb3 !== false &&
|
|
|
|
this.setState({
|
|
|
|
isWeb3: false,
|
|
|
|
isLogged: false
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fetchNetwork = async () => {
|
|
|
|
const { web3 } = window
|
|
|
|
const { ocean } = this.state
|
|
|
|
|
|
|
|
if (web3) {
|
|
|
|
const network = await ocean.keeper.getNetworkName()
|
|
|
|
const isNile = network === 'Nile'
|
|
|
|
|
|
|
|
network !== this.state.network && this.setState({ isNile, network })
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private startLoginProcess = async () => {
|
|
|
|
try {
|
|
|
|
if (this.state.isWeb3 && window.ethereum) {
|
|
|
|
await window.ethereum.enable()
|
|
|
|
const accounts = await this.state.ocean.accounts.list()
|
|
|
|
if (accounts.length > 0) {
|
|
|
|
const balance = await accounts[0].getBalance()
|
|
|
|
this.setState({
|
|
|
|
isLogged: true,
|
|
|
|
balance,
|
|
|
|
account: accounts[0].getId()
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// not unlocked
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no metamask/mist, show installation guide!
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
Logger.log('error logging', e)
|
|
|
|
// error in logging process
|
|
|
|
// show error
|
|
|
|
// rerun bootstrap process?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-08 13:29:29 +01:00
|
|
|
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}>
|
2019-04-03 18:01:00 +02:00
|
|
|
<Spinner message={this.state.message} />
|
2019-02-13 12:52:27 +01:00
|
|
|
</div>
|
2019-02-13 12:39:04 +01:00
|
|
|
) : (
|
2019-02-11 16:53:55 +01:00
|
|
|
<Routes />
|
2019-02-13 12:39:04 +01:00
|
|
|
)}
|
|
|
|
</main>
|
2019-02-08 14:06:55 +01:00
|
|
|
|
2019-02-08 13:29:29 +01:00
|
|
|
<Footer />
|
|
|
|
</>
|
|
|
|
</Router>
|
|
|
|
</User.Provider>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
2019-01-18 17:34:40 +01:00
|
|
|
}
|
|
|
|
|
2019-01-23 11:15:27 +01:00
|
|
|
export default App
|