Skip to content

Commit

Permalink
Refactored the estimated reward query
Browse files Browse the repository at this point in the history
  • Loading branch information
LeifuChen committed Jul 7, 2023
1 parent b6551ff commit 26bf8ce
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 14 deletions.
25 changes: 13 additions & 12 deletions packages/app/src/sections/dashboard/RewardsTab.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ZERO_WEI } from '@kwenta/sdk/constants'
import { formatDollars, formatPercent, truncateNumbers } from '@kwenta/sdk/utils'
import { formatDollars, formatNumber, formatPercent } from '@kwenta/sdk/utils'
import { wei } from '@synthetixio/wei'
import { FC, useCallback, useMemo } from 'react'
import { useTranslation } from 'react-i18next'
Expand All @@ -13,14 +13,16 @@ import { Body, Heading } from 'components/Text'
import { EXTERNAL_LINKS } from 'constants/links'
import { NO_VALUE } from 'constants/placeholder'
import useIsL2 from 'hooks/useIsL2'
import { TradingRewardProps, useEstimatedReward } from 'queries/staking/utils'
import { TradingRewardProps } from 'queries/staking/utils'
import { StakingCard } from 'sections/dashboard/Stake/card'
import { selectFuturesFees, selectFuturesFeesForAccount } from 'state/futures/selectors'
import { useAppDispatch, useAppSelector } from 'state/hooks'
import { claimMultipleAllRewards } from 'state/staking/actions'
import { setSelectedEpoch } from 'state/staking/reducer'
import {
selectEpochData,
selectEstimatedKwentaRewards,
selectEstimatedOpRewards,
selectKwentaRewards,
selectOpRewards,
selectSelectedEpoch,
Expand All @@ -44,16 +46,15 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
const kwentaRewards = useAppSelector(selectKwentaRewards)
const opRewards = useAppSelector(selectOpRewards)
const snxOpRewards = useAppSelector(selectSnxOpRewards)
const estimatedKwentaRewards = useAppSelector(selectEstimatedKwentaRewards)
const estimatedOpRewards = useAppSelector(selectEstimatedOpRewards)
const futuresFeePaid = useAppSelector(selectFuturesFeesForAccount)
const totalFuturesFeePaid = useAppSelector(selectFuturesFees)

const handleClaimAll = useCallback(() => {
dispatch(claimMultipleAllRewards())
}, [dispatch])

const estimatedKwentaReward = useEstimatedReward('epoch-current.json')
const estimatedOp = useEstimatedReward('epoch-current-op.json')

const claimDisabledAll = useMemo(
() => kwentaRewards.add(opRewards).add(snxOpRewards).lte(0),
[opRewards, snxOpRewards, kwentaRewards]
Expand All @@ -74,7 +75,7 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
labels: [
{
label: t('dashboard.stake.portfolio.rewards.title'),
value: truncateNumbers(kwentaRewards, 4),
value: formatNumber(kwentaRewards, { minDecimals: 4 }),
},
{
label: t('dashboard.stake.tabs.trading-rewards.fee-paid'),
Expand All @@ -96,7 +97,7 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
},
{
label: t('dashboard.rewards.estimated'),
value: truncateNumbers(wei(estimatedKwentaReward ?? ZERO_WEI), 4),
value: formatNumber(estimatedKwentaRewards, { minDecimals: 4 }),
},
],
kwentaIcon: true,
Expand All @@ -109,13 +110,13 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
labels: [
{
label: t('dashboard.stake.portfolio.rewards.title'),
value: truncateNumbers(wei(opRewards ?? ZERO_WEI), 4),
value: formatNumber(opRewards, { minDecimals: 4 }),
},
],
info: [
{
label: t('dashboard.rewards.estimated'),
value: truncateNumbers(wei(estimatedOp ?? ZERO_WEI), 4),
value: formatNumber(estimatedOpRewards, { minDecimals: 4 }),
},
],
kwentaIcon: false,
Expand All @@ -128,7 +129,7 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
labels: [
{
label: t('dashboard.stake.portfolio.rewards.title'),
value: truncateNumbers(wei(snxOpRewards ?? ZERO_WEI), 4),
value: formatNumber(snxOpRewards, { minDecimals: 4 }),
},
],
info: [
Expand All @@ -142,8 +143,8 @@ const RewardsTab: FC<TradingRewardProps> = ({ period = 0 }) => {
},
],
[
estimatedKwentaReward,
estimatedOp,
estimatedKwentaRewards,
estimatedOpRewards,
futuresFeePaid,
kwentaRewards,
opRewards,
Expand Down
17 changes: 15 additions & 2 deletions packages/app/src/state/staking/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ export const approveKwentaToken = createAsyncThunk<
monitorTransaction({
txHash: hash,
onTxConfirmed: () => {
dispatch(fetchStakingData())
dispatch(fetchStakeMigrateData())
},
})
})
Expand All @@ -118,7 +118,7 @@ export const redeemToken = createAsyncThunk<void, 'vKwenta' | 'veKwenta', ThunkC
monitorTransaction({
txHash: hash,
onTxConfirmed: () => {
dispatch(fetchStakingData())
dispatch(fetchStakeMigrateData())
},
})
}
Expand Down Expand Up @@ -160,6 +160,18 @@ export const fetchEscrowV2Data = createAsyncThunk<
}
})

export const fetchEstimatedRewards = createAsyncThunk<
{ estimatedKwentaRewards: string; estimatedOpRewards: string },
void,
ThunkConfig
>('staking/fetchEstimatedRewards', async (_, { extra: { sdk } }) => {
const { estimatedKwentaRewards, estimatedOpRewards } = await sdk.kwentaToken.getEstimatedRewards()
return {
estimatedKwentaRewards: estimatedKwentaRewards.toString(),
estimatedOpRewards: estimatedOpRewards.toString(),
}
})

export const fetchStakeMigrateData = createAsyncThunk<void, void, ThunkConfig>(
'stakeMigrateData/fetch',
async (_, { dispatch }) => {
Expand All @@ -168,6 +180,7 @@ export const fetchStakeMigrateData = createAsyncThunk<void, void, ThunkConfig>(
dispatch(fetchStakingV2Data())
dispatch(fetchEscrowData())
dispatch(fetchEscrowV2Data())
dispatch(fetchEstimatedRewards())
}
)

Expand Down
7 changes: 7 additions & 0 deletions packages/app/src/state/staking/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
vestEscrowedRewards,
fetchStakingV2Data,
fetchEscrowV2Data,
fetchEstimatedRewards,
} from './actions'
import { StakingState } from './types'

Expand Down Expand Up @@ -49,6 +50,8 @@ export const STAKING_INITIAL_STATE: StakingState = {
kwentaRewards: '0',
opRewards: '0',
snxOpRewards: '0',
estimatedKwentaRewards: '0',
estimatedOpRewards: '0',
claimableKwentaRewards: [],
claimableOpRewards: [],
claimableSnxOpRewards: [],
Expand Down Expand Up @@ -147,6 +150,10 @@ const stakingSlice = createSlice({
state.opRewards = action.payload.opRewards
state.snxOpRewards = action.payload.snxOpRewards
})
builder.addCase(fetchEstimatedRewards.fulfilled, (state, action) => {
state.estimatedKwentaRewards = action.payload.estimatedKwentaRewards
state.estimatedOpRewards = action.payload.estimatedOpRewards
})
builder.addCase(stakeKwenta.pending, (state) => {
state.stakeStatus = FetchStatus.Loading
})
Expand Down
10 changes: 10 additions & 0 deletions packages/app/src/state/staking/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,16 @@ export const selectSnxOpRewards = createSelector(
wei
)

export const selectEstimatedKwentaRewards = createSelector(
(state: RootState) => state.staking.estimatedKwentaRewards,
wei
)

export const selectEstimatedOpRewards = createSelector(
(state: RootState) => state.staking.estimatedOpRewards,
wei
)

export const selectTotalVestable = createSelector(
(state: RootState) => state.staking.totalVestable,
wei
Expand Down
2 changes: 2 additions & 0 deletions packages/app/src/state/staking/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ export type StakingState = {
kwentaRewards: string
opRewards: string
snxOpRewards: string
estimatedKwentaRewards: string
estimatedOpRewards: string
claimableKwentaRewards: ClaimParams[][]
claimableOpRewards: ClaimParams[]
claimableSnxOpRewards: ClaimParams[]
Expand Down
26 changes: 26 additions & 0 deletions packages/sdk/src/services/kwentaToken.ts
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,32 @@ export default class KwentaTokenService {
return this.performStakeAction('unstake', amount, { escrow: true, version: 2 })
}

public async getEstimatedRewards() {
const { networkId, walletAddress } = this.sdk.context
const fileNames = ['', '-op'].map(
(i) => `trading-rewards-snapshots/${networkId === 420 ? 'goerli-' : ''}epoch-current${i}.json`
)

const responses: EpochData[] = await Promise.all(
fileNames.map(async (fileName) => {
const response = await client.get(fileName)
return { ...response.data }
})
)

const [estimatedKwentaRewards, estimatedOpRewards] = responses.map((d) => {
const reward = d.claims[walletAddress]

if (reward) {
return weiFromWei(reward.amount)
}

return ZERO_WEI
})

return { estimatedKwentaRewards, estimatedOpRewards }
}

public async getClaimableRewards(epochPeriod: number, isOldDistributor: boolean = true) {
const { MultipleMerkleDistributor, MultipleMerkleDistributorPerpsV2 } =
this.sdk.context.multicallContracts
Expand Down

0 comments on commit 26bf8ce

Please sign in to comment.