2017-08-31 16:30:46 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
loadBityRatesSucceededSwap,
|
|
|
|
loadShapeshiftRatesSucceededSwap,
|
|
|
|
changeSwapProvider,
|
|
|
|
ChangeProviderSwapAcion
|
|
|
|
} from 'actions/swap';
|
2017-11-16 20:25:30 +00:00
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { getAllRates } from 'api/bity';
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
2018-01-02 18:04:50 +00:00
|
|
|
import { call, select, cancel, fork, put, take, takeLatest, race } from 'redux-saga/effects';
|
|
|
|
import shapeshift from 'api/shapeshift';
|
|
|
|
import { getSwap } from 'sagas/swap/orders';
|
2017-07-08 02:00:09 +00:00
|
|
|
|
2017-11-16 20:25:30 +00:00
|
|
|
const POLLING_CYCLE = 30000;
|
2018-01-02 18:04:50 +00:00
|
|
|
export const SHAPESHIFT_TIMEOUT = 10000;
|
2017-11-16 20:25:30 +00:00
|
|
|
|
2017-10-19 02:29:49 +00:00
|
|
|
export function* loadBityRates(): SagaIterator {
|
2017-08-31 16:30:46 +00:00
|
|
|
while (true) {
|
|
|
|
try {
|
2017-07-09 03:51:14 +00:00
|
|
|
const data = yield call(getAllRates);
|
2017-07-31 23:14:30 +00:00
|
|
|
yield put(loadBityRatesSucceededSwap(data));
|
2017-08-31 16:30:46 +00:00
|
|
|
} catch (error) {
|
2017-09-25 02:06:28 +00:00
|
|
|
yield put(showNotification('danger', error.message));
|
2017-07-09 03:51:14 +00:00
|
|
|
}
|
2017-11-16 20:25:30 +00:00
|
|
|
yield call(delay, POLLING_CYCLE);
|
2017-07-08 02:00:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export function* loadShapeshiftRates(): SagaIterator {
|
|
|
|
while (true) {
|
|
|
|
try {
|
|
|
|
// Race b/w api call and timeout
|
|
|
|
// getShapeShiftRates should be an api call that accepts a whitelisted arr of symbols
|
|
|
|
const { tokens } = yield race({
|
|
|
|
tokens: call(shapeshift.getAllRates),
|
|
|
|
timeout: call(delay, SHAPESHIFT_TIMEOUT)
|
|
|
|
});
|
|
|
|
// If tokens exist, put it into the redux state, otherwise switch to bity.
|
|
|
|
if (tokens) {
|
|
|
|
yield put(loadShapeshiftRatesSucceededSwap(tokens));
|
|
|
|
} else {
|
|
|
|
yield put(
|
|
|
|
showNotification('danger', 'Error loading ShapeShift tokens - reverting to Bity')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
yield put(showNotification('danger', `Error loading ShapeShift tokens - ${error}`));
|
|
|
|
}
|
|
|
|
yield call(delay, POLLING_CYCLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* swapProvider(action: ChangeProviderSwapAcion): SagaIterator {
|
|
|
|
const swap = yield select(getSwap);
|
|
|
|
if (swap.provider !== action.payload) {
|
|
|
|
yield put(changeSwapProvider(action.payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-31 16:30:46 +00:00
|
|
|
// Fork our recurring API call, watch for the need to cancel.
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* handleBityRates(): SagaIterator {
|
2017-08-31 16:30:46 +00:00
|
|
|
const loadBityRatesTask = yield fork(loadBityRates);
|
2017-11-16 20:25:30 +00:00
|
|
|
yield take(TypeKeys.SWAP_STOP_LOAD_BITY_RATES);
|
2017-08-31 16:30:46 +00:00
|
|
|
yield cancel(loadBityRatesTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for latest SWAP_LOAD_BITY_RATES_REQUESTED action.
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* getBityRatesSaga(): SagaIterator {
|
2017-11-16 20:25:30 +00:00
|
|
|
yield takeLatest(TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED, handleBityRates);
|
2017-07-08 02:00:09 +00:00
|
|
|
}
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
// Fork our API call
|
|
|
|
export function* handleShapeShiftRates(): SagaIterator {
|
|
|
|
const loadShapeShiftRatesTask = yield fork(loadShapeshiftRates);
|
|
|
|
yield take(TypeKeys.SWAP_STOP_LOAD_SHAPESHIFT_RATES);
|
|
|
|
yield cancel(loadShapeShiftRatesTask);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED action.
|
|
|
|
export function* getShapeShiftRatesSaga(): SagaIterator {
|
|
|
|
yield takeLatest(TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED, handleShapeShiftRates);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Watch for provider swaps
|
|
|
|
export function* swapProviderSaga(): SagaIterator {
|
|
|
|
yield takeLatest(TypeKeys.SWAP_CHANGE_PROVIDER, swapProvider);
|
|
|
|
}
|