Skip to content

Commit

Permalink
refactor (bitfinexcom#695)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmytroshch authored Sep 12, 2023
1 parent ef60ce2 commit 8a54905
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 53 deletions.
5 changes: 3 additions & 2 deletions public/locales/en-US/translations.json
Original file line number Diff line number Diff line change
Expand Up @@ -540,8 +540,9 @@
"reboot_notify": "Noticed several internet connection issues. Rebooting app...",
"strategyLaunchMissingAPIKey": "Please configure API keys first and relaunch strategy",
"algoOrderRemovedFromHistorySuccessfully": "The algo order has been successfully removed from history",
"recurringAoExecutedSuccessfully": "Recurring AO {{alias}} was executed successfully at {{time}}",
"recurringAoFailed": "Recurring AO {{alias}} was failed at {{time}}. Reason: {{reason}}"
"recurringAOExecutedSuccessfully": "Recurring AO {{alias}} was executed successfully at {{time}}",
"recurringAOFailed": "Recurring AO {{alias}} was failed at {{time}}. Reason: {{reason}}",
"recurringAOWasFinished": "Recurring AO {{alias}} was finished at {{time}}."
},
"confirmDMSModal": {
"title": "Enable Dead Man Switch",
Expand Down
4 changes: 2 additions & 2 deletions src/modals/AlgoOrderDetailsModal/AlgoOrderDetailsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import AlgoOrderDetailsModalHeader from './AlgoOrderDetailsModal.Header'
import { getComponentState, getFormatTimeFn, getUIState } from '../../redux/selectors/ui'
import { UI_KEYS } from '../../redux/constants/ui_keys'
import UIActions from '../../redux/actions/ui'
import { getFailedRecurringAoAtomics } from '../../redux/selectors/ao'
import { getFailedRecurringAOAtomics } from '../../redux/selectors/ao'

import './style.css'

Expand All @@ -42,7 +42,7 @@ const AlgoOrderDetailsModal = ({ onClose, algoOrderId, isOpen }) => {
const layoutID = useSelector((state) => getUIState(state, UI_KEYS.layoutID))
const tableState = useSelector((state) => getComponentState(state, layoutID, null, COMPONENT_ID),
)
const failedOrders = useSelector(getFailedRecurringAoAtomics)
const failedOrders = useSelector(getFailedRecurringAOAtomics)

const orderDetails = useMemo(
() => getOrderDetails(rowData, t, formatTime),
Expand Down
16 changes: 8 additions & 8 deletions src/redux/actions/ao.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,28 @@ export function setShowAOsHistory(showAOsHistory) {
}
}

export function recvRecurringAoAtomics(orders, gid, mode) {
export function recvRecurringAOAtomics(orders, gid, mode) {
return {
type: types.DATA_RECURRING_AO_ATOMICS,
payload: { orders, gid, mode },
}
}

export function recvRecurringAoAtomicsFailed(gid) {
export function recvRecurringAOAtomicsFailed(gid) {
return {
type: types.GET_RECURRING_AO_ATOMICS_FAILED,
payload: { gid },
}
}

export function requestRecurringAoAtomics(payload) {
export function requestRecurringAOAtomics(payload) {
return {
type: types.REQUEST_RECURRING_AO_ATOMICS,
payload,
}
}

export function setFailedRecurringAoAtomics(orders) {
export function setFailedRecurringAOAtomics(orders) {
return {
type: types.SET_FAILED_RECURRING_AO_ATOMICS,
payload: { orders },
Expand All @@ -134,8 +134,8 @@ export default {
setActiveAOParamsID,
appendAlgoOrderParams,
setShowAOsHistory,
recvRecurringAoAtomics,
setFailedRecurringAoAtomics,
recvRecurringAoAtomicsFailed,
requestRecurringAoAtomics,
recvRecurringAOAtomics,
setFailedRecurringAOAtomics,
recvRecurringAOAtomicsFailed,
requestRecurringAOAtomics,
}
4 changes: 2 additions & 2 deletions src/redux/middleware/ws/on_message.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ export default (alias, store) => (e = {}) => {

case 'data.recur_ao_atomic_orders': {
const [,, mode, gid, orders] = payload
store.dispatch(AOActions.recvRecurringAoAtomics(orders, gid, mode))
store.dispatch(AOActions.recvRecurringAOAtomics(orders, gid, mode))

break
}
Expand All @@ -438,7 +438,7 @@ export default (alias, store) => (e = {}) => {
const [, gid, status] = payload

if (status === 'failed') {
store.dispatch(AOActions.recvRecurringAoAtomicsFailed(gid))
store.dispatch(AOActions.recvRecurringAOAtomicsFailed(gid))
}

break
Expand Down
6 changes: 3 additions & 3 deletions src/redux/reducers/ao/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ function getInitialState() {
activeAOParamsID: null,
showAOsHistory: false,
aoParams: {}, // { symbol: { algoID: [/* data */] } }
failedRecurringAoAtomics: {},
failedRecurringAOAtomics: {},
}
}

Expand Down Expand Up @@ -111,8 +111,8 @@ function reducer(state = getInitialState(), action = {}) {

return {
...state,
failedRecurringAoAtomics: {
...state.failedRecurringAoAtomics,
failedRecurringAOAtomics: {
...state.failedRecurringAOAtomics,
...orders,
},
}
Expand Down
8 changes: 4 additions & 4 deletions src/redux/sagas/ao/fetch_recurring_ao_atomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ import Debug from 'debug'
import WSActions from '../../actions/ws'
import types from '../../constants/ao'
import { getAuthToken } from '../../selectors/ws'
import handleRecurringAoAtomics from './handle_recurring_ao_atomics'
import handleRecurringAOAtomics from './handle_recurring_ao_atomics'
import AOActions from '../../actions/ao'
import { RECURRING_DELAY_FOR_FETCH } from '../../helpers/recurring_ao'

const debug = Debug('hfui:recurring-ao')

export default function* fetchRecurringAoAtomics({
export default function* fetchRecurringAOAtomics({
payload: { gid, firstDataRequest },
}) {
const authToken = yield select(getAuthToken)
Expand All @@ -36,11 +36,11 @@ export default function* fetchRecurringAoAtomics({
debug('FAILED request atomics for recurring %s, retry in 1.5s', gid)
yield delay(RECURRING_DELAY_FOR_FETCH)
yield put(
AOActions.requestRecurringAoAtomics({ gid, firstDataRequest }),
AOActions.requestRecurringAOAtomics({ gid, firstDataRequest }),
)
break
}
yield call(handleRecurringAoAtomics, {
yield call(handleRecurringAOAtomics, {
gid,
orders,
firstDataRequest,
Expand Down
20 changes: 10 additions & 10 deletions src/redux/sagas/ao/handle_recurring_ao_atomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import WSActions from '../../actions/ws'
import AOActions from '../../actions/ao'
import UIActions from '../../actions/ui'
import { getAlgoOrderById, getOrderHistory } from '../../selectors/ws'
import { getFailedRecurringAoAtomics } from '../../selectors/ao'
import { getFailedRecurringAOAtomics } from '../../selectors/ao'
import { getMarketPair } from '../../selectors/meta'
import scheduleRecurringAo from './schedule_recurring_ao'
import runRecurringAOScheduler from './run_recurring_ao_scheduler'
import TIMEFRAME_WIDTHS from '../../../util/time_frame_widths'
import { getCurrentMode, getFormatTimeFn } from '../../selectors/ui'
import { getRecurringAtomicsShownNotifications, setRecurringAtomicsShownNotifications } from '../../../util/ui'
Expand All @@ -22,13 +22,13 @@ import i18n from '../../../locales/i18n'
const FAILED_ORDER_STATUS = 'FAILED'
const debug = Debug('hfui:recurring-ao')

export default function* handleRecurringAoAtomics({
export default function* handleRecurringAOAtomics({
mode,
gid,
orders,
firstDataRequest,
}) {
const failedOrders = yield select(getFailedRecurringAoAtomics)
const failedOrders = yield select(getFailedRecurringAOAtomics)
const placedOrders = yield select(getOrderHistory)
const getMarketPairState = yield select(getMarketPair)
const currentMode = yield select(getCurrentMode)
Expand All @@ -41,7 +41,6 @@ export default function* handleRecurringAoAtomics({
} = recurringAO

const previouslyShownNotifications = getRecurringAtomicsShownNotifications()
console.log(previouslyShownNotifications)

const newFailedOrders = {}
const newPlacedOrders = {}
Expand Down Expand Up @@ -74,7 +73,7 @@ export default function* handleRecurringAoAtomics({
notification = {
mts: Date.now(),
status: 'error',
text: i18n.t('notifications.recurringAoFailed', {
text: i18n.t('notifications.recurringAOFailed', {
alias,
time,
reason: `${status}: ${orderSubmitError}`,
Expand All @@ -85,7 +84,7 @@ export default function* handleRecurringAoAtomics({
notification = {
mts: Date.now(),
status: 'success',
text: i18n.t('notifications.recurringAoExecutedSuccessfully', {
text: i18n.t('notifications.recurringAOExecutedSuccessfully', {
alias,
time,
}),
Expand Down Expand Up @@ -127,7 +126,7 @@ export default function* handleRecurringAoAtomics({
let isResponseUseful = false

if (!_isEmpty(newFailedOrders)) {
yield put(AOActions.setFailedRecurringAoAtomics(newFailedOrders))
yield put(AOActions.setFailedRecurringAOAtomics(newFailedOrders))
isResponseUseful = true
}

Expand All @@ -151,15 +150,16 @@ export default function* handleRecurringAoAtomics({
})

if (isResponseUseful || firstDataRequest) {
yield call(scheduleRecurringAo, {
yield call(runRecurringAOScheduler, {
gid,
startedAt,
endedAt,
recurrence,
mode,
})
} else {
debug('there are not new orders for %s, fetch again in 1m', gid)
yield delay(TIMEFRAME_WIDTHS['1m'])
yield put(AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: false }))
yield put(AOActions.requestRecurringAOAtomics({ gid, firstDataRequest: false }))
}
}
4 changes: 2 additions & 2 deletions src/redux/sagas/ao/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import requestAOsHistory from './request_aos_history'
import handleActiveAlgoOrders from './handle_active_algo_orders'
import processRecurringAOs from './process_recurring_aos'
import processRecurringAO from './process_recurring_ao'
import fetchRecurringAoAtomics from './fetch_recurring_ao_atomics'
import fetchRecurringAOAtomics from './fetch_recurring_ao_atomics'

export default function* () {
yield takeEvery(types.RESUME_REMOVE_ACTIVE_AOS, onResumeRemoveActiveAlgoOrdersHandler)
Expand All @@ -22,5 +22,5 @@ export default function* () {
yield takeEvery(WSTypes.DATA_RECURRING_ALGO_ORDERS, processRecurringAOs)
yield takeLatest(types.HANDLE_ACTIVE_AOS, handleActiveAlgoOrders)
yield takeEvery(WSTypes.DATA_ALGO_ORDER, processRecurringAO)
yield takeEvery(types.REQUEST_RECURRING_AO_ATOMICS, fetchRecurringAoAtomics)
yield takeEvery(types.REQUEST_RECURRING_AO_ATOMICS, fetchRecurringAOAtomics)
}
9 changes: 5 additions & 4 deletions src/redux/sagas/ao/process_recurring_ao.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { call, put, delay } from 'redux-saga/effects'
import Debug from 'debug'
import { isFuture } from 'date-fns'
import { Recurring } from 'bfx-hf-algo'
import scheduleRecurringAo from './schedule_recurring_ao'
import runRecurringAOScheduler from './run_recurring_ao_scheduler'
import AOActions from '../../actions/ao'
import { RECURRING_DELAY_FOR_FETCH } from '../../helpers/recurring_ao'

Expand All @@ -13,6 +13,7 @@ export default function* processRecurringAO({ payload }) {
ao: {
gid, args, createdAt, lastActive, id,
},
mode,
} = payload
if (id !== Recurring.id) {
return
Expand All @@ -23,12 +24,12 @@ export default function* processRecurringAO({ payload }) {
const shouldAtomicsBeFetched = !isFuture(new Date(startedAt)) && isOrderNew

if (!shouldAtomicsBeFetched) {
yield call(scheduleRecurringAo, {
gid, startedAt, endedAt, recurrence,
yield call(runRecurringAOScheduler, {
gid, startedAt, endedAt, recurrence, mode,
})
return
}
debug('fetch recurring ao %s atomics in 1.5m', gid)
yield delay(RECURRING_DELAY_FOR_FETCH)
yield put(AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: false }))
yield put(AOActions.requestRecurringAOAtomics({ gid, firstDataRequest: false }))
}
10 changes: 5 additions & 5 deletions src/redux/sagas/ao/process_recurring_aos.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ import _map from 'lodash/map'
import { all, call, put } from 'redux-saga/effects'
import { isFuture } from 'date-fns'
import _isEmpty from 'lodash/isEmpty'
import scheduleRecurringAo from './schedule_recurring_ao'
import runRecurringAOScheduler from './run_recurring_ao_scheduler'
import AOActions from '../../actions/ao'

export default function* processRecurringAOs({ payload }) {
const { aos } = payload
const { aos, mode } = payload

if (_isEmpty(aos)) {
return
Expand All @@ -17,11 +17,11 @@ export default function* processRecurringAOs({ payload }) {
const shouldAtomicsBeFetched = !isFuture(new Date(startedAt))

if (!shouldAtomicsBeFetched) {
return call(scheduleRecurringAo, {
gid, startedAt, endedAt, recurrence,
return call(runRecurringAOScheduler, {
gid, startedAt, endedAt, recurrence, mode,
})
}
return put(AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: true }))
return put(AOActions.requestRecurringAOAtomics({ gid, firstDataRequest: true }))
})

yield all(operations)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import {
call, delay, put, race, take,
call, delay, put, race, select, take,
} from 'redux-saga/effects'
import _max from 'lodash/max'
import Debug from 'debug'
import { v4 } from 'uuid'
import {
RECURRING_DELAY_FOR_FETCH,
calculateNextExecutionTime,
} from '../../helpers/recurring_ao'
import AOActions from '../../actions/ao'
import WSActions from '../../actions/ws'
import UIActions from '../../actions/ui'
import WSTypes from '../../constants/ws'
import i18n from '../../../locales/i18n'
import { getFormatTimeFn } from '../../selectors/ui'
import { getAlgoOrderById } from '../../selectors/ws'

const debug = Debug('hfui:recurring-ao')

Expand Down Expand Up @@ -40,14 +45,28 @@ function* purgeSchedulerOnAlgoOrderChange({ gid, sagaToExecute }) {
return cancel
}

function* scheduleCancellation({ gid, endedAtTime, endedAt }) {
const delayTime = _max(endedAtTime - Date.now(), 0)
function* scheduleCancellation({
gid, endedAtTime, endedAt, mode,
}) {
const delayTime = _max([endedAtTime - Date.now(), 0])
debug('scheduled cancelation of %s', gid, {
endedAt,
delayTime,
})
const algoOrder = yield select(getAlgoOrderById(gid))
const { alias } = algoOrder
yield delay(delayTime)
yield put(WSActions.cancelAlgoOrder(gid))

yield put(WSActions.setAlgoOrderToHistory(gid, mode))
yield put(WSActions.removeAlgoOrder(gid, mode))

const formatTime = yield select(getFormatTimeFn)
yield put(UIActions.recvNotification({
mts: Date.now(),
status: 'info',
text: i18n.t('notifications.recurringAOWasFinished', { alias, time: formatTime(endedAt) }),
cid: v4(),
}))
}

function* scheduleFetching({
Expand All @@ -65,15 +84,16 @@ function* scheduleFetching({

yield delay(delayTime)
yield put(
AOActions.requestRecurringAoAtomics({ gid, firstDataRequest: false }),
AOActions.requestRecurringAOAtomics({ gid, firstDataRequest: false }),
)
}

export default function* scheduleRecurringAo({
export default function* runRecurringAOScheduler({
gid,
startedAt,
endedAt,
recurrence,
mode,
}) {
const nextExecutionTime = calculateNextExecutionTime(
startedAt,
Expand All @@ -86,7 +106,9 @@ export default function* scheduleRecurringAo({
if (shouldBeCancelled) {
yield call(purgeSchedulerOnAlgoOrderChange, {
gid,
sagaToExecute: scheduleCancellation({ gid, endedAtTime, endedAt }),
sagaToExecute: scheduleCancellation({
gid, endedAtTime, endedAt, mode,
}),
})
return
}
Expand Down
4 changes: 2 additions & 2 deletions src/redux/selectors/ao/get_failed_recurring_ao_atomics.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,6 @@ const path = REDUCER_PATHS.AOS

const EMPTY_OBJ = {}

const getFailedRecurringAoAtomics = (state) => _get(state, `${path}.failedRecurringAoAtomics`, EMPTY_OBJ)
const getFailedRecurringAOAtomics = (state) => _get(state, `${path}.failedRecurringAOAtomics`, EMPTY_OBJ)

export default getFailedRecurringAoAtomics
export default getFailedRecurringAOAtomics
Loading

0 comments on commit 8a54905

Please sign in to comment.