All files / src check_transactions.ts

100% Statements 22/22
100% Branches 2/2
100% Functions 7/7
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104                                  7x 7x 7x 7x                           7x         106x 1788x 809x   106x 1820x     106x     7x         35x   35x         35x           35x           7x         12x   12x         12x         12x           12x            
import { Transaction } from './transaction_pool';
 
export type CheckerFunction = (
	transactions: ReadonlyArray<Transaction>,
) => Promise<CheckerFunctionResponse>;
 
export interface CheckerFunctionResponse {
	status: Status;
	transactionsResponses: ReadonlyArray<TransactionResponse>;
}
 
export interface TransactionResponse {
	readonly errors: ReadonlyArray<Error>;
	readonly id: string;
	readonly status: Status;
}
 
export enum Status {
	FAIL = 0,
	OK = 1,
	PENDING = 2,
}
 
export interface CheckTransactionsResponseWithPassAndFail {
	failedTransactions: ReadonlyArray<Transaction>;
	passedTransactions: ReadonlyArray<Transaction>;
}
 
export interface CheckTransactionsResponseWithPassFailAndPending {
	failedTransactions: ReadonlyArray<Transaction>;
	passedTransactions: ReadonlyArray<Transaction>;
	pendingTransactions: ReadonlyArray<Transaction>;
}
 
const getTransactionByStatus = (
	transactions: ReadonlyArray<Transaction>,
	responses: ReadonlyArray<TransactionResponse>,
	status: Status,
): ReadonlyArray<Transaction> => {
	const transactionIdsByStatus = responses
		.filter(transactionResponse => transactionResponse.status === status)
		.map(transactionStatus => transactionStatus.id);
 
	const transactionsByStatus = transactions.filter(transaction =>
		transactionIdsByStatus.includes(transaction.id),
	);
 
	return transactionsByStatus;
};
 
export const checkTransactionsWithPassAndFail = async (
	transactions: ReadonlyArray<Transaction>,
	checkerFunction: CheckerFunction,
): Promise<CheckTransactionsResponseWithPassAndFail> => {
	// Process transactions and check their validity
	const { transactionsResponses } = await checkerFunction(transactions);
 
	const failedTransactions = getTransactionByStatus(
		transactions,
		transactionsResponses,
		Status.FAIL,
	);
	const passedTransactions = getTransactionByStatus(
		transactions,
		transactionsResponses,
		Status.OK,
	);
 
	return {
		failedTransactions,
		passedTransactions,
	};
};
 
export const checkTransactionsWithPassFailAndPending = async (
	transactions: ReadonlyArray<Transaction>,
	checkerFunction: CheckerFunction,
): Promise<CheckTransactionsResponseWithPassFailAndPending> => {
	// Process transactions and check their validity
	const { transactionsResponses } = await checkerFunction(transactions);
 
	const failedTransactions = getTransactionByStatus(
		transactions,
		transactionsResponses,
		Status.FAIL,
	);
	const passedTransactions = getTransactionByStatus(
		transactions,
		transactionsResponses,
		Status.OK,
	);
	const pendingTransactions = getTransactionByStatus(
		transactions,
		transactionsResponses,
		Status.PENDING,
	);
 
	return {
		failedTransactions,
		passedTransactions,
		pendingTransactions,
	};
};