Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(#594): Add caching for transactions and accounts #595

Merged
merged 4 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -1,4 +1,2 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx --no-install lint-staged
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ source 'https://rubygems.org'
# You may use http://rbenv.org/ or https://rvm.io/ to install and use this version
ruby '>= 2.6.10'

gem 'cocoapods', '>= 1.11.3'
gem 'cocoapods', '>= 1.14.1'
54 changes: 32 additions & 22 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,45 @@ GEM
specs:
CFPropertyList (3.0.6)
rexml
activesupport (6.1.7.2)
activesupport (7.1.2)
base64
bigdecimal
concurrent-ruby (~> 1.0, >= 1.0.2)
connection_pool (>= 2.2.5)
drb
i18n (>= 1.6, < 2)
minitest (>= 5.1)
mutex_m
tzinfo (~> 2.0)
zeitwerk (~> 2.3)
addressable (2.8.1)
addressable (2.8.6)
public_suffix (>= 2.0.2, < 6.0)
algoliasearch (1.27.5)
httpclient (~> 2.8, >= 2.8.3)
json (>= 1.5.1)
atomos (0.1.3)
base64 (0.2.0)
bigdecimal (3.1.5)
claide (1.1.0)
cocoapods (1.11.3)
cocoapods (1.14.3)
addressable (~> 2.8)
claide (>= 1.0.2, < 2.0)
cocoapods-core (= 1.11.3)
cocoapods-core (= 1.14.3)
cocoapods-deintegrate (>= 1.0.3, < 2.0)
cocoapods-downloader (>= 1.4.0, < 2.0)
cocoapods-downloader (>= 2.1, < 3.0)
cocoapods-plugins (>= 1.0.0, < 2.0)
cocoapods-search (>= 1.0.0, < 2.0)
cocoapods-trunk (>= 1.4.0, < 2.0)
cocoapods-trunk (>= 1.6.0, < 2.0)
cocoapods-try (>= 1.1.0, < 2.0)
colored2 (~> 3.1)
escape (~> 0.0.4)
fourflusher (>= 2.3.0, < 3.0)
gh_inspector (~> 1.0)
molinillo (~> 0.8.0)
nap (~> 1.0)
ruby-macho (>= 1.0, < 3.0)
xcodeproj (>= 1.21.0, < 2.0)
cocoapods-core (1.11.3)
activesupport (>= 5.0, < 7)
ruby-macho (>= 2.3.0, < 3.0)
xcodeproj (>= 1.23.0, < 2.0)
cocoapods-core (1.14.3)
activesupport (>= 5.0, < 8)
addressable (~> 2.8)
algoliasearch (~> 1.0)
concurrent-ruby (~> 1.1)
Expand All @@ -45,7 +51,7 @@ GEM
public_suffix (~> 4.0)
typhoeus (~> 1.0)
cocoapods-deintegrate (1.0.5)
cocoapods-downloader (1.6.3)
cocoapods-downloader (2.1)
cocoapods-plugins (1.0.0)
nap
cocoapods-search (1.0.1)
Expand All @@ -54,44 +60,48 @@ GEM
netrc (~> 0.11)
cocoapods-try (1.2.0)
colored2 (3.1.2)
concurrent-ruby (1.2.0)
concurrent-ruby (1.2.2)
connection_pool (2.4.1)
drb (2.2.0)
ruby2_keywords
escape (0.0.4)
ethon (0.16.0)
ffi (>= 1.15.0)
ffi (1.15.5)
ffi (1.16.3)
fourflusher (2.3.1)
fuzzy_match (2.0.4)
gh_inspector (1.1.3)
httpclient (2.8.3)
i18n (1.12.0)
i18n (1.14.1)
concurrent-ruby (~> 1.0)
json (2.6.3)
minitest (5.17.0)
json (2.7.1)
minitest (5.20.0)
molinillo (0.8.0)
mutex_m (0.2.0)
nanaimo (0.3.0)
nap (1.1.0)
netrc (0.11.0)
public_suffix (4.0.7)
rexml (3.2.5)
rexml (3.2.6)
ruby-macho (2.5.1)
typhoeus (1.4.0)
ruby2_keywords (0.0.5)
typhoeus (1.4.1)
ethon (>= 0.9.0)
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
xcodeproj (1.22.0)
xcodeproj (1.23.0)
CFPropertyList (>= 2.3.3, < 4.0)
atomos (~> 0.1.3)
claide (>= 1.0.2, < 2.0)
colored2 (~> 3.1)
nanaimo (~> 0.3.0)
rexml (~> 3.2.4)
zeitwerk (2.6.6)

PLATFORMS
ruby

DEPENDENCIES
cocoapods (>= 1.11.3)
cocoapods (>= 1.14.1)

RUBY VERSION
ruby 2.7.5p203
Expand Down
2 changes: 1 addition & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -957,4 +957,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: ca81fc3ff2cc73a5ab3e1674a7d2281321c928fb

COCOAPODS: 1.12.1
COCOAPODS: 1.14.3
49 changes: 20 additions & 29 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,47 +21,39 @@
"gen-types:all": "yarn gen-types",
"prepare": "husky install"
},
"lint-staged": {
"*.{js,ts,tsx,md,html}": [
"prettier --write ."
],
"*.{js,ts,tsx}": [
"eslint --quiet --fix"
]
},
"dependencies": {
"@babel/preset-typescript": "7.21.0",
"@bonfida/spl-name-service": "^1.1.1",
"@coral-xyz/anchor": "^0.28.0",
"@gorhom/bottom-sheet": "4.6.0",
"@gorhom/portal": "1.0.14",
"@helium/account-fetch-cache": "^0.6.13",
"@helium/account-fetch-cache-hooks": "^0.6.13",
"@helium/account-fetch-cache": "^0.6.14",
"@helium/account-fetch-cache-hooks": "^0.6.14",
"@helium/address": "4.10.2",
"@helium/circuit-breaker-sdk": "^0.6.13",
"@helium/circuit-breaker-sdk": "^0.6.14",
"@helium/crypto-react-native": "4.8.0",
"@helium/currency-utils": "^0.6.13",
"@helium/data-credits-sdk": "^0.6.13",
"@helium/distributor-oracle": "^0.6.13",
"@helium/fanout-sdk": "^0.6.13",
"@helium/helium-entity-manager-sdk": "^0.6.13",
"@helium/helium-react-hooks": "^0.6.13",
"@helium/helium-sub-daos-sdk": "^0.6.13",
"@helium/currency-utils": "^0.6.14",
"@helium/data-credits-sdk": "^0.6.14",
"@helium/distributor-oracle": "^0.6.14",
"@helium/fanout-sdk": "^0.6.14",
"@helium/helium-entity-manager-sdk": "^0.6.14",
"@helium/helium-react-hooks": "^0.6.14",
"@helium/helium-sub-daos-sdk": "^0.6.14",
"@helium/http": "4.7.5",
"@helium/idls": "^0.6.13",
"@helium/lazy-distributor-sdk": "^0.6.13",
"@helium/idls": "^0.6.14",
"@helium/lazy-distributor-sdk": "^0.6.14",
"@helium/modular-governance-hooks": "^0.0.8",
"@helium/modular-governance-idls": "^0.0.8-next",
"@helium/onboarding": "4.11.0",
"@helium/organization-sdk": "^0.0.8",
"@helium/proto-ble": "4.0.0",
"@helium/react-native-sdk": "^3.0.4",
"@helium/spl-utils": "^0.6.13",
"@helium/spl-utils": "^0.6.14",
"@helium/state-controller-sdk": "^0.0.8",
"@helium/transactions": "4.8.1",
"@helium/treasury-management-sdk": "^0.6.13",
"@helium/voter-stake-registry-hooks": "^0.6.13",
"@helium/voter-stake-registry-sdk": "^0.6.13",
"@helium/treasury-management-sdk": "^0.6.14",
"@helium/voter-stake-registry-hooks": "^0.6.14",
"@helium/voter-stake-registry-sdk": "^0.6.14",
"@helium/wallet-link": "4.11.0",
"@jup-ag/api": "^6.0.6",
"@ledgerhq/hw-app-solana": "7.0.13",
Expand Down Expand Up @@ -251,7 +243,6 @@
"husky": "7.0.4",
"jest": "29.2.1",
"jetifier": "1.6.8",
"lint-staged": "11.2.6",
"metro-react-native-babel-preset": "0.66.2",
"prettier": "2.7.1",
"react-native-cli-bump-version": "1.5.0",
Expand All @@ -265,10 +256,10 @@
},
"resolutions": {
"@types/react": "17",
"@helium/account-fetch-cache": "^0.6.13",
"@helium/account-fetch-cache-hooks": "^0.6.13",
"@helium/helium-react-hooks": "^0.6.13",
"@helium/voter-stake-registry-hooks": "^0.6.13",
"@helium/account-fetch-cache": "^0.6.14",
"@helium/account-fetch-cache-hooks": "^0.6.14",
"@helium/helium-react-hooks": "^0.6.14",
"@helium/voter-stake-registry-hooks": "^0.6.14",
"@helium/modular-governance-hooks": "^0.0.8",
"@helium/onboarding": "4.11.0"
},
Expand Down
5 changes: 3 additions & 2 deletions src/features/account/TokenListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -174,11 +174,12 @@ export const TokenListGovItem = ({ mint }: { mint: PublicKey }) => {
wallet &&
mint &&
connection && {
wallet: anchorProvider?.wallet.publicKey,
wallet,
mint,
provider: anchorProvider,
},
[mint, wallet, connection, anchorProvider],
// eslint-disable-next-line react-hooks/exhaustive-deps
[wallet?.toBase58(), mint.toBase58(), connection, anchorProvider],
)

const { result, loading: loadingPositionKeys } = useAsync(
Expand Down
82 changes: 76 additions & 6 deletions src/hooks/useEnrichedTransactions.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
import { useCallback, useEffect, useRef, useState } from 'react'
import AsyncStorage from '@react-native-async-storage/async-storage'
import { ConfirmedSignatureInfo } from '@solana/web3.js'
import { useCallback, useEffect, useMemo, useRef, useState } from 'react'
import { useSolana } from '../solana/SolanaProvider'
import { useAccountStorage } from '../storage/AccountStorageProvider'
import { useAppDispatch } from '../store/store'
import { EnrichedTransaction } from '../types/solana'
import {
getAllTransactions,
onLogs,
removeAccountChangeListener,
} from '../utils/solanaUtils'
import { useAppDispatch } from '../store/store'
import { useSolana } from '../solana/SolanaProvider'

const useEnrichedTransactions = (): {
transactions: (EnrichedTransaction | ConfirmedSignatureInfo)[]
Expand All @@ -32,6 +33,20 @@ const useEnrichedTransactions = (): {
const [hasNewTransactions, setHasNewTransactions] = useState(false)
const accountSubscriptionId = useRef<number>()
const dispatch = useAppDispatch()
const firstSig = useMemo(
() => (transactions[0] ? transactions[0].signature : ''),
[transactions],
)

useEffect(() => {
if (transactions.length > 0) {
AsyncStorage.setItem(
`txs-${cluster}-${currentAccount?.solanaAddress}`,
// Limit of 50 stored tx to not blow storage limit
JSON.stringify(transactions.slice(0, 50)),
)
}
}, [transactions, cluster, currentAccount])

// Reset new transactions when account changes. Maybe we add this all into a slice?
useEffect(() => {
Expand All @@ -57,6 +72,26 @@ const useEnrichedTransactions = (): {
setLoading(false)
setOnEndReached(false)
}, [currentAccount, cluster, anchorProvider])
const loadTransactionsFromStorage = useCallback(async () => {
try {
const storedTransactions = await AsyncStorage.getItem(
`txs-${cluster}-${currentAccount?.solanaAddress}`,
)
if (storedTransactions && storedTransactions.length > 0) {
const parsedTransactions = JSON.parse(storedTransactions)
setTransactions(parsedTransactions)
setOldestTransaction(
parsedTransactions.length > 0
? parsedTransactions[parsedTransactions.length - 1].signature
: '',
)
} else {
await refresh()
}
} catch (error) {
console.error('Error loading transactions from AsyncStorage:', error)
}
}, [refresh, cluster, currentAccount])

const fetchMore = useCallback(async () => {
if (
Expand Down Expand Up @@ -96,21 +131,56 @@ const useEnrichedTransactions = (): {
anchorProvider,
])

const fetchNew = useCallback(async () => {
if (!currentAccount?.solanaAddress || onEndReached || !anchorProvider) {
return
}

const fetchTransactions = await getAllTransactions(
currentAccount?.solanaAddress,
anchorProvider,
cluster,
undefined,
firstSig,
)

if (fetchTransactions.length !== 0) {
setTransactions((t) => [...fetchTransactions, ...t])
setOldestTransaction(
fetchTransactions.length > 0
? fetchTransactions[fetchTransactions.length - 1].signature
: '',
)
}
}, [
currentAccount?.solanaAddress,
onEndReached,
anchorProvider,
cluster,
firstSig,
])

useEffect(() => {
if (!currentAccount?.solanaAddress || !anchorProvider) return

refresh()
loadTransactionsFromStorage()

const subId = onLogs(anchorProvider, currentAccount?.solanaAddress, () => {
refresh()
fetchNew()
setHasNewTransactions(true)
})

if (accountSubscriptionId.current !== undefined) {
removeAccountChangeListener(anchorProvider, accountSubscriptionId.current)
}
accountSubscriptionId.current = subId
}, [anchorProvider, currentAccount, refresh, dispatch])
}, [
anchorProvider,
currentAccount,
dispatch,
loadTransactionsFromStorage,
fetchNew,
])

const resetNewTransactions = useCallback(() => {
setHasNewTransactions(false)
Expand Down
Loading
Loading