Skip to content

Commit

Permalink
only fetch ecosystem tokens
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Jan 13, 2025
1 parent 35d0583 commit 7777519
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 41 deletions.
38 changes: 27 additions & 11 deletions src/features/onboarding/import/ImportSubAccountsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@ import { FlatList } from 'react-native'
import ScrollBox from '@components/ScrollBox'
import CircleLoader from '@components/CircleLoader'
import ForwardButton from '@components/ForwardButton'
import { useOnboarding } from '../OnboardingProvider'
import { PublicKey } from '@solana/web3.js'
import { useMint } from '@helium/helium-react-hooks'
import { useMetaplexMetadata } from '@hooks/useMetaplexMetadata'
import { useOnboardingSheet } from '../OnboardingSheet'
import { useOnboarding } from '../OnboardingProvider'

const TokenItem = ({ mint, amount }: { mint: PublicKey; amount: bigint }) => {
const decimals = useMint(mint).info?.decimals
const { symbol } = useMetaplexMetadata(mint)

return (
<Text
variant="body2Medium"

Check failure on line 33 in src/features/onboarding/import/ImportSubAccountsScreen.tsx

View workflow job for this annotation

GitHub Actions / build

Type '"body2Medium"' is not assignable to type 'ResponsiveValue<"textSmMedium" | "textXsRegular" | "textXsMedium" | "textXsSemibold" | "textXsBold" | "textSmRegular" | "textSmSemibold" | "textSmBold" | "textMdRegular" | "textMdMedium" | ... 33 more ... | "display2XlBold", { ...; }> | undefined'.
color="secondaryText"
maxFontSizeMultiplier={1.3}
>
{humanReadable(new BN(amount.toString() || 0), decimals)} {symbol}
</Text>
)
}

export default () => {
const { t } = useTranslation()
Expand All @@ -38,7 +56,7 @@ export default () => {
const derivationAccounts = useMemo(() => {
return foundAccounts.filter(
(acc) =>
(acc.tokens?.value.length || 0) > 0 ||
(acc.tokens?.length || 0) > 0 ||
(acc?.balance || 0) > 0 ||
(acc.nfts?.length || 0) > 0 ||
acc.needsMigrated ||
Expand Down Expand Up @@ -126,15 +144,13 @@ export default () => {
>
{humanReadable(new BN(item?.balance || 0), 9)} SOL
</Text>
{(item.tokens?.value.length || 0) > 0 ? (
<Text
variant="textSmMedium"
color="secondaryText"
maxFontSizeMultiplier={1.3}
>
{`${item.tokens?.value.length} tokens`}
</Text>
) : null}
{item.tokens?.map((token) => (
<TokenItem
key={token.mint.toBase58()}
mint={token.mint}
amount={token.amount}
/>
))}
{(item.nfts?.length || 0) > 0 ? (
<Text
variant="textSmMedium"
Expand Down
64 changes: 34 additions & 30 deletions src/hooks/useDerivationAccounts.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Keypair as HeliumKeypair, Mnemonic } from '@helium/crypto'
import { Asset, truthy } from '@helium/spl-utils'
import {
AccountInfo,
Keypair,
PublicKey,
RpcResponseAndContext,
} from '@solana/web3.js'
Asset,
HNT_MINT,
IOT_MINT,
MOBILE_MINT,
truthy,
} from '@helium/spl-utils'
import { Keypair, PublicKey } from '@solana/web3.js'
import axios from 'axios'
import * as bip39 from 'bip39'
import { Buffer } from 'buffer'
Expand All @@ -14,6 +15,7 @@ import { useEffect, useMemo, useState } from 'react'
import Config from 'react-native-config'
import { retryWithBackoff } from '@utils/retryWithBackoff'
import { useSolana } from '@features/solana/SolanaProvider'
import { AccountLayout, getAssociatedTokenAddress } from '@solana/spl-token'

export const solanaDerivation = (account = -1, change: number | undefined) => {
if (account === -1) {
Expand Down Expand Up @@ -53,12 +55,7 @@ export type ResolvedPath = {
derivationPath: string
keypair: Keypair
balance?: number
tokens?: RpcResponseAndContext<
Array<{
pubkey: PublicKey
account: AccountInfo<Buffer>
}>
>
tokens?: { mint: PublicKey; amount: bigint }[]
nfts?: Asset[]
needsMigrated?: boolean
}
Expand Down Expand Up @@ -131,27 +128,33 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {

if (keypair) {
let needsMigrated = false
const [balance] = await Promise.all([
const ataMints = [HNT_MINT, MOBILE_MINT, IOT_MINT]
const atas = await Promise.all(
ataMints.map((mint) =>
getAssociatedTokenAddress(mint, keypair.publicKey),
),
)
const [balance, tokens] = await Promise.all([
retryWithBackoff(() =>
connection.getBalance(keypair.publicKey),
),
// retryWithBackoff(() =>
// connection.getTokenAccountsByOwner(
// keypair.publicKey,
// {
// programId: TOKEN_PROGRAM_ID,
// },
// ),
// ),
// retryWithBackoff(() =>
// getAssetsByOwner(
// connection.rpcEndpoint,
// keypair.publicKey.toBase58(),
// {
// limit: 10,
// },
// ),
// ),
retryWithBackoff(() =>
connection.getMultipleAccountsInfo(atas),
).then((tokenAccounts) =>
tokenAccounts
.map((acc, idx) => {
if (!acc) return null

const accInfo = AccountLayout.decode(acc.data)
const amount = BigInt(accInfo.amount)
if (amount <= 0n) return null
return {
mint: ataMints[idx],
amount,
}
})
.filter((account) => account !== null),
),
])

if (derivationPath === heliumDerivation(-1)) {
Expand All @@ -167,6 +170,7 @@ export const useDerivationAccounts = ({ mnemonic }: { mnemonic?: string }) => {
derivationPath,
keypair,
balance,
tokens,
needsMigrated,
} as ResolvedPath
}
Expand Down

0 comments on commit 7777519

Please sign in to comment.