Skip to content

Commit

Permalink
chore: rename fee type to ShapeshiftFeeMetadata
Browse files Browse the repository at this point in the history
  • Loading branch information
woodenfurniture committed Jan 16, 2025
1 parent dbe8838 commit eba3258
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,12 @@ export const TradeInput = ({ isCompact, tradeInputRef, onChangeTab }: TradeInput

// Set the confirmed quote for execution, with a snapshot of the affiliate fees for display after the trade is executed.
// This is done to handle the fox power calculation changing due to FOX balance changes after the trade is executed.
dispatch(tradeQuoteSlice.actions.setConfirmedQuote({ quote: activeQuote, calculatedFees }))
dispatch(
tradeQuoteSlice.actions.setConfirmedQuote({
quote: activeQuote,
shapeshiftFeeMetadata: calculatedFees,
}),
)
dispatch(tradeQuoteSlice.actions.clearQuoteExecutionState(activeQuote.id))

if (isLedger(wallet)) {
Expand Down
6 changes: 3 additions & 3 deletions src/lib/fees/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ type CalculateFeeBpsArgs = {

/**
* Represents the return type for calculating fee basis points (bps).
* @type {Object} CalculateFeeBpsReturn
* @type {Object} ShapeshiftFeeMetadata
* @property {BigNumber} feeBps - The net fee bps (i.e., including the fox discount) used for actual trades.
* @property {BigNumber} feeBpsFloat - `feeBps` as a floating point number, used for plotting the theoretical bps ignoring the realities of integer bps values.
* @property {BigNumber} feeUsd - The net USD value of the fee (i.e., including the fox discount).
Expand All @@ -27,7 +27,7 @@ type CalculateFeeBpsArgs = {
* @property {BigNumber} feeUsdBeforeDiscount - The gross USD value of the fee (i.e., excluding the fox discount).
* @property {BigNumber} feeBpsBeforeDiscount - The gross fee bps (i.e., excluding the fox discount).
*/
export type CalculateFeeBpsReturn = {
export type ShapeshiftFeeMetadata = {
feeBps: BigNumber
feeBpsFloat: BigNumber
feeUsd: BigNumber
Expand All @@ -36,7 +36,7 @@ export type CalculateFeeBpsReturn = {
feeUsdBeforeDiscount: BigNumber
feeBpsBeforeDiscount: BigNumber
}
type CalculateFeeBps = (args: CalculateFeeBpsArgs) => CalculateFeeBpsReturn
type CalculateFeeBps = (args: CalculateFeeBpsArgs) => ShapeshiftFeeMetadata

export const calculateFees: CalculateFeeBps = ({
tradeAmountUsd,
Expand Down
6 changes: 3 additions & 3 deletions src/state/apis/snapshot/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { bnOrZero } from '@shapeshiftoss/utils'
import createCachedSelector from 're-reselect'
import type { Selector } from 'reselect'
import { createSelector } from 'reselect'
import type { CalculateFeeBpsReturn } from 'lib/fees/model'
import type { ShapeshiftFeeMetadata } from 'lib/fees/model'
import { calculateFees } from 'lib/fees/model'
import type { ParameterModel } from 'lib/fees/parameters/types'
import { isSome } from 'lib/utils'
Expand Down Expand Up @@ -57,15 +57,15 @@ type AffiliateFeesProps = {
inputAmountUsd: string | undefined
}

export const selectCalculatedFees: Selector<ReduxState, CalculateFeeBpsReturn> =
export const selectCalculatedFees: Selector<ReduxState, ShapeshiftFeeMetadata> =
createCachedSelector(
(_state: ReduxState, { feeModel }: AffiliateFeesProps) => feeModel,
(_state: ReduxState, { inputAmountUsd }: AffiliateFeesProps) => inputAmountUsd,
selectVotingPower,
selectThorVotingPower,
selectIsSnapshotApiQueriesRejected,
(feeModel, inputAmountUsd, votingPower, thorVotingPower, isSnapshotApiQueriesRejected) => {
const fees: CalculateFeeBpsReturn = calculateFees({
const fees: ShapeshiftFeeMetadata = calculateFees({
tradeAmountUsd: bnOrZero(inputAmountUsd),
foxHeld: bnOrZero(votingPower),
thorHeld: bnOrZero(thorVotingPower),
Expand Down
2 changes: 1 addition & 1 deletion src/state/slices/tradeQuoteSlice/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const initialTradeExecutionState = {
export const initialState: TradeQuoteSliceState = {
activeQuoteMeta: undefined,
confirmedQuote: undefined,
confirmedFees: undefined,
confirmedShapeshiftFeeMetadata: undefined,
activeStep: undefined,
tradeExecution: {},
tradeQuotes: {},
Expand Down
28 changes: 15 additions & 13 deletions src/state/slices/tradeQuoteSlice/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import type { Asset } from '@shapeshiftoss/types'
import { identity } from 'lodash'
import type { Selector } from 'reselect'
import { bn, bnOrZero } from 'lib/bignumber/bignumber'
import type { CalculateFeeBpsReturn } from 'lib/fees/model'
import type { ShapeshiftFeeMetadata } from 'lib/fees/model'
import { fromBaseUnit } from 'lib/math'
import { validateQuoteRequest } from 'state/apis/swapper/helpers/validateQuoteRequest'
import { selectIsTradeQuoteApiQueryPending } from 'state/apis/swapper/selectors'
Expand Down Expand Up @@ -205,10 +205,12 @@ export const selectConfirmedQuote: Selector<ReduxState, TradeQuote | TradeRate |
return tradeQuoteState.confirmedQuote
})

export const selectConfirmedFees: Selector<ReduxState, CalculateFeeBpsReturn | undefined> =
createDeepEqualOutputSelector(selectTradeQuoteSlice, tradeQuoteState => {
return tradeQuoteState.confirmedFees
})
export const selectConfirmedShapeshiftFeeMetadata: Selector<
ReduxState,
ShapeshiftFeeMetadata | undefined
> = createDeepEqualOutputSelector(selectTradeQuoteSlice, tradeQuoteState => {
return tradeQuoteState.confirmedShapeshiftFeeMetadata
})

export const selectActiveQuoteMetaOrDefault: Selector<
ReduxState,
Expand Down Expand Up @@ -569,21 +571,21 @@ export const selectActiveQuoteAffiliateBps: Selector<ReduxState, string | undefi
})

export const selectTradeQuoteAffiliateFeeAfterDiscountUsd = createSelector(
selectConfirmedFees,
selectConfirmedShapeshiftFeeMetadata,
selectActiveQuoteAffiliateBps,
(calculatedFees, affiliateBps) => {
if (!affiliateBps || !calculatedFees) return
(confirmedShapeshiftFeeMetadata, affiliateBps) => {
if (!affiliateBps || !confirmedShapeshiftFeeMetadata) return
if (affiliateBps === '0') return bn(0)

return calculatedFees.feeUsd
return confirmedShapeshiftFeeMetadata.feeUsd
},
)

export const selectTradeQuoteAffiliateFeeDiscountUsd = createSelector(
selectConfirmedFees,
confirmedFees => {
if (!confirmedFees) return
return confirmedFees.foxDiscountUsd
selectConfirmedShapeshiftFeeMetadata,
confirmedShapeshiftFeeMetadata => {
if (!confirmedShapeshiftFeeMetadata) return
return confirmedShapeshiftFeeMetadata.foxDiscountUsd
},
)

Expand Down
6 changes: 3 additions & 3 deletions src/state/slices/tradeQuoteSlice/tradeQuoteSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { createSlice } from '@reduxjs/toolkit'
import type { SwapperName, TradeQuote, TradeRate } from '@shapeshiftoss/swapper'
import { orderBy, uniqBy } from 'lodash'
import type { InterpolationOptions } from 'node-polyglot'
import type { CalculateFeeBpsReturn } from 'lib/fees/model'
import type { ShapeshiftFeeMetadata } from 'lib/fees/model'
import type { ApiQuote } from 'state/apis/swapper/types'

import { initialState, initialTradeExecutionState } from './constants'
Expand Down Expand Up @@ -57,11 +57,11 @@ export const tradeQuoteSlice = createSlice({
state,
action: PayloadAction<{
quote: TradeQuote | TradeRate
calculatedFees: CalculateFeeBpsReturn
shapeshiftFeeMetadata: ShapeshiftFeeMetadata
}>,
) => {
state.confirmedQuote = action.payload.quote
state.confirmedFees = action.payload.calculatedFees
state.confirmedShapeshiftFeeMetadata = action.payload.shapeshiftFeeMetadata
},
clearQuoteExecutionState: (state, action: PayloadAction<TradeQuote['id']>) => {
state.tradeExecution[action.payload] = initialTradeExecutionState
Expand Down
4 changes: 2 additions & 2 deletions src/state/slices/tradeQuoteSlice/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { SwapperName, TradeQuote, TradeRate } from '@shapeshiftoss/swapper'
import type { PartialRecord } from '@shapeshiftoss/types'
import type { InterpolationOptions } from 'node-polyglot'
import type { CalculateFeeBpsReturn } from 'lib/fees/model'
import type { ShapeshiftFeeMetadata } from 'lib/fees/model'
import type { ApiQuote } from 'state/apis/swapper/types'

export type ActiveQuoteMeta = {
Expand All @@ -15,7 +15,7 @@ export type TradeQuoteSliceState = {
confirmedQuote: TradeQuote | TradeRate | undefined // the quote being executed
// Used to display the "You saved" message in the TradeSuccess component. This needs to be stored
// here because trading fox will affect the calculation after the trade has been executed.
confirmedFees: CalculateFeeBpsReturn | undefined
confirmedShapeshiftFeeMetadata: ShapeshiftFeeMetadata | undefined
tradeExecution: Record<TradeQuote['id'], TradeExecutionMetadata>
tradeQuotes: PartialRecord<SwapperName, Record<string, ApiQuote>> // mapping from swapperName to quoteId to ApiQuote
tradeQuoteDisplayCache: ApiQuote[]
Expand Down
2 changes: 1 addition & 1 deletion src/test/mocks/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,7 @@ export const mockStore: ReduxState = {
tradeQuoteSlice: {
activeQuoteMeta: undefined,
confirmedQuote: undefined,
confirmedFees: undefined,
confirmedShapeshiftFeeMetadata: undefined,
activeStep: undefined,
tradeExecution: {},
tradeQuotes: {},
Expand Down

0 comments on commit eba3258

Please sign in to comment.