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

CP-9517: Use tokenInCurrencyFormatter to show rewards and fees for staking #2159

Merged
merged 3 commits into from
Dec 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
61 changes: 37 additions & 24 deletions packages/core-mobile/app/screens/earn/ClaimRewards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import { ConfirmScreen } from './components/ConfirmScreen'
type ScreenProps = EarnScreenProps<typeof AppNavigation.Earn.ClaimRewards>

const ClaimRewards = (): JSX.Element | null => {
const { theme } = useApplicationContext()
const { theme, appHook } = useApplicationContext()
const { navigate, goBack } = useNavigation<ScreenProps['navigation']>()
const onBack = useRoute<ScreenProps['route']>().params?.onBack
const { data } = usePChainBalance()
Expand Down Expand Up @@ -85,29 +85,42 @@ const ClaimRewards = (): JSX.Element | null => {
}
}, [navigate, showFeeError, insufficientBalanceForFee])

const [claimableAmountInAvax, claimableAmountInCurrency] = useMemo(() => {
if (data?.balancePerType.unlockedUnstaked) {
const unlockedInUnit = new TokenUnit(
data.balancePerType.unlockedUnstaked,
pNetwork.networkToken.decimals,
pNetwork.networkToken.symbol
)
return [
unlockedInUnit.toDisplay(),
unlockedInUnit.mul(avaxPrice).toDisplay()
]
const [claimableAmountInAvax, formattedClaimableAmountInCurrency] =
useMemo(() => {
if (data?.balancePerType.unlockedUnstaked) {
const unlockedInUnit = new TokenUnit(
data.balancePerType.unlockedUnstaked,
pNetwork.networkToken.decimals,
pNetwork.networkToken.symbol
)
return [
unlockedInUnit.toDisplay(),
appHook.tokenInCurrencyFormatter(
unlockedInUnit.mul(avaxPrice).toDisplay({ asNumber: true })
)
]
}
return [UNKNOWN_AMOUNT, UNKNOWN_AMOUNT]
}, [
avaxPrice,
data?.balancePerType.unlockedUnstaked,
pNetwork.networkToken.decimals,
pNetwork.networkToken.symbol,
appHook
])

const [feesInAvax, formattedFeesInCurrency] = useMemo(() => {
if (totalFees === undefined) {
return [UNKNOWN_AMOUNT, UNKNOWN_AMOUNT]
}
return [UNKNOWN_AMOUNT, UNKNOWN_AMOUNT]
}, [
avaxPrice,
data?.balancePerType.unlockedUnstaked,
pNetwork.networkToken.decimals,
pNetwork.networkToken.symbol
])

const [feesInAvax, feesInCurrency] = useMemo(() => {
return [totalFees?.toDisplay(), totalFees?.mul(avaxPrice).toDisplay()]
}, [avaxPrice, totalFees])
return [
totalFees.toDisplay(),
appHook.tokenInCurrencyFormatter(
totalFees.mul(avaxPrice).toDisplay({ asNumber: true })
)
]
}, [avaxPrice, totalFees, appHook])

const cancelClaim = (): void => {
AnalyticsService.capture('StakeCancelClaim')
Expand All @@ -134,7 +147,7 @@ const ClaimRewards = (): JSX.Element | null => {
</AvaText.Heading6>
<Space y={4} />
<AvaText.Body3 testID="network_fee_currency" color={theme.colorText2}>
{`${feesInCurrency} ${selectedCurrency}`}
{formattedFeesInCurrency}
</AvaText.Body3>
</View>
)
Expand Down Expand Up @@ -198,7 +211,7 @@ const ClaimRewards = (): JSX.Element | null => {
<AvaText.Heading3
textStyle={{ alignSelf: 'flex-end', color: theme.colorText2 }}
testID="claimable_balance_currency">
{`${claimableAmountInCurrency} ${selectedCurrency}`}
{formattedClaimableAmountInCurrency}
</AvaText.Heading3>
</View>
<Separator />
Expand Down
14 changes: 6 additions & 8 deletions packages/core-mobile/app/screens/earn/components/StakeCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import NetworkService from 'services/network/NetworkService'
import { useSelector } from 'react-redux'
import { xpChainToken } from 'utils/units/knownTokens'
import { UTCDate } from '@date-fns/utc'
import { selectSelectedCurrency } from 'store/settings/currency/slice'
import { UNKNOWN_AMOUNT } from 'consts/amount'
import { selectActiveNetwork } from 'store/network'
import { isDevnet } from 'utils/isDevnet'
Expand Down Expand Up @@ -59,7 +58,6 @@ export const StakeCard = (props: Props): JSX.Element => {
const navigation = useNavigation<NavigationProp>()
const { txHash, status, title, stakeAmount } = props
const avaxPrice = useAvaxTokenPriceInSelectedCurrency()
const selectedCurrency = useSelector(selectSelectedCurrency)
const isDevMode = useSelector(selectIsDeveloperMode)
const activeNetwork = useSelector(selectActiveNetwork)
const { networkToken: pChainNetworkToken } =
Expand Down Expand Up @@ -112,7 +110,7 @@ export const StakeCard = (props: Props): JSX.Element => {
const stakeAmountInCurrency = stakeAmountInAvax?.mul(avaxPrice)
const stakeAmountInCurrencyDisplay = stakeAmountInCurrency
? tokenInCurrencyFormatter(
stakeAmountInCurrency.toDisplay({ fixedDp: 2, asNumber: true })
stakeAmountInCurrency.toDisplay({ asNumber: true })
)
: UNKNOWN_AMOUNT

Expand Down Expand Up @@ -157,7 +155,7 @@ export const StakeCard = (props: Props): JSX.Element => {
{stakeAmountInAvaxDisplay} AVAX
</AvaText.Heading6>
<AvaText.Overline>
{`${stakeAmountInCurrencyDisplay} ${selectedCurrency}`}
{`${stakeAmountInCurrencyDisplay}`}
</AvaText.Overline>
</View>
</Row>
Expand All @@ -176,7 +174,7 @@ export const StakeCard = (props: Props): JSX.Element => {
{estimatedRewardInAvaxDisplay} AVAX
</AvaText.Heading6>
<AvaText.Overline>
{`${estimatedRewardInCurrencyDisplay} ${selectedCurrency}`}
{`${estimatedRewardInCurrencyDisplay}`}
</AvaText.Overline>
</View>
</Row>
Expand All @@ -202,7 +200,7 @@ export const StakeCard = (props: Props): JSX.Element => {

const rewardAmountInCurrencyDisplay = rewardAmountInCurrency
? tokenInCurrencyFormatter(
rewardAmountInCurrency.toDisplay({ fixedDp: 2, asNumber: true })
rewardAmountInCurrency.toDisplay({ asNumber: true })
)
: UNKNOWN_AMOUNT

Expand All @@ -223,7 +221,7 @@ export const StakeCard = (props: Props): JSX.Element => {
{stakeAmountInAvaxDisplay} AVAX
</AvaText.Heading6>
<AvaText.Overline>
{`${stakeAmountInCurrencyDisplay} ${selectedCurrency}`}
{`${stakeAmountInCurrencyDisplay}`}
</AvaText.Overline>
</View>
</Row>
Expand All @@ -244,7 +242,7 @@ export const StakeCard = (props: Props): JSX.Element => {
{rewardAmountInAvaxDisplay} AVAX
</AvaText.Heading6>
<AvaText.Overline>
{`${rewardAmountInCurrencyDisplay} ${selectedCurrency}`}
{`${rewardAmountInCurrencyDisplay}`}
</AvaText.Overline>
</View>
</Row>
Expand Down
Loading