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'
|
|
|
|
import Header from './components/Header'
|
|
|
|
import Footer from './components/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-05 16:00:22 +01:00
|
|
|
import { nodeHost, nodePort, nodeScheme } from './config'
|
2019-01-30 17:33:56 +01:00
|
|
|
|
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-02-12 16:23:45 +01:00
|
|
|
account: string
|
2019-02-08 11:44:02 +01:00
|
|
|
web3: Web3
|
2019-02-05 16:00:22 +01:00
|
|
|
ocean: {}
|
|
|
|
startLogin: () => void
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
|
2019-02-05 17:05:28 +01:00
|
|
|
class App extends Component<{}, AppState> {
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
|
|
|
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-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-02-05 17:05:28 +01:00
|
|
|
ocean: {},
|
|
|
|
startLogin: this.startLogin
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public async componentDidMount() {
|
|
|
|
this.bootstrap()
|
|
|
|
}
|
2019-01-18 17:34:40 +01:00
|
|
|
|
2019-01-30 17:33:56 +01:00
|
|
|
private startLoginProcess = async () => {
|
2019-02-08 11:44:02 +01:00
|
|
|
if (window.web3) {
|
|
|
|
const web3 = new Web3(window.web3.currentProvider)
|
2019-01-30 17:33:56 +01:00
|
|
|
try {
|
|
|
|
const accounts = await web3.eth.getAccounts()
|
2019-02-08 11:44:02 +01:00
|
|
|
if (accounts.length > 0) {
|
|
|
|
this.setState({
|
2019-01-30 17:33:56 +01:00
|
|
|
isLogged: true,
|
2019-02-11 16:53:55 +01:00
|
|
|
isWeb3: true,
|
2019-02-12 16:23:45 +01:00
|
|
|
account: accounts[0],
|
2019-01-30 17:33:56 +01:00
|
|
|
web3
|
2019-02-08 11:44:02 +01:00
|
|
|
})
|
|
|
|
} else {
|
|
|
|
if (accounts.length === 0 && window.ethereum) {
|
|
|
|
await window.ethereum.enable()
|
2019-02-13 14:21:47 +01:00
|
|
|
const newAccounts = await web3.eth.getAccounts()
|
2019-02-13 14:24:19 +01:00
|
|
|
if (newAccounts.length > 0) {
|
2019-02-13 14:21:47 +01:00
|
|
|
this.setState({
|
|
|
|
isLogged: true,
|
|
|
|
isWeb3: true,
|
|
|
|
account: newAccounts[0],
|
|
|
|
web3
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
// failed to unlock
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// no unlock procedure
|
2019-02-08 11:44:02 +01:00
|
|
|
}
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-02-08 11:44:02 +01:00
|
|
|
// something went wrong, show error?
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
} else {
|
2019-02-08 11:44:02 +01:00
|
|
|
// no metamask/mist, show installation guide!
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bootstrap = async () => {
|
2019-02-08 11:44:02 +01:00
|
|
|
if (window.web3) {
|
2019-02-11 16:53:55 +01:00
|
|
|
this.setState({ isWeb3: true })
|
2019-02-08 11:44:02 +01:00
|
|
|
const web3 = new Web3(window.web3.currentProvider)
|
2019-01-30 17:33:56 +01:00
|
|
|
try {
|
|
|
|
const accounts = await web3.eth.getAccounts()
|
|
|
|
if (accounts.length > 0) {
|
2019-02-08 12:04:19 +01:00
|
|
|
this.setState({
|
2019-01-30 17:33:56 +01:00
|
|
|
isLogged: true,
|
2019-02-12 16:23:45 +01:00
|
|
|
account: accounts[0],
|
2019-02-08 11:44:02 +01:00
|
|
|
web3
|
2019-02-08 12:04:19 +01:00
|
|
|
})
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-02-08 11:44:02 +01:00
|
|
|
// continue with default
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
|
|
|
}
|
2019-02-11 16:53:55 +01:00
|
|
|
try {
|
|
|
|
const { ocean } = await provideOcean()
|
|
|
|
this.setState({
|
|
|
|
isLoading: false,
|
|
|
|
ocean
|
|
|
|
})
|
|
|
|
} catch (e) {
|
|
|
|
// show loading error / unable to initialize ocean
|
|
|
|
this.setState({
|
|
|
|
isLoading: false
|
|
|
|
})
|
|
|
|
}
|
2019-01-30 17:33:56 +01:00
|
|
|
}
|
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-02-13 13:04:11 +01:00
|
|
|
<Spinner message="Connecting to Ocean..." />
|
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
|