Skip to content

Commit

Permalink
fix: claimed nfts in tx history (#8316)
Browse files Browse the repository at this point in the history
* fix: claimed collectibles are shown as sent and pending to claim

* fix: update activities when sending or receiving an output
  • Loading branch information
cpl121 authored Apr 5, 2024
1 parent 39f28be commit d98d6c8
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 58 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { IWalletState } from '@core/wallet/interfaces'
import { activeProfileId } from '@core/profile'
import { ActivityDirection, IProcessedTransaction } from '@core/wallet'
import { ActivityDirection, IClaimedActivities, IProcessedTransaction } from '@core/wallet'
import { isOutputAsync } from '@core/wallet/utils/outputs/isOutputAsync'
import { get } from 'svelte/store'
import { addClaimedActivity, claimedActivities } from '../../stores'
Expand All @@ -17,9 +17,11 @@ export function linkTransactionsWithClaimingTransactions(
wallet: IWalletState
): IProcessedTransaction[] {
const resultingTransactions = []
const transactionsIncludedAsClaimingTransactions = []
const transactionsIncludedAsClaimingTransactions: string[] = []

const claimedWalletActivities: { [transactionId: string]: IClaimedActivities } =
get(claimedActivities)?.[get(activeProfileId)]?.[wallet.id]

const claimedWalletActivities = get(claimedActivities)?.[get(activeProfileId)]?.[wallet.id]
const sortedTransactions = transactions.sort((t1, t2) => (t1.time > t2.time ? 1 : -1))
const incomingAsyncTransactions: IProcessedTransaction[] = []
for (const transaction of sortedTransactions) {
Expand All @@ -28,13 +30,12 @@ export function linkTransactionsWithClaimingTransactions(
transaction.outputs.some((_output) => isOutputAsync(_output.output)) &&
(transaction.direction === ActivityDirection.Incoming ||
transaction.direction === ActivityDirection.SelfTransaction)

if (isClaimingTransaction) {
continue
} else if (isIncomingAsyncTransaction) {
// If we have the corresponding claiming transaction cached in local storage, we get that data and update the async transaction
const claimedActivity = claimedWalletActivities?.[transaction?.transactionId]
if (claimedActivity && claimedActivity.claimingTransactionId === transaction?.transactionId) {
if (claimedActivity) {
const claimingData = {
claimedDate: new Date(claimedActivity.claimedTimestamp),
claimingTransactionId: claimedActivity.claimingTransactionId,
Expand Down Expand Up @@ -72,14 +73,13 @@ export function linkTransactionsWithClaimingTransactions(
}
}
}

return resultingTransactions
}

function searchClaimedTransactionInIncomingAsyncTransactions(
allAsyncTransaction: IProcessedTransaction[],
transaction: IProcessedTransaction
): IProcessedTransaction {
): IProcessedTransaction | undefined {
return allAsyncTransaction.find((candidate) =>
transaction.utxoInputs?.some((input) => input?.transactionId === candidate?.transactionId)
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,16 @@ import { addNftsToDownloadQueue, addOrUpdateNftInAllWalletNfts, buildNftFromNftO
import { activeWallets, updateActiveWallet } from '@core/profile'
import { checkAndRemoveProfilePicture, updateActiveWalletPersistedData } from '@core/profile/actions'
import {
ActivityType,
IWrappedOutput,
WalletApiEventHandler,
addActivitiesToWalletActivitiesInAllWalletActivities,
addPersistedAsset,
generateActivities,
AddressConverter,
getOrRequestAssetFromPersistedAssets,
hasBlockIssuerFeature,
isAccountOutput,
isDelegationOutput,
isImplicitAccountOutput,
preprocessGroupedOutputs,
syncBalance,
validateWalletApiEvent,
generateAndStoreActivitiesForWallet,
} from '@core/wallet'
import {
AccountAddress,
Expand Down Expand Up @@ -64,18 +59,7 @@ export async function handleNewOutputEventInternal(walletId: string, payload: Ne
const accountOutputs = await wallet.accounts()
updateActiveWallet(wallet.id, { walletOutputs, accountOutputs, walletUnspentOutputs })

const processedOutput = preprocessGroupedOutputs([outputData], payload?.transactionInputs ?? [], wallet)

const activities = await generateActivities(processedOutput, wallet)
for (const activity of activities) {
if (activity.type === ActivityType.Basic || activity.type === ActivityType.Foundry) {
const asset = await getOrRequestAssetFromPersistedAssets(activity.assetId)
if (asset) {
addPersistedAsset(asset)
}
}
}
addActivitiesToWalletActivitiesInAllWalletActivities(wallet.id, activities)
await generateAndStoreActivitiesForWallet(wallet)
}
if (isImplicitAccountOutput(outputData)) {
await syncBalance(wallet.id, true)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import { getNftByIdFromAllWalletNfts, updateNftInAllWalletNfts } from '@core/nfts'
import { activeWallets, updateActiveWallet } from '@core/profile'
import {
ActivityAsyncStatus,
ActivityType,
WalletApiEventHandler,
allWalletActivities,
generateAndStoreActivitiesForWallet,
isDelegationOutput,
syncBalance,
updateAsyncDataByTransactionId,
validateWalletApiEvent,
} from '@core/wallet'
import { SpentOutputWalletEvent, WalletEvent, WalletEventType } from '@iota/sdk/out/types'
import { nodeInfoProtocolParameters } from '@core/network'
import { getUnixTimestampFromNodeInfoAndSlotIndex } from '@core/network/helpers/getSlotInfoFromNodeProtocolParameters'
import { get } from 'svelte/store'
import { closePopup } from 'shared/lib/auxiliary/popup'

Expand All @@ -37,33 +31,8 @@ export async function handleSpentOutputEventInternal(walletId: string, payload:
const walletUnspentOutputs = await wallet.unspentOutputs()
const implicitAccountOutputs = await wallet.implicitAccounts()
updateActiveWallet(walletId, { walletOutputs, accountOutputs, implicitAccountOutputs, walletUnspentOutputs })
}
const outputId = output?.outputId
const activity = get(allWalletActivities)?.[walletId]?.find((_activity) => _activity.outputId === outputId)

if (activity && activity.asyncData?.asyncStatus === ActivityAsyncStatus.Unclaimed) {
const transactionId = output?.metadata?.included.transactionId
updateAsyncDataByTransactionId(walletId, transactionId, {
asyncStatus: ActivityAsyncStatus.Claimed,
})
}

if (activity?.type === ActivityType.Nft) {
const previousOutputId = getNftByIdFromAllWalletNfts(walletId, activity.nftId)?.latestOutputId
const protocolParameters = get(nodeInfoProtocolParameters)
if (!wallet || !previousOutputId || !protocolParameters) return
const previousOutput = await wallet.getOutput(previousOutputId)
const unixTimestampOutputMetadata = getUnixTimestampFromNodeInfoAndSlotIndex(
protocolParameters,
output.metadata.included.slot
)
const unixTimestampPreviousOutputMetadata = getUnixTimestampFromNodeInfoAndSlotIndex(
protocolParameters,
previousOutput.metadata.included.slot
)
if (unixTimestampOutputMetadata > unixTimestampPreviousOutputMetadata) {
updateNftInAllWalletNfts(walletId, activity.nftId, { isSpendable: false })
}
await generateAndStoreActivitiesForWallet(wallet)
}

if (_isDelegationOutput && wallet?.hasDelegationRewardClaimTransactionInProgress) {
Expand Down

0 comments on commit d98d6c8

Please sign in to comment.