Skip to content

Commit

Permalink
Merge pull request #85 from helium/feat/caching
Browse files Browse the repository at this point in the history
Add ability to pass cached connection
  • Loading branch information
ChewingGlass authored Dec 19, 2023
2 parents 3df7c09 + 77386fc commit 9f0bcb4
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 35 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@helium/react-native-sdk",
"version": "3.0.0",
"version": "3.0.2",
"description": "Helium React Native SDK",
"main": "lib/commonjs/index.js",
"module": "lib/module/index.js",
Expand Down
5 changes: 4 additions & 1 deletion src/Solana/SolanaProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,21 @@ const SolanaProvider = ({
heliumWallet,
cluster,
rpcEndpoint,
connection,
}: {
children: ReactNode
cluster?: 'devnet' | 'testnet' | 'mainnet-beta'
heliumWallet?: string
rpcEndpoint: string
rpcEndpoint?: string
connection?: web3.Connection
}) => {
return (
<Provider
value={useSolana({
cluster,
heliumWallet,
rpcEndpoint,
connection,
})}
>
{children}
Expand Down
75 changes: 42 additions & 33 deletions src/Solana/useSolana.tsx
Original file line number Diff line number Diff line change
@@ -1,50 +1,49 @@
import { useCallback, useEffect, useMemo, useState } from 'react'
import { Buffer } from 'buffer'
import { AnchorProvider, BN, Program, Wallet } from '@coral-xyz/anchor'
import Address from '@helium/address'
import {
Connection,
PublicKey,
VersionedTransaction,
AccountInfo,
Cluster,
} from '@solana/web3.js'
import * as Currency from '@helium/currency-utils'
import { getBalance } from '@helium/currency-utils'
import { init as initDc } from '@helium/data-credits-sdk'
import {
entityCreatorKey,
init as initHem,
iotInfoKey,
makerKey,
mobileInfoKey,
rewardableEntityConfigKey,
} from '@helium/helium-entity-manager-sdk'
import { daoKey, init as initHsd, subDaoKey } from '@helium/helium-sub-daos-sdk'
import * as Hotspot from '@helium/hotspot-utils'
import { DataCredits } from '@helium/idls/lib/types/data_credits'
import { HeliumEntityManager } from '@helium/idls/lib/types/helium_entity_manager'
import { HeliumSubDaos } from '@helium/idls/lib/types/helium_sub_daos'
import {
Asset,
heliumAddressToSolPublicKey,
DC_MINT,
HNT_MINT,
IOT_MINT,
MOBILE_MINT,
SearchAssetsOpts,
getAsset,
heliumAddressToSolPublicKey,
searchAssets,
SearchAssetsOpts,
sendAndConfirmWithRetry,
DC_MINT,
} from '@helium/spl-utils'
import * as Hotspot from '@helium/hotspot-utils'
import { init as initHsd, subDaoKey } from '@helium/helium-sub-daos-sdk'
import { init as initDc } from '@helium/data-credits-sdk'
import {
AccountLayout,
getAssociatedTokenAddress,
TOKEN_PROGRAM_ID,
getAssociatedTokenAddress,
} from '@solana/spl-token'
import {
entityCreatorKey,
init as initHem,
iotInfoKey,
makerKey,
mobileInfoKey,
rewardableEntityConfigKey,
} from '@helium/helium-entity-manager-sdk'
import { getBalance } from '@helium/currency-utils'
AccountInfo,
Cluster,
Connection,
PublicKey,
VersionedTransaction,
} from '@solana/web3.js'
import axios from 'axios'
import { AnchorProvider, Wallet, Program, BN } from '@coral-xyz/anchor'
import { HeliumEntityManager } from '@helium/idls/lib/types/helium_entity_manager'
import { HeliumSubDaos } from '@helium/idls/lib/types/helium_sub_daos'
import { DataCredits } from '@helium/idls/lib/types/data_credits'
import { daoKey } from '@helium/helium-sub-daos-sdk'
import { Buffer } from 'buffer'
import { cellToLatLng } from 'h3-js'
import { useCallback, useEffect, useMemo, useState } from 'react'

const HOTSPOT_PAGE_LIMIT = 100
const HOTSPOT_CREATOR_ADDRESS = entityCreatorKey(
Expand All @@ -65,14 +64,24 @@ export type HotspotMeta = {

const useSolana = ({
heliumWallet,
rpcEndpoint,
rpcEndpoint: rpcEndpointIn,
cluster: propsCluster = 'devnet',
connection: connectionIn,
}: {
cluster?: Cluster
heliumWallet?: string
rpcEndpoint: string
rpcEndpoint?: string
connection?: Connection
}) => {
const connection = useMemo(() => new Connection(rpcEndpoint), [rpcEndpoint])
const rpcEndpoint = useMemo(() => {
if (!connectionIn && !rpcEndpointIn) {
throw new Error('Must provide one of connection, rpcEndpoint')
}
return (rpcEndpointIn || connectionIn?.rpcEndpoint)!
}, [rpcEndpointIn, connectionIn])
const connection = useMemo(() => {
return connectionIn || new Connection(rpcEndpoint)
}, [rpcEndpoint, connectionIn])
const [dcProgram, setDcProgram] = useState<Program<DataCredits>>()
const [hemProgram, setHemProgram] = useState<Program<HeliumEntityManager>>()
const [hsdProgram, setHsdProgram] = useState<Program<HeliumSubDaos>>()
Expand Down Expand Up @@ -199,7 +208,7 @@ const useSolana = ({
owner: wallet,
recipient,
connection,
url: rpcEndpoint,
url: rpcEndpoint!,
})
},
[connection, wallet, rpcEndpoint]
Expand Down

0 comments on commit 9f0bcb4

Please sign in to comment.