Skip to content

Commit

Permalink
Lower fetching of IDLs
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Dec 19, 2023
1 parent 1a36c63 commit 1490b4a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 50 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
"@helium/lazy-distributor-sdk": "^0.6.6",
"@helium/onboarding": "^4.10.3",
"@helium/proto-ble": "4.0.0",
"@helium/react-native-sdk": "^3.0.2",
"@helium/react-native-sdk": "^3.0.3",
"@helium/spl-utils": "^0.6.6",
"@helium/transactions": "4.8.1",
"@helium/treasury-management-sdk": "^0.6.6",
Expand Down
20 changes: 11 additions & 9 deletions src/features/txnDelegation/useSolTxns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,12 @@ import {
Instruction,
Program,
} from '@coral-xyz/anchor'
import { getAsset, Asset, heliumAddressToSolAddress } from '@helium/spl-utils'
import {
decodeEntityKey,
init,
keyToAssetForAsset,
} from '@helium/helium-entity-manager-sdk'
import { Asset, getAsset, heliumAddressToSolAddress } from '@helium/spl-utils'
import { SignHotspotResponse } from '@helium/wallet-link'
import { getLeafAssetId } from '@metaplex-foundation/mpl-bubblegum'
import * as web3 from '@solana/web3.js'
Expand All @@ -13,10 +18,6 @@ import bs58 from 'bs58'
import { get, last } from 'lodash'
import { useCallback, useMemo, useRef, useState } from 'react'
import { useAsync } from 'react-async-hook'
import {
decodeEntityKey,
keyToAssetForAsset,
} from '@helium/helium-entity-manager-sdk'
import { useSolana } from '../../solana/SolanaProvider'
import { getSolanaKeypair } from '../../storage/secureStorage'
import { submitSolana } from '../../utils/solanaUtils'
Expand All @@ -43,7 +44,7 @@ type Txn = {
}

const useSolTxns = (heliumAddress: string, solanaTransactions?: string) => {
const { anchorProvider, hemProgram } = useSolana()
const { anchorProvider } = useSolana()
const [submitLoading, setSubmitLoading] = useState(false)
const handledTxnStr = useRef('')
const [transactions, setTransactions] = useState<
Expand Down Expand Up @@ -152,8 +153,9 @@ const useSolTxns = (heliumAddress: string, solanaTransactions?: string) => {
)

const assetToAddress = useCallback(
async (asset?: Asset) => {
if (!hemProgram || !asset) return ''
async (asset?: Asset): Promise<string> => {
if (!anchorProvider || !asset) return ''
const hemProgram = await init(anchorProvider)
const keyToAssetKey = keyToAssetForAsset(asset)
const keyToAsset = await hemProgram.account.keyToAssetV0.fetch(
keyToAssetKey,
Expand All @@ -164,7 +166,7 @@ const useSolTxns = (heliumAddress: string, solanaTransactions?: string) => {
)
return entityKey || ''
},
[hemProgram],
[anchorProvider],
)

const handleUpdateMeta = useCallback(
Expand Down
45 changes: 5 additions & 40 deletions src/solana/SolanaProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import { AnchorProvider, Wallet } from '@coral-xyz/anchor'
import { AccountFetchCache } from '@helium/account-fetch-cache'
import { init as initDc } from '@helium/data-credits-sdk'
import { init as initHem } from '@helium/helium-entity-manager-sdk'
import { init as initHsd } from '@helium/helium-sub-daos-sdk'
import { init as initLazy } from '@helium/lazy-distributor-sdk'
import { DC_MINT, HNT_MINT } from '@helium/spl-utils'
import { AccountContext } from '@helium/account-fetch-cache-hooks'
import { SolanaProvider as SolanaProviderRnHelium } from '@helium/react-native-sdk'
import { DC_MINT, HNT_MINT } from '@helium/spl-utils'
import {
AccountInfo,
Cluster,
Expand All @@ -15,6 +12,7 @@ import {
Transaction,
VersionedTransaction,
} from '@solana/web3.js'
import { WrappedConnection } from '@utils/WrappedConnection'
import React, {
ReactNode,
createContext,
Expand All @@ -23,33 +21,25 @@ import React, {
useEffect,
useMemo,
useRef,
useState,
} from 'react'
import { useAsync } from 'react-async-hook'
import Config from 'react-native-config'
import { useSelector } from 'react-redux'
import nacl from 'tweetnacl'
import { WrappedConnection } from '@utils/WrappedConnection'
import { AccountContext } from '@helium/account-fetch-cache-hooks'
import LedgerModal, { LedgerModalRef } from '../features/ledger/LedgerModal'
import { useAccountStorage } from '../storage/AccountStorageProvider'
import { getSessionKey, getSolanaKeypair } from '../storage/secureStorage'
import { RootState } from '../store/rootReducer'
import { appSlice } from '../store/slices/appSlice'
import { useAppDispatch } from '../store/store'
import { DcProgram, HemProgram, HsdProgram, LazyProgram } from '../types/solana'
import { getConnection, isVersionedTransaction } from '../utils/solanaUtils'
import LedgerModal, { LedgerModalRef } from '../features/ledger/LedgerModal'

const useSolanaHook = () => {
const { currentAccount } = useAccountStorage()
const dispatch = useAppDispatch()
const cluster = useSelector(
(state: RootState) => state.app.cluster || 'mainnet-beta',
)
const [dcProgram, setDcProgram] = useState<DcProgram>()
const [hemProgram, setHemProgram] = useState<HemProgram>()
const [hsdProgram, setHsdProgram] = useState<HsdProgram>()
const [lazyProgram, setLazyProgram] = useState<LazyProgram>()
const { loading, result: sessionKey } = useAsync(getSessionKey, [])
const ledgerModalRef = useRef<LedgerModalRef>()
const connection = useMemo(() => {
Expand Down Expand Up @@ -241,19 +231,6 @@ const useSolanaHook = () => {
return () => cache?.close()
}, [cache])

const handleConnectionChanged = useCallback(async () => {
if (!anchorProvider) return

initHem(anchorProvider).then(setHemProgram)
initHsd(anchorProvider).then(setHsdProgram)
initDc(anchorProvider).then(setDcProgram)
initLazy(anchorProvider).then(setLazyProgram)
}, [anchorProvider])

useEffect(() => {
handleConnectionChanged()
}, [cluster, address, handleConnectionChanged])

const updateCluster = useCallback(
(nextCluster: Cluster) => {
dispatch(appSlice.actions.setCluster(nextCluster))
Expand All @@ -266,10 +243,6 @@ const useSolanaHook = () => {
cluster,
isDevnet,
connection,
dcProgram,
hemProgram,
hsdProgram,
lazyProgram,
updateCluster,
cache,
signMsg,
Expand All @@ -282,10 +255,6 @@ const initialState: {
cluster: Cluster
isDevnet: boolean
connection: WrappedConnection | undefined
dcProgram: DcProgram | undefined
hemProgram: HemProgram | undefined
hsdProgram: HsdProgram | undefined
lazyProgram: LazyProgram | undefined
cache: AccountFetchCache | undefined
updateCluster: (nextCluster: Cluster) => void
signMsg: (msg: Buffer) => Promise<Buffer>
Expand All @@ -295,10 +264,6 @@ const initialState: {
cluster: 'mainnet-beta' as Cluster,
isDevnet: false,
connection: undefined,
dcProgram: undefined,
hemProgram: undefined,
hsdProgram: undefined,
lazyProgram: undefined,
cache: undefined,
updateCluster: (_nextCluster: Cluster) => {},
signMsg: (_msg: Buffer) => Promise.resolve(_msg),
Expand All @@ -315,7 +280,7 @@ const SolanaProvider = ({ children }: { children: ReactNode }) => {
{values.cache && values.connection && (
<AccountContext.Provider value={values.cache}>
<SolanaProviderRnHelium
rpcEndpoint={values.connection.rpcEndpoint}
connection={values.connection}
cluster={values.cluster}
>
<LedgerModal ref={values?.ledgerModalRef}>{children}</LedgerModal>
Expand Down

0 comments on commit 1490b4a

Please sign in to comment.