Skip to content

Commit

Permalink
refactor complete order mapping
Browse files Browse the repository at this point in the history
  • Loading branch information
SorinC6 committed Oct 11, 2023
1 parent de0f245 commit 956eb54
Show file tree
Hide file tree
Showing 4 changed files with 466 additions and 473 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {useFocusEffect} from '@react-navigation/native'
import {Swap} from '@yoroi/types'
import BigNumber from 'bignumber.js'
import _ from 'lodash'
import {capitalize} from 'lodash'
import React from 'react'
Expand All @@ -19,22 +20,59 @@ import {
import {useMetrics} from '../../../../../metrics/metricsManager'
import {useSelectedWallet} from '../../../../../SelectedWallet'
import {COLORS} from '../../../../../theme'
import {useTransactionInfos} from '../../../../../yoroi-wallets/hooks'
import {TransactionInfo} from '../../../../../yoroi-wallets/types'
import {NETWORK_CONFIG} from '../../../../../yoroi-wallets/cardano/constants/mainnet/constants'
import {YoroiWallet} from '../../../../../yoroi-wallets/cardano/types'
import {useTokenInfo, useTransactionInfos} from '../../../../../yoroi-wallets/hooks'
import {TransactionInfo, TxMetadataInfo} from '../../../../../yoroi-wallets/types'
import {asQuantity, Quantities} from '../../../../../yoroi-wallets/utils'
import {Counter} from '../../../common/Counter/Counter'
import {PoolIcon} from '../../../common/PoolIcon/PoolIcon'
import {useStrings} from '../../../common/strings'
import {mapCompletedOrders} from './mapOrders'
import {MappedCompleteOrder, MAX_DECIMALS} from './mapOrders'

const findCompletedOrderTx = (transactions: TransactionInfo[]): TransactionInfo[] => {
const findCompletedOrderTx = (transactions: TransactionInfo[], wallet: YoroiWallet): MappedCompleteOrder[] => {
const sentTransactions = transactions.filter((tx) => tx.direction === 'SENT')
const receivedTransactions = transactions.filter((tx) => tx.direction === 'RECEIVED')

const filteredTx = sentTransactions.filter((sentTx) => {
return receivedTransactions.filter((receivedTx) => {
return sentTx.id === receivedTx.inputs[1]?.id?.slice(0, -1)
const filteredTx = sentTransactions.reduce((acc, sentTx) => {
const result: {id?: string; metadata?: TxMetadataInfo; date?: string} = {}
receivedTransactions.forEach((receivedTx) => {
receivedTx.inputs.forEach((input) => {
if (Boolean(input.id) && input?.id?.slice(0, -1) === sentTx?.id) {
result['id'] = sentTx?.id
result['metadata'] = sentTx?.metadata
result['date'] = receivedTx?.lastUpdatedAt
}
})
})
})

if (result['id'] !== undefined && result['metadata'] !== undefined) {
const metadata = JSON.parse(result.metadata as string)

const buyTokenInfo = useTokenInfo({wallet, tokenId: metadata.buyTokenId})
const sellTokenInfo = useTokenInfo({wallet, tokenId: metadata.sellTokenId})
const formattedBuyQuantity = Quantities.format(metadata.buyQuantity, buyTokenInfo.decimals ?? 0)
const formattedSellQuantity = Quantities.format(metadata.sellQuantity, sellTokenInfo.decimals ?? 0)
const tokenPrice = asQuantity(new BigNumber(metadata.sellQuantity).dividedBy(metadata.buyQuantity).toString())

const mappedResult: MappedCompleteOrder = {
id: result.id,
date: result?.date ?? '',
provider: metadata.provider,
sellLabel: sellTokenInfo?.ticker ?? sellTokenInfo?.name ?? '-',
sellQuantity: formattedSellQuantity,
sellTokenId: metadata.sellTokenId,
buyLabel: buyTokenInfo?.ticker ?? buyTokenInfo?.name ?? '-',
buyQuantity: formattedBuyQuantity,
buyTokenId: metadata.buyTokenId,
txLink: NETWORK_CONFIG.EXPLORER_URL_FOR_TX(metadata.buyTokenId),
tokenPrice: Quantities.format(tokenPrice, sellTokenInfo.decimals ?? 0, MAX_DECIMALS),
}
return acc.concat(mappedResult)
}
return acc
}, [] as Array<MappedCompleteOrder>)

return filteredTx
}

Expand All @@ -45,7 +83,7 @@ export const CompletedOrders = () => {

const transactionsInfos = useTransactionInfos(wallet)

const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos))
const completeOrders = findCompletedOrderTx(Object.values(transactionsInfos), wallet)

const intl = useIntl()

Expand All @@ -57,12 +95,10 @@ export const CompletedOrders = () => {
}, [track]),
)

const normalizedOrders = mapCompletedOrders(completeOrders, wallet)

return (
<>
<ScrollView style={styles.container}>
{normalizedOrders?.map((order) => {
{completeOrders?.map((order) => {
const id = order.id
const expanded = id === hiddenInfoOpenId
const sellIcon = <TokenIcon wallet={wallet} tokenId={order.sellTokenId} variant="swap" />
Expand Down Expand Up @@ -102,11 +138,7 @@ export const CompletedOrders = () => {
})}
</ScrollView>

<Counter
style={styles.counter}
counter={normalizedOrders?.length ?? 0}
customText={strings.listCompletedOrders}
/>
<Counter style={styles.counter} counter={completeOrders?.length ?? 0} customText={strings.listCompletedOrders} />
</>
)
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
import {isString} from '@yoroi/common'
import {isNonNullable} from '@yoroi/common'
import {getPoolUrlByProvider} from '@yoroi/swap'
import {Balance, Swap} from '@yoroi/types'
import BigNumber from 'bignumber.js'

import {NumberLocale} from '../../../../../i18n/languages'
import {NETWORK_CONFIG} from '../../../../../yoroi-wallets/cardano/constants/mainnet/constants'
import {YoroiWallet} from '../../../../../yoroi-wallets/cardano/types'
import {useTokenInfo} from '../../../../../yoroi-wallets/hooks'
import {TransactionInfo} from '../../../../../yoroi-wallets/types'
import {asQuantity, Quantities} from '../../../../../yoroi-wallets/utils'
import {Quantities} from '../../../../../yoroi-wallets/utils'

const MAX_DECIMALS = 10
export const MAX_DECIMALS = 10

export type MappedCompleteOrder = {
id: string
Expand Down Expand Up @@ -47,57 +44,6 @@ export type MappedOpenOrder = {
to: Balance.Amount
}

export const mapCompletedOrders = (orders: TransactionInfo[], wallet: YoroiWallet): Array<MappedCompleteOrder> => {
if (orders.length === 0) return []
const result = orders
.map((order) => {
console.log('order', order)
let metadata: {
buyTokenId: string
sellTokenId: string
sellQuantity: Balance.Quantity
buyQuantity: Balance.Quantity
provider: Swap.SupportedProvider
}

try {
metadata = JSON.parse(order.metadata as string)
const buyTokenInfo = useTokenInfo({wallet, tokenId: metadata.buyTokenId})
const sellTokenInfo = useTokenInfo({wallet, tokenId: metadata.sellTokenId})

const buyLabel = buyTokenInfo?.ticker ?? buyTokenInfo?.name ?? '-'
const sellLabel = sellTokenInfo?.ticker ?? sellTokenInfo?.name ?? '-'

const txLink = NETWORK_CONFIG.EXPLORER_URL_FOR_TX(order.id)
const tokenPrice = asQuantity(new BigNumber(metadata.sellQuantity).dividedBy(metadata.buyQuantity).toString())

const formattedBuyQuantity = Quantities.format(metadata.buyQuantity, buyTokenInfo.decimals ?? 0)
const formattedSellQuantity = Quantities.format(metadata.sellQuantity, sellTokenInfo.decimals ?? 0)

return {
id: order.id,
provider: metadata.provider,
date: order?.lastUpdatedAt,
sellLabel,
sellQuantity: formattedSellQuantity,
sellTokenId: metadata.sellTokenId,
buyLabel,
buyQuantity: formattedBuyQuantity,
buyTokenId: metadata.buyTokenId,
txLink,
tokenPrice: Quantities.format(tokenPrice, sellTokenInfo.decimals ?? 0, MAX_DECIMALS),
}
} catch (error) {
console.error('Error parsing JSON: ', error)
return null
}
})
.filter(isNonNullable)
.sort((a, b) => (a.date > b.date ? -1 : 1))

return result
}

export const mapOpenOrders = (
orders: Array<Swap.OpenOrder | Swap.CompletedOrder>,
tokenInfos: Balance.TokenInfo[],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,74 +4,74 @@
"defaultMessage": "!!!Select token",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 135,
"line": 134,
"column": 15,
"index": 4171
"index": 4170
},
"end": {
"line": 138,
"line": 137,
"column": 3,
"index": 4254
"index": 4253
}
},
{
"id": "swap.swapScreen.currentBalance",
"defaultMessage": "!!!Current Balance",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 139,
"line": 138,
"column": 18,
"index": 4274
"index": 4273
},
"end": {
"line": 142,
"line": 141,
"column": 3,
"index": 4363
"index": 4362
}
},
{
"id": "swap.swapScreen.notEnoughBalance",
"defaultMessage": "!!!Not enough balance",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 143,
"line": 142,
"column": 20,
"index": 4385
"index": 4384
},
"end": {
"line": 146,
"line": 145,
"column": 3,
"index": 4479
"index": 4478
}
},
{
"id": "swap.swapScreen.notEnoughSupply",
"defaultMessage": "!!!Not enough supply in the pool",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 147,
"line": 146,
"column": 19,
"index": 4500
"index": 4499
},
"end": {
"line": 150,
"line": 149,
"column": 3,
"index": 4604
"index": 4603
}
},
{
"id": "swap.swapScreen.noPool",
"defaultMessage": "!!! This pair is not available in any liquidity pool",
"file": "src/features/Swap/common/AmountCard/AmountCard.tsx",
"start": {
"line": 151,
"line": 150,
"column": 10,
"index": 4616
"index": 4615
},
"end": {
"line": 154,
"line": 153,
"column": 3,
"index": 4731
"index": 4730
}
}
]
Loading

0 comments on commit 956eb54

Please sign in to comment.