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,
|
|
|
|
orderStatusRequestedSwap,
|
|
|
|
orderStatusSucceededSwap,
|
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,
|
2017-07-31 23:14:30 +00:00
|
|
|
stopPollBityOrderStatus
|
|
|
|
} 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';
|
|
|
|
import {
|
|
|
|
call,
|
|
|
|
cancel,
|
|
|
|
cancelled,
|
|
|
|
fork,
|
|
|
|
put,
|
|
|
|
select,
|
|
|
|
take,
|
|
|
|
takeEvery
|
|
|
|
} from 'redux-saga/effects';
|
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;
|
2017-11-30 04:07:16 +00:00
|
|
|
export const BITY_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.
|
|
|
|
`;
|
|
|
|
|
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) {
|
|
|
|
yield put(orderStatusRequestedSwap());
|
|
|
|
const orderStatus = yield call(getOrderStatus, swap.orderId);
|
|
|
|
if (orderStatus.error) {
|
|
|
|
yield put(
|
|
|
|
showNotification(
|
|
|
|
'danger',
|
|
|
|
`Bity Error: ${orderStatus.msg}`,
|
|
|
|
TEN_SECONDS
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
yield put(orderStatusSucceededSwap(orderStatus.data));
|
|
|
|
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!'))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* pollBityOrderStatusSaga(): SagaIterator {
|
2017-07-31 23:14:30 +00:00
|
|
|
while (yield take('SWAP_START_POLL_BITY_ORDER_STATUS')) {
|
|
|
|
// 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
|
|
|
|
yield take('SWAP_STOP_POLL_BITY_ORDER_STATUS');
|
|
|
|
// cancel the background task
|
|
|
|
// this will cause the forked loadBityRates task to jump into its finally block
|
|
|
|
yield cancel(pollBityOrderStatusTask);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-30 04:07:16 +00:00
|
|
|
export function* postBityOrderCreate(
|
2017-09-04 16:30:31 +00:00
|
|
|
action: BityOrderCreateRequestedSwapAction
|
2017-09-25 02:06:28 +00:00
|
|
|
): 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?
|
|
|
|
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';
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* postBityOrderSaga(): SagaIterator {
|
2017-07-31 23:14:30 +00:00
|
|
|
yield takeEvery('SWAP_ORDER_CREATE_REQUESTED', postBityOrderCreate);
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
export function* bityTimeRemaining(): SagaIterator {
|
2017-07-31 23:14:30 +00:00
|
|
|
while (yield take('SWAP_ORDER_START_TIMER')) {
|
|
|
|
let hasShownNotification = false;
|
|
|
|
while (true) {
|
|
|
|
yield call(delay, ONE_SECOND);
|
|
|
|
const swap = yield select(getSwap);
|
|
|
|
// if (swap.bityOrder.status === 'OPEN') {
|
|
|
|
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)));
|
2017-07-31 23:14:30 +00:00
|
|
|
// TODO (!Important) - check orderStatus here and stop polling / show notifications based on status
|
|
|
|
} else {
|
|
|
|
switch (swap.orderStatus) {
|
|
|
|
case 'OPEN':
|
|
|
|
yield put(orderTimeSwap(0));
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put({ type: 'SWAP_STOP_LOAD_BITY_RATES' });
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(
|
2017-11-29 00:17:26 +00:00
|
|
|
showNotification('danger', BITY_TIMEOUT_MESSAGE, Infinity)
|
2017-07-31 23:14:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'CANC':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put({ type: 'SWAP_STOP_LOAD_BITY_RATES' });
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(
|
2017-11-29 00:17:26 +00:00
|
|
|
showNotification('danger', BITY_TIMEOUT_MESSAGE, Infinity)
|
2017-07-31 23:14:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'RCVE':
|
|
|
|
if (!hasShownNotification) {
|
|
|
|
hasShownNotification = true;
|
|
|
|
yield put(
|
2017-11-29 00:17:26 +00:00
|
|
|
showNotification('warning', BITY_TIMEOUT_MESSAGE, Infinity)
|
2017-07-31 23:14:30 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'FILL':
|
|
|
|
yield put(stopPollBityOrderStatus());
|
|
|
|
yield put({ type: 'SWAP_STOP_LOAD_BITY_RATES' });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|