mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-10 02:55:41 +00:00
5d4b36d453
* Refactor babel/types * Refactor entry point * Refactor actions * Refactor api * Full project refactor -- Broad type fixing sweep * - completely fix merge conflicts - handle various type errors * Add tslint to package.json * Dependency cleanup * Fix module resolution * Work on type definitions for untyped libs * progress commit * Add more definition typing * various type additions * Add unit types * Fix sagaiterator + unit types * various types added * additional type additions * Fix typing on Sagas * remove flowfixmes; swap translate for translateRaw * Get rid of contracts - awaiting Henry's contract PR * Remove contracts from routing * Fix most of actions/reducers * refactor actions directory structure * fix reducer action type imports * Fix most of type errors pre-actions refactor * fix action creator imports in containers * Refactor more * Refactor index of actions * fix action imports; use module level index export * package-lock.json updated * Use action types in props * Type up action creators * Fix most of connect errors * Typefixing progress * More types * Fix run-time errors * Caching improvements for webpack * Remove path resolve from webpack * Update non-breaking packages to latest version * Fix token typing * Remove unused color code * Fix wallet decrypt dispatch * Set redux-form related props/functions to ANY, since we're stripping it out later on * Revert BigNumber.js package changes * Extend window to custom object for Perf * Format Navigation * Typecase keystore errors as any (since we shouldnt touch this) * Push wallet context fix * - find/replace value->payload in swap - properly type swap state properties - extract inline reducer into reducer function * - type local storage retrieved items as generic * - bind all RPCClient methods with fat arrow * - reformat * Change to enums for constants * Change state into any * Fix swap errors * ensure that seconds are passed into state as integers * Fix rest of errors * use parseInt explicitly instead of type coercion * Fix derivation-checker, remove flow command, add tslint command, add tslint-react, tell travis to use tslint instead of flow. * Whoops, remove those tests. * Remove unsupported (yet) config option. * Fix precommit to target ts and tsx files. * Fix some errors, ignore some silly rules. * Revert jest to v19, use ts-jest and make all tests typescript. Fixes all but one. * Get rid of saga tests * Fix tslint errors
180 lines
5.6 KiB
TypeScript
180 lines
5.6 KiB
TypeScript
import { showNotification } from 'actions/notifications';
|
|
import {
|
|
bityOrderCreateFailedSwap,
|
|
BityOrderCreateRequestedSwapAction,
|
|
bityOrderCreateSucceededSwap,
|
|
changeStepSwap,
|
|
orderStatusRequestedSwap,
|
|
orderStatusSucceededSwap,
|
|
orderTimeSwap,
|
|
startOrderTimerSwap,
|
|
startPollBityOrderStatus,
|
|
stopLoadBityRatesSwap,
|
|
stopPollBityOrderStatus
|
|
} from 'actions/swap';
|
|
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';
|
|
|
|
export const getSwap = (state: AppState): SwapState => state.swap;
|
|
const ONE_SECOND = 1000;
|
|
const TEN_SECONDS = ONE_SECOND * 10;
|
|
const BITY_TIMEOUT_MESSAGE = `
|
|
Time has run out.
|
|
If you have already sent, please wait 1 hour.
|
|
If your order has not be processed after 1 hour,
|
|
please press the orange 'Issue with your Swap?' button.
|
|
`;
|
|
|
|
export function* pollBityOrderStatus(): SagaIterator {
|
|
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!'))
|
|
}
|
|
}
|
|
}
|
|
|
|
export function* pollBityOrderStatusSaga(): SagaIterator {
|
|
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);
|
|
}
|
|
}
|
|
|
|
function* postBityOrderCreate(
|
|
action: BityOrderCreateRequestedSwapAction
|
|
): SagaIterator {
|
|
const payload = action.payload;
|
|
try {
|
|
yield put(stopLoadBityRatesSwap());
|
|
const order = yield call(
|
|
postOrder,
|
|
payload.amount,
|
|
payload.destinationAddress,
|
|
payload.mode,
|
|
payload.pair
|
|
);
|
|
if (order.error) {
|
|
// TODO - handle better / like existing site?
|
|
yield put(
|
|
showNotification('danger', `Bity Error: ${order.msg}`, TEN_SECONDS)
|
|
);
|
|
yield put(bityOrderCreateFailedSwap());
|
|
} 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));
|
|
yield put(bityOrderCreateFailedSwap());
|
|
}
|
|
}
|
|
|
|
export function* postBityOrderSaga(): SagaIterator {
|
|
yield takeEvery('SWAP_ORDER_CREATE_REQUESTED', postBityOrderCreate);
|
|
}
|
|
|
|
export function* bityTimeRemaining(): SagaIterator {
|
|
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
|
|
);
|
|
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)));
|
|
// 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(
|
|
showNotification('danger', BITY_TIMEOUT_MESSAGE, 'infinity')
|
|
);
|
|
}
|
|
break;
|
|
case 'CANC':
|
|
yield put(stopPollBityOrderStatus());
|
|
yield put({ type: 'SWAP_STOP_LOAD_BITY_RATES' });
|
|
if (!hasShownNotification) {
|
|
hasShownNotification = true;
|
|
yield put(
|
|
showNotification('danger', BITY_TIMEOUT_MESSAGE, 'infinity')
|
|
);
|
|
}
|
|
break;
|
|
case 'RCVE':
|
|
if (!hasShownNotification) {
|
|
hasShownNotification = true;
|
|
yield put(
|
|
showNotification('warning', BITY_TIMEOUT_MESSAGE, 'infinity')
|
|
);
|
|
}
|
|
break;
|
|
case 'FILL':
|
|
yield put(stopPollBityOrderStatus());
|
|
yield put({ type: 'SWAP_STOP_LOAD_BITY_RATES' });
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|