From 9d77761e7da49270b2e294039220d50b70a01407 Mon Sep 17 00:00:00 2001 From: "razvan.tomegea" Date: Wed, 4 Dec 2024 15:30:42 +0200 Subject: [PATCH] Added getTransactionsByHashes.ts --- .../transactions/getTransactionsByHashes.ts | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 src/apiCalls/transactions/getTransactionsByHashes.ts diff --git a/src/apiCalls/transactions/getTransactionsByHashes.ts b/src/apiCalls/transactions/getTransactionsByHashes.ts new file mode 100644 index 0000000..ed5856e --- /dev/null +++ b/src/apiCalls/transactions/getTransactionsByHashes.ts @@ -0,0 +1,44 @@ +import axios from 'axios'; +import { TRANSACTIONS_ENDPOINT } from 'apiCalls/endpoints'; + +import { networkSelector } from 'store/selectors'; +import { getState } from 'store/store'; +import { + GetTransactionsByHashesReturnType, + PendingTransactionsType +} from 'types/transactions.types'; + +export const getTransactionsByHashes = async ( + pendingTransactions: PendingTransactionsType +): Promise => { + const { apiAddress } = networkSelector(getState()); + const hashes = pendingTransactions.map((tx) => tx.hash); + + const { data: responseData } = await axios.get( + `${apiAddress}/${TRANSACTIONS_ENDPOINT}`, + { + params: { + hashes: hashes.join(','), + withScResults: true + } + } + ); + + return pendingTransactions.map(({ hash, previousStatus }) => { + const txOnNetwork = responseData.find( + (txResponse: any) => txResponse?.txHash === hash + ); + + return { + hash, + data: txOnNetwork?.data, + invalidTransaction: txOnNetwork == null, + status: txOnNetwork?.status, + results: txOnNetwork?.results, + sender: txOnNetwork?.sender, + receiver: txOnNetwork?.receiver, + previousStatus, + hasStatusChanged: txOnNetwork && txOnNetwork.status !== previousStatus + }; + }); +};