Skip to content

Commit

Permalink
feat: add a fail over endpoint for the nodeos
Browse files Browse the repository at this point in the history
  • Loading branch information
adriexnet committed Sep 12, 2020
1 parent 3ecbd2b commit c49da77
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 49 deletions.
5 changes: 4 additions & 1 deletion .env.example
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
PORT=3000
REACT_APP_EOS_API_HOST=api.jungle3.alohaeos.com
REACT_APP_EOS_API_HOST=jungle.eosio.cr
REACT_APP_EOS_API_PORT=443
REACT_APP_EOS_API_PROTOCOL=https
REACT_APP_EOS_API_HOST_FAILOVER=jungle.eosargentina.io
REACT_APP_EOS_API_PORT_FAILOVER=443
REACT_APP_EOS_API_PROTOCOL_FAILOVER=https
REACT_APP_EOS_CHAIN_ID=2a02a0053e5a8cf73a56ba0fda11e4d92e0238a4a2aa74fccf46d5a910746840
REACT_APP_EOS_APP_NAME=EvoDex
REACT_APP_EOS_BLOCK_EXPLORER_URL=https://jungle3.bloks.io
Expand Down
50 changes: 17 additions & 33 deletions src/config/ual.config.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,20 @@
import { Scatter } from 'ual-scatter'
import { Ledger } from 'ual-ledger'
import { Lynx } from 'ual-lynx'
import { TokenPocket } from 'ual-token-pocket'
import { MeetOne } from 'ual-meetone'
import { Anchor } from 'ual-anchor'

const appName = process.env.REACT_APP_EOS_APP_NAME || 'evodex.io'
const network = {
export const ualConfig = {
appName: process.env.REACT_APP_EOS_APP_NAME || 'evodex.io',
blockExplorerUrl: process.env.REACT_APP_EOS_BLOCK_EXPLORER_URL,
chainId:
process.env.REACT_APP_EOS_CHAIN_ID ||
'e70aaab8997e1dfce58fbfac80cbbb8fecec7b99cf982a9444273cbc64c41473',
rpcEndpoints: [
{
blockchain: 'eos',
protocol: process.env.REACT_APP_EOS_API_PROTOCOL || 'https',
host: process.env.REACT_APP_EOS_API_HOST || 'jungle.eosio.cr',
port: parseInt(process.env.REACT_APP_EOS_API_PORT || '443')
}
]
}
const authenticators = [
new Lynx([network]),
new Ledger([network]),
new Scatter([network], { appName }),
new TokenPocket([network.chainId]),
new MeetOne([network.chainId]),
new Anchor([network], { appName })
]

export const ualConfig = {
appName,
network,
authenticators,
blockExplorerUrl: process.env.REACT_APP_EOS_BLOCK_EXPLORER_URL
'2a02a0053e5a8cf73a56ba0fda11e4d92e0238a4a2aa74fccf46d5a910746840',
api: {
blockchain: 'eos',
protocol: process.env.REACT_APP_EOS_API_PROTOCOL || 'https',
host: process.env.REACT_APP_EOS_API_HOST || 'jungle.eosio.cr',
port: parseInt(process.env.REACT_APP_EOS_API_PORT || '443')
},
apiFailover: {
blockchain: 'eos',
protocol: process.env.REACT_APP_EOS_API_PROTOCOL_FAILOVER || 'https',
host:
process.env.REACT_APP_EOS_API_HOST_FAILOVER || 'jungle.eosargentina.io',
port: parseInt(process.env.REACT_APP_EOS_API_PORT_FAILOVER || '443')
}
}
35 changes: 20 additions & 15 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,32 @@ import CssBaseline from '@material-ui/core/CssBaseline'
import { ThemeProvider } from '@material-ui/core/styles'
import { UALProvider, withUAL } from 'ual-reactjs-renderer'

import { ualConfig } from './config'
import ual from './ual'
import App from './App'
import theme from './theme'
import * as serviceWorker from './serviceWorker'
import './i18n'

const AppWithUAL = withUAL(App)
const init = async () => {
const AppWithUAL = withUAL(App)
await ual.init()

render(
<UALProvider
chains={[ualConfig.network]}
authenticators={ualConfig.authenticators}
appName={ualConfig.appName}
>
<CssBaseline />
<ThemeProvider theme={theme}>
<AppWithUAL />
</ThemeProvider>
</UALProvider>,
document.getElementById('root')
)
render(
<UALProvider
chains={ual.chains}
authenticators={ual.authenticators}
appName={ual.appName}
>
<CssBaseline />
<ThemeProvider theme={theme}>
<AppWithUAL />
</ThemeProvider>
</UALProvider>,
document.getElementById('root')
)
}

init()

// If you want your app to work offline and load faster, you can change
// unregister() to register() below. Note this comes with some pitfalls.
Expand Down
48 changes: 48 additions & 0 deletions src/ual.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { Scatter } from 'ual-scatter'
import { Ledger } from 'ual-ledger'
import { Lynx } from 'ual-lynx'
import { TokenPocket } from 'ual-token-pocket'
import { MeetOne } from 'ual-meetone'
import { Anchor } from 'ual-anchor'

import { ualConfig } from './config'

export default {
useFailover: false,
init: async function () {
try {
const response = await fetch(
`${ualConfig.api.protocol}://${ualConfig.api.host}/v1/chain/get_info`
)
this.useFailover = response.status !== 200
} catch (error) {
this.useFailover = true
console.error(error)
}
},
get appName() {
return ualConfig.appName
},
get chainId() {
return ualConfig.chainId
},
get network() {
return {
chainId: this.chainId,
rpcEndpoints: [this.useFailover ? ualConfig.apiFailover : ualConfig.api]
}
},
get chains() {
return [this.network]
},
get authenticators() {
return [
new Lynx([this.network]),
new Ledger([this.network]),
new Scatter([this.network], { appName: this.appName }),
new TokenPocket([this.network.chainId]),
new MeetOne([this.network.chainId]),
new Anchor([this.network], { appName: this.appName })
]
}
}

0 comments on commit c49da77

Please sign in to comment.