Skip to content

Commit

Permalink
fix: stkLYRA claimable rewards + stake button
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelxuwu authored Mar 2, 2023
1 parent 6a5b819 commit c9a28d7
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 26 deletions.
12 changes: 7 additions & 5 deletions app/src/containers/rewards/StakeModal/StakeFormButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import React, { useCallback } from 'react'

import { LogEvent } from '@/app/constants/logEvents'
import { TransactionType } from '@/app/constants/screen'
import useAccountLyraBalances from '@/app/hooks/account/useAccountLyraBalances'
import useAccountLyraBalances, { useMutateAccountLyraBalances } from '@/app/hooks/account/useAccountLyraBalances'
import useTransaction from '@/app/hooks/account/useTransaction'
import useWalletAccount from '@/app/hooks/account/useWalletAccount'
import withSuspense from '@/app/hooks/data/withSuspense'
Expand All @@ -28,8 +28,10 @@ const StakeFormButton = withSuspense(
const account = useWalletAccount()
const execute = useTransaction('ethereum')
const insufficientBalance = lyraBalances.ethereumLyra.lt(amount)
const insufficientAllowance = lyraBalances.stakingAllowance.lt(amount)
const mutateAccountStaking = useMutateAccountStaking()
const mutateLyraStaking = useMutateLyraStaking()
const mutateLyraBalances = useMutateAccountLyraBalances()
const handleClickApprove = useCallback(async () => {
if (!account) {
console.warn('Account does not exist')
Expand All @@ -40,11 +42,11 @@ const StakeFormButton = withSuspense(
await execute(tx, {
onComplete: async () => {
logEvent(LogEvent.StakeLyraApproveSuccess)
await Promise.all([mutateAccountStaking()])
await Promise.all([mutateLyraBalances()])
},
onError: error => logEvent(LogEvent.StakeLyraApproveError, { error: error?.message }),
})
}, [account, execute, mutateAccountStaking])
}, [account, execute, mutateLyraBalances])

const handleClickStake = useCallback(async () => {
if (!account) {
Expand Down Expand Up @@ -72,7 +74,7 @@ const StakeFormButton = withSuspense(
<TransactionButton
transactionType={TransactionType.StakeLyra}
requireAllowance={
!insufficientBalance
!insufficientBalance && insufficientAllowance
? {
address: '0x01ba67aac7f75f647d94220cc98fb30fcc5105bf',
symbol: 'LYRA',
Expand All @@ -82,7 +84,7 @@ const StakeFormButton = withSuspense(
: undefined
}
width="100%"
isDisabled={amount.lte(0)}
isDisabled={amount.lte(0) || insufficientBalance}
network={'ethereum'}
label={amount.lte(0) ? 'Enter Amount' : insufficientBalance ? 'Insufficient Balance' : 'Stake'}
onClick={handleClickStake}
Expand Down
4 changes: 3 additions & 1 deletion app/src/page_helpers/RewardsVaultsPageHelper/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,9 @@ const RewardsVaultsPageHelper = ({ market, latestRewardEpoch, accountRewardEpoch
value={`${formatAPY(vaultApy, {
showSymbol: false,
showEmptyDash: true,
})} ${vaultApyMultiplier ? `(${formatNumber(vaultApyMultiplier)}x)` : ''}`}
})} ${
vaultApyMultiplier && vaultApyMultiplier > 1.01 ? `(${formatNumber(vaultApyMultiplier)}x)` : ''
}`}
valueColor={vaultApy.reduce((total, t) => total + t.amount, 0) > 0 ? 'primaryText' : 'text'}
/>
<LabelItem
Expand Down
19 changes: 7 additions & 12 deletions sdk/src/account_reward_epoch/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import fromBigNumber from '../utils/fromBigNumber'
import getGlobalContract from '../utils/getGlobalContract'
import getUniqueRewardTokenAmounts from '../utils/getUniqueRewardTokenAmounts'
import multicall, { MulticallRequest } from '../utils/multicall'
import parseClaimAddedEvents from './parseClaimAddedTags'
import parseClaimAddedEvents from './parseClaimAddedEvents'

export type ClaimAddedEvent = {
amount: BigNumber
Expand Down Expand Up @@ -87,7 +87,7 @@ export class AccountRewardEpoch {
balances: AccountBalances[],
lyraBalances: AccountLyraBalances,
claimAddedEvents: ClaimAddedEvent[],
claimedEvents: ClaimEvent[]
claimableRewards: ClaimableRewards
) {
this.lyra = lyra
this.account = account
Expand Down Expand Up @@ -137,7 +137,7 @@ export class AccountRewardEpoch {
: this.accountEpoch.tradingRewards.shortCollateralRewardDollars
)
const claimAddedCurrEpoch = claimAddedEvents.filter(ev => ev.epochTimestamp === globalEpoch.startTimestamp)
const claimableRewardsThisEpoch = parseClaimAddedEvents(claimAddedCurrEpoch, globalEpoch.epoch)
const claimableRewardsThisEpoch = parseClaimAddedEvents(claimAddedCurrEpoch, [], [globalEpoch])
const isTradingPending = claimableRewardsThisEpoch.tradingRewards.some(
rewardTokenAmount => rewardTokenAmount.amount > 0
)
Expand All @@ -151,14 +151,7 @@ export class AccountRewardEpoch {
percentShare: 0,
}

// Get claimable rewards across all previous epochs
const latestClaimedEvent = claimedEvents.length
? claimedEvents.sort((a, b) => b.blockNumber - a.blockNumber)[0]
: null
const claimableClaimAddedEvents = latestClaimedEvent
? claimAddedEvents.filter(ev => ev.blockNumber >= latestClaimedEvent.blockNumber)
: claimAddedEvents
this.claimableRewards = parseClaimAddedEvents(claimableClaimAddedEvents, globalEpoch.epoch)
this.claimableRewards = claimableRewards
this.totalClaimableVaultRewards = getUniqueRewardTokenAmounts(
Object.values(this.claimableRewards.vaultRewards).flat()
)
Expand All @@ -184,6 +177,8 @@ export class AccountRewardEpoch {
const claimAddedEvents = _claimAddedEvents.filter(
event => event.rewardToken.toLowerCase() !== '0xCb9f85730f57732fc899fb158164b9Ed60c77D49'.toLowerCase()
)
// Get claimable rewards across all previous epochs
const claimableRewards = parseClaimAddedEvents(claimAddedEvents, claimEvents, globalEpochs)
return accountEpochDatas
.map(accountEpochData => {
const globalEpoch = globalEpochs.find(
Expand All @@ -202,7 +197,7 @@ export class AccountRewardEpoch {
balances,
lyraBalances,
claimAddedEvents,
claimEvents
claimableRewards
)
})
.sort((a, b) => a.globalEpoch.endTimestamp - b.globalEpoch.endTimestamp)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { ClaimAddedEvent, RewardEpochTokenAmount } from '..'
import { GlobalRewardEpochData } from '../utils/fetchGlobalRewardEpochData'
import { ClaimAddedEvent, ClaimEvent, GlobalRewardEpoch, RewardEpochTokenAmount } from '..'
import fromBigNumber from '../utils/fromBigNumber'
import getUniqueRewardTokenAmounts from '../utils/getUniqueRewardTokenAmounts'

Expand All @@ -13,7 +12,8 @@ enum ClaimAddedProgramTags {

export default function parseClaimAddedEvents(
claimAddedEvents: ClaimAddedEvent[],
globalRewardEpoch: GlobalRewardEpochData
claimEvents: ClaimEvent[],
globalRewardEpochs: GlobalRewardEpoch[]
): {
vaultRewards: Record<string, RewardEpochTokenAmount[]>
tradingRewards: RewardEpochTokenAmount[]
Expand All @@ -30,9 +30,18 @@ export default function parseClaimAddedEvents(
const tag = event.tag
const amount = fromBigNumber(event.amount)
const rewardTokenAddress = event.rewardToken
const globalRewardEpoch = globalRewardEpochs.find(
globalEpoch => globalEpoch.startTimestamp === event.epochTimestamp
)
const claimed = claimEvents.find(
claimEvent => claimEvent.timestamp > event.timestamp && claimEvent.rewardToken === rewardTokenAddress
)
if (!globalRewardEpoch || claimed) {
return
}
if (tag.startsWith(ClaimAddedProgramTags.MMV)) {
const [_program, marketKey] = tag.split('-')
const token = globalRewardEpoch.MMVConfig[marketKey]?.tokens?.find(
const token = globalRewardEpoch.epoch.MMVConfig[marketKey]?.tokens?.find(
token => token.address.toLowerCase() === rewardTokenAddress
)
if (!vaultRewards[marketKey]) {
Expand All @@ -45,7 +54,7 @@ export default function parseClaimAddedEvents(
}
claimableToken.amount += amount
} else if (tag.startsWith(ClaimAddedProgramTags.TRADING)) {
const token = globalRewardEpoch.tradingRewardConfig.tokens.find(
const token = globalRewardEpoch.epoch.tradingRewardConfig.tokens.find(
token => token.address.toLowerCase() === rewardTokenAddress
)
if (!token) {
Expand All @@ -58,7 +67,7 @@ export default function parseClaimAddedEvents(
}
claimableToken.amount += amount
} else if (tag.startsWith(ClaimAddedProgramTags.STAKING)) {
const token = globalRewardEpoch.stakingRewardConfig.find(
const token = globalRewardEpoch.epoch.stakingRewardConfig.find(
token => token.address.toLowerCase() === rewardTokenAddress
)
if (!token) {
Expand All @@ -71,7 +80,7 @@ export default function parseClaimAddedEvents(
}
claimableToken.amount += amount
} else if (tag.startsWith(ClaimAddedProgramTags.WETHLYRA)) {
const token = globalRewardEpoch.wethLyraStakingRewardConfig?.find(
const token = globalRewardEpoch.epoch.wethLyraStakingRewardConfig?.find(
token => token.address.toLowerCase() === rewardTokenAddress
)
if (!token) {
Expand All @@ -85,7 +94,11 @@ export default function parseClaimAddedEvents(
claimableToken.amount += amount
}
})
const totalRewards = getUniqueRewardTokenAmounts([...Object.values(vaultRewards).flat(), ...tradingRewards])
const totalRewards = getUniqueRewardTokenAmounts([
...Object.values(vaultRewards).flat(),
...tradingRewards,
...wethLyraRewards,
])

return { vaultRewards, tradingRewards, stakingRewards, wethLyraRewards, totalRewards }
}

0 comments on commit c9a28d7

Please sign in to comment.