2017-09-04 16:30:31 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
2017-07-31 23:14:30 +00:00
|
|
|
import {
|
2017-09-25 02:06:28 +00:00
|
|
|
bityOrderCreateFailedSwap,
|
|
|
|
BityOrderCreateRequestedSwapAction,
|
2017-07-31 23:14:30 +00:00
|
|
|
bityOrderCreateSucceededSwap,
|
|
|
|
changeStepSwap,
|
2017-09-25 02:06:28 +00:00
|
|
|
orderTimeSwap,
|
2017-07-31 23:14:30 +00:00
|
|
|
startOrderTimerSwap,
|
|
|
|
startPollBityOrderStatus,
|
2017-09-25 02:06:28 +00:00
|
|
|
stopLoadBityRatesSwap,
|
2018-01-02 18:04:50 +00:00
|
|
|
stopPollBityOrderStatus,
|
|
|
|
shapeshiftOrderStatusSucceededSwap,
|
|
|
|
ShapeshiftOrderCreateRequestedSwapAction,
|
|
|
|
stopLoadShapeshiftRatesSwap,
|
|
|
|
shapeshiftOrderCreateFailedSwap,
|
|
|
|
shapeshiftOrderCreateSucceededSwap,
|
|
|
|
startPollShapeshiftOrderStatus,
|
|
|
|
stopPollShapeshiftOrderStatus,
|
|
|
|
bityOrderStatusRequested,
|
|
|
|
stopOrderTimerSwap,
|
|
|
|
bityOrderStatusSucceededSwap,
|
|
|
|
shapeshiftOrderStatusRequested,
|
|
|
|
loadShapeshiftRatesRequestedSwap
|
2017-07-31 23:14:30 +00:00
|
|
|
} from 'actions/swap';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { getOrderStatus, postOrder } from 'api/bity';
|
|
|
|
import moment from 'moment';
|
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import { State as SwapState } from 'reducers/swap';
|
|
|
|
import { delay, SagaIterator } from 'redux-saga';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
call,
|
|
|
|
cancel,
|
|
|
|
apply,
|
|
|
|
cancelled,
|
|
|
|
fork,
|
|
|
|
put,
|
|
|
|
select,
|
|
|
|
take,
|
|
|
|
takeEvery
|
|
|
|
} from 'redux-saga/effects';
|
|
|
|
import shapeshift from 'api/shapeshift';
|
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
|
|
|
import { resetWallet } from 'actions/wallet';
|
2017-07-31 23:14:30 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export const getSwap = (state: AppState): SwapState => state.swap;
|
2017-07-31 23:14:30 +00:00
|
|
|
const ONE_SECOND = 1000;
|
|
|
|
const TEN_SECONDS = ONE_SECOND * 10;
|
2018-01-02 18:04:50 +00:00
|
|
|
export const ORDER_TIMEOUT_MESSAGE = `
|
2017-09-04 16:30:31 +00:00
|
|
|
Time has run out.
|
|
|
|
If you have already sent, please wait 1 hour.
|
|
|
|
If your order has not be processed after 1 hour,
|
2017-07-31 23:14:30 +00:00
|
|
|
please press the orange 'Issue with your Swap?' button.
|
|
|
|
`;
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export const ORDER_RECEIVED_MESSAGE = `
|
|
|
|
The order was recieved.
|
|
|
|
It may take some time to process the transaction.
|
|
|
|
Please wait 1 hour. If your order has not been processed by then,
|
|
|
|
please press the orange 'Issue with your Swap?' button.
|
|
|
|
`;
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* pollBityOrderStatus(): SagaIterator {
|
2017-07-31 23:14:30 +00:00
|
|
|
try {
|
|
|
|
let swap = yield select(getSwap);
|
|
|
|
while (true) {
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(bityOrderStatusRequested());
|
2017-07-31 23:14:30 +00:00
|
|
|
const orderStatus = yield call(getOrderStatus, swap.orderId);
|
|
|
|
if (orderStatus.error) {
|
2017-12-18 23:29:26 +00:00
|
|
|
yield put(showNotification('danger', `Bity Error: ${orderStatus.msg}`, TEN_SECONDS));
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(bityOrderStatusSucceededSwap(orderStatus.data));
|
2017-07-31 23:14:30 +00:00
|
|
|
yield call(delay, ONE_SECOND * 5);
|
|
|
|
swap = yield select(getSwap);
|
|
|
|
if (swap === 'CANC') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (yield cancelled()) {
|
|
|
|
// TODO - implement request cancel if needed
|
|
|
|
// yield put(actions.requestFailure('Request cancelled!'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export function* pollShapeshiftOrderStatus(): SagaIterator {
|
|
|
|
try {
|
|
|
|
let swap = yield select(getSwap);
|
|
|
|
while (true) {
|
|
|
|
yield put(shapeshiftOrderStatusRequested());
|
|
|
|
const orderStatus = yield apply(shapeshift, shapeshift.checkStatus, [swap.paymentAddress]);
|
|
|
|
if (orderStatus.status === 'failed') {
|
|
|
|
yield put(showNotification('danger', `Shapeshift Error: ${orderStatus.error}`, Infinity));
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
} else {
|
|
|
|
yield put(shapeshiftOrderStatusSucceededSwap(orderStatus));
|
|
|
|
yield call(delay, ONE_SECOND * 5);
|
|
|
|
swap = yield select(getSwap);
|
|
|
|
if (swap === 'CANC') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} finally {
|
|
|
|
if (yield cancelled()) {
|
|
|
|
// Request canclled
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* pollBityOrderStatusSaga(): SagaIterator {
|
2018-01-02 18:04:50 +00:00
|
|
|
while (yield take(TypeKeys.SWAP_START_POLL_BITY_ORDER_STATUS)) {
|
2017-07-31 23:14:30 +00:00
|
|
|
// starts the task in the background
|
|
|
|
const pollBityOrderStatusTask = yield fork(pollBityOrderStatus);
|
|
|
|
// wait for the user to get to point where refresh is no longer needed
|
2018-01-02 18:04:50 +00:00
|
|
|
yield take(TypeKeys.SWAP_STOP_POLL_BITY_ORDER_STATUS);
|
2017-07-31 23:14:30 +00:00
|
|
|
// cancel the background task
|
|
|
|
// this will cause the forked loadBityRates task to jump into its finally block
|
|
|
|
yield cancel(pollBityOrderStatusTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export function* pollShapeshiftOrderStatusSaga(): SagaIterator {
|
|
|
|
while (yield take(TypeKeys.SWAP_START_POLL_SHAPESHIFT_ORDER_STATUS)) {
|
|
|
|
const pollShapeshiftOrderStatusTask = yield fork(pollShapeshiftOrderStatus);
|
|
|
|
yield take(TypeKeys.SWAP_STOP_POLL_SHAPESHIFT_ORDER_STATUS);
|
|
|
|
yield cancel(pollShapeshiftOrderStatusTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-18 23:29:26 +00:00
|
|
|
export function* postBityOrderCreate(action: BityOrderCreateRequestedSwapAction): SagaIterator {
|
2017-07-31 23:14:30 +00:00
|
|
|
const payload = action.payload;
|
|
|
|
try {
|
|
|
|
yield put(stopLoadBityRatesSwap());
|
|
|
|
const order = yield call(
|
|
|
|
postOrder,
|
|
|
|
payload.amount,
|
2017-09-25 02:06:28 +00:00
|
|
|
payload.destinationAddress,
|
2017-07-31 23:14:30 +00:00
|
|
|
payload.mode,
|
|
|
|
payload.pair
|
|
|
|
);
|
|
|
|
if (order.error) {
|
|
|
|
// TODO - handle better / like existing site?
|
2017-12-18 23:29:26 +00:00
|
|
|
yield put(showNotification('danger', `Bity Error: ${order.msg}`, TEN_SECONDS));
|
2017-09-25 02:06:28 +00:00
|
|
|
yield put(bityOrderCreateFailedSwap());
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
|
|
|
yield put(bityOrderCreateSucceededSwap(order.data));
|
|
|
|
yield put(changeStepSwap(3));
|
|
|
|
// start countdown
|
|
|
|
yield put(startOrderTimerSwap());
|
|
|
|
// start bity order status polling
|
|
|
|
yield put(startPollBityOrderStatus());
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
const message =
|
|
|
|
'Connection Error. Please check the developer console for more details and/or contact support';
|
2018-01-15 06:09:38 +00:00
|
|
|
console.error(e);
|
2017-07-31 23:14:30 +00:00
|
|
|
yield put(showNotification('danger', message, TEN_SECONDS));
|
2017-09-25 02:06:28 +00:00
|
|
|
yield put(bityOrderCreateFailedSwap());
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
export function* postShapeshiftOrderCreate(
|
|
|
|
action: ShapeshiftOrderCreateRequestedSwapAction
|
|
|
|
): SagaIterator {
|
|
|
|
const payload = action.payload;
|
|
|
|
try {
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
|
|
|
const order = yield apply(shapeshift, shapeshift.sendAmount, [
|
|
|
|
payload.withdrawal,
|
|
|
|
payload.originKind,
|
|
|
|
payload.destinationKind,
|
|
|
|
payload.destinationAmount
|
|
|
|
]);
|
|
|
|
if (order.error) {
|
|
|
|
yield put(showNotification('danger', `Shapeshift Error: ${order.error}`, TEN_SECONDS));
|
|
|
|
yield put(shapeshiftOrderCreateFailedSwap());
|
|
|
|
} else {
|
|
|
|
yield put(shapeshiftOrderCreateSucceededSwap(order.success));
|
|
|
|
yield put(changeStepSwap(3));
|
|
|
|
// start countdown
|
|
|
|
yield put(startOrderTimerSwap());
|
|
|
|
// start shapeshift order status polling
|
|
|
|
yield put(startPollShapeshiftOrderStatus());
|
|
|
|
}
|
|
|
|
} catch (e) {
|
2018-01-15 06:09:38 +00:00
|
|
|
if (e && e.message) {
|
|
|
|
yield put(showNotification('danger', e.message, TEN_SECONDS));
|
|
|
|
} else {
|
|
|
|
const message =
|
|
|
|
'Connection Error. Please check the developer console for more details and/or contact support';
|
|
|
|
console.error(e);
|
|
|
|
yield put(showNotification('danger', message, TEN_SECONDS));
|
|
|
|
}
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(shapeshiftOrderCreateFailedSwap());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* restartSwap() {
|
|
|
|
yield put(resetWallet());
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put(loadShapeshiftRatesRequestedSwap());
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* bityOrderTimeRemaining(): SagaIterator {
|
|
|
|
while (true) {
|
2017-07-31 23:14:30 +00:00
|
|
|
let hasShownNotification = false;
|
|
|
|
while (true) {
|
|
|
|
yield call(delay, ONE_SECOND);
|
|
|
|
const swap = yield select(getSwap);
|
2017-12-18 23:29:26 +00:00
|
|
|
const createdTimeStampMoment = moment(swap.orderTimestampCreatedISOString);
|
2017-09-25 02:06:28 +00:00
|
|
|
const validUntil = moment(createdTimeStampMoment).add(swap.validFor, 's');
|
|
|
|
const now = moment();
|
2017-07-31 23:14:30 +00:00
|
|
|
if (validUntil.isAfter(now)) {
|
2017-09-25 02:06:28 +00:00
|
|
|
const duration = moment.duration(validUntil.diff(now));
|
|
|
|
const seconds = duration.asSeconds();
|
|
|
|
yield put(orderTimeSwap(parseInt(seconds.toString(), 10)));
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
switch (swap.bityOrderStatus) {
|
|
|
|
case 'CANC':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put(stopLoadBityRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'FILL':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put(stopLoadBityRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
break;
|
|
|
|
}
|
2017-07-31 23:14:30 +00:00
|
|
|
} else {
|
2018-01-02 18:04:50 +00:00
|
|
|
switch (swap.bityOrderStatus) {
|
2017-07-31 23:14:30 +00:00
|
|
|
case 'OPEN':
|
|
|
|
yield put(orderTimeSwap(0));
|
|
|
|
yield put(stopPollBityOrderStatus());
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(stopLoadBityRatesSwap());
|
2017-07-31 23:14:30 +00:00
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'CANC':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(stopLoadBityRatesSwap());
|
2017-07-31 23:14:30 +00:00
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'RCVE':
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(showNotification('warning', ORDER_TIMEOUT_MESSAGE, Infinity));
|
2017-07-31 23:14:30 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'FILL':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(stopLoadBityRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
2017-07-31 23:14:30 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
export function* shapeshiftOrderTimeRemaining(): SagaIterator {
|
|
|
|
while (true) {
|
|
|
|
let hasShownNotification = false;
|
|
|
|
while (true) {
|
|
|
|
yield call(delay, ONE_SECOND);
|
|
|
|
const swap = yield select(getSwap);
|
|
|
|
const createdTimeStampMoment = moment(swap.orderTimestampCreatedISOString);
|
|
|
|
const validUntil = moment(createdTimeStampMoment).add(swap.validFor, 's');
|
|
|
|
const now = moment();
|
|
|
|
if (validUntil.isAfter(now)) {
|
|
|
|
const duration = moment.duration(validUntil.diff(now));
|
|
|
|
const seconds = duration.asSeconds();
|
|
|
|
yield put(orderTimeSwap(parseInt(seconds.toString(), 10)));
|
|
|
|
switch (swap.shapeshiftOrderStatus) {
|
|
|
|
case 'failed':
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
|
|
|
}
|
|
|
|
break;
|
2018-01-11 07:08:36 +00:00
|
|
|
case 'received':
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
break;
|
2018-01-02 18:04:50 +00:00
|
|
|
case 'complete':
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
switch (swap.shapeshiftOrderStatus) {
|
|
|
|
case 'no_deposits':
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
2018-02-09 16:07:38 +00:00
|
|
|
yield put(orderTimeSwap(0));
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'failed':
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
2018-02-09 16:07:38 +00:00
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
2018-01-02 18:04:50 +00:00
|
|
|
yield put(showNotification('danger', ORDER_TIMEOUT_MESSAGE, Infinity));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'received':
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(showNotification('warning', ORDER_RECEIVED_MESSAGE, Infinity));
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'complete':
|
|
|
|
yield put(stopPollShapeshiftOrderStatus());
|
|
|
|
yield put(stopLoadShapeshiftRatesSwap());
|
|
|
|
yield put(stopOrderTimerSwap());
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* handleOrderTimeRemaining(): SagaIterator {
|
|
|
|
const swap = yield select(getSwap);
|
|
|
|
let orderTimeRemainingTask;
|
|
|
|
if (swap.provider === 'shapeshift') {
|
|
|
|
orderTimeRemainingTask = yield fork(shapeshiftOrderTimeRemaining);
|
|
|
|
} else {
|
|
|
|
orderTimeRemainingTask = yield fork(bityOrderTimeRemaining);
|
|
|
|
}
|
|
|
|
yield take(TypeKeys.SWAP_ORDER_STOP_TIMER);
|
|
|
|
yield cancel(orderTimeRemainingTask);
|
|
|
|
}
|
|
|
|
|
2018-02-25 01:29:34 +00:00
|
|
|
export default function* swapOrders(): SagaIterator {
|
|
|
|
yield fork(handleOrderTimeRemaining);
|
|
|
|
yield fork(pollShapeshiftOrderStatusSaga);
|
|
|
|
yield fork(pollBityOrderStatusSaga);
|
|
|
|
yield takeEvery(TypeKeys.SWAP_BITY_ORDER_CREATE_REQUESTED, postBityOrderCreate);
|
|
|
|
yield takeEvery(TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_REQUESTED, postShapeshiftOrderCreate);
|
2018-01-02 18:04:50 +00:00
|
|
|
yield takeEvery(TypeKeys.SWAP_ORDER_START_TIMER, handleOrderTimeRemaining);
|
2018-02-25 01:29:34 +00:00
|
|
|
yield takeEvery(TypeKeys.SWAP_RESTART, restartSwap);
|
2018-01-02 18:04:50 +00:00
|
|
|
}
|