Skip to content

Commit

Permalink
refactor: remove transformTransactionData code repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
iccicci committed Nov 25, 2024
1 parent 6eac431 commit 054635b
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
6 changes: 2 additions & 4 deletions src/methods/get-transaction.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import { prepareMessage } from '../utils/message.js';
import { blockfrostAPI } from '../utils/blockfrost-api.js';
import { transformTransactionData } from '../utils/transaction.js';
import { fetchTransactionData } from '../utils/transaction.js';
import { MessageId } from '../types/message.js';

export default async (id: MessageId, clientId: string, txId: string): Promise<string> => {
const tx = await blockfrostAPI.txs(txId);
const data = await transformTransactionData(tx);
const data = await fetchTransactionData(txId);
const message = prepareMessage({ id, clientId, data });

return message;
Expand Down
14 changes: 6 additions & 8 deletions src/utils/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ export const sortTransactionsCmp = <

const fetchTxWithUtxo = async (txHash: string, address: string) => {
try {
const tx = await blockfrostAPI.txs(txHash);
const txUtxo = await blockfrostAPI.txsUtxos(txHash);
const txData = await transformTransactionData(tx);
const txData = await fetchTransactionData(txHash);
const txUtxos = await transformTransactionUtxo(txUtxo);

return { txData, txUtxos, address, txHash };
Expand Down Expand Up @@ -49,7 +48,7 @@ export const txIdsToTransactions = async (

for (const item of txIdsPerAddress) {
for (const txId of item.txIds) {
promises.push(limiter(() => fetchTxWithUtxo(txId, item.address)));
promises.push(fetchTxWithUtxo(txId, item.address));
}
}

Expand All @@ -71,11 +70,7 @@ export interface GetTransactionsDetails {
export const getTransactionsWithDetails = async (
txs: GetTransactionsDetails[],
): Promise<Pick<TxIdsToTransactionsResponse, 'txData' | 'txUtxos' | 'txCbor'>[]> => {
const txsData = await Promise.all(
txs.map(({ txId }) =>
limiter(() => blockfrostAPI.txs(txId).then(data => transformTransactionData(data))),
),
);
const txsData = await Promise.all(txs.map(({ txId }) => fetchTransactionData(txId)));
const txsUtxo = await Promise.all(
txs.map(({ txId }) =>
limiter(() => blockfrostAPI.txsUtxos(txId).then(data => transformTransactionUtxo(data))),
Expand Down Expand Up @@ -112,6 +107,9 @@ export const transformTransactionData = async (
};
};

export const fetchTransactionData = (txId: string) =>
limiter(() => blockfrostAPI.txs(txId)).then(data => transformTransactionData(data));

export const transformTransactionUtxo = async (
utxo: Responses['tx_content_utxo'],
): Promise<Types.TransformedTransactionUtxo> => {
Expand Down

0 comments on commit 054635b

Please sign in to comment.