Skip to content
This repository was archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
add applyFeeMiddlware
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 9, 2020
1 parent 21953fe commit 3753a9a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
44 changes: 44 additions & 0 deletions src/custom/state/fee/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Middleware } from '@reduxjs/toolkit'
import { getFee } from 'utils/fees'
import { selectCurrency } from '@src/state/swap/actions'
import { updateFee } from './actions'
import { FeeInformationState } from './reducer'

export const applyFeeMiddleware: Middleware = ({ dispatch, getState }) => next => async action => {
// check that incoming action is a selected token
const isTriggerAction = action.type === selectCurrency.type

if (isTriggerAction) {
const { payload } = action

// check actions as several should trigger this update
// e.g direct selection? yes, also has payload to check token type
// but switchCurrencies? has no payload, but must be used to check tip

const {
fee: { feesMap }
}: { fee: FeeInformationState } = getState()

const tokenFee = feesMap[payload.currencyId]?.fee

if (!tokenFee) {
const fee = await getFee(payload.currencyId)
dispatch(
updateFee({
token: payload.currencyId,
fee
})
)
}

console.debug(`
========================================================
🚀 [FEE MIDDLEWARE] SELECT_CURRENCY ACTION DETECTED
PAYLOAD: ${JSON.stringify(payload, null, 2)}
FEE: ${JSON.stringify(getState().fee.feesMap[payload.currencyId]?.fee, null, 2)}
========================================================
`)
}

next(action)
}
4 changes: 2 additions & 2 deletions src/custom/state/fee/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ interface FeeInformationObject {
// {token address => FeeInformationObject} mapping
type FeesMap = Record<string, FeeInformationObject>

export interface OperatorState {
export interface FeeInformationState {
readonly feesMap: Partial<FeesMap>
}

const initialState: OperatorState = {
const initialState: FeeInformationState = {
feesMap: {}
}

Expand Down
4 changes: 3 additions & 1 deletion src/custom/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ import multicall from '@src/state/multicall/reducer'
import operator from './operator/reducer'
import orders from './orders/reducer'
import fee from './fee/reducer'
// CUSTOM MIDDLEWARE
import { applyFeeMiddleware } from './fee/middleware'

const UNISWAP_REDUCERS = {
application,
Expand All @@ -36,7 +38,7 @@ const store = configureStore({
orders,
fee
},
middleware: [...getDefaultMiddleware({ thunk: false }), save({ states: PERSISTED_KEYS })],
middleware: [...getDefaultMiddleware({ thunk: false }), save({ states: PERSISTED_KEYS }), applyFeeMiddleware],
preloadedState: load({ states: PERSISTED_KEYS })
})

Expand Down

0 comments on commit 3753a9a

Please sign in to comment.