Skip to content
This repository has been archived by the owner on Nov 20, 2024. It is now read-only.

Commit

Permalink
Merge pull request #588 from hermeznetwork/replace-exchange-rates-api
Browse files Browse the repository at this point in the history
Replace exchange rates api
  • Loading branch information
elias-garcia authored Jul 7, 2021
2 parents 0a4c9d1 + e27d21b commit d5ec0d4
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 32 deletions.
2 changes: 0 additions & 2 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ REACT_APP_AIRDROP_ID=8
REACT_APP_ACCUMULATED_AIRDROP_IDS="1,2,3"
# Enables token swap feature
REACT_APP_ENABLE_TOKEN_SWAP=false
# Exchange rates API Key
REACT_APP_EXCHANGE_RATES_API_KEY=example
# Infura API Key
REACT_APP_INFURA_API_KEY=example
# Can be production or development
Expand Down
38 changes: 16 additions & 22 deletions src/apis/fiat-exchange-rates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,29 @@ import { FIAT_EXCHANGE_RATES_API_URL } from '../constants'

const baseApiUrl = FIAT_EXCHANGE_RATES_API_URL

const mockedFiatExchangeRates = {
EUR: 0.837775,
CNY: 6.446304,
JPY: 110.253954,
GBP: 0.716945
}

/**
* Fetches the USD exchange rates for the requested currency symbols
* @param {string[]} symbols - ISO 4217 currency codes
* @returns {Object} - USD exchange rates for the requested symbols
*/
// eslint-disable-next-line no-unused-vars
function getFiatExchangeRates (symbols) {
const params = { base: CurrencySymbol.USD.code, symbols: symbols.join(','), access_key: process.env.REACT_APP_EXCHANGE_RATES_API_KEY }

return axios.get(`${baseApiUrl}/latest`, { params })
.then(res => res.data)
}
const params = { base: CurrencySymbol.USD.code, symbols: symbols.join('|') }

/**
* Mocks the USD exchange rates for the requested currency symbols
* @param {string[]} symbols - ISO 4217 currency codes
* @returns {Object} - USD exchange rates for the requested symbols
*/
function getMockedFiatExchangeRates () {
return Promise.resolve({
success: true,
base: 'USD',
rates: {
EUR: 0.837775,
CNY: 6.446304,
JPY: 110.253954,
GBP: 0.716945
}
})
return axios.get(`${baseApiUrl}/currencies`, { params })
.then((res) => {
return res.data.currencies.reduce((acc, rate) => ({
...acc,
[rate.currency]: rate.price
}), {})
})
}

export { getMockedFiatExchangeRates as getFiatExchangeRates }
export { mockedFiatExchangeRates, getFiatExchangeRates }
2 changes: 1 addition & 1 deletion src/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const MY_ACCOUNT = {
DEFAULT_PREFERRED_CURRENCY: CurrencySymbol.USD.code
}

export const FIAT_EXCHANGE_RATES_API_URL = 'https://api.exchangeratesapi.io/v1'
export const FIAT_EXCHANGE_RATES_API_URL = 'https://api.hermez.io/v1'

export const MY_CODE = {
QR_CODE_SIZE: 216
Expand Down
9 changes: 2 additions & 7 deletions src/store/global/global.thunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,9 @@ function fetchFiatExchangeRates () {
dispatch(globalActions.loadFiatExchangeRates())

return fiatExchangeRatesApi.getFiatExchangeRates(symbols)
.then(res => dispatch(globalActions.loadFiatExchangeRatesSuccess(res.rates)))
.then(res => dispatch(globalActions.loadFiatExchangeRatesSuccess(res)))
.catch(() => {
const rates = {}
for (const rate of symbols) {
rates[rate] = undefined
}

dispatch(globalActions.loadFiatExchangeRatesSuccess(rates))
dispatch(globalActions.loadFiatExchangeRatesSuccess(fiatExchangeRatesApi.mockedFiatExchangeRates))
})
}
}
Expand Down

0 comments on commit d5ec0d4

Please sign in to comment.