Skip to content
This repository has been archived by the owner on Feb 2, 2024. It is now read-only.

[Batch Viewer] Avoid multiple requests to Tenderly API #128

Merged
merged 5 commits into from
Jun 22, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 33 additions & 36 deletions src/hooks/useTxBatchTrades.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -39,51 +39,48 @@ export function useTxBatchTrades(
const [accounts, setAccounts] = useState<Accounts>()
const [erc20Addresses, setErc20Addresses] = useState<string[]>([])
const { value: valueErc20s, isLoading: areErc20Loading } = useMultipleErc20({ networkId, addresses: erc20Addresses })
const ordersFoundInTx = orders?.length

const _fetchTxTrades = useCallback(
async (network: Network, _txHash: string): Promise<void> => {
setIsLoading(true)
setError('')
const _fetchTxTrades = useCallback(async (network: Network, _txHash: string, orders: Order[]): Promise<void> => {
setIsLoading(true)
setError('')

try {
const { transfers, trades } = await getTradesAndTransfers(network, _txHash)
const _accounts = Object.fromEntries(await getTradesAccount(network, _txHash, trades, transfers))
const orderIds = orders?.map((order) => order.owner) || []
const transfersWithKind: Transfer[] = transfers.reduce(
(acc, transfer) =>
!orderIds.includes(transfer.from) && !orderIds.includes(transfer.to) ? [...acc, transfer] : acc,
[],
)
try {
const { transfers, trades } = await getTradesAndTransfers(network, _txHash)
const _accounts = Object.fromEntries(await getTradesAccount(network, _txHash, trades, transfers))
const orderIds = orders.map((order) => order.owner) || []
const transfersWithKind: Transfer[] = transfers.reduce(
(acc, transfer) =>
!orderIds.includes(transfer.from) && !orderIds.includes(transfer.to) ? [...acc, transfer] : acc,
[],
)

orders?.forEach((order) => {
const { owner, kind } = order
transfersWithKind.push(
...transfers.filter((t) => [t.from, t.to].includes(owner)).map((transfer) => ({ ...transfer, kind })),
)
})
orders.forEach((order) => {
const { owner, kind } = order
transfersWithKind.push(
...transfers.filter((t) => [t.from, t.to].includes(owner)).map((transfer) => ({ ...transfer, kind })),
)
})

setErc20Addresses(transfers.map((transfer: Transfer): string => transfer.token))
setTxBatchTrades({ trades, transfers: transfersWithKind })
setAccounts(_accounts)
} catch (e) {
const msg = `Failed to fetch tx batch trades`
console.error(msg, e)
setError(msg)
} finally {
setIsLoading(false)
}
},
[orders],
)
setErc20Addresses(transfers.map((transfer: Transfer): string => transfer.token))
setTxBatchTrades({ trades, transfers: transfersWithKind })
setAccounts(_accounts)
} catch (e) {
const msg = `Failed to fetch tx batch trades`
console.error(msg, e)
setError(msg)
} finally {
setIsLoading(false)
}
}, [])

useEffect(() => {
if (!networkId || !ordersFoundInTx) {
if (!networkId || !orders?.length) {
return
}

_fetchTxTrades(networkId, txHash)
}, [_fetchTxTrades, networkId, ordersFoundInTx, txHash])
_fetchTxTrades(networkId, txHash, orders)
// eslint-disable-next-line react-hooks/exhaustive-deps
matextrem marked this conversation as resolved.
Show resolved Hide resolved
}, [_fetchTxTrades, networkId, txHash, orders?.length])

return {
txSettlement: { ...txBatchTrades, tokens: valueErc20s, accounts },
Expand Down