move bity getRates to saga (#35)

* move bity getRates to saga

* typo

* bity polling
This commit is contained in:
crptm 2017-07-08 06:00:09 +04:00 committed by Daniel Ternyak
parent 46ec6fe235
commit ecb11133dd
7 changed files with 46 additions and 33 deletions

View File

@ -7,7 +7,8 @@ import {
SWAP_PART_ONE_COMPLETE, SWAP_PART_ONE_COMPLETE,
SWAP_PART_TWO_COMPLETE, SWAP_PART_TWO_COMPLETE,
SWAP_DESTINATION_ADDRESS, SWAP_DESTINATION_ADDRESS,
SWAP_RESTART SWAP_RESTART,
SWAP_LOAD_BITY_RATES
} from './swapConstants'; } from './swapConstants';
export const originKindSwap = value => { export const originKindSwap = value => {
@ -71,3 +72,9 @@ export const restartSwap = () => {
type: SWAP_RESTART type: SWAP_RESTART
}; };
}; };
export const loadBityRates = () => {
return {
type: SWAP_LOAD_BITY_RATES
};
};

View File

@ -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_PART_TWO_COMPLETE = 'SWAP_PART_TWO_COMPLETE';
export const SWAP_DESTINATION_ADDRESS = 'SWAP_DESTINATION_ADDRESS'; export const SWAP_DESTINATION_ADDRESS = 'SWAP_DESTINATION_ADDRESS';
export const SWAP_RESTART = 'SWAP_RESTART'; export const SWAP_RESTART = 'SWAP_RESTART';
export const SWAP_LOAD_BITY_RATES = 'SWAP_LOAD_BITY_RATES';

View File

@ -1,24 +1,18 @@
import axios from 'axios'; // @flow
import bityConfig from 'config/bity'; import bityConfig from 'config/bity';
// https://stackoverflow.com/questions/9828684/how-to-get-all-arguments-of-a-callback-function export function combineAndUpper(...args: string[]) {
export function combineAndUpper() { return args.reduce((acc, item) => acc.concat(item.toUpperCase()), '');
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;
} }
function findRateFromBityRateList(rateObjects, pairName) { function findRateFromBityRateList(rateObjects, pairName) {
return rateObjects.find(x => x.pair === 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 pairName = combineAndUpper(origin, destination);
const rateObjects = bityRates.data.objects; const rateObjects = bityRates.objects;
return findRateFromBityRateList(rateObjects, pairName); return findRateFromBityRateList(rateObjects, pairName);
} }
@ -44,7 +38,7 @@ function getMultipleRates(arrayOfOriginAndDestinationDicts) {
export function getAllRates() { export function getAllRates() {
const mappedRates = {}; const mappedRates = {};
return _getAllRates().then(bityRates => { return _getAllRates().then(bityRates => {
bityRates.data.objects.forEach(each => { bityRates.objects.forEach(each => {
const pairName = each.pair; const pairName = each.pair;
mappedRates[pairName] = parseFloat(each.rate_we_sell); mappedRates[pairName] = parseFloat(each.rate_we_sell);
}); });
@ -54,9 +48,7 @@ export function getAllRates() {
} }
function _getAllRates() { function _getAllRates() {
const path = '/v1/rate2/'; return fetch(`${bityConfig.bityAPI}/v1/rate2/`).then(r => r.json());
const bityURL = bityConfig.bityAPI + path;
return axios.get(bityURL);
} }
function requestStatus() {} function requestStatus() {}

View File

@ -8,7 +8,6 @@ import OnGoingSwapInformation from './components/onGoingSwapInformation';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import * as swapActions from 'actions/swap'; import * as swapActions from 'actions/swap';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { getAllRates } from 'api/bity';
class Swap extends Component { class Swap extends Component {
constructor(props) { constructor(props) {
@ -29,7 +28,7 @@ class Swap extends Component {
destinationKindSwap: PropTypes.func, destinationKindSwap: PropTypes.func,
originAmountSwap: PropTypes.func, originAmountSwap: PropTypes.func,
destinationAmountSwap: PropTypes.func, destinationAmountSwap: PropTypes.func,
updateBityRatesSwap: PropTypes.func, loadBityRates: PropTypes.func,
partOneCompleteSwap: PropTypes.func, partOneCompleteSwap: PropTypes.func,
destinationAddressSwap: PropTypes.func, destinationAddressSwap: PropTypes.func,
restartSwap: PropTypes.func, restartSwap: PropTypes.func,
@ -38,18 +37,7 @@ class Swap extends Component {
}; };
componentDidMount() { componentDidMount() {
let { bityRates } = this.props; this.props.loadBityRates();
if (
!bityRates.ETHBTC ||
!bityRates.ETHREP ||
!bityRates.BTCETH ||
!bityRates.BTCREP
) {
getAllRates().then(data => {
this.props.updateBityRatesSwap(data);
});
}
} }
render() { render() {

23
common/sagas/bity.js Normal file
View File

@ -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<Effect, void, any> {
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<Effect, void, any> {
yield takeLatest(SWAP_LOAD_BITY_RATES, loadBityRates);
}

View File

@ -4,6 +4,7 @@ import createSagaMiddleware from 'redux-saga';
import notificationsSaga from './sagas/notifications'; import notificationsSaga from './sagas/notifications';
import ensSaga from './sagas/ens'; import ensSaga from './sagas/ens';
import walletSaga from './sagas/wallet'; import walletSaga from './sagas/wallet';
import bitySaga from './sagas/bity';
import { initialState as configInitialState } from 'reducers/config'; import { initialState as configInitialState } from 'reducers/config';
import throttle from 'lodash/throttle'; import throttle from 'lodash/throttle';
import { composeWithDevTools } from 'redux-devtools-extension'; import { composeWithDevTools } from 'redux-devtools-extension';
@ -44,6 +45,7 @@ const configureStore = () => {
sagaMiddleware.run(notificationsSaga); sagaMiddleware.run(notificationsSaga);
sagaMiddleware.run(ensSaga); sagaMiddleware.run(ensSaga);
sagaMiddleware.run(walletSaga); sagaMiddleware.run(walletSaga);
sagaMiddleware.run(bitySaga);
store.subscribe( store.subscribe(
throttle(() => { throttle(() => {

View File

@ -4,12 +4,12 @@
"main": "common/index.jsx", "main": "common/index.jsx",
"description": "MyEtherWallet v4", "description": "MyEtherWallet v4",
"dependencies": { "dependencies": {
"axios": "^0.16.2", "big.js": "^3.1.3",
"ethereum-blockies": "https://github.com/MyEtherWallet/blockies.git", "ethereum-blockies": "https://github.com/MyEtherWallet/blockies.git",
"ethereumjs-util": "^5.1.2", "ethereumjs-util": "^5.1.2",
"ethereumjs-wallet": "^0.6.0",
"idna-uts46": "^1.1.0", "idna-uts46": "^1.1.0",
"lodash": "^4.17.4", "lodash": "^4.17.4",
"ethereumjs-wallet": "^0.6.0",
"prop-types": "^15.5.8", "prop-types": "^15.5.8",
"react": "^15.4.2", "react": "^15.4.2",
"react-dom": "^15.4.2", "react-dom": "^15.4.2",