mirror of
https://github.com/oceanprotocol/commons.git
synced 2023-03-15 18:03:00 +01:00
leaner bootstrap, remove window any casting
This commit is contained in:
parent
513d615df3
commit
b968952ef4
97
src/App.tsx
97
src/App.tsx
@ -8,9 +8,17 @@ import './styles/global.scss'
|
|||||||
|
|
||||||
import { nodeHost, nodePort, nodeScheme } from './config'
|
import { nodeHost, nodePort, nodeScheme } from './config'
|
||||||
|
|
||||||
|
declare global {
|
||||||
|
interface Window {
|
||||||
|
web3: Web3,
|
||||||
|
ethereum: any
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
interface AppState {
|
interface AppState {
|
||||||
isLogged: boolean
|
isLogged: boolean
|
||||||
web3: any
|
isLoading: boolean
|
||||||
|
web3: Web3
|
||||||
ocean: {}
|
ocean: {}
|
||||||
startLogin: () => void
|
startLogin: () => void
|
||||||
}
|
}
|
||||||
@ -25,7 +33,12 @@ class App extends Component<{}, AppState> {
|
|||||||
|
|
||||||
public state = {
|
public state = {
|
||||||
isLogged: false,
|
isLogged: false,
|
||||||
web3: {},
|
isLoading: true,
|
||||||
|
web3: new Web3(
|
||||||
|
new Web3.providers.HttpProvider(
|
||||||
|
`${nodeScheme}://${nodeHost}:${nodePort}`
|
||||||
|
)
|
||||||
|
),
|
||||||
ocean: {},
|
ocean: {},
|
||||||
startLogin: this.startLogin
|
startLogin: this.startLogin
|
||||||
}
|
}
|
||||||
@ -45,62 +58,54 @@ class App extends Component<{}, AppState> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private startLoginProcess = async () => {
|
private startLoginProcess = async () => {
|
||||||
if ((window as any).web3) {
|
this.setState({isLoading: true})
|
||||||
const web3 = new Web3((window as any).web3.currentProvider)
|
if (window.web3) {
|
||||||
|
const web3 = new Web3(window.web3.currentProvider)
|
||||||
try {
|
try {
|
||||||
const accounts = await web3.eth.getAccounts()
|
const accounts = await web3.eth.getAccounts()
|
||||||
if (accounts.length === 0 && (window as any).ethereum) {
|
if (accounts.length > 0) {
|
||||||
await (window as any).ethereum.enable()
|
this.setState({
|
||||||
const { ocean } = await provideOcean()
|
|
||||||
this.setState(state => ({
|
|
||||||
isLogged: true,
|
isLogged: true,
|
||||||
web3,
|
web3
|
||||||
ocean
|
})
|
||||||
}))
|
|
||||||
} else {
|
} else {
|
||||||
|
if (accounts.length === 0 && window.ethereum) {
|
||||||
|
await window.ethereum.enable()
|
||||||
|
this.setState({
|
||||||
|
isLogged: true,
|
||||||
|
web3
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (e) {
|
||||||
|
// something went wrong, show error?
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// no metamask/mist, show installation guide!
|
||||||
|
}
|
||||||
|
this.setState({isLoading: false})
|
||||||
|
}
|
||||||
|
|
||||||
|
private bootstrap = async () => {
|
||||||
|
if (window.web3) {
|
||||||
|
const web3 = new Web3(window.web3.currentProvider)
|
||||||
|
try {
|
||||||
|
const accounts = await web3.eth.getAccounts()
|
||||||
|
if (accounts.length > 0) {
|
||||||
this.setState(state => ({
|
this.setState(state => ({
|
||||||
isLogged: true,
|
isLogged: true,
|
||||||
web3
|
web3
|
||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
this.setDefaultProvider()
|
// continue with default
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
this.setDefaultProvider()
|
|
||||||
}
|
}
|
||||||
}
|
const { ocean } = await provideOcean()
|
||||||
|
this.setState({
|
||||||
private bootstrap = async () => {
|
isLoading: false,
|
||||||
if ((window as any).web3) {
|
ocean
|
||||||
const web3 = new Web3((window as any).web3.currentProvider)
|
})
|
||||||
try {
|
|
||||||
const accounts = await web3.eth.getAccounts()
|
|
||||||
if (accounts.length > 0) {
|
|
||||||
const { ocean } = await provideOcean()
|
|
||||||
this.setState(state => ({
|
|
||||||
isLogged: true,
|
|
||||||
web3,
|
|
||||||
ocean
|
|
||||||
}))
|
|
||||||
}
|
|
||||||
} catch (e) {
|
|
||||||
this.setDefaultProvider()
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
this.setDefaultProvider()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private setDefaultProvider = () => {
|
|
||||||
this.setState(state => ({
|
|
||||||
isLogged: false,
|
|
||||||
web3: new Web3(
|
|
||||||
new Web3.providers.HttpProvider(
|
|
||||||
`${nodeScheme}://${nodeHost}:${nodePort}`
|
|
||||||
)
|
|
||||||
)
|
|
||||||
}))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2,6 +2,7 @@ import React from 'react'
|
|||||||
|
|
||||||
export const User = React.createContext({
|
export const User = React.createContext({
|
||||||
isLogged: false,
|
isLogged: false,
|
||||||
|
isLoading: false,
|
||||||
web3: {},
|
web3: {},
|
||||||
ocean: {},
|
ocean: {},
|
||||||
startLogin: () => {
|
startLogin: () => {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user