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_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
};
};

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_DESTINATION_ADDRESS = 'SWAP_DESTINATION_ADDRESS';
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';
// 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() {}

View File

@ -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() {

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 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(() => {

View File

@ -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",