Skip to content

Commit

Permalink
Add HNT claimable rewards
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Nov 22, 2024
1 parent 3f512eb commit 6160f65
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 41 deletions.
4 changes: 2 additions & 2 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2046,9 +2046,9 @@ SPEC CHECKSUMS:
Toast: 91b396c56ee72a5790816f40d3a94dd357abc196
Turf: aa2ede4298009639d10db36aba1a7ebaad072a5e
VisionCamera: cb84d0d8485b3e67c91b62931d3aa88f49747c92
Yoga: 950bbfd7e6f04790fdb51149ed51df41f329fcc8
Yoga: 2246eea72aaf1b816a68a35e6e4b74563653ae09
ZXingObjC: 8898711ab495761b2dbbdec76d90164a6d7e14c5

PODFILE CHECKSUM: af8e0f0904c0f9cc43a95eef4e1feb504870ee6e

COCOAPODS: 1.16.1
COCOAPODS: 1.15.2
87 changes: 63 additions & 24 deletions src/app/services/HotspotService/pages/ClaimTokensPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import { useTranslation } from 'react-i18next'
import { Image, RefreshControl } from 'react-native'
import MobileIcon from '@assets/svgs/mobileIconNew.svg'
import IotIcon from '@assets/svgs/iotIconNew.svg'
import HntIcon from '@assets/svgs/hnt.svg'
import TouchableContainer from '@components/TouchableContainer'
import BalanceText from '@components/BalanceText'
import useHotspots from '@hooks/useHotspots'
import { toNumber } from '@helium/spl-utils'
import {
MOBILE_LAZY_KEY,
IOT_LAZY_KEY,
HNT_LAZY_KEY,
MIN_BALANCE_THRESHOLD,
} from '@utils/constants'
import useSubmitTxn from '@hooks/useSubmitTxn'
Expand Down Expand Up @@ -44,6 +46,7 @@ const ClaimTokensPage = () => {

const {
pendingIotRewards,
pendingHntRewards,
pendingMobileRewards,
hotspotsWithMeta,
totalHotspots,
Expand All @@ -62,6 +65,11 @@ const ClaimTokensPage = () => {
return toNumber(pendingIotRewards, 6)
}, [pendingIotRewards])

const totalPendingHnt = useMemo(() => {
if (!pendingHntRewards) return 0
return toNumber(pendingHntRewards, 6)
}, [pendingHntRewards])

const totalPendingMobile = useMemo(() => {
if (!pendingMobileRewards) return 0
return toNumber(pendingMobileRewards, 6)
Expand All @@ -83,15 +91,23 @@ const ClaimTokensPage = () => {
return (
claiming ||
!hasEnoughSol ||
(totalPendingIot === 0 && totalPendingMobile === 0)
(totalPendingIot === 0 &&
totalPendingMobile === 0 &&
totalPendingHnt === 0)
)
}, [claiming, hasEnoughSol, totalPendingIot, totalPendingMobile])
}, [
claiming,
hasEnoughSol,
totalPendingIot,
totalPendingMobile,
totalPendingHnt,
])

const onClaim = useCallback(async () => {
try {
const claim = async () => {
await submitClaimAllRewards(
[IOT_LAZY_KEY, MOBILE_LAZY_KEY],
[IOT_LAZY_KEY, MOBILE_LAZY_KEY, HNT_LAZY_KEY],
hotspotsWithMeta,
totalHotspots,
)
Expand Down Expand Up @@ -154,25 +170,48 @@ const ClaimTokensPage = () => {
gap="1"
marginTop="4xl"
>
<TouchableContainer
padding="xl"
gap="2.5"
backgroundColor="bg.brand-secondary"
backgroundColorPressed="blue.light-200"
pressableStyles={{
flex: 1,
}}
>
<Box flexDirection="row" gap="2.5" alignItems="center">
<MobileIcon />
<Box flexDirection="column">
<BalanceText amount={totalPendingMobile} />
<Text variant="textXsMedium" color="blue.dark-500">
MOBILE
</Text>
{totalPendingMobile > 0 && (
<TouchableContainer
padding="xl"
gap="2.5"
backgroundColor="bg.brand-secondary"
backgroundColorPressed="blue.light-200"
pressableStyles={{
flex: 1,
}}
>
<Box flexDirection="row" gap="2.5" alignItems="center">
<MobileIcon />
<Box flexDirection="column">
<BalanceText amount={totalPendingMobile} />
<Text variant="textXsMedium" color="blue.dark-500">
MOBILE
</Text>
</Box>
</Box>
</Box>
</TouchableContainer>
</TouchableContainer>
)}
{totalPendingIot > 0 && (
<TouchableContainer
padding="xl"
gap="2.5"
backgroundColor="bg.success-primary"
backgroundColorPressed="success.100"
pressableStyles={{
flex: 1,
}}
>
<Box flexDirection="row" gap="2.5" alignItems="center">
<IotIcon />
<Box flexDirection="column">
<BalanceText amount={totalPendingIot} />
<Text variant="textXsMedium" color="success.500">
IOT
</Text>
</Box>
</Box>
</TouchableContainer>
)}
<TouchableContainer
padding="xl"
gap="2.5"
Expand All @@ -183,11 +222,11 @@ const ClaimTokensPage = () => {
}}
>
<Box flexDirection="row" gap="2.5" alignItems="center">
<IotIcon />
<HntIcon width={50} height={50} />
<Box flexDirection="column">
<BalanceText amount={totalPendingIot} />
<BalanceText amount={totalPendingHnt} />
<Text variant="textXsMedium" color="success.500">
IOT
HNT
</Text>
</Box>
</Box>
Expand Down
6 changes: 3 additions & 3 deletions src/features/hotspots/ChangeRewardsRecipientScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import {
TouchableWithoutFeedback,
} from 'react-native'
import { useSafeAreaInsets } from 'react-native-safe-area-context'
import { IOT_LAZY_KEY, MOBILE_LAZY_KEY } from '@utils/constants'
import { HNT_LAZY_KEY, IOT_LAZY_KEY, MOBILE_LAZY_KEY } from '@utils/constants'
import { PublicKey } from '@solana/web3.js'
import TouchableOpacityBox from '@components/TouchableOpacityBox'
import { useCurrentWallet } from '@hooks/useCurrentWallet'
Expand Down Expand Up @@ -139,7 +139,7 @@ const ChangeRewardsRecipientScreen = () => {
setUpdating(true)
try {
await submitUpdateRewardsDestination({
lazyDistributors: [IOT_LAZY_KEY, MOBILE_LAZY_KEY],
lazyDistributors: [IOT_LAZY_KEY, MOBILE_LAZY_KEY, HNT_LAZY_KEY],
destination: recipient,
assetId: hotspot.id,
})
Expand All @@ -157,7 +157,7 @@ const ChangeRewardsRecipientScreen = () => {
setRemoving(true)
try {
await submitUpdateRewardsDestination({
lazyDistributors: [IOT_LAZY_KEY, MOBILE_LAZY_KEY],
lazyDistributors: [IOT_LAZY_KEY, MOBILE_LAZY_KEY, HNT_LAZY_KEY],
destination: PublicKey.default.toBase58(),
assetId: hotspot.id,
})
Expand Down
3 changes: 2 additions & 1 deletion src/features/hotspots/ClaimAllRewardsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import useSubmitTxn from '@hooks/useSubmitTxn'
import { useNavigation } from '@react-navigation/native'
import { useModal } from '@config/storage/ModalsProvider'
import {
HNT_LAZY_KEY,
IOT_LAZY_KEY,
MIN_BALANCE_THRESHOLD,
MOBILE_LAZY_KEY,
Expand Down Expand Up @@ -61,7 +62,7 @@ const ClaimAllRewardsScreen = () => {
setRedeeming(true)
const claim = async () => {
await submitClaimAllRewards(
[IOT_LAZY_KEY, MOBILE_LAZY_KEY],
[IOT_LAZY_KEY, MOBILE_LAZY_KEY, HNT_LAZY_KEY],
hotspotsWithMeta,
totalHotspots,
)
Expand Down
20 changes: 19 additions & 1 deletion src/features/hotspots/ClaimRewardsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const ClaimRewardsScreen = () => {
const mint = useMemo(() => new PublicKey(hotspot.id), [hotspot.id])
const { submitClaimRewards } = useSubmitTxn()

const { createClaimMobileTx, createClaimIotTx } = useHotspot(mint)
const { createClaimMobileTx, createClaimIotTx, createClaimHntTx } =
useHotspot(mint)

const pendingIotRewards = useMemo(
() =>
Expand All @@ -51,6 +52,14 @@ const ClaimRewardsScreen = () => {
[hotspot],
)

const pendingHntRewards = useMemo(
() =>
hotspot &&
hotspot.pendingRewards &&
new BN(hotspot.pendingRewards[Mints.HNT]),
[hotspot],
)

const title = useMemo(() => {
return t('collectablesScreen.hotspots.claimRewards')
}, [t])
Expand All @@ -73,6 +82,11 @@ const ClaimRewardsScreen = () => {
pendingMobileRewards && !pendingMobileRewards.eq(new BN(0))
? await createClaimMobileTx()
: undefined
const claimHntTx =
pendingHntRewards && !pendingHntRewards.eq(new BN(0))
? await createClaimHntTx()
: undefined

const transactions: VersionedTransaction[] = []

if (claimIotTx && pendingIotRewards) {
Expand All @@ -83,6 +97,10 @@ const ClaimRewardsScreen = () => {
transactions.push(claimMobileTx)
}

if (claimHntTx && pendingHntRewards) {
transactions.push(claimHntTx)
}

if (transactions.length > 0) {
await submitClaimRewards(transactions)
nav.push('ClaimingRewardsScreen')
Expand Down
70 changes: 65 additions & 5 deletions src/features/hotspots/HotspotMapHotspotDetails.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import CopyAddress from '@assets/svgs/copyAddress.svg'
import Hex from '@assets/svgs/hex.svg'
import IotSymbol from '@assets/svgs/iotSymbol.svg'
import HntSymbol from '@assets/svgs/hntSymbol.svg'
import MobileSymbol from '@assets/svgs/mobileSymbol.svg'
import { ReAnimatedBlurBox } from '@components/AnimatedBox'
import Box from '@components/Box'
Expand All @@ -13,7 +14,7 @@ import TouchableOpacityBox from '@components/TouchableOpacityBox'
import { makerApprovalKey } from '@helium/helium-entity-manager-sdk'
import { useMint } from '@helium/helium-react-hooks'
import { NetworkType } from '@helium/onboarding'
import { IOT_MINT, MOBILE_MINT, toNumber } from '@helium/spl-utils'
import { HNT_MINT, IOT_MINT, MOBILE_MINT, toNumber } from '@helium/spl-utils'
import useCopyText from '@hooks/useCopyText'
import { useCurrentWallet } from '@hooks/useCurrentWallet'
import { useEntityKey } from '@hooks/useEntityKey'
Expand Down Expand Up @@ -161,6 +162,7 @@ export const HotspotMapHotspotDetails = ({
const streetAddress = useHotspotAddress(hotspotWithMeta)
const { info: iotMint } = useMint(IOT_MINT)
const { info: mobileMint } = useMint(MOBILE_MINT)
const { info: hntMint } = useMint(HNT_MINT)
// Use entity key from kta since it's a buffer
const iotInfoAcc = useIotInfo(kta?.entityKey)
const mobileInfoAcc = useMobileInfo(kta?.entityKey)
Expand Down Expand Up @@ -239,6 +241,21 @@ export const HotspotMapHotspotDetails = ({
return formatLargeNumber(new BigNumber(num))
}, [hotspotWithMeta, mobileMint])

const pendingHntRewards = useMemo(
() =>
hotspotWithMeta?.pendingRewards &&
new BN(hotspotWithMeta?.pendingRewards[Mints.HNT]),
[hotspotWithMeta],
)
const pendingHntRewardsString = useMemo(() => {
if (!hotspotWithMeta?.pendingRewards) return
const num = toNumber(
new BN(hotspotWithMeta?.pendingRewards[Mints.HNT]),
hntMint?.decimals || 6,
)
return formatLargeNumber(new BigNumber(num))
}, [hotspotWithMeta, hntMint])

const hasIotRewards = useMemo(
() => pendingIotRewards && pendingIotRewards.gt(new BN(0)),
[pendingIotRewards],
Expand All @@ -247,10 +264,14 @@ export const HotspotMapHotspotDetails = ({
() => pendingMobileRewards && pendingMobileRewards.gt(new BN(0)),
[pendingMobileRewards],
)
const hasHntRewards = useMemo(
() => pendingHntRewards && pendingHntRewards.gt(new BN(0)),
[pendingHntRewards],
)

const hasRewards = useMemo(
() => hasIotRewards || hasMobileRewards,
[hasIotRewards, hasMobileRewards],
() => hasIotRewards || hasMobileRewards || hasHntRewards,
[hasIotRewards, hasMobileRewards, hasHntRewards],
)

const mobileRecipient = useMemo(
Expand All @@ -263,6 +284,11 @@ export const HotspotMapHotspotDetails = ({
[hotspotWithMeta],
)

const hntRecipient = useMemo(
() => hotspotWithMeta?.rewardRecipients?.[Mints.HNT],
[hotspotWithMeta],
)

const hasIotRecipient = useMemo(
() =>
iotRecipient?.destination &&
Expand All @@ -272,6 +298,15 @@ export const HotspotMapHotspotDetails = ({
[iotRecipient, wallet],
)

const hasHntRecipient = useMemo(
() =>
hntRecipient?.destination &&
wallet &&
!new PublicKey(hntRecipient.destination).equals(wallet) &&
!new PublicKey(hntRecipient.destination).equals(PublicKey.default),
[hntRecipient, wallet],
)

const hasMobileRecipient = useMemo(
() =>
mobileRecipient?.destination &&
Expand All @@ -282,8 +317,8 @@ export const HotspotMapHotspotDetails = ({
)

const hasRecipientSet = useMemo(
() => hasIotRecipient || hasMobileRecipient,
[hasIotRecipient, hasMobileRecipient],
() => hasIotRecipient || hasMobileRecipient || hasHntRecipient,
[hasIotRecipient, hasMobileRecipient, hasHntRecipient],
)

const isLoading = useMemo(
Expand Down Expand Up @@ -686,6 +721,31 @@ export const HotspotMapHotspotDetails = ({
</Text>
</Box>
)}
{!!hasHntRewards && (
<Box
flexDirection="row"
justifyContent="space-between"
alignItems="center"
backgroundColor="iotDarkGreen"
borderRadius="2xl"
paddingVertical="xs"
paddingLeft="xs"
paddingRight="2"
>
<HntSymbol
color={colors.hntBlue}
width={20}
height={20}
/>
<Text
variant="textXsMedium"
marginLeft="2"
color="hntBlue"
>
{pendingHntRewardsString}
</Text>
</Box>
)}
</Box>
</Box>
</TouchableOpacityBox>
Expand Down
Loading

0 comments on commit 6160f65

Please sign in to comment.