Skip to content

Commit 7c3baab

Browse files
authored
chore: blocks refactor (#28)
* chore: rename blocks
1 parent 309a3a0 commit 7c3baab

File tree

17 files changed

+89
-82
lines changed

17 files changed

+89
-82
lines changed

src/features/assets/data/asset.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,11 @@ import { AssetLookupResult } from '@algorandfoundation/algokit-utils/types/index
33
import { atom, useAtomValue, useStore } from 'jotai'
44
import { JotaiStore } from '@/features/common/data/types'
55
import { atomEffect } from 'jotai-effect'
6-
import { AssetIndex, assetsAtom } from '.'
6+
import { assetsAtom } from './core'
77
import { useMemo } from 'react'
88
import { loadable } from 'jotai/utils'
99
import { asAsset } from '../mappers'
10+
import { AssetIndex } from './types'
1011

1112
const fetchAssetResultAtomBuilder = (assetIndex: AssetIndex) =>
1213
atom(async (_get) => {
@@ -37,15 +38,15 @@ export const getAssetAtomBuilder = (store: JotaiStore, assetIndex: AssetIndex) =
3738

3839
return atom(async (get) => {
3940
const assets = store.get(assetsAtom)
40-
const asset = assets.get(assetIndex)
41-
if (asset) {
42-
return asAsset(asset)
41+
const cachedAsset = assets.get(assetIndex)
42+
if (cachedAsset) {
43+
return asAsset(cachedAsset)
4344
}
4445

4546
get(syncEffect)
4647

47-
const assetResult = await get(fetchAssetResultAtom)
48-
return asAsset(assetResult)
48+
const fetchedAsset = await get(fetchAssetResultAtom)
49+
return asAsset(fetchedAsset)
4950
})
5051
}
5152

src/features/assets/data/core.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { AssetResult } from '@algorandfoundation/algokit-utils/types/indexer'
2+
import { AssetIndex } from './types'
3+
import { atom } from 'jotai'
4+
import { ZERO_ADDRESS } from '@/features/common/constants'
5+
6+
export const algoAssetResult = {
7+
index: 0,
8+
'created-at-round': 0,
9+
params: {
10+
creator: ZERO_ADDRESS,
11+
decimals: 6,
12+
total: 10_000_000_000,
13+
name: 'ALGO',
14+
'unit-name': 'ALGO',
15+
url: 'https://www.algorand.foundation',
16+
},
17+
} as AssetResult
18+
19+
export const assetsAtom = atom<Map<AssetIndex, AssetResult>>(new Map([[algoAssetResult.index, algoAssetResult]]))

src/features/assets/data/index.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1 @@
1-
import { atom } from 'jotai'
2-
import { AssetResult } from '@algorandfoundation/algokit-utils/types/indexer'
3-
import { AssetIndex } from './types'
4-
import { ZERO_ADDRESS } from '@/features/common/constants'
5-
export * from './types'
61
export * from './asset'
7-
8-
// TODO: Size should be capped at some limit, so memory usage doesn't grow indefinitely
9-
10-
export const algoAssetResult = {
11-
index: 0,
12-
'created-at-round': 0,
13-
params: {
14-
creator: ZERO_ADDRESS,
15-
decimals: 6,
16-
total: 10_000_000_000,
17-
name: 'ALGO',
18-
'unit-name': 'ALGO',
19-
url: 'https://www.algorand.foundation',
20-
},
21-
} as AssetResult
22-
23-
export const assetsAtom = atom<Map<AssetIndex, AssetResult>>(new Map([[algoAssetResult.index, algoAssetResult]]))

src/features/blocks/components/block.tsx renamed to src/features/blocks/components/block-details.tsx

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ import { cn } from '@/features/common/utils'
55
import { dateFormatter } from '@/utils/format'
66
import { BlockLink } from './block-link'
77
import { TransactionsTable } from './transactions'
8-
import { BlockDetails } from '../models'
8+
import { Block } from '../models'
99
import { Badge } from '@/features/common/components/badge'
1010

1111
type Props = {
12-
block: BlockDetails
12+
block: Block
1313
}
1414

1515
export const roundLabel = 'Round'
@@ -18,7 +18,7 @@ export const transactionsLabel = 'Transactions'
1818
export const previousRoundLabel = 'Previous Round'
1919
export const nextRoundLabel = 'Next Round'
2020

21-
export function Block({ block }: Props) {
21+
export function BlockDetails({ block }: Props) {
2222
const blockItems = useMemo(
2323
() => [
2424
{

src/features/blocks/components/latest-blocks.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { BlockLink } from './block-link'
33
import { Card, CardContent } from '@/features/common/components/card'
44
import { dateFormatter } from '@/utils/format'
55
import SvgBlock from '@/features/common/components/icons/block'
6-
import { useLatestBlockSummaries } from '../data/latest-blocks'
6+
import { useLatestBlockSummaries } from '../data'
77

88
export const latestBlocksTitle = 'Latest Blocks'
99

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
import { JotaiStore } from '@/features/common/data/types'
22
import { atom, useAtomValue, useStore } from 'jotai'
3-
import { BlockResult, Round } from './types'
4-
import { blocksAtom, syncedRoundAtom } from '.'
53
import { indexer } from '@/features/common/data'
64
import { TransactionResult } from '@algorandfoundation/algokit-utils/types/indexer'
75
import { atomEffect } from 'jotai-effect'
86
import { fetchTransactionsAtomBuilder, fetchTransactionsModelAtomBuilder, transactionsAtom } from '@/features/transactions/data'
9-
import { asBlockDetails } from '../mappers'
7+
import { asBlock } from '../mappers'
108
import { useMemo } from 'react'
119
import { loadable } from 'jotai/utils'
10+
import { blocksAtom, syncedRoundAtom } from './core'
11+
import { BlockResult, Round } from './types'
1212

1313
const nextRoundAvailableAtomBuilder = (store: JotaiStore, round: Round) => {
1414
// This atom conditionally subscribes to updates on the syncedRoundAtom
@@ -19,7 +19,7 @@ const nextRoundAvailableAtomBuilder = (store: JotaiStore, round: Round) => {
1919
})
2020
}
2121

22-
const fetchBlockAtomBuilder = (round: Round) => {
22+
const fetchBlockResultAtomBuilder = (round: Round) => {
2323
return atom(async (_get) => {
2424
return await indexer
2525
.lookupBlock(round)
@@ -37,22 +37,25 @@ const fetchBlockAtomBuilder = (round: Round) => {
3737
})
3838
}
3939

40-
const getBlockDetailsAtomBuilder = (store: JotaiStore, round: Round) => {
41-
const fetchBlockAtom = fetchBlockAtomBuilder(round)
40+
const getBlockAtomBuilder = (store: JotaiStore, round: Round) => {
41+
const fetchBlockResultAtom = fetchBlockResultAtomBuilder(round)
4242

4343
const syncEffect = atomEffect((get, set) => {
4444
;(async () => {
4545
try {
46-
const [block, transactions] = await get(fetchBlockAtom)
46+
const [blockResult, transactionResults] = await get(fetchBlockResultAtom)
4747

48-
if (transactions && transactions.length > 0) {
48+
if (transactionResults && transactionResults.length > 0) {
4949
set(transactionsAtom, (prev) => {
50-
return new Map([...prev, ...transactions.map((t: TransactionResult) => [t.id, t] as const)])
50+
transactionResults.forEach((t) => {
51+
prev.set(t.id, t)
52+
})
53+
return prev
5154
})
5255
}
5356

5457
set(blocksAtom, (prev) => {
55-
return new Map([...prev, [block.round, block]])
58+
return prev.set(blockResult.round, blockResult)
5659
})
5760
} catch (e) {
5861
// Ignore any errors as there is nothing to sync
@@ -62,31 +65,31 @@ const getBlockDetailsAtomBuilder = (store: JotaiStore, round: Round) => {
6265

6366
return atom(async (get) => {
6467
const blocks = store.get(blocksAtom)
65-
const cachedBlock = blocks.get(round)
68+
const cachedBlockResult = blocks.get(round)
6669
const nextRoundAvailable = get(nextRoundAvailableAtomBuilder(store, round))
67-
if (cachedBlock) {
70+
if (cachedBlockResult) {
6871
const transactions = await get(
69-
fetchTransactionsModelAtomBuilder(store, fetchTransactionsAtomBuilder(store, cachedBlock.transactionIds))
72+
fetchTransactionsModelAtomBuilder(store, fetchTransactionsAtomBuilder(store, cachedBlockResult.transactionIds))
7073
)
71-
return asBlockDetails(cachedBlock, transactions, nextRoundAvailable)
74+
return asBlock(cachedBlockResult, transactions, nextRoundAvailable)
7275
}
7376

7477
get(syncEffect)
7578

76-
const [fetchedBlock, fetchedBlockTransactions] = await get(fetchBlockAtom)
77-
const transactions = await get(fetchTransactionsModelAtomBuilder(store, fetchedBlockTransactions))
78-
return asBlockDetails(fetchedBlock, transactions, nextRoundAvailable)
79+
const [blockResult, transactionResults] = await get(fetchBlockResultAtom)
80+
const transactions = await get(fetchTransactionsModelAtomBuilder(store, transactionResults))
81+
return asBlock(blockResult, transactions, nextRoundAvailable)
7982
})
8083
}
8184

82-
const useBlockDetailsAtom = (round: Round) => {
85+
const useBlockAtom = (round: Round) => {
8386
const store = useStore()
8487

8588
return useMemo(() => {
86-
return getBlockDetailsAtomBuilder(store, round)
89+
return getBlockAtomBuilder(store, round)
8790
}, [store, round])
8891
}
8992

90-
export const useLoadableBlockDetails = (round: Round) => {
91-
return useAtomValue(loadable(useBlockDetailsAtom(round)))
93+
export const useLoadableBlock = (round: Round) => {
94+
return useAtomValue(loadable(useBlockAtom(round)))
9295
}

src/features/blocks/data/core.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { atom } from 'jotai'
2+
import { BlockResult, Round } from './types'
3+
4+
export const syncedRoundAtom = atom<Round | undefined>(undefined)
5+
6+
// TODO: Size should be capped at some limit, so memory usage doesn't grow indefinitely
7+
export const blocksAtom = atom<Map<Round, BlockResult>>(new Map())

src/features/blocks/data/index.ts

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,2 @@
1-
import { atom } from 'jotai'
2-
import { BlockResult, Round } from './types'
3-
4-
export const syncedRoundAtom = atom<Round | undefined>(undefined)
5-
6-
// TODO: Size should be capped at some limit, so memory usage doesn't grow indefinitely
7-
export const blocksAtom = atom<Map<Round, BlockResult>>(new Map())
1+
export * from './block'
2+
export * from './latest-blocks'

src/features/blocks/data/latest-blocks.ts

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,17 @@
11
import { JotaiStore } from '@/features/common/data/types'
22
import { atom, useAtom, useAtomValue, useStore } from 'jotai'
33
import { useMemo } from 'react'
4-
import { blocksAtom, syncedRoundAtom } from '.'
54
import { isDefined } from '@/utils/is-defined'
65
import { asBlockSummary } from '../mappers'
76
import { transactionsAtom } from '@/features/transactions/data'
87
import { asTransactionSummary } from '@/features/transactions/mappers/transaction-mappers'
98
import { atomEffect } from 'jotai-effect'
109
import { AlgorandSubscriber } from '@algorandfoundation/algokit-subscriber'
1110
import { algod } from '@/features/common/data'
12-
import { BlockResult, Round } from './types'
1311
import { TransactionId } from '@/features/transactions/data/types'
1412
import { TransactionResult } from '@algorandfoundation/algokit-utils/types/indexer'
13+
import { blocksAtom, syncedRoundAtom } from './core'
14+
import { BlockResult, Round } from './types'
1515

1616
const maxBlocksToDisplay = 5
1717

@@ -86,10 +86,8 @@ const subscribeToBlocksEffect = atomEffect((get, set) => {
8686
const { filtersMatched, balanceChanges, ...transaction } = t
8787
const round = transaction['confirmed-round']!
8888

89-
return [
90-
new Map<Round, string[]>([...acc[0], [round, (acc[0].get(round) ?? []).concat(transaction.id)]]),
91-
new Map<TransactionId, TransactionResult>([...acc[1], [transaction.id, transaction]]),
92-
] as const
89+
acc[0].set(round, (acc[0].get(round) ?? []).concat(transaction.id))
90+
acc[1].set(transaction.id, transaction)
9391
}
9492
return acc
9593
},
@@ -108,11 +106,17 @@ const subscribeToBlocksEffect = atomEffect((get, set) => {
108106
})
109107

110108
set(transactionsAtom, (prev) => {
111-
return new Map([...prev, ...transactions])
109+
transactions.forEach((value, key) => {
110+
prev.set(key, value)
111+
})
112+
return prev
112113
})
113114

114115
set(blocksAtom, (prev) => {
115-
return new Map([...prev, ...blocks])
116+
blocks.forEach(([key, value]) => {
117+
prev.set(key, value)
118+
})
119+
return prev
116120
})
117121
})
118122

src/features/blocks/mappers/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { TransactionModel, TransactionSummary, TransactionType } from '@/features/transactions/models'
2-
import { BlockDetails, BlockSummary, CommonBlockProperties } from '../models'
2+
import { Block, BlockSummary, CommonBlockProperties } from '../models'
33
import { BlockResult } from '../data/types'
44

55
const asCommonBlock = (block: BlockResult, transactions: Pick<TransactionModel, 'type'>[]): CommonBlockProperties => {
@@ -24,7 +24,7 @@ export const asBlockSummary = (block: BlockResult, transactions: TransactionSumm
2424
return { ...asCommonBlock(block, transactions), transactions }
2525
}
2626

27-
export const asBlockDetails = (block: BlockResult, transactions: TransactionModel[], nextRoundAvailable: boolean): BlockDetails => {
27+
export const asBlock = (block: BlockResult, transactions: TransactionModel[], nextRoundAvailable: boolean): Block => {
2828
return {
2929
...asCommonBlock(block, transactions),
3030
previousRound: block.round > 0 ? block.round - 1 : undefined,

0 commit comments

Comments
 (0)