2017-08-31 16:30:46 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
loadBityRatesSucceededSwap,
|
|
|
|
loadShapeshiftRatesSucceededSwap,
|
2018-02-25 01:29:34 +00:00
|
|
|
loadBityRatesFailedSwap,
|
|
|
|
loadShapeshiftRatesFailedSwap,
|
2018-01-02 18:04:50 +00:00
|
|
|
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-02-25 01:29:34 +00:00
|
|
|
import { call, select, put, takeLatest, race, take, cancel, fork } from 'redux-saga/effects';
|
2018-01-02 18:04:50 +00:00
|
|
|
import shapeshift from 'api/shapeshift';
|
|
|
|
import { getSwap } from 'sagas/swap/orders';
|
2018-02-25 01:29:34 +00:00
|
|
|
import { getHasNotifiedRatesFailure } from 'selectors/swap';
|
2017-07-08 02:00:09 +00:00
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export const SHAPESHIFT_TIMEOUT = 10000;
|
2018-02-25 01:29:34 +00:00
|
|
|
export const POLLING_CYCLE = 30000;
|
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) {
|
2018-02-25 01:29:34 +00:00
|
|
|
const hasNotified = yield select(getHasNotifiedRatesFailure);
|
|
|
|
if (!hasNotified) {
|
|
|
|
console.error('Failed to load rates from Bity:', error);
|
|
|
|
yield put(showNotification('danger', error.message));
|
|
|
|
}
|
|
|
|
yield put(loadBityRatesFailedSwap());
|
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-02-25 01:29:34 +00:00
|
|
|
export function* handleBityRates(): SagaIterator {
|
|
|
|
const loadBityRatesTask = yield fork(loadBityRates);
|
|
|
|
yield take(TypeKeys.SWAP_STOP_LOAD_BITY_RATES);
|
|
|
|
yield cancel(loadBityRatesTask);
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-02-25 01:29:34 +00:00
|
|
|
throw new Error('ShapeShift rates request timed out.');
|
|
|
|
}
|
|
|
|
} catch (error) {
|
|
|
|
const hasNotified = yield select(getHasNotifiedRatesFailure);
|
|
|
|
if (!hasNotified) {
|
|
|
|
console.error('Failed to fetch rates from shapeshift:', error);
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(
|
2018-02-25 01:29:34 +00:00
|
|
|
showNotification(
|
|
|
|
'danger',
|
|
|
|
'Failed to load swap rates from ShapeShift, please try again later'
|
|
|
|
)
|
2018-01-02 18:04:50 +00:00
|
|
|
);
|
|
|
|
}
|
2018-02-25 01:29:34 +00:00
|
|
|
yield put(loadShapeshiftRatesFailedSwap());
|
2018-01-02 18:04:50 +00:00
|
|
|
}
|
|
|
|
yield call(delay, POLLING_CYCLE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
export function* handleShapeshiftRates(): SagaIterator {
|
|
|
|
const loadShapeshiftRatesTask = yield fork(loadShapeshiftRates);
|
|
|
|
yield take(TypeKeys.SWAP_STOP_LOAD_SHAPESHIFT_RATES);
|
|
|
|
yield cancel(loadShapeshiftRatesTask);
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export function* swapProvider(action: ChangeProviderSwapAcion): SagaIterator {
|
|
|
|
const swap = yield select(getSwap);
|
|
|
|
if (swap.provider !== action.payload) {
|
|
|
|
yield put(changeSwapProvider(action.payload));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
export default function* swapRates(): SagaIterator {
|
2017-11-16 20:25:30 +00:00
|
|
|
yield takeLatest(TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED, handleBityRates);
|
2018-02-25 01:29:34 +00:00
|
|
|
yield takeLatest(TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED, handleShapeshiftRates);
|
2018-01-02 18:04:50 +00:00
|
|
|
yield takeLatest(TypeKeys.SWAP_CHANGE_PROVIDER, swapProvider);
|
|
|
|
}
|