From e57b0640e69619e48731e9f19f3facd3735be463 Mon Sep 17 00:00:00 2001 From: Milan Vasic Date: Fri, 7 Jun 2024 12:08:50 +0200 Subject: [PATCH] feat(rates): removed usage of socket for getting rates --- .../blockchain-wallet-v4-frontend/codegen.yml | 14 +- .../src/data/middleware/sagaRegister.js | 4 +- .../src/data/middleware/sagas.js | 5 +- .../data/middleware/webSocket/actionTypes.js | 3 +- .../src/data/middleware/webSocket/actions.js | 3 +- .../middleware/webSocket/rates/actionTypes.js | 23 -- .../middleware/webSocket/rates/actions.js | 57 ---- .../webSocket/rates/sagaRegister.js | 20 -- .../data/middleware/webSocket/rates/sagas.js | 197 ------------ .../data/middleware/webSocket/sagaRegister.js | 4 +- .../src/data/middleware/webSocket/sagas.js | 4 +- .../src/data/model.ts | 3 +- .../src/data/modules/actionTypes.ts | 3 +- .../src/data/modules/actions.ts | 3 +- .../src/data/modules/profile/sagas.ts | 7 - .../src/data/modules/rates/actionTypes.js | 20 -- .../src/data/modules/rates/actions.js | 60 ---- .../src/data/modules/rates/model.js | 164 ---------- .../src/data/modules/rates/rates.spec.js | 280 ------------------ .../src/data/modules/rates/reducers.js | 70 ----- .../src/data/modules/rates/sagaRegister.js | 17 -- .../src/data/modules/rates/sagas.js | 62 ---- .../src/data/modules/rates/selectors.js | 22 -- .../src/data/modules/sagaRegister.js | 2 - .../src/data/modules/sagas.js | 2 - .../src/data/modules/selectors.ts | 3 +- .../src/data/rootReducer.ts | 2 - .../src/data/rootSaga.ts | 4 +- .../src/middleware/index.js | 3 +- .../src/middleware/webSocketRates.js | 42 --- .../src/store/index.js | 11 +- 31 files changed, 24 insertions(+), 1090 deletions(-) delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actionTypes.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actions.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagaRegister.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagas.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actionTypes.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actions.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/model.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/rates.spec.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/reducers.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagaRegister.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagas.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/data/modules/rates/selectors.js delete mode 100644 packages/blockchain-wallet-v4-frontend/src/middleware/webSocketRates.js diff --git a/packages/blockchain-wallet-v4-frontend/codegen.yml b/packages/blockchain-wallet-v4-frontend/codegen.yml index c50e5ef39e7..5847901aadf 100644 --- a/packages/blockchain-wallet-v4-frontend/codegen.yml +++ b/packages/blockchain-wallet-v4-frontend/codegen.yml @@ -1,13 +1,13 @@ overwrite: true -schema: "https://api.dev.blockchain.info/nft-market-api/graphql/" -documents: "src/**/*.graphql" +schema: 'https://api.dev.blockchain.info/nft-market-api/graphql/' +documents: 'src/**/*.graphql' generates: src/generated/graphql.types.ts: plugins: - - "typescript" - - "typescript-operations" - - "urql-introspection" - - "typescript-urql" + - 'typescript' + - 'typescript-operations' + - 'urql-introspection' + - 'typescript-urql' ./graphql.schema.json: plugins: - - "introspection" + - 'introspection' diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagaRegister.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagaRegister.js index c221d05487c..675b3140e06 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagaRegister.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagaRegister.js @@ -2,7 +2,7 @@ import { fork } from 'redux-saga/effects' import webSocket from './webSocket/sagaRegister' -export default ({ api, coinsSocket, ratesSocket }) => +export default ({ api, coinsSocket }) => function* middlewareSaga() { - yield fork(webSocket({ api, coinsSocket, ratesSocket })) + yield fork(webSocket({ api, coinsSocket })) } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagas.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagas.js index 4eab26a2094..2c57fdffb5d 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagas.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/sagas.js @@ -1,9 +1,8 @@ import webSocket from './webSocket/sagas' -export default ({ api, coinsSocket, ratesSocket }) => ({ +export default ({ api, coinsSocket }) => ({ webSocket: webSocket({ api, - coinsSocket, - ratesSocket + coinsSocket }) }) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actionTypes.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actionTypes.js index 10836d46837..ddff6493d26 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actionTypes.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actionTypes.js @@ -1,5 +1,4 @@ import * as coins from './coins/actionTypes' -import * as rates from './rates/actionTypes' import * as xlm from './xlm/actionTypes' -export { coins, rates, xlm } +export { coins, xlm } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actions.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actions.js index 511867f27ee..753cb498ee2 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actions.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/actions.js @@ -1,5 +1,4 @@ import * as coins from './coins/actions' -import * as rates from './rates/actions' import * as xlm from './xlm/actions' -export { coins, rates, xlm } +export { coins, xlm } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actionTypes.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actionTypes.js deleted file mode 100644 index c3573cf4fd6..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actionTypes.js +++ /dev/null @@ -1,23 +0,0 @@ -export const START_SOCKET = '@EVENT.RATES_SOCKET.WEBSOCKET_START' -export const STOP_SOCKET = '@EVENT.RATES_SOCKET.WEBSOCKET_STOP' - -export const OPEN_SOCKET = '@EVENT.RATES_SOCKET.WEBSOCKET_OPEN' -export const MESSAGE_SOCKET = '@EVENT.RATES_SOCKET.WEBSOCKET_MESSAGE' -export const CLOSE_SOCKET = '@EVENT.RATES_SOCKET.WEBSOCKET_CLOSE' -export const REST_FALLBACK = '@EVENT.RATES_SOCKET.REST_FALLBACK' - -export const AUTHENTICATE_SOCKET = '@EVENT.RATES_SOCKET.AUTHENTICATE_SOCKET' - -export const OPEN_RATES_CHANNEL = '@EVENT.RATES_SOCKET.OPEN_RATES_CHANNEL' -export const CLOSE_RATES_CHANNEL = '@EVENT.RATES_SOCKET.CLOSE_RATES_CHANNEL' - -export const OPEN_ADVICE_CHANNEL = '@EVENT.RATES_SOCKET.OPEN_ADVICE_CHANNEL' -export const CLOSE_ADVICE_CHANNEL = '@EVENT.RATES_SOCKET.CLOSE_ADVICE_CHANNEL' - -export const ADVICE_SUBSCRIBE_SUCCESS = '@EVENT.RATES_SOCKET.ADVICE_SUBSCRIBE_SUCCESS' -export const ADVICE_UNSUBSCRIBE_SUCCESS = '@EVENT.RATES_SOCKET.ADVICE_UNSUBSCRIBE_SUCCESS' -export const ADVICE_SUBSCRIBE_ERROR = '@EVENT.RATES_SOCKET.ADVICE_SUBSCRIBE_ERROR' - -export const RATES_SUBSCRIBE_SUCCESS = '@EVENT.RATES_SOCKET.RATES_SUBSCRIBE_SUCCESS' -export const RATES_UNSUBSCRIBE_SUCCESS = '@EVENT.RATES_SOCKET.RATES_UNSUBSCRIBE_SUCCESS' -export const RATES_SUBSCRIBE_ERROR = '@EVENT.RATES_SOCKET.RATES_SUBSCRIBE_ERROR' diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actions.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actions.js deleted file mode 100644 index 82bf8bd3804..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/actions.js +++ /dev/null @@ -1,57 +0,0 @@ -import * as AT from './actionTypes' - -export const startSocket = () => ({ type: AT.START_SOCKET }) -export const stopSocket = () => ({ type: AT.STOP_SOCKET }) - -export const openSocket = () => ({ type: AT.OPEN_SOCKET }) -export const messageSocket = (message) => ({ - payload: { message }, - type: AT.MESSAGE_SOCKET -}) -export const closeSocket = () => ({ type: AT.CLOSE_SOCKET }) -export const restFallback = () => ({ type: AT.REST_FALLBACK }) - -export const authenticateSocket = () => ({ type: AT.AUTHENTICATE_SOCKET }) - -export const openRatesChannel = (pairs) => ({ - payload: { pairs }, - type: AT.OPEN_RATES_CHANNEL -}) -export const closeRatesChannel = () => ({ - type: AT.CLOSE_RATES_CHANNEL -}) - -export const openAdviceChannel = (pair, volume, fix, fiatCurrency) => ({ - payload: { fiatCurrency, fix, pair, volume }, - type: AT.OPEN_ADVICE_CHANNEL -}) -export const closeAdviceChannel = (pair) => ({ - payload: { pair }, - type: AT.CLOSE_ADVICE_CHANNEL -}) - -export const adviceSubscribeSuccess = (pair) => ({ - payload: { pair }, - type: AT.ADVICE_SUBSCRIBE_SUCCESS -}) -export const adviceUnsubscribeSuccess = (pair) => ({ - payload: { pair }, - type: AT.ADVICE_UNSUBSCRIBE_SUCCESS -}) -export const adviceSubscribeError = (pair, error) => ({ - payload: { error, pair }, - type: AT.ADVICE_SUBSCRIBE_ERROR -}) - -export const ratesSubscribeSuccess = (pairs) => ({ - payload: { pairs }, - type: AT.RATES_SUBSCRIBE_SUCCESS -}) -export const ratesUnsubscribeSuccess = (pairs) => ({ - payload: { pairs }, - type: AT.RATES_UNSUBSCRIBE_SUCCESS -}) -export const ratesSubscribeError = (pairs, error) => ({ - payload: { error, pairs }, - type: AT.RATES_SUBSCRIBE_ERROR -}) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagaRegister.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagaRegister.js deleted file mode 100644 index c2a98228641..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagaRegister.js +++ /dev/null @@ -1,20 +0,0 @@ -import { takeEvery } from 'redux-saga/effects' - -import * as AT from './actionTypes' -import sagasFactory from './sagas' - -export default ({ api, ratesSocket }) => { - const sagas = sagasFactory({ api, ratesSocket }) - - return function* ratesSocketSaga() { - yield takeEvery(AT.OPEN_SOCKET, sagas.onOpen) - yield takeEvery(AT.MESSAGE_SOCKET, sagas.onMessage) - yield takeEvery(AT.CLOSE_SOCKET, sagas.onClose) - yield takeEvery(AT.REST_FALLBACK, sagas.restFallback) - yield takeEvery(AT.AUTHENTICATE_SOCKET, sagas.authenticateSocket) - yield takeEvery(AT.OPEN_ADVICE_CHANNEL, sagas.openAdviceChannel) - yield takeEvery(AT.CLOSE_ADVICE_CHANNEL, sagas.closeAdviceChannel) - yield takeEvery(AT.OPEN_RATES_CHANNEL, sagas.openRatesChannel) - yield takeEvery(AT.CLOSE_RATES_CHANNEL, sagas.closeRatesChannel) - } -} diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagas.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagas.js deleted file mode 100644 index 1f2159c48ff..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/rates/sagas.js +++ /dev/null @@ -1,197 +0,0 @@ -import { - both, - complement, - compose, - either, - has, - indexBy, - isEmpty, - isNil, - map, - prop, - unnest, - values, - whereEq -} from 'ramda' -import { all, call, delay, put, select } from 'redux-saga/effects' - -import { actions, model, selectors } from 'data' - -import * as A from './actions' - -export const socketAuthRetryDelay = 5000 -const openChannels = { - advice: {}, - rates: {} -} - -export default ({ api, ratesSocket }) => { - const isAuthError = whereEq(model.rates.AUTH_ERROR_MESSAGE) - - const isAdviceSubscribeSuccess = whereEq(model.rates.ADVICE_SUBSCRIBE_SUCCESS_MESSAGE) - - const isAdviceSubscribeError = either( - whereEq(model.rates.ADVICE_SUBSCRIBE_ERROR_MESSAGE), - both(whereEq(model.rates.ADVICE_UPDATED_MESSAGE), has('error')) - ) - - const isAdviceUnsubscribeSuccess = whereEq(model.rates.ADVICE_UNSUBSCRIBE_SUCCESS_MESSAGE) - - const isAdviceMessage = both( - either( - whereEq(model.rates.ADVICE_UPDATED_MESSAGE), - whereEq(model.rates.ADVICE_SNAPSHOT_MESSAGE) - ), - complement(has('error')) - ) - - const isRatesSubscribeSuccess = whereEq(model.rates.RATES_SUBSCRIBE_SUCCESS_MESSAGE) - - const isRatesSubscribeError = whereEq(model.rates.RATES_SUBSCRIBE_ERROR_MESSAGE) - - const isRatesUnubscribeSuccess = whereEq(model.rates.RATES_UNSUBSCRIBE_SUCCESS_MESSAGE) - - const isRatesMessage = either( - whereEq(model.rates.RATES_UPDATED_MESSAGE), - whereEq(model.rates.RATES_SNAPSHOT_MESSAGE) - ) - - const authenticateSocket = function* () { - const token = (yield select(selectors.modules.profile.getApiToken)).getOrElse('') - ratesSocket.send(model.rates.getAuthMessage(token)) - } - - const reopenChannels = function () { - map(ratesSocket.send.bind(ratesSocket), values(openChannels.rates)) - map(ratesSocket.send.bind(ratesSocket), values(openChannels.advice)) - } - - const onOpen = function* () { - yield call(authenticateSocket) - yield call(reopenChannels) - } - - const onMessage = function* ({ payload: { message } }) { - if (isAuthError(message)) { - yield delay(socketAuthRetryDelay) - yield call(authenticateSocket) - } - if (isAdviceSubscribeSuccess(message)) yield put(A.adviceSubscribeSuccess(message.pair)) - if (isAdviceSubscribeError(message)) - yield put(A.adviceSubscribeError(message.pair, message.error)) - if (isAdviceUnsubscribeSuccess(message)) yield put(A.adviceUnsubscribeSuccess(message.pair)) - if (isAdviceMessage(message)) yield put(actions.modules.rates.updateAdvice(message.quote)) - if (isRatesSubscribeSuccess(message)) yield put(A.ratesSubscribeSuccess(message.pairs)) - if (isRatesSubscribeError(message)) - yield put(A.ratesSubscribeError(message.pairs, message.error)) - if (isRatesUnubscribeSuccess(message)) yield put(A.ratesUnsubscribeSuccess(message.pairs)) - if (isRatesMessage(message)) - yield put(actions.modules.rates.updateBestRates(indexBy(prop('pair'), message.rates))) - } - - const fetchAdvice = function* ({ config: { fiatCurrency, fix, volume }, pair }) { - try { - const { error, ratio } = yield call(api.fetchAdvice, pair, volume, fix, fiatCurrency) - if (error) throw error - const date = new Date() - const isoDate = date.toISOString() - // manually create UTC format - const time = `${isoDate.substr(0, 10)}T${isoDate.substr(11, 8)}Z` - - yield put( - actions.modules.rates.updateAdvice({ - currencyRatio: ratio, - fiatCurrency, - fix, - pair, - time, - volume - }) - ) - } catch (e) { - yield put(A.adviceSubscribeError(pair, e)) - } - } - - const fetchRates = function* (pairs) { - try { - const { rates } = yield call(api.fetchBestRates, pairs) - yield put( - actions.modules.rates.updateBestRates( - compose( - indexBy(prop('pair')), - map(({ symbol, value }) => ({ pair: symbol, price: value })) - )(rates) - ) - ) - } catch (e) { - yield put(A.ratesSubscribeError(pairs, e)) - } - } - - const restFallback = function* () { - const pairs = yield select(selectors.modules.rates.getActivePairs) - if (!isEmpty(pairs)) { - yield all( - unnest( - map((pair) => { - const pairs = model.rates.getBestRatesPairs( - ...model.rates.splitPair(pair.pair), - pair.config.fiatCurrency - ) - return [fetchAdvice(pair), fetchRates(pairs)] - }, pairs) - ) - ) - } - } - - const onClose = function* (action) {} - - const openRatesChannel = function* ({ payload }) { - const { pairs } = payload - if (ratesSocket.isReady()) { - const message = model.rates.getRatesSubscribeMessage(pairs) - openChannels.rates[pairs] = message - return ratesSocket.send(message) - } - yield call(fetchRates, pairs) - } - - const closeRatesChannel = function () { - openChannels.rates = {} - - if (ratesSocket.isReady()) { - ratesSocket.send(model.rates.getRatesUnsubscribeMessage()) - } - } - - const openAdviceChannel = function* ({ payload }) { - const { fiatCurrency, fix, pair, volume } = payload - if (isNil(volume) || !fix || !fiatCurrency) return - if (ratesSocket.isReady()) { - const message = model.rates.getAdviceSubscribeMessage(pair, volume, fix, fiatCurrency) - openChannels.advice[pair] = message - return ratesSocket.send(message) - } - yield call(fetchAdvice, { config: { fiatCurrency, fix, volume }, pair }) - } - - const closeAdviceChannel = function ({ payload }) { - const { pair } = payload - delete openChannels.advice[pair] - if (ratesSocket.isReady()) ratesSocket.send(model.rates.getAdviceUnsubscribeMessage(pair)) - } - - return { - authenticateSocket, - closeAdviceChannel, - closeRatesChannel, - onClose, - onMessage, - onOpen, - openAdviceChannel, - openRatesChannel, - restFallback - } -} diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagaRegister.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagaRegister.js index fd6b64decf8..69e0a690fd5 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagaRegister.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagaRegister.js @@ -1,12 +1,10 @@ import { fork } from 'redux-saga/effects' import coins from './coins/sagaRegister' -import rates from './rates/sagaRegister' import xlm from './xlm/sagaRegister' -export default ({ api, coinsSocket, ratesSocket }) => +export default ({ api, coinsSocket }) => function* webSocketSaga() { yield fork(xlm()) - yield fork(rates({ api, ratesSocket })) yield fork(coins({ api, coinsSocket })) } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagas.js b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagas.js index bac12cad907..13b7b25cf1e 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagas.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/middleware/webSocket/sagas.js @@ -1,9 +1,7 @@ import coins from './coins/sagas' -import rates from './rates/sagas' import xlm from './xlm/sagas' -export default ({ api, coinsSocket, ratesSocket }) => ({ - rates: rates({ api, ratesSocket }), +export default ({ api, coinsSocket }) => ({ sds: coins({ api, coinsSocket }), xlm: xlm() }) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/model.ts b/packages/blockchain-wallet-v4-frontend/src/data/model.ts index 6c72a50dd1c..16638237d3d 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/model.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/model.ts @@ -3,6 +3,5 @@ import * as components from './components/model' import * as form from './form/model' import * as logs from './logs/model' import * as profile from './modules/profile/model' -import * as rates from './modules/rates/model' -export { coins, components, form, logs, profile, rates } +export { coins, components, form, logs, profile } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/actionTypes.ts b/packages/blockchain-wallet-v4-frontend/src/data/modules/actionTypes.ts index 660d5944287..eaad2af9507 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/actionTypes.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/actionTypes.ts @@ -1,7 +1,6 @@ import * as addressesBch from './addressesBch/actionTypes' import * as profile from './profile/actionTypes' -import * as rates from './rates/actionTypes' import * as securityCenter from './securityCenter/actionTypes' import * as settings from './settings/actionTypes' -export { addressesBch, profile, rates, securityCenter, settings } +export { addressesBch, profile, securityCenter, settings } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/actions.ts b/packages/blockchain-wallet-v4-frontend/src/data/modules/actions.ts index 3be4c03326b..c0437084eff 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/actions.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/actions.ts @@ -1,8 +1,7 @@ import * as addressesBch from './addressesBch/actions' import * as profile from './profile/actions' -import * as rates from './rates/actions' import * as securityCenter from './securityCenter/actions' import * as settings from './settings/actions' import { actions as transferEth } from './transferEth/transferEthSlice' -export { addressesBch, profile, rates, securityCenter, settings, transferEth } +export { addressesBch, profile, securityCenter, settings, transferEth } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/profile/sagas.ts b/packages/blockchain-wallet-v4-frontend/src/data/modules/profile/sagas.ts index 72064de79f7..5e04faa5e37 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/profile/sagas.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/profile/sagas.ts @@ -28,11 +28,6 @@ let renewSessionTask = null let renewUserTask = null export default ({ api, coreSagas, networks }) => { - const renewApiSockets = function* () { - yield put(actions.middleware.webSocket.rates.stopSocket()) - yield put(actions.middleware.webSocket.rates.startSocket()) - } - const waitForUserId = function* () { const userCredentials = yield select( selectors.core.kvStore.unifiedCredentials.getUnifiedOrLegacyNabuEntry @@ -177,7 +172,6 @@ export default ({ api, coreSagas, networks }) => { ) yield put(A.setApiTokenSuccess(apiToken)) yield call(fetchUser) - yield call(renewApiSockets) const expiresIn = Math.abs( differenceInMilliseconds(subSeconds(new Date(expiresAt), 5), new Date()) ) @@ -931,7 +925,6 @@ export default ({ api, coreSagas, networks }) => { migrateSofiUser, recoverUser, redirectAfterAssociation, - renewApiSockets, renewSession, renewUser, setSession, diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actionTypes.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actionTypes.js deleted file mode 100644 index f762baf39b8..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actionTypes.js +++ /dev/null @@ -1,20 +0,0 @@ -export const SUBSCRIBE_TO_RATES = '@EVENT.RATES.SUBSCRIBE_TO_RATES' -export const UNSUBSCRIBE_FROM_RATES = '@EVENT.RATES.UNSUBSCRIBE_FROM_RATES' - -export const SUBSCRIBE_TO_ADVICE = '@EVENT.RATES.SUBSCRIBE_TO_ADVICE' -export const UNSUBSCRIBE_FROM_ADVICE = '@EVENT.RATES.UNSUBSCRIBE_FROM_ADVICE' -export const UPDATE_ADVICE = '@EVENT.RATES_SOCKET.UPDATE_ADVICE' - -export const UPDATE_PAIR_CONFIG = '@DATA.RATES.UPDATE_PAIR_CONFIG' -export const SET_PAIR_QUOTE = '@DATA.RATES.SET_PAIR_QUOTE' -export const PAIR_UPDATED = '@EVENT.RATES.PAIR_UPDATED' - -export const FETCH_AVAILABLE_PAIRS = '@EVENT.RATES.FETCH_AVAILABLE_PAIRS' - -export const AVAILABLE_PAIRS_LOADING = '@DATA.RATES.AVAILABLE_PAIRS_LOADING' -export const AVAILABLE_PAIRS_SUCCESS = '@DATA.RATES.AVAILABLE_PAIRS_SUCCESS' -export const AVAILABLE_PAIRS_ERROR = '@DATA.RATES.AVAILABLE_PAIRS_ERROR' - -export const UPDATE_BEST_RATES = '@DATA.RATES.UPDATE_BEST_RATES' - -export const REMOVE_ADVICE = '@DATA.RATES.REMOVE_ADVICE' diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actions.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actions.js deleted file mode 100644 index bc59f6e7015..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/actions.js +++ /dev/null @@ -1,60 +0,0 @@ -import * as AT from './actionTypes' - -export const subscribeToAdvice = (pair, volume, fix, fiatCurrency) => ({ - payload: { fiatCurrency, fix, pair, volume }, - type: AT.SUBSCRIBE_TO_ADVICE -}) -export const unsubscribeFromAdvice = (pair) => ({ - payload: { pair }, - type: AT.UNSUBSCRIBE_FROM_ADVICE -}) -export const updateAdvice = (quote) => ({ - payload: { quote }, - type: AT.UPDATE_ADVICE -}) - -export const updatePairConfig = (pair, volume, fix, fiatCurrency) => ({ - payload: { config: { fiatCurrency, fix, volume }, pair }, - type: AT.UPDATE_PAIR_CONFIG -}) -export const setPairQuote = (pair, quote) => ({ - payload: { pair, quote }, - type: AT.SET_PAIR_QUOTE -}) -export const pairUpdated = (pair) => ({ - payload: { pair }, - type: AT.PAIR_UPDATED -}) - -export const fetchAvailablePairs = () => ({ - type: AT.FETCH_AVAILABLE_PAIRS -}) -export const availablePairsLoading = () => ({ - type: AT.AVAILABLE_PAIRS_LOADING -}) -export const availablePairsSuccess = (pairs) => ({ - payload: { pairs }, - type: AT.AVAILABLE_PAIRS_SUCCESS -}) -export const availablePairsError = (error) => ({ - payload: { error }, - type: AT.AVAILABLE_PAIRS_ERROR -}) - -export const subscribeToRates = (pairs) => ({ - payload: { pairs }, - type: AT.SUBSCRIBE_TO_RATES -}) -export const unsubscribeFromRates = () => ({ - type: AT.UNSUBSCRIBE_FROM_RATES -}) - -export const updateBestRates = (rates) => ({ - payload: { rates }, - type: AT.UPDATE_BEST_RATES -}) - -export const removeAdvice = (pair) => ({ - payload: { pair }, - type: AT.REMOVE_ADVICE -}) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/model.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/model.js deleted file mode 100644 index 749746848e7..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/model.js +++ /dev/null @@ -1,164 +0,0 @@ -import { contains, eqBy, flip, lensProp, over, prop, split } from 'ramda' - -export const formatPair = (source, target) => `${source}-${target}` -export const splitPair = split('-') - -export const AUTH_ERROR_MESSAGE = { - channel: 'auth', - description: 'Can not process auth request, token can not be found', - event: 'rejected' -} - -export const ADVICE_SUBSCRIBE_SUCCESS_MESSAGE = { - channel: 'conversion', - event: 'subscribed' -} - -export const ADVICE_SUBSCRIBE_ERROR_MESSAGE = { - channel: 'conversion', - event: 'rejcted' -} - -export const ADVICE_UNSUBSCRIBE_SUCCESS_MESSAGE = { - channel: 'conversion', - event: 'unsubscribed' -} - -export const ADVICE_SNAPSHOT_MESSAGE = { - channel: 'conversion', - event: 'snapshot' -} - -export const ADVICE_UPDATED_MESSAGE = { - channel: 'conversion', - event: 'updated' -} - -export const RATES_SUBSCRIBE_SUCCESS_MESSAGE = { - channel: 'exchange_rate', - event: 'subscribed' -} - -export const RATES_SUBSCRIBE_ERROR_MESSAGE = { - channel: 'exchange_rate', - event: 'error' -} - -export const RATES_UNSUBSCRIBE_SUCCESS_MESSAGE = { - channel: 'exchange_rate', - event: 'unsubscribed' -} - -export const RATES_SNAPSHOT_MESSAGE = { - channel: 'exchange_rate', - event: 'snapshot' -} - -export const RATES_UPDATED_MESSAGE = { - channel: 'exchange_rate', - event: 'updated' -} - -export const getRatesSubscribeMessage = (pairs) => ({ - action: 'subscribe', - channel: 'exchange_rate', - params: { - pairs, - type: 'exchangeRates' - } -}) - -export const getRatesUnsubscribeMessage = () => ({ - action: 'unsubscribe', - channel: 'exchange_rate', - params: { - type: 'allCurrencyPairs' - } -}) - -export const getAdviceSubscribeMessage = (pair, volume, fix, fiatCurrency) => ({ - action: 'subscribe', - channel: 'conversion', - params: { - fiatCurrency, - fix, - pair, - type: 'conversionSpecification', - volume - } -}) - -export const getAdviceUnsubscribeMessage = (pair) => ({ - action: 'unsubscribe', - channel: 'conversion', - params: { - pair, - type: 'conversionPair' - } -}) - -export const getAuthMessage = (token) => ({ - action: 'subscribe', - channel: 'auth', - params: { - token, - type: 'auth' - } -}) - -export const MIN_ERROR = 'Result volume is too small' -export const MAX_ERROR = 'Too big volume' - -export const FIX_TYPES = { - BASE: 'base', - BASE_IN_FIAT: 'baseInFiat', - COUNTER: 'counter', - COUNTER_IN_FIAT: 'counterInFiat' -} - -const { BASE, BASE_IN_FIAT, COUNTER, COUNTER_IN_FIAT } = FIX_TYPES - -export const getComplementaryField = flip(prop)({ - sourceAmount: 'sourceFiat', - sourceFiat: 'sourceAmount', - targetAmount: 'targetFiat', - targetFiat: 'targetAmount' -}) - -export const mapFixToFieldName = flip(prop)({ - [BASE]: 'sourceAmount', - [BASE_IN_FIAT]: 'sourceFiat', - [COUNTER]: 'targetAmount', - [COUNTER_IN_FIAT]: 'targetFiat' -}) - -export const swapCoinAndFiat = flip(prop)({ - [BASE]: BASE_IN_FIAT, - [BASE_IN_FIAT]: BASE, - [COUNTER]: COUNTER_IN_FIAT, - [COUNTER_IN_FIAT]: COUNTER -}) - -export const swapBaseAndCounter = flip(prop)({ - [BASE]: COUNTER, - [BASE_IN_FIAT]: COUNTER_IN_FIAT, - [COUNTER]: BASE, - [COUNTER_IN_FIAT]: BASE_IN_FIAT -}) - -export const coinActive = flip(contains)([BASE, COUNTER]) -export const fiatActive = flip(contains)([BASE_IN_FIAT, COUNTER_IN_FIAT]) -export const sourceActive = flip(contains)([BASE, BASE_IN_FIAT]) -export const targetActive = flip(contains)([COUNTER, COUNTER_IN_FIAT]) - -const volumeLens = lensProp('volume') -export const configEquals = eqBy(over(volumeLens, Number)) - -export const getBestRatesPairs = (sourceCoin, targetCoin, fiatCurrency) => [ - formatPair(sourceCoin, targetCoin), - formatPair(targetCoin, sourceCoin), - formatPair(sourceCoin, fiatCurrency), - formatPair(fiatCurrency, sourceCoin), - formatPair(targetCoin, fiatCurrency), - formatPair(fiatCurrency, targetCoin) -] diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/rates.spec.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/rates.spec.js deleted file mode 100644 index ddcd89bc587..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/rates.spec.js +++ /dev/null @@ -1,280 +0,0 @@ -import { compose, groupBy, head, map, path, prop } from 'ramda' - -import { Remote } from '@core' -import { actions, model, selectors } from 'data' -import ratesSocketSagas from 'data/middleware/webSocket/rates/sagaRegister' -import { socketAuthRetryDelay } from 'data/middleware/webSocket/rates/sagas' -import profileReducer from 'data/modules/profile/reducers' -import webSocketRates, { fallbackInterval } from 'middleware/webSocketRates' -import { createTestStore, getDispatchSpyReducer } from 'utils/testbed' - -import ratesReducer from './reducers' -import ratesSagas from './sagaRegister' - -jest.useFakeTimers() - -const { dispatchSpy, spyReducer } = getDispatchSpyReducer() - -const reducers = { - profile: profileReducer, - rates: ratesReducer, - spy: spyReducer -} - -const pair = 'BTC-ETH' -const pairs = [pair, 'BTC-USD', 'ETH-USD'] -const volume = 100 -const fix = 'counterInFiat' -const fiatCurrency = 'USD' -const stubAdvice = { - currencyRatio: { - base: { - crypto: { - symbol: 'BTC', - value: 0.15 - }, - fiat: { - symbol: 'USD', - value: 96.77 - } - }, - counter: { - crypto: { - symbol: 'ETH', - value: 0.27 - }, - fiat: { - symbol: 'USD', - value: 100.0 - } - } - }, - fiatCurrency, - fix, - pair, - volume -} -const ratesSocket = { - close: jest.fn(), - connect(onOpen, onMessage, onClose, onError, fallback) { - this.triggerOpen = onOpen - this.triggerMessage = onMessage - this.triggerClose = onClose - this.triggerError = onError - this.triggerFallback = fallback - }, - isReady: jest.fn().mockReturnValue(true), - send: jest.fn() -} - -jest.spyOn(ratesSocket, 'connect') - -const api = { - fetchAdvice: jest.fn() -} - -api.fetchAdvice.mockReturnValue({ ratio: stubAdvice.currencyRatio }) - -const sagas = [ratesSagas({ api }), ratesSocketSagas({ api, ratesSocket })] - -const middlewares = [webSocketRates(ratesSocket)] - -const stubToken = - 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJyZXRhaWwtY29yZSIsImV4cCI6MTUzNDgyMTgwNCwiaWF0IjoxNTM0Nzc4NjA0LCJ1c2VySUQiOiJmM2M5YWEyNy1mMDgwLTRiZDQtOTI0ZS1iMWQzOWQ4OWE0OTEiLCJqdGkiOiI4NWZkNmVhNC0yNzI1LTQ5NzUtOTQzOC01Mjg4MDliNTJhYmIifQ.wd_RucCDBc7D6snalfb1piI06J_lOD7rXjJ3z38nc3U' - -describe('rates service', () => { - let store - beforeEach(() => { - store = createTestStore(reducers, sagas, middlewares) - store.dispatch(actions.middleware.webSocket.rates.startSocket()) - dispatchSpy.mockClear() - api.fetchAdvice.mockClear() - jest.clearAllTimers() - }) - - it('should connect to ratesSocket', () => { - expect(ratesSocket.connect).toHaveBeenCalledTimes(1) - }) - - it('should close ratesSocket on stop', () => { - store.dispatch(actions.middleware.webSocket.rates.stopSocket()) - expect(ratesSocket.close).toHaveBeenCalledTimes(1) - }) - - describe('authentication', () => { - beforeEach(() => { - ratesSocket.send.mockClear() - store.dispatch(actions.modules.profile.setApiTokenSuccess(stubToken)) - store.dispatch(actions.middleware.webSocket.rates.authenticateSocket()) - }) - - it('should send authentication message to ratesSocket', () => { - expect(ratesSocket.send).toHaveBeenCalledTimes(1) - expect(ratesSocket.send).toHaveBeenCalledWith(model.rates.getAuthMessage(stubToken)) - }) - }) - - describe('new advice subscriptions', () => { - beforeEach(() => { - ratesSocket.send.mockClear() - store.dispatch(actions.modules.rates.subscribeToAdvice(pair, volume, fix, fiatCurrency)) - }) - - it('should set initial advice for pair upon new subscription', () => { - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual(Remote.Loading) - }) - - it('should set initial config for pair upon new subscription', () => { - expect(path(['rates', 'pairs', pair, 'config'], store.getState())).toEqual({ - fiatCurrency, - fix, - volume - }) - }) - - it('should set send subscription socket message upon new subscription', () => { - expect(ratesSocket.send).toHaveBeenCalledTimes(1) - expect(ratesSocket.send).toHaveBeenCalledWith( - model.rates.getAdviceSubscribeMessage(pair, volume, fix, fiatCurrency) - ) - }) - - it('should trigger advice fetch if socket is not ready', () => { - ratesSocket.send.mockClear() - ratesSocket.isReady.mockReturnValueOnce(false) - store.dispatch(actions.modules.rates.subscribeToAdvice(pair, volume, fix, fiatCurrency)) - expect(ratesSocket.send).toHaveBeenCalledTimes(0) - expect(api.fetchAdvice).toHaveBeenCalledTimes(1) - expect(api.fetchAdvice).toHaveBeenCalledWith(pair, volume, fix, fiatCurrency) - }) - }) - - describe('advice unsubscription', () => { - beforeEach(() => { - store.dispatch(actions.modules.rates.subscribeToAdvice(pair)) - ratesSocket.send.mockClear() - store.dispatch(actions.modules.rates.unsubscribeFromAdvice(pair)) - }) - - it('should send unsubscription socket message', () => { - expect(ratesSocket.send).toHaveBeenCalledTimes(1) - expect(ratesSocket.send).toHaveBeenCalledWith(model.rates.getAdviceUnsubscribeMessage(pair)) - }) - - it('should set pair to loading upon unsubscription', () => { - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual(Remote.Loading) - }) - }) - - describe('new rates subscriptions', () => { - beforeEach(() => { - ratesSocket.send.mockClear() - store.dispatch(actions.modules.rates.subscribeToRates(pairs)) - }) - - it('should unsubscribe and set send subscription socket message upon new subscription', () => { - expect(ratesSocket.send).toHaveBeenCalledTimes(2) - expect(ratesSocket.send.mock.calls).toEqual([ - [model.rates.getRatesUnsubscribeMessage()], - [model.rates.getRatesSubscribeMessage(pairs)] - ]) - }) - }) - - describe('rates unsubscription', () => { - beforeEach(() => { - store.dispatch(actions.modules.rates.subscribeToRates(pairs)) - ratesSocket.send.mockClear() - store.dispatch(actions.modules.rates.unsubscribeFromRates()) - }) - - it('should send unsubscription socket message', () => { - expect(ratesSocket.send).toHaveBeenCalledTimes(1) - expect(ratesSocket.send).toHaveBeenCalledWith(model.rates.getRatesUnsubscribeMessage()) - }) - - it('should reset bestRates', () => { - expect(store.getState().rates.bestRates).toEqual(Remote.NotAsked) - }) - }) - - describe('message handling', () => { - beforeEach(() => { - ratesSocket.send.mockClear() - store.dispatch(actions.modules.rates.subscribeToAdvice(pair)) - store.dispatch(actions.modules.rates.updatePairConfig(pair, volume, fix, fiatCurrency)) - }) - - it('should set pair rate to loading upon subscription success message', () => { - ratesSocket.triggerMessage({ - ...model.rates.ADVICE_SUBSCRIBE_SUCCESS_MESSAGE, - pair - }) - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual(Remote.Loading) - }) - - it('should set pair rate to success upon advice message if fix and volume match', () => { - ratesSocket.triggerMessage({ - ...model.rates.ADVICE_UPDATED_MESSAGE, - quote: { ...stubAdvice } - }) - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual( - Remote.of(stubAdvice.currencyRatio) - ) - }) - - it('should update bestRates upon rates message', () => { - const rates = pairs.map((pair) => ({ pair, rate: Math.random() })) - const resultRates = compose(map(head), groupBy(prop('pair')))(rates) - ratesSocket.triggerMessage({ - ...model.rates.RATES_UPDATED_MESSAGE, - pairs, - rates - }) - expect(selectors.modules.rates.getBestRates(store.getState())).toEqual( - Remote.Success(resultRates) - ) - }) - - it('should retry authentication after delay', async () => { - ratesSocket.triggerMessage(model.rates.AUTH_ERROR_MESSAGE) - expect(ratesSocket.send).toHaveBeenCalledTimes(0) - await jest.advanceTimersByTime(socketAuthRetryDelay) - expect(ratesSocket.send).toHaveBeenCalledTimes(1) - }) - }) - - describe('fallback', () => { - beforeEach(() => { - store.dispatch(actions.modules.rates.subscribeToAdvice(pair)) - ratesSocket.triggerFallback() - }) - - it('should start fetchingRates once per fallbackInterval when falbback is triggered by socket', () => { - expect(api.fetchAdvice).toHaveBeenCalledTimes(0) - jest.advanceTimersByTime(fallbackInterval) - expect(api.fetchAdvice).toHaveBeenCalledTimes(1) - jest.advanceTimersByTime(fallbackInterval) - expect(api.fetchAdvice).toHaveBeenCalledTimes(2) - }) - - it('should set rates when received response', () => { - jest.advanceTimersByTime(fallbackInterval) - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual( - Remote.of(stubAdvice.currencyRatio) - ) - }) - - it('should set rates error when request fails', () => { - const stubError = new Error('stubError') - api.fetchAdvice.mockImplementation(() => { - throw stubError - }) - jest.advanceTimersByTime(fallbackInterval) - expect(selectors.modules.rates.getPairAdvice(pair, store.getState())).toEqual( - Remote.Failure(stubError) - ) - api.fetchAdvice.mockReturnValue(stubAdvice.currencyRatio) - }) - }) -}) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/reducers.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/reducers.js deleted file mode 100644 index 9f3195336db..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/reducers.js +++ /dev/null @@ -1,70 +0,0 @@ -import { assoc, assocPath, dissocPath, lensProp, prop, propOr, set } from 'ramda' - -import { Remote } from '@core' -import * as socketActionTypes from 'data/middleware/webSocket/rates/actionTypes' - -import * as AT from './actionTypes' -import { FIX_TYPES, MAX_ERROR, MIN_ERROR } from './model' - -const INITIAL_STATE = { - availablePairs: Remote.NotAsked, - bestRates: Remote.NotAsked, - pairs: {} -} -const INITIAL_PAIR = { - config: { - fiatCurrency: 'USD', - fix: FIX_TYPES.BASE, - volume: 0 - }, - quote: Remote.NotAsked -} -const getPair = propOr(INITIAL_PAIR) -const quoteLens = lensProp('quote') -const configLens = lensProp('config') -const bestRatesLens = lensProp('bestRates') -const setPairProp = (lens, fn, pair, state) => { - const pairValue = set(lens, fn, getPair(pair, state.pairs)) - return assocPath(['pairs', pair], pairValue, state) -} -const getError = (error) => { - const description = prop('description', error) - if (description === MIN_ERROR) return MIN_ERROR - if (new RegExp(MAX_ERROR).test(description)) return MAX_ERROR - - return error -} - -export default (state = INITIAL_STATE, action) => { - const { payload, type } = action - - switch (type) { - case AT.AVAILABLE_PAIRS_LOADING: - return assoc('availablePairs', Remote.Loading, state) - case AT.AVAILABLE_PAIRS_SUCCESS: - return assoc('availablePairs', Remote.Success(payload.pairs), state) - case AT.AVAILABLE_PAIRS_ERROR: - return assoc('availablePairs', Remote.Failure(payload.error), state) - case AT.UPDATE_PAIR_CONFIG: - return setPairProp(configLens, payload.config, payload.pair, state) - case AT.SET_PAIR_QUOTE: - return setPairProp(quoteLens, Remote.Success(payload.quote), payload.pair, state) - case AT.SUBSCRIBE_TO_ADVICE: - return setPairProp(quoteLens, Remote.Loading, payload.pair, state) - case socketActionTypes.ADVICE_SUBSCRIBE_ERROR: - return setPairProp(quoteLens, Remote.Failure(getError(payload.error)), payload.pair, state) - case AT.UPDATE_BEST_RATES: { - return set(bestRatesLens, Remote.Success(payload.rates), state) - } - case AT.SUBSCRIBE_TO_RATES: - return set(bestRatesLens, Remote.Loading, state) - case socketActionTypes.RATES_SUBSCRIBE_ERROR: - return set(bestRatesLens, Remote.Failure(payload.error), state) - case AT.UNSUBSCRIBE_FROM_RATES: - return set(bestRatesLens, Remote.NotAsked, state) - case AT.REMOVE_ADVICE: - return dissocPath(['pairs', payload.pair], state) - default: - return state - } -} diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagaRegister.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagaRegister.js deleted file mode 100644 index cada5245a40..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagaRegister.js +++ /dev/null @@ -1,17 +0,0 @@ -import { takeLatest } from 'redux-saga/effects' - -import * as AT from './actionTypes' -import sagas from './sagas' - -export default ({ api }) => { - const ratesSagas = sagas({ api }) - - return function* ratesSaga() { - yield takeLatest(AT.SUBSCRIBE_TO_ADVICE, ratesSagas.subscribeToAdvice) - yield takeLatest(AT.UNSUBSCRIBE_FROM_ADVICE, ratesSagas.unsubscribeFromAdvice) - yield takeLatest(AT.FETCH_AVAILABLE_PAIRS, ratesSagas.fetchAvailablePairs) - yield takeLatest(AT.UPDATE_ADVICE, ratesSagas.updateAdvice) - yield takeLatest(AT.SUBSCRIBE_TO_RATES, ratesSagas.subscribeToRates) - yield takeLatest(AT.UNSUBSCRIBE_FROM_RATES, ratesSagas.unsubscribeFromRates) - } -} diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagas.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagas.js deleted file mode 100644 index e888c40fbf1..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/sagas.js +++ /dev/null @@ -1,62 +0,0 @@ -import { call, put, select } from 'redux-saga/effects' - -import { actions } from 'data' - -import * as A from './actions' -import { configEquals } from './model' -import * as S from './selectors' - -export default ({ api }) => { - const subscribeToAdvice = function* ({ payload }) { - const { fiatCurrency, fix, pair, volume } = payload - - yield put(A.updatePairConfig(pair, volume, fix, fiatCurrency)) - yield put(actions.middleware.webSocket.rates.openAdviceChannel(pair, volume, fix, fiatCurrency)) - } - - const unsubscribeFromAdvice = function* ({ payload }) { - const { pair } = payload - - yield put(actions.middleware.webSocket.rates.closeAdviceChannel(pair)) - } - - const subscribeToRates = function* ({ payload }) { - const { pairs } = payload - - yield put(actions.middleware.webSocket.rates.closeRatesChannel()) - yield put(actions.middleware.webSocket.rates.openRatesChannel(pairs)) - } - - const unsubscribeFromRates = function* () { - yield put(actions.middleware.webSocket.rates.closeRatesChannel()) - } - - const fetchAvailablePairs = function* () { - try { - yield put(A.availablePairsLoading()) - const { pairs } = yield call(api.fetchAvailablePairs) - - yield put(A.availablePairsSuccess(pairs)) - } catch (e) { - yield put(A.availablePairsError(e)) - } - } - - const updateAdvice = function* ({ payload: { quote } }) { - const { fiatCurrency, fix, pair, volume } = quote - const currentConfig = yield select(S.getPairConfig(pair)) - if (configEquals(currentConfig, { fiatCurrency, fix, volume })) { - yield put(A.setPairQuote(pair, quote)) - yield put(A.pairUpdated(pair)) - } - } - - return { - fetchAvailablePairs, - subscribeToAdvice, - subscribeToRates, - unsubscribeFromAdvice, - unsubscribeFromRates, - updateAdvice - } -} diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/selectors.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/selectors.js deleted file mode 100644 index 03fac4711b3..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/rates/selectors.js +++ /dev/null @@ -1,22 +0,0 @@ -import { compose, curry, lift, mapObjIndexed, path, prop, propOr, values } from 'ramda' - -import { Remote } from '@core' - -export const getActivePairs = compose( - values, - mapObjIndexed((pair, pairName) => ({ ...pair, pair: pairName })), - path(['rates', 'pairs']) -) - -const getPair = curry((pair, state) => path(['rates', 'pairs', pair], state)) - -export const getPairQuote = curry(compose(propOr(Remote.NotAsked, 'quote'), getPair)) - -export const getPairAdvice = curry(compose(lift(prop('currencyRatio')), getPairQuote)) -export const getPairConfig = curry(compose(prop('config'), getPair)) -export const getPairFix = curry(compose(prop('fix'), getPairConfig)) - -export const getBestRates = path(['rates', 'bestRates']) -export const getBestRate = curry((pair, state) => getBestRates(state).map(prop(pair))) - -export const getAvailablePairs = path(['rates', 'availablePairs']) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/sagaRegister.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/sagaRegister.js index a5215011ebc..b10f4d660ef 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/sagaRegister.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/sagaRegister.js @@ -2,7 +2,6 @@ import { fork } from 'redux-saga/effects' import addressesBch from './addressesBch/sagaRegister' import profile from './profile/sagaRegister' -import rates from './rates/sagaRegister' import securityCenter from './securityCenter/sagaRegister' import settings from './settings/sagaRegister' import transferEth from './transferEth/sagaRegister' @@ -11,7 +10,6 @@ export default ({ api, coreSagas, networks }) => function* modulesSaga() { yield fork(addressesBch({ coreSagas, networks })) yield fork(profile({ api, coreSagas, networks })) - yield fork(rates({ api })) yield fork(settings({ api, coreSagas })) yield fork(securityCenter({ coreSagas })) yield fork(transferEth({ coreSagas, networks })) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/sagas.js b/packages/blockchain-wallet-v4-frontend/src/data/modules/sagas.js index c884288efea..cef07fd2748 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/sagas.js +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/sagas.js @@ -1,6 +1,5 @@ import addressesBch from './addressesBch/sagas' import profile from './profile/sagas.ts' -import rates from './rates/sagas' import securityCenter from './securityCenter/sagas' import settings from './settings/sagas' import transferEth from './transferEth/sagas' @@ -8,7 +7,6 @@ import transferEth from './transferEth/sagas' export default ({ api, coreSagas, networks }) => ({ addressesBch: addressesBch({ coreSagas }), profile: profile({ api, coreSagas, networks }), - rates: rates({ api }), securityCenter: securityCenter({ coreSagas }), settings: settings({ api, coreSagas }), transferEth: transferEth({ coreSagas, networks }) diff --git a/packages/blockchain-wallet-v4-frontend/src/data/modules/selectors.ts b/packages/blockchain-wallet-v4-frontend/src/data/modules/selectors.ts index ae4d0bce717..902795307b3 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/modules/selectors.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/modules/selectors.ts @@ -1,5 +1,4 @@ import * as profile from './profile/selectors' -import * as rates from './rates/selectors' import * as transferEth from './transferEth/selectors' -export { profile, rates, transferEth } +export { profile, transferEth } diff --git a/packages/blockchain-wallet-v4-frontend/src/data/rootReducer.ts b/packages/blockchain-wallet-v4-frontend/src/data/rootReducer.ts index cb6ff30e1c2..28d6d78f01d 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/rootReducer.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/rootReducer.ts @@ -14,7 +14,6 @@ import { logsReducer as logs } from './logs/slice' import { miscReducer as misc } from './misc/slice' import { modalReducer } from './modals/slice' import profile from './modules/profile/reducers' -import rates from './modules/rates/reducers' import securityCenter from './modules/settings/reducers' import { transferEthReducer } from './modules/transferEth/transferEthSlice' import { networkConfigReducer as networkConfig } from './networkConfig/slice' @@ -40,7 +39,6 @@ const rootReducer = { preferences: preferencesReducer, prices, profile, - rates, remoteConfig, securityCenter, session, diff --git a/packages/blockchain-wallet-v4-frontend/src/data/rootSaga.ts b/packages/blockchain-wallet-v4-frontend/src/data/rootSaga.ts index 1f3bc38c939..026e96b4fed 100644 --- a/packages/blockchain-wallet-v4-frontend/src/data/rootSaga.ts +++ b/packages/blockchain-wallet-v4-frontend/src/data/rootSaga.ts @@ -22,7 +22,7 @@ import session from './session/sagaRegister' import signup from './signup/sagaRegister' import wallet from './wallet/sagaRegister' -export default function* rootSaga({ api, coinsSocket, networks, options, ratesSocket }) { +export default function* rootSaga({ api, coinsSocket, networks, options }) { const coreSagas = coreSagasFactory({ api, networks, options }) const { initAppLanguage, logAppConsoleWarning } = miscSagas() @@ -42,7 +42,7 @@ export default function* rootSaga({ api, coinsSocket, networks, options, ratesSo fork(prices({ api })), fork(goals({ api, coreSagas, networks })), fork(wallet({ coreSagas })), - fork(middleware({ api, coinsSocket, ratesSocket })), + fork(middleware({ api, coinsSocket })), fork(coreRootSagaFactory({ api, networks, options })), fork(router()), fork(session({ api })), diff --git a/packages/blockchain-wallet-v4-frontend/src/middleware/index.js b/packages/blockchain-wallet-v4-frontend/src/middleware/index.js index 5dad97549ee..8ddde778e1a 100644 --- a/packages/blockchain-wallet-v4-frontend/src/middleware/index.js +++ b/packages/blockchain-wallet-v4-frontend/src/middleware/index.js @@ -1,6 +1,5 @@ import analyticsMiddleware from './analyticsMiddleware' import streamingXlm from './streamingXlm' import webSocketCoins from './webSocketCoins' -import webSocketRates from './webSocketRates' -export { analyticsMiddleware, streamingXlm, webSocketCoins, webSocketRates } +export { analyticsMiddleware, streamingXlm, webSocketCoins } diff --git a/packages/blockchain-wallet-v4-frontend/src/middleware/webSocketRates.js b/packages/blockchain-wallet-v4-frontend/src/middleware/webSocketRates.js deleted file mode 100644 index df552cacc5d..00000000000 --- a/packages/blockchain-wallet-v4-frontend/src/middleware/webSocketRates.js +++ /dev/null @@ -1,42 +0,0 @@ -import { compose } from 'ramda' - -import { actions, actionTypes } from 'data' - -export const fallbackInterval = 5000 -let fallbackIntervalPID = null - -const socket = (socket) => (store) => { - return (next) => (action) => { - const { type } = action - - if (type === actionTypes.middleware.webSocket.rates.START_SOCKET) { - clearInterval(fallbackIntervalPID) - socket.connect( - // onOpen - compose(store.dispatch, actions.middleware.webSocket.rates.openSocket), - // onMessage - compose(store.dispatch, actions.middleware.webSocket.rates.messageSocket), - // onClose - compose(store.dispatch, actions.middleware.webSocket.rates.closeSocket), - // onError - // eslint-disable-next-line no-console - (e) => console.error('Failed to connect to websocket', e), - // fallback - () => { - fallbackIntervalPID = setInterval( - compose(store.dispatch, actions.middleware.webSocket.rates.restFallback), - fallbackInterval - ) - } - ) - } - - if (type === actionTypes.middleware.webSocket.rates.STOP_SOCKET) { - socket.close() - } - - return next(action) - } -} - -export default socket diff --git a/packages/blockchain-wallet-v4-frontend/src/store/index.js b/packages/blockchain-wallet-v4-frontend/src/store/index.js index 3624f91623c..cc6200e5dd9 100644 --- a/packages/blockchain-wallet-v4-frontend/src/store/index.js +++ b/packages/blockchain-wallet-v4-frontend/src/store/index.js @@ -12,7 +12,7 @@ import Worker from 'web-worker' import { v4 as uuidv4 } from 'uuid' import { coreMiddleware } from '@core' -import { ApiSocket, createWalletApi, HorizonStreamingService, Socket } from '@core/network' +import { createWalletApi, HorizonStreamingService, Socket } from '@core/network' import { serializer } from '@core/types' import { actions, rootReducer, rootSaga, selectors, model } from 'data' import { isBrowserSupported } from 'services/browser' @@ -89,11 +89,6 @@ const configuredStore = async function () { options, url: `${socketUrl}/coins` }) - const ratesSocket = new ApiSocket({ - maxReconnects: 3, - options, - url: `${socketUrl}/nabu-gateway/markets/quotes` - }) const xlmStreamingService = new HorizonStreamingService({ url: horizonUrl }) @@ -142,7 +137,6 @@ const configuredStore = async function () { routerMiddleware(history), coreMiddleware.kvStore({ api, isAuthenticated, kvStorePath: 'wallet.kvstore' }), streamingXlm(xlmStreamingService, api), - webSocketRates(ratesSocket), webSocketCoins(coinsSocket), coreMiddleware.walletSync({ api, isAuthenticated, walletPath: 'wallet.payload' }), analyticsMiddleware() @@ -168,8 +162,7 @@ const configuredStore = async function () { api, coinsSocket, networks, - options, - ratesSocket + options }) store.dispatch(actions.goals.defineGoals())