Skip to content

Commit

Permalink
feat: implement api failover endpoint, resolve #124
Browse files Browse the repository at this point in the history
  • Loading branch information
adriexnet committed Sep 10, 2020
1 parent 414d47c commit e209f97
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 4 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ REACT_APP_EOS_BLOCK_EXPLORER_URL=https://jungle3.bloks.io
REACT_APP_VOTE_CONTRACT=wevotethefee
REACT_APP_EVODEX_CONTRACT=evolutiondex
REACT_APP_EVODEX_API_URL=https://jungle.api.evolutiondex.io
REACT_APP_EVODEX_API_URL_FAILOVER=https://jungle.api-2.evolutiondex.io
REACT_APP_PROJECT_VERSION=v0.1.0-beta
1 change: 1 addition & 0 deletions src/config/evodex.config.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export const evodexConfig = {
api: process.env.REACT_APP_EVODEX_API_URL,
apiFailover: process.env.REACT_APP_EVODEX_API_URL_FAILOVER,
contract: process.env.REACT_APP_EVODEX_CONTRACT,
voteContract: process.env.REACT_APP_VOTE_CONTRACT
}
32 changes: 32 additions & 0 deletions src/utils/axios.util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import axios from 'axios'

export default (url, failover) => {
// Create instance
const axiosApi = axios.create({
baseURL: url,
headers: {
'Content-Type': 'application/json'
}
})

// Response interceptor failover for API calls
axiosApi.interceptors.response.use(
(response) => {
return response
},
(error) => {
const config = error.config

if (!config.retry) {
config.retry = true
config.baseURL = failover

return axiosApi(config)
}

return Promise.reject(error)
}
)

return axiosApi
}
9 changes: 5 additions & 4 deletions src/utils/evolutiondex.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import axios from 'axios'
import * as eosCommon from 'eos-common'

import { evodexConfig } from '../config'

import { getScatterError } from './getScatterError'
import axiosUtil from './axios.util'

const { asset, number_to_asset: numberToAsset } = eosCommon
const defaultState = { pairs: [], tokens: [] }
const axios = axiosUtil(evodexConfig.api, evodexConfig.apiFailover)

const amountToAsset = (amount = '0', currentAsset) => {
if (isNaN(amount)) {
Expand All @@ -23,7 +24,7 @@ const amountToAsset = (amount = '0', currentAsset) => {
return asset(`${validAmount} ${currentAsset.symbol.code().toString()}`)
}
const getInfo = async (ual) => {
const { data } = await axios.get(`${evodexConfig.api}/list`)
const { data } = await axios.get('/list')
let userPools = []

if (ual?.activeUser) {
Expand All @@ -43,7 +44,7 @@ const getInfo = async (ual) => {
Pool2contract: pool2Contract,
error
}
} = await axios.get(`${evodexConfig.api}/info`, {
} = await axios.get('/info', {
params: {
pair: tokenPair
}
Expand Down Expand Up @@ -197,7 +198,7 @@ const exchange = async (amount, pair, ual) => {
quantity: assetToGive.toString(),
memo: `exchange: ${
pair.token
},${assetToReceive.toString()},sent using evodex.io`
},${assetToReceive.toString()},sent using evodex.io`
}
}
]
Expand Down

0 comments on commit e209f97

Please sign in to comment.