Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: foxwifhat discount #8663

Merged
merged 18 commits into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions src/components/FeeExplainer/FeeExplainer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { CardProps, StackProps } from '@chakra-ui/react'
import { Box, Card, CardBody, Flex, Heading, Stack, useToken } from '@chakra-ui/react'
import { foxWifHatAssetId } from '@shapeshiftoss/caip'
import { bnOrZero } from '@shapeshiftoss/chain-adapters'
import { LinearGradient } from '@visx/gradient'
import { GridColumns, GridRows } from '@visx/grid'
Expand Down Expand Up @@ -31,6 +32,7 @@ import {
selectIsSnapshotApiQueriesRejected,
selectVotingPower,
} from 'state/apis/snapshot/selectors'
import { selectPortfolioCryptoBalanceBaseUnitByFilter } from 'state/slices/common-selectors'
import { useAppSelector } from 'state/store'

import { CHART_TRADE_SIZE_MAX_USD } from './common'
Expand Down Expand Up @@ -149,6 +151,9 @@ const FeeChart: React.FC<FeeChartProps> = ({ foxHolding, tradeSize, feeModel })
foxHeld: bn(debouncedFoxHolding),
feeModel,
isSnapshotApiQueriesRejected,
// This is for feeExplainer which is not supporting anything else than FOX discount for now
foxWifHatHeld: bn(0),
thorHeld: bn(0),
}).feeBpsFloat.toNumber()
return { x: trade, y: feeBps }
})
Expand All @@ -161,6 +166,9 @@ const FeeChart: React.FC<FeeChartProps> = ({ foxHolding, tradeSize, feeModel })
foxHeld: bn(debouncedFoxHolding),
feeModel,
isSnapshotApiQueriesRejected,
// This is for feeExplainer which is not supporting anything else than FOX discount for now
foxWifHatHeld: bn(0),
thorHeld: bn(0),
}).feeBpsFloat.toNumber()

return [{ x: tradeSize, y: feeBps }]
Expand Down Expand Up @@ -262,12 +270,20 @@ type FeeOutputProps = {

export const FeeOutput: React.FC<FeeOutputProps> = ({ tradeSizeUSD, foxHolding, feeModel }) => {
const isSnapshotApiQueriesRejected = useAppSelector(selectIsSnapshotApiQueriesRejected)

const foxWifHatHeld = useAppSelector(state =>
selectPortfolioCryptoBalanceBaseUnitByFilter(state, { assetId: foxWifHatAssetId }),
)

const { feeUsd, feeBps, foxDiscountPercent, feeUsdBeforeDiscount, feeBpsBeforeDiscount } =
calculateFees({
tradeAmountUsd: bn(tradeSizeUSD),
foxHeld: bn(foxHolding),
feeModel,
isSnapshotApiQueriesRejected,
foxWifHatHeld: bn(foxWifHatHeld),
// @TODO: remove this when thor swap discount is removed
thorHeld: bn(0),
})

const basedOnFeeTranslation: TextPropTypes['translation'] = useMemo(
Expand Down
26 changes: 26 additions & 0 deletions src/lib/fees/model.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ describe('calculateFees', () => {
const { feeBps } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -54,6 +56,8 @@ describe('calculateFees', () => {
const { feeBps } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -67,6 +71,8 @@ describe('calculateFees', () => {
const { feeBps } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -80,6 +86,8 @@ describe('calculateFees', () => {
const { feeBps } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -93,6 +101,8 @@ describe('calculateFees', () => {
const { feeBps } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -106,6 +116,8 @@ describe('calculateFees', () => {
const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -120,6 +132,8 @@ describe('calculateFees', () => {
const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -136,6 +150,8 @@ describe('calculateFees', () => {
const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld: bn(0),
foxWifHatHeld: bn(0),
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
})
Expand All @@ -156,12 +172,14 @@ describe('calculateFees', () => {
vi.setSystemTime(new Date(FOX_WIF_HAT_CAMPAIGN_STARTING_TIME_MS))
const tradeAmountUsd = bn(FEE_CURVE_MIDPOINT_USD)
const foxHeld = bn(0)
const thorHeld = bn(0)
const foxWifHatHeld = bn(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT)
const isSnapshotApiQueriesRejected = selectIsSnapshotApiQueriesRejected(store.getState())

const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld,
foxWifHatHeld,
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
Expand All @@ -179,12 +197,14 @@ describe('calculateFees', () => {
vi.setSystemTime(new Date(campaignMidpoint))
const tradeAmountUsd = bn(FEE_CURVE_MIDPOINT_USD)
const foxHeld = bn(0)
const thorHeld = bn(0)
const foxWifHatHeld = bn(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT)
const isSnapshotApiQueriesRejected = selectIsSnapshotApiQueriesRejected(store.getState())

const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld,
foxWifHatHeld,
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
Expand All @@ -198,12 +218,14 @@ describe('calculateFees', () => {
vi.setSystemTime(new Date(FOX_WIF_HAT_CAMPAIGN_ENDING_TIME_MS))
const tradeAmountUsd = bn(FEE_CURVE_MIDPOINT_USD)
const foxHeld = bn(0)
const thorHeld = bn(0)
const foxWifHatHeld = bn(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT)
const isSnapshotApiQueriesRejected = selectIsSnapshotApiQueriesRejected(store.getState())

const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld,
foxWifHatHeld,
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
Expand All @@ -217,12 +239,14 @@ describe('calculateFees', () => {
vi.setSystemTime(new Date(FOX_WIF_HAT_CAMPAIGN_STARTING_TIME_MS))
const tradeAmountUsd = bn(FEE_CURVE_MIDPOINT_USD)
const foxHeld = bn(0)
const thorHeld = bn(0)
const foxWifHatHeld = bn(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT).minus(1)
const isSnapshotApiQueriesRejected = selectIsSnapshotApiQueriesRejected(store.getState())

const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld,
foxWifHatHeld,
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
Expand All @@ -236,12 +260,14 @@ describe('calculateFees', () => {
vi.setSystemTime(new Date(FOX_WIF_HAT_CAMPAIGN_ENDING_TIME_MS + 1))
const tradeAmountUsd = bn(FEE_CURVE_MIDPOINT_USD)
const foxHeld = bn(0)
const thorHeld = bn(0)
const foxWifHatHeld = bn(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT)
const isSnapshotApiQueriesRejected = selectIsSnapshotApiQueriesRejected(store.getState())

const { feeBps, foxDiscountPercent } = calculateFees({
tradeAmountUsd,
foxHeld,
thorHeld,
foxWifHatHeld,
feeModel: 'SWAPPER',
isSnapshotApiQueriesRejected,
Expand Down
19 changes: 7 additions & 12 deletions src/lib/fees/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ export const THORSWAP_MAXIMUM_YEAR_TRESHOLD = 2025
type CalculateFeeBpsArgs = {
tradeAmountUsd: BigNumber
foxHeld: BigNumber
thorHeld?: BigNumber
foxWifHatHeld?: BigNumber
thorHeld: BigNumber
foxWifHatHeld: BigNumber
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
feeModel: ParameterModel
isSnapshotApiQueriesRejected: boolean
}
Expand Down Expand Up @@ -92,26 +92,21 @@ export const calculateFees: CalculateFeeBps = ({

const isThorFree =
isThorFreeEnabled &&
thorHeld?.gte(THORSWAP_UNIT_THRESHOLD) &&
thorHeld.gte(THORSWAP_UNIT_THRESHOLD) &&
new Date().getUTCFullYear() < THORSWAP_MAXIMUM_YEAR_TRESHOLD

// failure to fetch fox discount results in free trades.
const isFallbackFees =
isSnapshotApiQueriesRejected &&
(!foxWifHatHeld ||
foxWifHatHeld?.lt(FOX_WIF_HAT_MINIMUM_AMOUNT_BASE_UNIT) ||
!isFoxWifHatCampaignActive)
const isFallbackFees = isSnapshotApiQueriesRejected

// the fox discount before any other logic is applied
const foxBaseDiscountPercent = (() => {
if (isFree) return bn(100)
// THOR holder before TIP014 are trade free until 2025
if (isThorFree) return bn(100)

const foxDiscountPercent = BigNumber.minimum(
bn(100),
bnOrZero(foxHeld).times(100).div(bn(FEE_CURVE_FOX_MAX_DISCOUNT_THRESHOLD)),
)
const foxDiscountPercent = isFoxWifHatCampaignActive
? bnOrZero(foxHeld).times(100).div(bn(FEE_CURVE_FOX_MAX_DISCOUNT_THRESHOLD))
: bn(0)

// No discount if we cannot fetch FOX holdings
if (isFallbackFees) return bn(0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,13 @@ import {
usePrevious,
} from '@chakra-ui/react'
import type { AccountId, AssetId, ChainId } from '@shapeshiftoss/caip'
import { fromAccountId, fromAssetId, thorchainAssetId, thorchainChainId } from '@shapeshiftoss/caip'
import {
foxWifHatAssetId,
fromAccountId,
fromAssetId,
thorchainAssetId,
thorchainChainId,
} from '@shapeshiftoss/caip'
import { SwapperName } from '@shapeshiftoss/swapper'
import {
assetIdToPoolAssetId,
Expand Down Expand Up @@ -202,6 +208,10 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
const [virtualRuneDepositAmountFiatUserCurrency, setVirtualRuneDepositAmountFiatUserCurrency] =
useState<string | undefined>()

const foxWifHatHeld = useAppSelector(state =>
selectPortfolioCryptoBalanceBaseUnitByFilter(state, { assetId: foxWifHatAssetId }),
)

const [slippageDecimalPercentage, setSlippageDecimalPercentage] = useState<string | undefined>()

const isUnsafeQuote = useMemo(
Expand Down Expand Up @@ -1014,6 +1024,9 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
const { feeBps, feeUsd } = calculateFees({
tradeAmountUsd: bn(totalAmountUsd),
foxHeld: bnOrZero(votingPower),
foxWifHatHeld: bn(foxWifHatHeld),
// @TODO: remove this when thor swap discount is removed
NeOMakinG marked this conversation as resolved.
Show resolved Hide resolved
thorHeld: bn(0),
feeModel: 'THORCHAIN_LP',
isSnapshotApiQueriesRejected,
})
Expand Down Expand Up @@ -1058,6 +1071,7 @@ export const AddLiquidityInput: React.FC<AddLiquidityInputProps> = ({
userCurrencyToUsdRate,
votingPower,
isSnapshotApiQueriesRejected,
foxWifHatHeld,
])

const percentOptions = useMemo(() => [], [])
Expand Down
Loading