Skip to content

Commit

Permalink
chore: type safe access to activeProfileId (#2161)
Browse files Browse the repository at this point in the history
* fix: access activeProfileId w/ type safety

* chore: don't rely on global vars in sendAndPersistTxFromEvm

* fix

---------

Co-authored-by: Mark Nardi <[email protected]>
  • Loading branch information
Tuditi and MarkNerdi authored Mar 19, 2024
1 parent ecf5227 commit f5514a7
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import { IConnectedDapp } from '@auxiliary/wallet-connect/interface'
import { CallbackParameters } from '@auxiliary/wallet-connect/types'
import { sendAndPersistTransactionFromEvm, signEvmTransaction } from '@core/wallet/actions'
import { selectedAccount } from '@core/account/stores'
import { getSelectedAccount, selectedAccount } from '@core/account/stores'
import { ExplorerEndpoint, IChain, getDefaultExplorerUrl } from '@core/network'
import { DappInfo, TransactionAssetSection } from '@ui'
import PopupTemplate from '../PopupTemplate.svelte'
Expand All @@ -31,7 +31,8 @@
import { LedgerAppName } from '@core/ledger'
import { DappVerification, RpcMethod } from '@auxiliary/wallet-connect/enums'
import { LegacyTransaction } from '@ethereumjs/tx'
import { activeProfileId } from '@core/profile/stores'
import { getActiveProfileId } from '@core/profile/stores'
import { IAccountState } from '@core/account'
export let preparedTransaction: EvmTransactionData
export let chain: IChain
Expand Down Expand Up @@ -87,27 +88,30 @@
}
}
async function getSignedTransaction(): Promise<string> {
async function getSignedTransaction(account: IAccountState): Promise<string> {
if (preparedTransaction?.v && preparedTransaction?.s && preparedTransaction?.r) {
const transaction = LegacyTransaction.fromTxData(preparedTransaction)
return getHexEncodedTransaction(transaction)
} else {
return await signEvmTransaction(preparedTransaction, chain, $selectedAccount)
return await signEvmTransaction(preparedTransaction, chain, account)
}
}
async function signOrSend(): Promise<void> {
const signedTransaction = await getSignedTransaction()
const profileId = getActiveProfileId()
const account = getSelectedAccount()
const signedTransaction = await getSignedTransaction(account)
if (method === RpcMethod.EthSignTransaction) {
callback({ result: signedTransaction })
return
}
const transactionHash = await sendAndPersistTransactionFromEvm(
preparedTransaction,
signedTransaction,
chain,
$selectedAccount,
$activeProfileId
profileId,
account
)
callback({ result: transactionHash })
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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, activeProfileId, visibleActiveAccounts } from '@core/profile/stores'
import { activeAccounts, activeProfile, getActiveProfileId, visibleActiveAccounts } from '@core/profile/stores'
import { formatTokenAmountBestMatch } from '@core/token'
import { refreshAccountTokensForActiveProfile } from '@core/token/actions'
import { closePopup } from '@desktop/auxiliary/popup'
Expand Down Expand Up @@ -140,8 +140,9 @@
onDestroy(async () => {
if (hasUsedWalletFinder) {
const profileId = getActiveProfileId()
await refreshAccountTokensForActiveProfile()
await generateAndStoreActivitiesForAllAccounts($activeProfileId)
await generateAndStoreActivitiesForAllAccounts(profileId)
loadNftsForActiveProfile()
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
let selectedIndex = 0
$: connectedDappsForProfile = $connectedDapps.filter(
(dapp) => !!$persistedDappNamespaces[$activeProfileId]?.[dapp.metadata?.url]
(dapp) => !!$persistedDappNamespaces[$activeProfileId as string]?.[dapp.metadata?.url ?? '']
)
$: displayedDapps = connectedDappsForProfile.filter(
(dapp) => (selectedIndex === 0 && !!dapp.session) || (selectedIndex === 1 && !dapp.session)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script lang="ts">
import { Spinner } from '@bloomwalletio/ui'
import { PopupTemplate } from '@components'
import { selectedAccount } from '@core/account/stores'
import { getSelectedAccount, selectedAccount } from '@core/account/stores'
import { handleError } from '@core/error/handlers'
import { localize } from '@core/i18n'
import { IChain, getNetwork, isEvmChain } from '@core/network'
Expand All @@ -25,7 +25,7 @@
import { showNotification } from '@auxiliary/notification'
import { checkActiveProfileAuthAsync } from '@core/profile/actions'
import { LedgerAppName, ledgerPreparedOutput } from '@core/ledger'
import { activeProfileId, getIsActiveLedgerProfile } from '@core/profile/stores'
import { getActiveProfileId, getIsActiveLedgerProfile } from '@core/profile/stores'
export let transactionSummaryProps: TransactionSummaryProps
let { _onMount, preparedOutput, preparedTransaction } = transactionSummaryProps ?? {}
Expand Down Expand Up @@ -106,13 +106,15 @@
modifyPopupState({ preventClose: true })
if (isSourceNetworkLayer2) {
const signedTransaction = await signEvmTransaction(preparedTransaction, chain, $selectedAccount)
const profileId = getActiveProfileId()
const account = getSelectedAccount()
await sendAndPersistTransactionFromEvm(
preparedTransaction,
signedTransaction,
chain,
$selectedAccount,
$activeProfileId
profileId,
account
)
} else {
await signAndSendStardustTransaction(preparedOutput, $selectedAccount)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export async function generateAndStoreActivitiesForAccount(
]

// Step 2: link transactions with corresponding claiming transactions
const linkedProcessedTransactions = linkTransactionsWithClaimingTransactions(processedTransactions, account)
const linkedProcessedTransactions = linkTransactionsWithClaimingTransactions(
processedTransactions,
account,
profileId
)

// Step 3: generate activities from processed transactions
const activities = await generateActivitiesFromProcessedTransactions(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { IAccountState } from '@core/account'
import { addClaimedActivity, claimedActivities } from '@core/activity/stores'
import { activeProfileId } from '@core/profile/stores'
import { get } from 'svelte/store'
import { ActivityDirection } from '../enums'
import { IProcessedTransaction } from '../types'
Expand All @@ -15,12 +14,13 @@ import { isOutputAsync } from '../utils/outputs'
*/
export function linkTransactionsWithClaimingTransactions(
transactions: IProcessedTransaction[],
account: IAccountState
account: IAccountState,
profileId: string
): IProcessedTransaction[] {
const resultingTransactions = []
const transactionsIncludedAsClaimingTransactions: string[] = []

const claimedAccountActivities = get(claimedActivities)?.[get(activeProfileId)]?.[account.index]
const claimedAccountActivities = get(claimedActivities)?.[profileId]?.[account.index]
const sortedTransactions = transactions.sort((t1, t2) => (t1.time > t2.time ? 1 : -1))
const incomingAsyncTransactions: IProcessedTransaction[] = []
for (const transaction of sortedTransactions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { get } from 'svelte/store'

import { activeProfileId } from '@core/profile/stores'
import { getActiveProfileId } from '@core/profile/stores'
import { persistent } from '@core/utils/store'
import { IClaimedActivities, IClaimedActivitiesPerProfile } from '../types'

Expand All @@ -12,7 +10,7 @@ export function addClaimedActivity(
claimedActivity: IClaimedActivities
): void {
claimedActivities.update((state) => {
const profileId = get(activeProfileId)
const profileId = getActiveProfileId()
if (Array.isArray(state)) {
// needed because of legacy way to store claimed activities
state = {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { activeProfileId } from '@core/profile/stores'
import { getActiveProfileId } from '@core/profile/stores'
import { persistent } from '@core/utils/store'
import { get } from 'svelte/store'
import type { IHiddenActivities } from '../types'

export const hiddenActivities = persistent<IHiddenActivities>('hiddenActivities', {})

export function isActivityHiddenForAccountIndex(accountIndex: number, activityId: string): boolean {
const activities = get(hiddenActivities)?.[get(activeProfileId)]?.[accountIndex]
const activities = get(hiddenActivities)?.[getActiveProfileId()]?.[accountIndex]
return activities ? activities.includes(activityId) : false
}

export function removeActivityFromHiddenActivities(accountIndex: number, activityId: string): void {
const activities = get(hiddenActivities)?.[get(activeProfileId)]?.[accountIndex]
const activities = get(hiddenActivities)?.[getActiveProfileId()]?.[accountIndex]
if (activities) {
hiddenActivities.update((state) => {
state[get(activeProfileId)][accountIndex] = activities.filter((id) => id !== activityId)
state[getActiveProfileId()][accountIndex] = activities.filter((id) => id !== activityId)
return state
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ITokenBalanceChange } from '../types/token-balance-change.interface'
import { INftBalanceChange } from '../types/nft-balance-change.interface'
import { get } from 'svelte/store'
import { NetworkId } from '@core/network'
import { activeProfileId } from '@core/profile/stores'
import { getActiveProfileId } from '@core/profile/stores'

type IPersistedBalanceChangesStore = {
[profileId: string]: {
Expand Down Expand Up @@ -33,7 +33,7 @@ export function getBalanceChanges(
[tokenId: string]: ITokenBalanceChange[]
}
} {
return get(persistedBalanceChanges)?.[get(activeProfileId)]?.[accountIndex]?.[networkId] ?? { nfts: {}, tokens: {} }
return get(persistedBalanceChanges)?.[getActiveProfileId()]?.[accountIndex]?.[networkId] ?? { nfts: {}, tokens: {} }
}

export function addPersistedTokenBalanceChange(
Expand All @@ -43,7 +43,8 @@ export function addPersistedTokenBalanceChange(
...newPersistedTokenBalanceChanges: ITokenBalanceChange[]
): void {
persistedBalanceChanges.update((state) => {
let profileBalanceChanges = state[get(activeProfileId)]
const profileId = getActiveProfileId()
let profileBalanceChanges = state[profileId]

if (!profileBalanceChanges) {
profileBalanceChanges = {}
Expand Down Expand Up @@ -72,7 +73,7 @@ export function addPersistedTokenBalanceChange(

accountBalanceChanges[networkId] = networkBalanceChanges
profileBalanceChanges[accountIndex] = accountBalanceChanges
state[get(activeProfileId)] = profileBalanceChanges
state[profileId] = profileBalanceChanges
return state
})
}
Expand All @@ -84,7 +85,8 @@ export function addPersistedNftBalanceChange(
...newPersistedNftBalanceChanges: INftBalanceChange[]
): void {
persistedBalanceChanges.update((state) => {
let profileBalanceChanges = state[get(activeProfileId)]
const profileId = getActiveProfileId()
let profileBalanceChanges = state[profileId]

if (!profileBalanceChanges) {
profileBalanceChanges = {}
Expand Down Expand Up @@ -113,7 +115,7 @@ export function addPersistedNftBalanceChange(

accountBalanceChanges[networkId] = networkBalanceChanges
profileBalanceChanges[accountIndex] = accountBalanceChanges
state[get(activeProfileId)] = profileBalanceChanges
state[profileId] = profileBalanceChanges
return state
})
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { StardustActivityType } 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 { activeAccounts, getActiveProfileId } from '@core/profile/stores'
import { getPersistedTransactionsForChain } from '@core/transactions/stores'
import { IWrappedOutput } from '@core/wallet/interfaces'
import { NftOutput, OutputType } from '@iota/sdk/out/types'
Expand All @@ -18,7 +18,7 @@ import { setAccountNftsInAllAccountNfts } from './setAccountNftsInAllAccountNfts

export async function loadNftsForActiveProfile(): Promise<void> {
let nftsToDownload: Nft[] = []
const profileId = get(activeProfileId)
const profileId = getActiveProfileId()
const allAccounts = get(activeAccounts)
for (const account of allAccounts) {
const accountNfts = await loadNftsForAccount(profileId, account)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
import { persistent } from '@core/utils/store'
import { get } from 'svelte/store'

export const activeProfileId = persistent<string>('activeProfileId', null)
export const activeProfileId = persistent<string | null>('activeProfileId', null)

export function getActiveProfileId(): string {
const profileId = get(activeProfileId)
if (!profileId) {
throw new Error('No active profile ID set!')
}
return profileId
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { IBlockscoutTokenTransfer, IBlockscoutTransaction } from '@auxiliary/blockscout/interfaces'
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'
import { LocalEvmTransaction, PersistedTransaction } from '../types/'
Expand Down Expand Up @@ -46,7 +45,7 @@ export function addLocalTransactionToPersistedTransaction(
state[profileId][accountIndex][networkId] = {}
}

const _transactions = state[get(activeProfileId)][accountIndex][networkId] ?? {}
const _transactions = state[profileId][accountIndex][networkId] ?? {}
for (const transaction of newTransactions) {
const existingTransaction = _transactions?.[transaction.transactionHash.toLowerCase()]
const updatedTransaction: PersistedTransaction = {
Expand All @@ -55,7 +54,7 @@ export function addLocalTransactionToPersistedTransaction(
}
_transactions[transaction.transactionHash.toLowerCase()] = updatedTransaction
}
state[get(activeProfileId)][accountIndex][networkId] = _transactions
state[profileId][accountIndex][networkId] = _transactions

return state
})
Expand All @@ -80,7 +79,7 @@ export function addBlockscoutTransactionToPersistedTransactions(
state[profileId][accountIndex][networkId] = {}
}

const _transactions = state[get(activeProfileId)][accountIndex][networkId] ?? {}
const _transactions = state[profileId][accountIndex][networkId] ?? {}
for (const transaction of newTransactions) {
const existingTransaction = _transactions?.[transaction.hash.toLowerCase()]
const updatedTransaction: PersistedTransaction = {
Expand All @@ -89,7 +88,7 @@ export function addBlockscoutTransactionToPersistedTransactions(
}
_transactions[transaction.hash.toLowerCase()] = updatedTransaction
}
state[get(activeProfileId)][accountIndex][networkId] = _transactions
state[profileId][accountIndex][networkId] = _transactions

return state
})
Expand All @@ -114,7 +113,7 @@ export function addBlockscoutTokenTransferToPersistedTransactions(
state[profileId][accountIndex][networkId] = {}
}

const _transactions = state[get(activeProfileId)][accountIndex][networkId] ?? {}
const _transactions = state[profileId][accountIndex][networkId] ?? {}
for (const tokenTransfer of newTokenTransfers) {
const existingTransaction = _transactions?.[tokenTransfer.tx_hash.toLowerCase()]
const updatedTransaction: PersistedTransaction = {
Expand All @@ -123,7 +122,7 @@ export function addBlockscoutTokenTransferToPersistedTransactions(
}
_transactions[tokenTransfer.tx_hash.toLowerCase()] = updatedTransaction
}
state[get(activeProfileId)][accountIndex][networkId] = _transactions
state[profileId][accountIndex][networkId] = _transactions

return state
})
Expand Down
5 changes: 2 additions & 3 deletions packages/shared/src/lib/core/wallet/actions/rejectActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ import { showNotification } from '@auxiliary/notification'
import { getSelectedAccount } from '@core/account/stores'
import { hiddenActivities, updateAsyncDataByActivityId } from '@core/activity/stores'
import { localize } from '@core/i18n'
import { activeProfileId } from '@core/profile/stores'
import { get } from 'svelte/store'
import { getActiveProfileId } from '@core/profile/stores'

export function rejectActivity(id: string): void {
const accountIndex = getSelectedAccount().index
hiddenActivities.update((state) => {
const profileId = get(activeProfileId)
const profileId = getActiveProfileId()
if (Array.isArray(state)) {
// needed because of legacy way to store hidden activities
state = {}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ export async function sendAndPersistTransactionFromEvm(
preparedTransaction: EvmTransactionData,
signedTransaction: string,
chain: IChain,
account: IAccountState,
profileId: string
profileId: string,
account: IAccountState
): Promise<string> {
const transactionReceipt = await sendSignedEvmTransaction(chain, signedTransaction)
if (!transactionReceipt) {
Expand Down

0 comments on commit f5514a7

Please sign in to comment.