This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters