diff --git a/common/actions/swap.js b/common/actions/swap.js index b4ae9437..9e8198fc 100644 --- a/common/actions/swap.js +++ b/common/actions/swap.js @@ -7,7 +7,8 @@ import { SWAP_PART_ONE_COMPLETE, SWAP_PART_TWO_COMPLETE, SWAP_DESTINATION_ADDRESS, - SWAP_RESTART + SWAP_RESTART, + SWAP_LOAD_BITY_RATES } from './swapConstants'; export const originKindSwap = value => { @@ -71,3 +72,9 @@ export const restartSwap = () => { type: SWAP_RESTART }; }; + +export const loadBityRates = () => { + return { + type: SWAP_LOAD_BITY_RATES + }; +}; diff --git a/common/actions/swapConstants.js b/common/actions/swapConstants.js index a1c5a718..c64741c9 100644 --- a/common/actions/swapConstants.js +++ b/common/actions/swapConstants.js @@ -7,3 +7,4 @@ export const SWAP_PART_ONE_COMPLETE = 'SWAP_PART_ONE_COMPLETE'; export const SWAP_PART_TWO_COMPLETE = 'SWAP_PART_TWO_COMPLETE'; export const SWAP_DESTINATION_ADDRESS = 'SWAP_DESTINATION_ADDRESS'; export const SWAP_RESTART = 'SWAP_RESTART'; +export const SWAP_LOAD_BITY_RATES = 'SWAP_LOAD_BITY_RATES'; diff --git a/common/api/bity.js b/common/api/bity.js index cbc92c27..979cbd8a 100644 --- a/common/api/bity.js +++ b/common/api/bity.js @@ -1,24 +1,18 @@ -import axios from 'axios'; +// @flow import bityConfig from 'config/bity'; -// https://stackoverflow.com/questions/9828684/how-to-get-all-arguments-of-a-callback-function -export function combineAndUpper() { - const args = []; - let newString = ''; - for (let i = 0; i < arguments.length; ++i) args[i] = arguments[i]; - args.forEach(each => { - newString = newString.concat(each.toUpperCase()); - }); - return newString; +export function combineAndUpper(...args: string[]) { + return args.reduce((acc, item) => acc.concat(item.toUpperCase()), ''); } function findRateFromBityRateList(rateObjects, pairName) { return rateObjects.find(x => x.pair === pairName); } -function _getRate(bityRates, origin, destination) { +// FIXME better types +function _getRate(bityRates, origin: string, destination: string) { const pairName = combineAndUpper(origin, destination); - const rateObjects = bityRates.data.objects; + const rateObjects = bityRates.objects; return findRateFromBityRateList(rateObjects, pairName); } @@ -44,7 +38,7 @@ function getMultipleRates(arrayOfOriginAndDestinationDicts) { export function getAllRates() { const mappedRates = {}; return _getAllRates().then(bityRates => { - bityRates.data.objects.forEach(each => { + bityRates.objects.forEach(each => { const pairName = each.pair; mappedRates[pairName] = parseFloat(each.rate_we_sell); }); @@ -54,9 +48,7 @@ export function getAllRates() { } function _getAllRates() { - const path = '/v1/rate2/'; - const bityURL = bityConfig.bityAPI + path; - return axios.get(bityURL); + return fetch(`${bityConfig.bityAPI}/v1/rate2/`).then(r => r.json()); } function requestStatus() {} diff --git a/common/containers/Tabs/Swap/index.js b/common/containers/Tabs/Swap/index.js index 192438db..6eb756bc 100644 --- a/common/containers/Tabs/Swap/index.js +++ b/common/containers/Tabs/Swap/index.js @@ -8,7 +8,6 @@ import OnGoingSwapInformation from './components/onGoingSwapInformation'; import { connect } from 'react-redux'; import * as swapActions from 'actions/swap'; import PropTypes from 'prop-types'; -import { getAllRates } from 'api/bity'; class Swap extends Component { constructor(props) { @@ -29,7 +28,7 @@ class Swap extends Component { destinationKindSwap: PropTypes.func, originAmountSwap: PropTypes.func, destinationAmountSwap: PropTypes.func, - updateBityRatesSwap: PropTypes.func, + loadBityRates: PropTypes.func, partOneCompleteSwap: PropTypes.func, destinationAddressSwap: PropTypes.func, restartSwap: PropTypes.func, @@ -38,18 +37,7 @@ class Swap extends Component { }; componentDidMount() { - let { bityRates } = this.props; - - if ( - !bityRates.ETHBTC || - !bityRates.ETHREP || - !bityRates.BTCETH || - !bityRates.BTCREP - ) { - getAllRates().then(data => { - this.props.updateBityRatesSwap(data); - }); - } + this.props.loadBityRates(); } render() { diff --git a/common/sagas/bity.js b/common/sagas/bity.js new file mode 100644 index 00000000..166b48da --- /dev/null +++ b/common/sagas/bity.js @@ -0,0 +1,23 @@ +// @flow + +import { takeLatest, call, apply, put, select, fork } from 'redux-saga/effects'; +import type { Effect } from 'redux-saga/effects'; +import { delay } from 'redux-saga'; +import { updateBityRatesSwap } from 'actions/swap'; +import { SWAP_LOAD_BITY_RATES } from 'actions/swapConstants'; +import type { UnlockPrivateKeyAction } from 'actions/wallet'; +import { getAllRates } from 'api/bity'; + +export function* loadBityRates(action?: any): Generator { + if (!action) return; + // eslint-disable-next-line + while (true) { + const data = yield call(getAllRates); + yield put(updateBityRatesSwap(data)); + yield call(delay, 5000); + } +} + +export default function* bitySaga(): Generator { + yield takeLatest(SWAP_LOAD_BITY_RATES, loadBityRates); +} diff --git a/common/store.js b/common/store.js index d270f825..86978944 100644 --- a/common/store.js +++ b/common/store.js @@ -4,6 +4,7 @@ import createSagaMiddleware from 'redux-saga'; import notificationsSaga from './sagas/notifications'; import ensSaga from './sagas/ens'; import walletSaga from './sagas/wallet'; +import bitySaga from './sagas/bity'; import { initialState as configInitialState } from 'reducers/config'; import throttle from 'lodash/throttle'; import { composeWithDevTools } from 'redux-devtools-extension'; @@ -44,6 +45,7 @@ const configureStore = () => { sagaMiddleware.run(notificationsSaga); sagaMiddleware.run(ensSaga); sagaMiddleware.run(walletSaga); + sagaMiddleware.run(bitySaga); store.subscribe( throttle(() => { diff --git a/package.json b/package.json index cfca6607..284a2132 100644 --- a/package.json +++ b/package.json @@ -4,12 +4,12 @@ "main": "common/index.jsx", "description": "MyEtherWallet v4", "dependencies": { - "axios": "^0.16.2", + "big.js": "^3.1.3", "ethereum-blockies": "https://github.com/MyEtherWallet/blockies.git", "ethereumjs-util": "^5.1.2", + "ethereumjs-wallet": "^0.6.0", "idna-uts46": "^1.1.0", "lodash": "^4.17.4", - "ethereumjs-wallet": "^0.6.0", "prop-types": "^15.5.8", "react": "^15.4.2", "react-dom": "^15.4.2",