Skip to content

Commit

Permalink
Fix performance of claim so it no longer crashes apps
Browse files Browse the repository at this point in the history
  • Loading branch information
ChewingGlass committed Sep 15, 2023
1 parent 207b517 commit f0f6199
Show file tree
Hide file tree
Showing 7 changed files with 298 additions and 196 deletions.
6 changes: 3 additions & 3 deletions ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -861,17 +861,17 @@ SPEC CHECKSUMS:
FBLazyVector: f1897022b53abf1469d6ad692ee2c69f57d967f3
FBReactNativeSpec: 627fd07f1b9d498c9fa572e76d7f1a6b1ee9a444
fmt: ff9d55029c625d3757ed641535fd4a75fedc7ce9
glog: 791fe035093b84822da7f0870421a25839ca7870
glog: 04b94705f318337d7ead9e6d17c019bd9b1f6b1b
helium-react-native-sdk: 32c0a7e3abc733a7f3d291013b2db31475fc6980
hermes-engine: 7a53ccac09146018a08239c5425625fdb79a6162
hermes-engine: 0784cadad14b011580615c496f77e0ae112eed75
libevent: 4049cae6c81cdb3654a443be001fb9bdceff7913
MapboxCommon: fdf7fd31c90b7b607cd9c63e37797f023c01d860
MapboxCoreMaps: 24270c7c6b8cb71819fc2f3c549db9620ee4d019
MapboxMaps: cb76511b98d3b95c74b0771ed105bc69f30ace6b
MapboxMobileEvents: de50b3a4de180dd129c326e09cd12c8adaaa46d6
MultiplatformBleAdapter: 5a6a897b006764392f9cef785e4360f54fb9477d
OneSignalXCFramework: 81ceac017a290f23793443323090cfbe888f74ea
RCT-Folly: 85766c3226c7ec638f05ad7cb3cf6a268d6c4241
RCT-Folly: 424b8c9a7a0b9ab2886ffe9c3b041ef628fd4fb1
RCTRequired: bd6045fbd511da5efe6db89eecb21e4e36bd7cbf
RCTTypeSafety: c06d9f906faa69dd1c88223204c3a24767725fd8
React: b9ea33557ef1372af247f95d110fbdea114ed3b2
Expand Down
24 changes: 12 additions & 12 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,25 @@
"@coral-xyz/anchor": "0.26.0",
"@gorhom/bottom-sheet": "4.4.6",
"@gorhom/portal": "1.0.14",
"@helium/account-fetch-cache": "^0.2.17",
"@helium/account-fetch-cache-hooks": "^0.2.17",
"@helium/account-fetch-cache": "^0.3.0",
"@helium/account-fetch-cache-hooks": "^0.3.0",
"@helium/address": "4.10.2",
"@helium/circuit-breaker-sdk": "^0.2.17",
"@helium/circuit-breaker-sdk": "^0.3.0",
"@helium/crypto-react-native": "4.8.0",
"@helium/currency-utils": "0.1.1",
"@helium/data-credits-sdk": "^0.2.17",
"@helium/distributor-oracle": "^0.2.17",
"@helium/fanout-sdk": "^0.2.17",
"@helium/helium-entity-manager-sdk": "^0.2.17",
"@helium/helium-react-hooks": "^0.2.17",
"@helium/helium-sub-daos-sdk": "^0.2.17",
"@helium/data-credits-sdk": "^0.3.0",
"@helium/distributor-oracle": "^0.3.0",
"@helium/fanout-sdk": "^0.3.0",
"@helium/helium-entity-manager-sdk": "^0.3.0",
"@helium/helium-react-hooks": "^0.3.0",
"@helium/helium-sub-daos-sdk": "^0.3.0",
"@helium/http": "4.7.5",
"@helium/idls": "^0.2.17",
"@helium/lazy-distributor-sdk": "^0.2.17",
"@helium/idls": "^0.3.0",
"@helium/lazy-distributor-sdk": "^0.3.0",
"@helium/onboarding": "4.9.0",
"@helium/proto-ble": "4.0.0",
"@helium/react-native-sdk": "1.0.0",
"@helium/spl-utils": "^0.2.17",
"@helium/spl-utils": "^0.3.0",
"@helium/transactions": "4.8.1",
"@helium/treasury-management-sdk": "0.1.2",
"@helium/voter-stake-registry-sdk": "0.1.2",
Expand Down
50 changes: 28 additions & 22 deletions src/solana/SolanaProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -99,30 +99,36 @@ const useSolanaHook = () => {
missingRefetchDelay: 60 * 1000,
extendConnection: true,
})
const oldGetAccountinfoAndContext =
connection.getAccountInfoAndContext.bind(connection)

// Anchor uses this call on .fetch and .fetchNullable even though it doesn't actually need the context. Add caching.
connection.getAccountInfoAndContext = async (
publicKey: PublicKey,
com?: Commitment,
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> => {
if (
(com || connection.commitment) === 'confirmed' ||
typeof (com || connection.commitment) === 'undefined'
) {
const [result, dispose] = await c.searchAndWatch(publicKey)
setTimeout(dispose, 30 * 1000) // cache for 30s
return {
value: result?.account || null,
context: {
slot: 0,
},
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
if (!connection.wrapped) {
const oldGetAccountinfoAndContext =
connection.getAccountInfoAndContext.bind(connection)

// Anchor uses this call on .fetch and .fetchNullable even though it doesn't actually need the context. Add caching.
connection.getAccountInfoAndContext = async (
publicKey: PublicKey,
com?: Commitment,
): Promise<RpcResponseAndContext<AccountInfo<Buffer> | null>> => {
if (
(com || connection.commitment) === 'confirmed' ||
typeof (com || connection.commitment) === 'undefined'
) {
const [result, dispose] = await c.searchAndWatch(publicKey)
setTimeout(dispose, 30 * 1000) // cache for 30s
return {
value: result?.account || null,
context: {
slot: 0,
},
}
}
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return oldGetAccountinfoAndContext!(publicKey, com)
}

// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
return oldGetAccountinfoAndContext!(publicKey, com)
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
connection.wrapped = true
}

return c
Expand Down
7 changes: 3 additions & 4 deletions src/store/slices/solanaSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -369,10 +369,9 @@ export const claimAllRewards = createAsyncThunk(
const keyToAssets = chunk.map((h) =>
keyToAssetForAsset(solUtils.toAsset(h)),
)
const ktaAccs = await Promise.all(
keyToAssets.map((kta) =>
hemProgram.account.keyToAssetV0.fetch(kta),
),
const ktaAccs = await solUtils.getCachedKeyToAssets(
hemProgram,
keyToAssets,
)
const entityKeys = ktaAccs.map(
(kta) =>
Expand Down
2 changes: 1 addition & 1 deletion src/store/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ const store = configureStore({
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware({
serializableCheck: false,
immutableCheck: { warnAfter: 500 },
immutableCheck: false,
}).concat([solanaStatusApi.middleware]),
enhancers,
})
Expand Down
32 changes: 28 additions & 4 deletions src/utils/solanaUtils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/* eslint-disable no-underscore-dangle */
import { AnchorProvider, BN } from '@coral-xyz/anchor'
import { AnchorProvider, BN, IdlAccounts, Program } from '@coral-xyz/anchor'
import { getSingleton } from '@helium/account-fetch-cache'
import * as dc from '@helium/data-credits-sdk'
import {
delegatedDataCreditsKey,
Expand All @@ -25,6 +26,7 @@ import {
updateMobileMetadata,
} from '@helium/helium-entity-manager-sdk'
import { subDaoKey } from '@helium/helium-sub-daos-sdk'
import { HeliumEntityManager } from '@helium/idls/lib/types/helium_entity_manager'
import * as lz from '@helium/lazy-distributor-sdk'
import { HotspotType } from '@helium/onboarding'
import {
Expand All @@ -37,6 +39,7 @@ import {
searchAssets,
sendAndConfirmWithRetry,
toBN,
truthy,
} from '@helium/spl-utils'
import * as tm from '@helium/treasury-management-sdk'
import {
Expand Down Expand Up @@ -1104,6 +1107,29 @@ export async function exists(
return Boolean(await connection.getAccountInfo(account))
}

export async function getCachedKeyToAssets(
hemProgram: Program<HeliumEntityManager>,
keyToAssets: PublicKey[],
) {
const cache = await getSingleton(hemProgram.provider.connection)
return (
await cache.searchMultiple(
keyToAssets,
(pubkey, account) => ({
pubkey,
account,
info: hemProgram.coder.accounts.decode<
IdlAccounts<HeliumEntityManager>['keyToAssetV0']
>('KeyToAssetV0', account.data),
}),
true,
false,
)
)
.map((kta) => kta?.info)
.filter(truthy)
}

export async function annotateWithPendingRewards(
provider: AnchorProvider,
hotspots: CompressedNFT[],
Expand All @@ -1114,9 +1140,7 @@ export async function annotateWithPendingRewards(
const keyToAssets = hotspots.map((h) =>
keyToAssetForAsset(toAsset(h as CompressedNFT)),
)
const ktaAccs = await Promise.all(
keyToAssets.map((kta) => hemProgram.account.keyToAssetV0.fetch(kta)),
)
const ktaAccs = await getCachedKeyToAssets(hemProgram, keyToAssets)
const entityKeys = ktaAccs.map(
(kta) =>
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand Down
Loading

0 comments on commit f0f6199

Please sign in to comment.