-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: persist evm blockscout transactions (#2008)
* refactor: improve blockscout transaction interface Co-authored-by: Mark Nardi <[email protected]> * feat: add new persisted transactions store * feat: fetch and persist transactions from blockscout on login * feat: add exit function to recursive blockscout api calls * interupt recursion if error occured --------- Co-authored-by: Mark Nardi <[email protected]>
- Loading branch information
1 parent
f8879a9
commit 482db21
Showing
7 changed files
with
227 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
packages/shared/src/lib/core/transactions/actions/fetchAndPersistTransactionsForAccounts.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { IBlockscoutTransaction } from '@auxiliary/blockscout/interfaces' | ||
import { IAccountState, getAddressFromAccountForNetwork } from '@core/account' | ||
import { addBlockscoutTransactionToPersistedTransactions, isBlockscoutTransactionPersisted } from '../stores' | ||
import { BlockscoutApi } from '@auxiliary/blockscout/api' | ||
import { EvmNetworkId, getNetwork } from '@core/network' | ||
|
||
export async function fetchAndPersistTransactionsForAccounts( | ||
profileId: string, | ||
accounts: IAccountState[] | ||
): Promise<void> { | ||
const chains = getNetwork()?.getChains() ?? [] | ||
for (const chain of chains) { | ||
const networkId = chain.getConfiguration().id as EvmNetworkId | ||
for (const account of accounts) { | ||
try { | ||
const blockscoutTransactions = await fetchBlockscoutTransactionsForAccount( | ||
profileId, | ||
account, | ||
networkId | ||
) | ||
blockscoutTransactions && | ||
addBlockscoutTransactionToPersistedTransactions( | ||
profileId, | ||
account.index, | ||
networkId, | ||
blockscoutTransactions | ||
) | ||
} catch (err) { | ||
console.error(err) | ||
} | ||
} | ||
} | ||
} | ||
|
||
function getTransactionsExitFunction( | ||
items: IBlockscoutTransaction[], | ||
profileId: string, | ||
accountIndex: number, | ||
networkId: EvmNetworkId | ||
): boolean { | ||
const lastItem = items[items.length - 1] | ||
return lastItem ? isBlockscoutTransactionPersisted(profileId, accountIndex, networkId, lastItem.hash) : false | ||
} | ||
|
||
async function fetchBlockscoutTransactionsForAccount( | ||
profileId: string, | ||
account: IAccountState, | ||
networkId: EvmNetworkId | ||
): Promise<IBlockscoutTransaction[] | undefined> { | ||
const address = getAddressFromAccountForNetwork(account, networkId) | ||
if (!address) { | ||
return undefined | ||
} | ||
const blockscoutApi = new BlockscoutApi(networkId) | ||
const transactions = await blockscoutApi.getTransactionsForAddress(address, (items: IBlockscoutTransaction[]) => | ||
getTransactionsExitFunction(items, profileId, account.index, networkId) | ||
) | ||
return transactions | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './fetchAndPersistTransactionsForAccounts' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export * from './transactions.store' |
126 changes: 126 additions & 0 deletions
126
packages/shared/src/lib/core/transactions/stores/transactions.store.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
import { IBlockscoutTransaction } from '@auxiliary/blockscout/interfaces' | ||
import { PersistedEvmTransaction } from '@core/activity' | ||
import { EvmNetworkId } from '@core/network' | ||
import { IChain } from '@core/network/interfaces' | ||
import { activeProfileId } from '@core/profile/stores' | ||
import { persistent } from '@core/utils/store' | ||
import { get } from 'svelte/store' | ||
|
||
type PersistedTransaction = | ||
| { | ||
blockscout: IBlockscoutTransaction | ||
local: PersistedEvmTransaction | ||
} | ||
| { | ||
blockscout?: IBlockscoutTransaction | ||
local: PersistedEvmTransaction | ||
} | ||
| { | ||
blockscout: IBlockscoutTransaction | ||
local?: PersistedEvmTransaction | ||
} | ||
|
||
type PersistedTransactions = { | ||
[profileId: string]: { | ||
[accountId: string]: { | ||
[networkId in EvmNetworkId]?: { | ||
[transactionHash: string]: PersistedTransaction | ||
} | ||
} | ||
} | ||
} | ||
|
||
export const persistedTransactions = persistent<PersistedTransactions>('transactions', {}) | ||
|
||
export function getPersistedTransactionsForChain( | ||
profileId: string, | ||
accountIndex: number, | ||
chain: IChain | ||
): PersistedTransaction[] { | ||
const networkId = chain.getConfiguration().id as EvmNetworkId | ||
return Object.values(get(persistedTransactions)?.[profileId]?.[accountIndex]?.[networkId] ?? {}) ?? [] | ||
} | ||
|
||
export function addLocalTransactionToPersistedTransaction( | ||
profileId: string, | ||
accountIndex: number, | ||
networkId: EvmNetworkId, | ||
newTransactions: PersistedEvmTransaction[] | ||
): void { | ||
persistedTransactions.update((state) => { | ||
if (!state[profileId]) { | ||
state[profileId] = {} | ||
} | ||
if (!state[profileId][accountIndex]) { | ||
state[profileId][accountIndex] = { | ||
[networkId]: {}, | ||
} | ||
} | ||
if (!state[profileId][accountIndex][networkId]) { | ||
state[profileId][accountIndex][networkId] = {} | ||
} | ||
|
||
const _transactions = state[get(activeProfileId)][accountIndex][networkId] ?? {} | ||
for (const transaction of newTransactions) { | ||
const existingTransaction = _transactions?.[transaction.transactionHash.toLowerCase()] | ||
const updatedTransaction: PersistedTransaction = { | ||
...existingTransaction, | ||
local: transaction, | ||
} | ||
_transactions[transaction.transactionHash.toLowerCase()] = updatedTransaction | ||
} | ||
state[get(activeProfileId)][accountIndex][networkId] = _transactions | ||
|
||
return state | ||
}) | ||
} | ||
|
||
export function addBlockscoutTransactionToPersistedTransactions( | ||
profileId: string, | ||
accountIndex: number, | ||
networkId: EvmNetworkId, | ||
newTransactions: IBlockscoutTransaction[] | ||
): void { | ||
persistedTransactions.update((state) => { | ||
if (!state[profileId]) { | ||
state[profileId] = {} | ||
} | ||
if (!state[profileId][accountIndex]) { | ||
state[profileId][accountIndex] = { | ||
[networkId]: {}, | ||
} | ||
} | ||
if (!state[profileId][accountIndex][networkId]) { | ||
state[profileId][accountIndex][networkId] = {} | ||
} | ||
|
||
const _transactions = state[get(activeProfileId)][accountIndex][networkId] ?? {} | ||
for (const transaction of newTransactions) { | ||
const existingTransaction = _transactions?.[transaction.hash.toLowerCase()] | ||
const updatedTransaction: PersistedTransaction = { | ||
...existingTransaction, | ||
blockscout: transaction, | ||
} | ||
_transactions[transaction.hash.toLowerCase()] = updatedTransaction | ||
} | ||
state[get(activeProfileId)][accountIndex][networkId] = _transactions | ||
|
||
return state | ||
}) | ||
} | ||
|
||
export function removePersistedTransactionsForProfile(profileId: string): void { | ||
persistedTransactions.update((state) => { | ||
delete state[profileId] | ||
return state | ||
}) | ||
} | ||
|
||
export function isBlockscoutTransactionPersisted( | ||
profileId: string, | ||
accountIndex: number, | ||
networkId: EvmNetworkId, | ||
transactionHash: string | ||
): boolean { | ||
return !!get(persistedTransactions)?.[profileId]?.[accountIndex]?.[networkId]?.[transactionHash]?.blockscout | ||
} |