From d9d4cb336a6be1c03700c796eb7c830cc4ecba2d Mon Sep 17 00:00:00 2001 From: Nicole O'Brien Date: Thu, 29 Feb 2024 08:17:52 +0000 Subject: [PATCH] refactor: store local evm transactions in same store as blockscout evm transactions (#2013) Co-authored-by: Mark Nardi --- .../popup/popups/SyncAccountsPopup.svelte | 4 +- .../generateAndStoreActivitiesForAccount.ts | 3 +- ...enerateAndStoreActivitiesForAllAccounts.ts | 4 +- .../stores/persisted-transactions.store.ts | 42 +------------------ .../utils/generateActivitiesFromChains.ts | 21 ++++++---- .../nfts/actions/loadNftsForActiveProfile.ts | 29 +++++++------ .../profile/actions/active-profile/login.ts | 2 +- .../actions/profiles/removeAllProfileData.ts | 4 +- .../send/signAndSendTransactionFromEvm.ts | 30 +++++++------ 9 files changed, 56 insertions(+), 83 deletions(-) diff --git a/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte b/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte index cfc389888a..c06f02376c 100644 --- a/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte +++ b/packages/desktop/components/popup/popups/SyncAccountsPopup.svelte @@ -9,7 +9,7 @@ import { DEFAULT_ACCOUNT_RECOVERY_CONFIGURATION } from '@core/profile' import { RecoverAccountsPayload, recoverAccounts } from '@core/profile-manager' import { checkActiveProfileAuth, getBaseToken, loadAccounts } from '@core/profile/actions' - import { activeAccounts, activeProfile, visibleActiveAccounts } from '@core/profile/stores' + import { activeAccounts, activeProfile, activeProfileId, visibleActiveAccounts } from '@core/profile/stores' import { formatTokenAmountBestMatch } from '@core/token' import { refreshAccountTokensForActiveProfile } from '@core/token/actions' import { closePopup } from '@desktop/auxiliary/popup' @@ -141,7 +141,7 @@ onDestroy(async () => { if (hasUsedWalletFinder) { await refreshAccountTokensForActiveProfile() - await generateAndStoreActivitiesForAllAccounts() + await generateAndStoreActivitiesForAllAccounts($activeProfileId) loadNftsForActiveProfile() } }) diff --git a/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAccount.ts b/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAccount.ts index ced967a1c4..73672c13c3 100644 --- a/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAccount.ts +++ b/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAccount.ts @@ -13,6 +13,7 @@ import { NetworkId } from '@core/network' import { setOutgoingAsyncActivitiesToClaimed } from './setOutgoingAsyncActivitiesToClaimed' export async function generateAndStoreActivitiesForAccount( + profileId: string, account: IAccountState, networkId: NetworkId ): Promise { @@ -34,7 +35,7 @@ export async function generateAndStoreActivitiesForAccount( const balanceChangeActivities = await generateActivitiesFromBalanceChanges(account) activities.push(...balanceChangeActivities) - const chainActivities = await generateActivitiesFromChains(account) + const chainActivities = await generateActivitiesFromChains(profileId, account) activities.push(...chainActivities) // Step 4: set account activities with generated activities diff --git a/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAllAccounts.ts b/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAllAccounts.ts index 864599c0b8..52805d78e2 100644 --- a/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAllAccounts.ts +++ b/packages/shared/src/lib/core/activity/actions/generateAndStoreActivitiesForAllAccounts.ts @@ -4,13 +4,13 @@ import { activeAccounts } from '@core/profile/stores' import { generateAndStoreActivitiesForAccount } from './generateAndStoreActivitiesForAccount' import { getActiveNetworkId } from '@core/network' -export async function generateAndStoreActivitiesForAllAccounts(): Promise { +export async function generateAndStoreActivitiesForAllAccounts(profileId: string): Promise { try { const networkId = getActiveNetworkId() const accounts = get(activeAccounts) await Promise.all( - accounts.map((activeAccount) => generateAndStoreActivitiesForAccount(activeAccount, networkId)) + accounts.map((activeAccount) => generateAndStoreActivitiesForAccount(profileId, activeAccount, networkId)) ) } catch (err) { console.error(err) diff --git a/packages/shared/src/lib/core/activity/stores/persisted-transactions.store.ts b/packages/shared/src/lib/core/activity/stores/persisted-transactions.store.ts index fc7d640c41..2a9f66e91d 100644 --- a/packages/shared/src/lib/core/activity/stores/persisted-transactions.store.ts +++ b/packages/shared/src/lib/core/activity/stores/persisted-transactions.store.ts @@ -1,9 +1,6 @@ -import { activeProfileId } from '@core/profile/stores' +import { NetworkId } from '@core/network/types' import { persistent } from '@core/utils/store' -import { get } from 'svelte/store' import { PersistedEvmTransaction } from '../types' -import { NetworkId } from '@core/network/types' -import { IChain } from '@core/network/interfaces' type PersistedEvmTransactions = { [profileId: string]: { @@ -14,40 +11,3 @@ type PersistedEvmTransactions = { } export const persistedEvmTransactions = persistent('evmTransactions', {}) - -export function getPersistedEvmTransactions(accountIndex: number, chain: IChain): PersistedEvmTransaction[] { - const networkId = chain.getConfiguration().id - return get(persistedEvmTransactions)?.[get(activeProfileId)]?.[accountIndex]?.[networkId] ?? [] -} - -export function addPersistedTransaction( - accountIndex: number, - chain: IChain, - ...newTransactions: PersistedEvmTransaction[] -): void { - const networkId = chain.getConfiguration().id - const profileId = get(activeProfileId) - persistedEvmTransactions.update((state) => { - if (!state[profileId]) { - state[profileId] = {} - } - if (!state[profileId][accountIndex]) { - state[profileId][accountIndex] = { - [networkId]: [], - } - } - if (!state[profileId][accountIndex][networkId]) { - state[profileId][accountIndex][networkId] = [] - } - - state[get(activeProfileId)][accountIndex][networkId]?.push(...newTransactions) - return state - }) -} - -export function removePersistedEvmTransactionsForProfile(profileId: string): void { - persistedEvmTransactions.update((state) => { - delete state[profileId] - return state - }) -} diff --git a/packages/shared/src/lib/core/activity/utils/generateActivitiesFromChains.ts b/packages/shared/src/lib/core/activity/utils/generateActivitiesFromChains.ts index 7f485d1b12..b814d9ddd4 100644 --- a/packages/shared/src/lib/core/activity/utils/generateActivitiesFromChains.ts +++ b/packages/shared/src/lib/core/activity/utils/generateActivitiesFromChains.ts @@ -1,21 +1,24 @@ import { IAccountState } from '@core/account' +import { network } from '@core/network' +import { getPersistedTransactionsForChain } from '@core/transactions/stores' +import { get } from 'svelte/store' import { Activity } from '../types' -import { getPersistedEvmTransactions } from '../stores' import { generateActivityFromEvmTransaction } from './generateActivityFromEvmTransaction' -import { get } from 'svelte/store' -import { network } from '@core/network' -export async function generateActivitiesFromChains(account: IAccountState): Promise { +export async function generateActivitiesFromChains(profileId: string, account: IAccountState): Promise { const activities: Activity[] = [] const chains = get(network)?.getChains() ?? [] for (const chain of chains) { - const transactions = getPersistedEvmTransactions(account.index, chain) - for (const transaction of transactions) { + const persistedTransactions = getPersistedTransactionsForChain(profileId, account.index, chain) + for (const transaction of persistedTransactions) { try { - const activity = await generateActivityFromEvmTransaction(transaction, chain, account) - if (activity) { - activities.push(activity) + if (transaction.local) { + // TODO: build activities from persisted transactions + const activity = await generateActivityFromEvmTransaction(transaction.local, chain, account) + if (activity) { + activities.push(activity) + } } } catch (error) { console.error(error) diff --git a/packages/shared/src/lib/core/nfts/actions/loadNftsForActiveProfile.ts b/packages/shared/src/lib/core/nfts/actions/loadNftsForActiveProfile.ts index 12c7f89985..8df381d0c2 100644 --- a/packages/shared/src/lib/core/nfts/actions/loadNftsForActiveProfile.ts +++ b/packages/shared/src/lib/core/nfts/actions/loadNftsForActiveProfile.ts @@ -1,25 +1,27 @@ -import { NftOutput, OutputType } from '@iota/sdk/out/types' +import { getAddressFromAccountForNetwork } from '@core/account' import { IAccountState } from '@core/account/interfaces' -import { activeAccounts } from '@core/profile/stores' +import { ActivityType } from '@core/activity' import { getNftId } from '@core/activity/utils/outputs' +import { getTransferInfoFromTransactionData } from '@core/layer-2/utils/getTransferInfoFromTransactionData' +import { getActiveNetworkId, getNetwork } from '@core/network' +import { activeAccounts, activeProfileId } from '@core/profile/stores' +import { getPersistedTransactionsForChain } from '@core/transactions/stores' import { IWrappedOutput } from '@core/wallet/interfaces' +import { NftOutput, OutputType } from '@iota/sdk/out/types' import { get } from 'svelte/store' import { Nft } from '../interfaces' -import { buildNftFromNftOutput } from './buildNftFromNftOutput' -import { setAccountNftsInAllAccountNfts } from './setAccountNftsInAllAccountNfts' -import { getActiveNetworkId, getNetwork } from '@core/network' -import { ActivityType, getPersistedEvmTransactions } from '@core/activity' -import { getTransferInfoFromTransactionData } from '@core/layer-2/utils/getTransferInfoFromTransactionData' import { buildNftFromPersistedErc721Nft, getNftsFromNftIds } from '../utils' import { addNftsToDownloadQueue } from './addNftsToDownloadQueue' +import { buildNftFromNftOutput } from './buildNftFromNftOutput' import { getPersistedErc721Nfts } from './getPersistedErc721Nfts' -import { getAddressFromAccountForNetwork } from '@core/account' +import { setAccountNftsInAllAccountNfts } from './setAccountNftsInAllAccountNfts' export async function loadNftsForActiveProfile(): Promise { let nftsToDownload: Nft[] = [] + const profileId = get(activeProfileId) const allAccounts = get(activeAccounts) for (const account of allAccounts) { - const accountNfts = await loadNftsForAccount(account) + const accountNfts = await loadNftsForAccount(profileId, account) nftsToDownload = [...nftsToDownload, ...accountNfts] } @@ -27,7 +29,7 @@ export async function loadNftsForActiveProfile(): Promise { void addNftsToDownloadQueue(nftsToDownload) } -export async function loadNftsForAccount(account: IAccountState): Promise { +export async function loadNftsForAccount(profileId: string, account: IAccountState): Promise { const accountNfts: Nft[] = [] const unspentOutputs = await account.unspentOutputs() const networkId = getActiveNetworkId() @@ -40,10 +42,13 @@ export async function loadNftsForAccount(account: IAccountState): Promise for (const chain of getNetwork()?.getChains() ?? []) { // Wrapped L1 NFTs - const transactionsOnChain = getPersistedEvmTransactions(account.index, chain) + const transactionsOnChain = getPersistedTransactionsForChain(profileId, account.index, chain) const nftIdsOnChain = [] for (const transaction of transactionsOnChain) { - const transferInfo = getTransferInfoFromTransactionData(transaction, chain) + if (!transaction.local) { + continue + } + const transferInfo = getTransferInfoFromTransactionData(transaction.local, chain) if (transferInfo?.type !== ActivityType.Nft) { continue } diff --git a/packages/shared/src/lib/core/profile/actions/active-profile/login.ts b/packages/shared/src/lib/core/profile/actions/active-profile/login.ts index 24dc6d6d8f..be09aaa201 100644 --- a/packages/shared/src/lib/core/profile/actions/active-profile/login.ts +++ b/packages/shared/src/lib/core/profile/actions/active-profile/login.ts @@ -85,7 +85,7 @@ export async function login(loginOptions?: ILoginOptions): Promise { // Step 5: generate and store activities for all accounts incrementLoginProgress() - await generateAndStoreActivitiesForAllAccounts() + await generateAndStoreActivitiesForAllAccounts(_activeProfile.id) if (type === ProfileType.Software) { // Step 6: set initial stronghold status diff --git a/packages/shared/src/lib/core/profile/actions/profiles/removeAllProfileData.ts b/packages/shared/src/lib/core/profile/actions/profiles/removeAllProfileData.ts index 5871946a87..47016e7d06 100644 --- a/packages/shared/src/lib/core/profile/actions/profiles/removeAllProfileData.ts +++ b/packages/shared/src/lib/core/profile/actions/profiles/removeAllProfileData.ts @@ -3,15 +3,15 @@ import { removeClaimedActivitiesForProfile, removeHiddenActivitiesForProfile, removePersistedBalanceChangesForProfile, - removePersistedEvmTransactionsForProfile, } from '@core/activity/stores' import { removePersistedNftsForProfile } from '@core/nfts/stores' import { removePersistedProfile } from '@core/profile/stores' import { removePersistedTokensForProfile } from '@core/token/stores' +import { removePersistedTransactionsForProfile } from '@core/transactions/stores' export function removeAllProfileData(profileId: string): void { removePersistedProfile(profileId) - removePersistedEvmTransactionsForProfile(profileId) + removePersistedTransactionsForProfile(profileId) removePersistedBalanceChangesForProfile(profileId) removeClaimedActivitiesForProfile(profileId) removePersistedNftsForProfile(profileId) diff --git a/packages/shared/src/lib/core/wallet/actions/send/signAndSendTransactionFromEvm.ts b/packages/shared/src/lib/core/wallet/actions/send/signAndSendTransactionFromEvm.ts index 328f40da0a..501bfb18ee 100644 --- a/packages/shared/src/lib/core/wallet/actions/send/signAndSendTransactionFromEvm.ts +++ b/packages/shared/src/lib/core/wallet/actions/send/signAndSendTransactionFromEvm.ts @@ -1,8 +1,4 @@ -import { addAccountActivity, addPersistedTransaction } from '@core/activity/stores' -import { EvmTransactionData } from '@core/layer-2' -import { IChain } from '@core/network' -import { signEvmTransaction } from '../signEvmTransaction' -import { generateActivityFromEvmTransaction } from '@core/activity/utils/generateActivityFromEvmTransaction' +import { IAccountState } from '@core/account' import { Activity, ActivityDirection, @@ -10,15 +6,21 @@ import { PersistedEvmTransaction, calculateAndAddPersistedNftBalanceChange, } from '@core/activity' -import { IAccountState } from '@core/account' -import { updateL2BalanceWithoutActivity } from '../updateL2BalanceWithoutActivity' +import { addAccountActivity } from '@core/activity/stores' +import { generateActivityFromEvmTransaction } from '@core/activity/utils/generateActivityFromEvmTransaction' +import { EvmTransactionData } from '@core/layer-2' +import { EvmNetworkId, IChain } from '@core/network' +import { addLocalTransactionToPersistedTransaction } from '@core/transactions/stores' import { sendSignedEvmTransaction } from '@core/wallet/actions/sendSignedEvmTransaction' +import { signEvmTransaction } from '../signEvmTransaction' +import { updateL2BalanceWithoutActivity } from '../updateL2BalanceWithoutActivity' export async function signAndSendTransactionFromEvm( preparedTransaction: EvmTransactionData, chain: IChain, account: IAccountState, - signAndSend: boolean + signAndSend: boolean, + profileId: string ): Promise { const signedTransaction = await signEvmTransaction(preparedTransaction, chain, account) if (!signedTransaction) { @@ -40,16 +42,18 @@ export async function signAndSendTransactionFromEvm( ...transactionReceipt, timestamp: Date.now(), } - await persistEvmTransaction(evmTransaction, chain, account) + await persistEvmTransaction(profileId, account, chain, evmTransaction) return transactionReceipt.transactionHash } async function persistEvmTransaction( - evmTransaction: PersistedEvmTransaction, + profileId: string, + account: IAccountState, chain: IChain, - account: IAccountState + evmTransaction: PersistedEvmTransaction ): Promise { - addPersistedTransaction(account.index, chain, evmTransaction) + const networkId = chain.getConfiguration().id as EvmNetworkId + addLocalTransactionToPersistedTransaction(profileId, account.index, networkId, [evmTransaction]) const activity = await generateActivityFromEvmTransaction(evmTransaction, chain, account) if (!activity) { @@ -62,7 +66,7 @@ async function persistEvmTransaction( if (activity.recipient?.type === 'account') { const recipientAccount = activity.recipient.account - addPersistedTransaction(recipientAccount.index, chain, evmTransaction) + addLocalTransactionToPersistedTransaction(profileId, recipientAccount.index, networkId, [evmTransaction]) const receiveActivity = await generateActivityFromEvmTransaction(evmTransaction, chain, recipientAccount) if (!receiveActivity) { return