MyCrypto/common/sagas/swap/liteSend.ts
Danny Skubak b939e8ceda Request Payment SubTab - EIP 681 (#671)
* progress

* Normalize bity api response

* Filter api response

* Track swap information in component state

* Update dropdown onchange

* remove dead code

* Update Min Max Validation

* Update minmax err msg && fix onChangeOriginKind

* Add origin & destination to redux state

* Update types & Update tests

* Update types

* Update swap.spec.ts test

* Remove commented out code

* Remove hardcoded coin array

* Create types.ts for swap reducer

* Update swapinput type

* Update bityRates in localStorage & Replace all instances of ...Kind / ...Amount props

* Add shapeshift banner

* initial work for sagas

* Update Types

* Update swap reducer initial state

* Update Types & Store empty obj for bityRates / options

* Update more types

* added shapeshift file and rates comments

* action reducers and prop mapping to components

* add typings and swap icon

* more actions reducers and sagas

* debugging shapeshift service

* add Headers

* Fix content type

* add order reset saga and ui fixes

* remove console log and swap b/w Bity and Shapeshift

* working state for Shapeshift and Bity - tested with mainnet

* add icon component

* UI improvements and fix select bug

* fix timer bug

* add bity fallback options and toFixed floats

* tslint errors

* add arrow to dropdown and add support footer

* Add service provider

* fix minor $ bug and stop timer on order complete

* better load UX and dropdown UX

* fixed single test

* currRate prop bugs and reduce LS bloat

* takeEvery on timer saga and don't clear state.options to restartSwap reducer

* export tx sagas and fix minor type

* Add ShapeShift Rates functionality when selecting a ShapeShift pair.

* type fixes

* BugFix: Don't change displayed ShapeShift Rate Inputs on every dropdown change
Also contains some caching / performance improvements

* BugFix: Don't remote rate inputs when falsy amount

* fix type error

* Progress commit

* Implement saga logic

* Make address field factory component

* Shorten debounce time

* Make new actions / sagas  for handling single token lookup

* Implement working version of litesend

* make unit dropdown searchable, add props for all tokens, custom validation

* add string generators for EIP 681 token & ether transactions

* add new selectors

* add request payment tab

* Change saga into selector

* Add failing spec

* fix broken test

* add debounce to error message

* fix tests

* update snapshots

* test coverage

* move setState disabled property from debounce so we instantly can go to next step on valid amounts

* reset amount value (useful for switching between tabs)

* much deeper test coverage, fix debounce ux, and fix bity flashing at swap page load

* fix minor failing test

* seperate shapeshift erc20 token whitelist

* fix saveState store bug

* break orderTimeRemaining saga up and rewrite tests

* fix tslint error

* add isReadOnly prop to address field

* use AddressField component, add additional validation

* make prop optional

* correct validation

* allow for request tab to be used with view only wallet

* account for undefined activeTab

* add types
2018-01-02 21:18:10 -06:00

109 lines
3.6 KiB
TypeScript

import { SagaIterator, delay } from 'redux-saga';
import { select, put, call, take, race, fork, cancel, takeEvery } from 'redux-saga/effects';
import { getOrigin, getPaymentAddress } from 'selectors/swap';
import {
setUnitMeta,
setCurrentTo,
setCurrentValue,
TypeKeys as TransactionTK,
reset
} from 'actions/transaction';
import { TypeKeys as WalletTK, setTokenBalancePending } from 'actions/wallet';
import { AppState } from 'reducers';
import { showNotification } from 'actions/notifications';
import { isSupportedUnit } from 'selectors/config';
import { isEtherUnit } from 'libs/units';
import { showLiteSend, configureLiteSend } from 'actions/swap';
import { TypeKeys as SwapTK } from 'actions/swap/constants';
import { isUnlocked } from 'selectors/wallet';
type SwapState = AppState['swap'];
export function* configureLiteSendSaga(): SagaIterator {
const { amount, id }: SwapState['origin'] = yield select(getOrigin);
const paymentAddress: SwapState['paymentAddress'] = yield call(fetchPaymentAddress);
if (!paymentAddress) {
yield put(showNotification('danger', 'Could not fetch payment address'));
return yield put(showLiteSend(false));
}
const supportedUnit: boolean = yield select(isSupportedUnit, id);
if (!supportedUnit) {
return yield put(showLiteSend(false));
}
const unlocked: boolean = yield select(isUnlocked);
yield put(showLiteSend(true));
// wait for wallet to be unlocked to continue
if (!unlocked) {
yield take(WalletTK.WALLET_SET);
}
//if it's a token, manually scan for that tokens balance and wait for it to resolve
if (!isEtherUnit(id)) {
yield put(setTokenBalancePending({ tokenSymbol: id }));
yield take([
WalletTK.WALLET_SET_TOKEN_BALANCE_FULFILLED,
WalletTK.WALLET_SET_TOKEN_BALANCE_REJECTED
]);
}
yield put(setUnitMeta(id));
yield put(setCurrentValue(amount.toString()));
yield put(setCurrentTo(paymentAddress));
}
export function* handleConfigureLiteSend(): SagaIterator {
while (true) {
const liteSendProc = yield fork(configureLiteSendSaga);
const result = yield race({
transactionReset: take(TransactionTK.RESET),
userNavigatedAway: take(WalletTK.WALLET_RESET),
bityPollingFinished: take(SwapTK.SWAP_STOP_POLL_BITY_ORDER_STATUS),
shapeshiftPollingFinished: take(SwapTK.SWAP_STOP_POLL_SHAPESHIFT_ORDER_STATUS)
});
//if polling is finished we should clear state and hide this tab
if (result.bityPollingFinished || result.shapeshiftPollingFinished) {
//clear transaction state and cancel saga
yield cancel(liteSendProc);
yield put(showLiteSend(false));
return yield put(reset());
}
if (result.transactionReset) {
yield cancel(liteSendProc);
}
// if wallet reset is called, that means the user navigated away from the page, so we cancel everything
if (result.userNavigatedAway) {
yield cancel(liteSendProc);
yield put(showLiteSend(false));
return yield put(configureLiteSend());
}
// else the user just swapped to a new wallet, and we'll race against liteSend again to re-apply
// the same transaction parameters again
}
}
export function* fetchPaymentAddress(): SagaIterator {
const MAX_RETRIES = 5;
let currentTry = 0;
while (currentTry <= MAX_RETRIES) {
yield call(delay, 500);
const paymentAddress: SwapState['paymentAddress'] = yield select(getPaymentAddress);
if (paymentAddress) {
return paymentAddress;
}
currentTry++;
}
yield put(showNotification('danger', 'Payment address not found'));
return false;
}
export function* liteSend(): SagaIterator {
yield takeEvery(SwapTK.SWAP_CONFIGURE_LITE_SEND, handleConfigureLiteSend);
}