From 8eb7be88e21d4963a2dbbf04dee57be483806d99 Mon Sep 17 00:00:00 2001 From: Jean Ribeiro Date: Tue, 15 Oct 2024 12:02:42 -0300 Subject: [PATCH] feat: adds latest block fetched logic and store for noves --- .../fetchAndPersistTransactionsForAccounts.ts | 28 +++++++++-- .../src/lib/core/transactions/stores/index.ts | 1 + ...-persisted-for-noves-transactions.store.ts | 47 +++++++++++++++++++ 3 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 packages/shared/src/lib/core/transactions/stores/latest-block-persisted-for-noves-transactions.store.ts diff --git a/packages/shared/src/lib/core/transactions/actions/fetchAndPersistTransactionsForAccounts.ts b/packages/shared/src/lib/core/transactions/actions/fetchAndPersistTransactionsForAccounts.ts index 8e7084a214..8f5cf3ed2a 100644 --- a/packages/shared/src/lib/core/transactions/actions/fetchAndPersistTransactionsForAccounts.ts +++ b/packages/shared/src/lib/core/transactions/actions/fetchAndPersistTransactionsForAccounts.ts @@ -4,9 +4,11 @@ import { addBlockscoutTokenTransferToPersistedTransactions, addBlockscoutTransactionToPersistedTransactions, addNovesTransactionToPersistedTransactions, + getLatestBlockForPersistedNovesTransactionsForAccountNetwork, getPersistedTransactionsForChain, isBlockscoutTokenTransferPersisted, isBlockscoutTransactionPersisted, + updateLatestBlockForPersistedNovesTransactionsForAccountNetwork, } from '../stores' import { BlockscoutApi } from '@auxiliary/blockscout/api' import { EvmNetworkId, IEvmNetwork, getEvmNetworks } from '@core/network' @@ -39,7 +41,7 @@ export async function fetchAndPersistTransactionsForNetwork( try { const [blockscoutTransactionsPromise, novesTransactionsPromise] = await Promise.allSettled([ fetchBlockscoutTransactionsForAccount(profileId, account, network), - fetchNovesTransactionsForAccount(account, network), + fetchNovesTransactionsForAccount(profileId, account, network), ]) const blockscoutTransactions = @@ -54,9 +56,20 @@ export async function fetchAndPersistTransactionsForNetwork( blockscoutTransactions ) - novesTransactions && + if (novesTransactions && novesTransactions.length > 0) { addNovesTransactionToPersistedTransactions(profileId, account.index, network.id, novesTransactions) + // Transactions are in reverse-chronological order + // So first transaction will be the latest one + const latestBlock = novesTransactions[0].rawTransactionData.blockNumber + updateLatestBlockForPersistedNovesTransactionsForAccountNetwork( + profileId, + account.index, + network.id, + latestBlock + ) + } + const blockscoutTokenTransfers = await fetchBlockscoutTokenTransfersForAccount(profileId, account, network) blockscoutTokenTransfers && addBlockscoutTokenTransferToPersistedTransactions( @@ -133,6 +146,7 @@ async function fetchBlockscoutTransactionsForAccount( } async function fetchNovesTransactionsForAccount( + profileId: string, account: IAccountState, network: IEvmNetwork ): Promise { @@ -142,8 +156,16 @@ async function fetchNovesTransactionsForAccount( return [] } + const latestBlock = getLatestBlockForPersistedNovesTransactionsForAccountNetwork( + profileId, + account.index, + network.id + ) + const novesApi = new NovesApi() - const transactions = await novesApi.translate.getTransactionsFromAddress(address, novesChain) + const transactions = await novesApi.translate.getTransactionsFromAddress(address, novesChain, { + startBlock: latestBlock, + }) return transactions } diff --git a/packages/shared/src/lib/core/transactions/stores/index.ts b/packages/shared/src/lib/core/transactions/stores/index.ts index 07282a2660..4a2b74f22b 100644 --- a/packages/shared/src/lib/core/transactions/stores/index.ts +++ b/packages/shared/src/lib/core/transactions/stores/index.ts @@ -1 +1,2 @@ +export * from './latest-block-persisted-for-noves-transactions.store' export * from './transactions.store' diff --git a/packages/shared/src/lib/core/transactions/stores/latest-block-persisted-for-noves-transactions.store.ts b/packages/shared/src/lib/core/transactions/stores/latest-block-persisted-for-noves-transactions.store.ts new file mode 100644 index 0000000000..efdd0b4dc1 --- /dev/null +++ b/packages/shared/src/lib/core/transactions/stores/latest-block-persisted-for-noves-transactions.store.ts @@ -0,0 +1,47 @@ +import { NetworkId } from '@core/network' +import { get, writable } from 'svelte/store' + +type LatestBlockPersistedForAccountNetwork = { + [profileId: string]: { + [accountId: number]: { + [networkId in NetworkId]?: { + latestBlock?: number + } + } + } +} + +export const latestBlockPersistedForNovesTransactions = writable({}) + +export function getLatestBlockForPersistedNovesTransactionsForAccountNetwork( + profileId: string, + accountId: number, + networkId: NetworkId +): number | undefined { + return get(latestBlockPersistedForNovesTransactions)[profileId]?.[accountId]?.[networkId]?.latestBlock +} + +export function updateLatestBlockForPersistedNovesTransactionsForAccountNetwork( + profileId: string, + accountId: number, + networkId: NetworkId, + latestBlock: number +): void { + latestBlockPersistedForNovesTransactions.update((state) => { + if (!state[profileId]) { + state[profileId] = {} + } + + if (!state[profileId][accountId]) { + state[profileId][accountId] = {} + } + + if (!state[profileId][accountId][networkId]) { + state[profileId][accountId][networkId] = {} + } + + state[profileId][accountId][networkId].latestBlock = latestBlock + + return state + }) +}