Skip to content

Commit

Permalink
feat: adds latest block fetched logic and store for noves
Browse files Browse the repository at this point in the history
  • Loading branch information
jeeanribeiro committed Oct 15, 2024
1 parent d5b595e commit 8eb7be8
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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 =
Expand All @@ -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(
Expand Down Expand Up @@ -133,6 +146,7 @@ async function fetchBlockscoutTransactionsForAccount(
}

async function fetchNovesTransactionsForAccount(
profileId: string,
account: IAccountState,
network: IEvmNetwork
): Promise<NovesTxResponse[]> {
Expand All @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions packages/shared/src/lib/core/transactions/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './latest-block-persisted-for-noves-transactions.store'
export * from './transactions.store'
Original file line number Diff line number Diff line change
@@ -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<LatestBlockPersistedForAccountNetwork>({})

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
})
}

0 comments on commit 8eb7be8

Please sign in to comment.