|
| 1 | +import { atom, useAtom, useAtomValue } from 'jotai' |
| 2 | +import * as algokit from '@algorandfoundation/algokit-utils' |
| 3 | +import { atomEffect } from 'jotai-effect' |
| 4 | +import { transactionsAtom } from '@/features/transactions/data' |
| 5 | +import { AlgorandSubscriber } from '@algorandfoundation/algokit-subscriber' |
| 6 | +// import { BlockMetadata } from '@algorandfoundation/algokit-subscriber/types/subscription' |
| 7 | +import { Buffer } from 'buffer' |
| 8 | + |
| 9 | +type BlockMetadata = { |
| 10 | + round: number |
| 11 | + parentTransactionCount: number |
| 12 | +} |
| 13 | + |
| 14 | +// TODO: NC - Remove once https://github.com/algorandfoundation/algokit-subscriber-ts/pull/49 is merged |
| 15 | +window.Buffer = Buffer |
| 16 | + |
| 17 | +// TODO: Move this elsewhere and make it configurable once we start using it more |
| 18 | +const algod = algokit.getAlgoClient({ |
| 19 | + server: 'https://testnet-api.algonode.cloud/', |
| 20 | + port: 443, |
| 21 | +}) |
| 22 | + |
| 23 | +const syncedRoundAtom = atom<number | undefined>(undefined) |
| 24 | + |
| 25 | +// TODO: Size should be capped at some limit, so memory usage doesn't grow indefinitely |
| 26 | +const blocksAtom = atom<BlockMetadata[]>([]) |
| 27 | + |
| 28 | +const latestBlockAtom = atom((get) => { |
| 29 | + return get(blocksAtom).slice(0, 5) |
| 30 | +}) |
| 31 | + |
| 32 | +const subscribeToBlocksEffect = atomEffect((get, set) => { |
| 33 | + algokit.Config.configure({ |
| 34 | + logger: algokit.Config.getLogger(true), |
| 35 | + }) |
| 36 | + const subscriber = new AlgorandSubscriber( |
| 37 | + { |
| 38 | + filters: [ |
| 39 | + { |
| 40 | + name: 'all-transactions', |
| 41 | + filter: { |
| 42 | + customFilter: () => true, |
| 43 | + }, |
| 44 | + }, |
| 45 | + ], |
| 46 | + maxRoundsToSync: 1, // TODO: NC - Do we want this higher? |
| 47 | + waitForBlockWhenAtTip: true, |
| 48 | + syncBehaviour: 'skip-sync-newest', |
| 49 | + watermarkPersistence: { |
| 50 | + get: async () => get(syncedRoundAtom) ?? 0, |
| 51 | + set: async (watermark) => { |
| 52 | + set(syncedRoundAtom, watermark) |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + algod |
| 57 | + ) |
| 58 | + |
| 59 | + subscriber.onPoll(async (result) => { |
| 60 | + // TODO: NC - Add this back in once subscriber supports it |
| 61 | + // const blocks = result.blockMetadata |
| 62 | + const blocks = [ |
| 63 | + { |
| 64 | + round: result.syncedRoundRange[0], |
| 65 | + parentTransactionCount: result.subscribedTransactions.length, |
| 66 | + } satisfies BlockMetadata, |
| 67 | + ] |
| 68 | + |
| 69 | + if (blocks) { |
| 70 | + set(blocksAtom, (previous) => { |
| 71 | + return blocks.concat(previous) |
| 72 | + }) |
| 73 | + } |
| 74 | + |
| 75 | + set(transactionsAtom, (previous) => { |
| 76 | + return result.subscribedTransactions.concat(previous) |
| 77 | + }) |
| 78 | + }) |
| 79 | + |
| 80 | + subscriber.start() |
| 81 | + |
| 82 | + return async () => { |
| 83 | + await subscriber.stop('unmounted') |
| 84 | + } |
| 85 | +}) |
| 86 | + |
| 87 | +export const useSubscribeToBlocksEffect = () => { |
| 88 | + useAtom(subscribeToBlocksEffect) |
| 89 | +} |
| 90 | + |
| 91 | +export const useLatestBlocks = () => { |
| 92 | + return useAtomValue(latestBlockAtom) |
| 93 | +} |
0 commit comments