Skip to content

Commit

Permalink
handle failed eth txs
Browse files Browse the repository at this point in the history
  • Loading branch information
Strernd committed Apr 6, 2021
1 parent 7bea670 commit bda852f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
24 changes: 21 additions & 3 deletions src/implementations/eth/handlers/tx/createTransaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { logger } from '../../../../log';

export async function createTransaction(
transactionData: AssetTransactionData[],
): Promise<{ status: CreateTransactionStatus; partialTx?: object; tosign?: string[] }> {
): Promise<{ status: CreateTransactionStatus; partialTx?: object; tosign?: string[]; failedTxIdxs?: number[] }> {
try {
const gasPrice = await eth.getGasPrice();
if (!gasPrice) {
Expand All @@ -19,9 +19,27 @@ export async function createTransaction(
return { status: 'ERROR' };
}
const transactions = await Promise.all(
transactionData.map((data, i) => createSingleTransaction(data, Number(txCount) + i, gasPrice)),
transactionData.map((data, i) =>
createSingleTransaction(data, Number(txCount) + i, gasPrice).catch((error) => {
if (error === 'INSUFFICIENT_AMOUNT') {
return { status: 'INSUFFICIENT_FUNDS', isError: true };
}
return { status: 'ERROR', isError: true };
}),
),
);
return { status: 'OK', partialTx: transactions.map((x) => x.partialTx), tosign: transactions.map((x) => x.tosign) };
const successTxs = transactions.filter((x) => !x.hasOwnProperty('isError')) as {
partialTx: utils.UnsignedTransaction;
tosign: string;
}[];
const failedTxIdxs: number[] = [];
transactions.forEach((x, idx) => x.hasOwnProperty('isError') && failedTxIdxs.push(idx));
return {
status: 'OK',
partialTx: successTxs.map((x) => x.partialTx),
tosign: successTxs.map((x) => x.tosign),
failedTxIdxs,
};
} catch (error) {
logger.warn('Error in create transaction', { error });
if (error === 'INSUFFICIENT_AMOUNT') {
Expand Down
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export type CreateTransactionResponse = {
status: CreateTransactionStatus;
partialTx?: PartialTransaction;
tosign?: string[];
failedTxIdxs?: number[];
};
export interface TransactionServiceHandlers {
createTransaction: (transactionData: AssetTransactionData[], symbol?: string) => Promise<CreateTransactionResponse>;
Expand Down

0 comments on commit bda852f

Please sign in to comment.