Skip to content

Commit

Permalink
fix(wallet-mobile): Normalise primary token id when parsing orders (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeljscript authored Sep 4, 2024
1 parent d811ddd commit 7392539
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
17 changes: 15 additions & 2 deletions apps/wallet-mobile/src/features/Swap/common/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {createTypeGuardFromSchema, parseSafe} from '@yoroi/common'
import {useTheme} from '@yoroi/theme'
import {Balance} from '@yoroi/types'
import {SwapApi} from '@yoroi/types/src/swap/api'
import {freeze} from 'immer'
import {useMutation, UseMutationOptions} from 'react-query'
import {z} from 'zod'

Expand Down Expand Up @@ -60,11 +61,23 @@ const isOrderTxMetadata = createTypeGuardFromSchema(OrderTxMetadataSchema)
* Parses and validates a JSON metadata string, transforming it into a structure compliant with MappedRawOrder['metadata'].
*
* @param metadataJson - The JSON string representation of metadata.
* @param primaryTokenId - The primary token ID to use when the metadata specifies a '.' or empty string.
* @returns The parsed metadata object or null if parsing fails or validation fails.
*/
export const parseOrderTxMetadata = (metadataJson: string): OrderTxMetadata | null => {
export const parseOrderTxMetadata = (metadataJson: string, primaryTokenId: string): OrderTxMetadata | null => {
const parsedMetadata = parseSafe(metadataJson)
return isOrderTxMetadata(parsedMetadata) ? parsedMetadata : null
if (!isOrderTxMetadata(parsedMetadata)) return null

return {
...parsedMetadata,
buyTokenId: normalisePrimaryTokenId(parsedMetadata.buyTokenId, primaryTokenId),
sellTokenId: normalisePrimaryTokenId(parsedMetadata.sellTokenId, primaryTokenId),
}
}

const swapPtTokenIds = freeze(['', '.'])
const normalisePrimaryTokenId = (tokenId: string, primaryTokenId: string) => {
return swapPtTokenIds.includes(tokenId) ? primaryTokenId : tokenId
}

function containsOnlyValidChars(str?: string): boolean {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ const compareByDate = (a: MappedRawOrder, b: MappedRawOrder) => {
return new Date(b.date).getTime() - new Date(a.date).getTime()
}

const findCompletedOrderTx = (transactions: TransactionInfo[]): MappedRawOrder[] => {
const findCompletedOrderTx = (transactions: TransactionInfo[], primaryTokenId: string): MappedRawOrder[] => {
const sentTransactions = transactions.filter((tx) => tx.direction === 'SENT')
const receivedTransactions = transactions.filter((tx) => tx.direction === 'RECEIVED')

Expand All @@ -67,7 +67,7 @@ const findCompletedOrderTx = (transactions: TransactionInfo[]): MappedRawOrder[]
if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id && receivedTx.inputs.length > 1) {
result['id'] = sentTx?.id
result['date'] = receivedTx?.lastUpdatedAt
const metadata = parseOrderTxMetadata(sentTx?.metadata?.['674'])
const metadata = parseOrderTxMetadata(sentTx?.metadata?.['674'], primaryTokenId)
if (metadata) {
result['metadata'] = metadata
return acc.push(result as MappedRawOrder)
Expand Down Expand Up @@ -114,7 +114,7 @@ export const CompletedOrders = () => {
}, [])

const transactionsInfos = useTransactionInfos(wallet)
const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos))
const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos), wallet.primaryTokenInfo.id)
const tokenIds = React.useMemo(
() => _.uniq(completeOrders?.flatMap((o) => [o.metadata.sellTokenId, o.metadata.buyTokenId])),
[completeOrders],
Expand Down

0 comments on commit 7392539

Please sign in to comment.