Skip to content

Commit

Permalink
fix(TXL-207): tx validation and log async errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dbrans committed Aug 12, 2024
1 parent fbf2afa commit 0aca7a1
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
30 changes: 20 additions & 10 deletions packages/transaction-controller/src/TransactionController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1107,11 +1107,15 @@ export class TransactionController extends BaseController<
},
);

// Ensure transaction parameters are still valid before adding metadata to state.
validateTxParams(addedTransactionMeta.txParams, isEIP1559Compatible);
this.addMetadata(addedTransactionMeta);

if (requireApproval !== false) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.#updateSimulationData(addedTransactionMeta);
this.#updateSimulationData(addedTransactionMeta).catch((error) => {
log('Error while updating simulation data', error);
throw error;
});
} else {
log('Skipping simulation as approval not required');
}
Expand Down Expand Up @@ -1694,8 +1698,10 @@ export class TransactionController extends BaseController<
this.onTransactionStatusChange(updatedTransactionMeta);

// Intentional given potential duration of process.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.updatePostBalance(updatedTransactionMeta);
this.updatePostBalance(updatedTransactionMeta).catch((error) => {
log('Error while updating post balance', error);
throw error;
});

this.messagingSystem.publish(
`${controllerName}:transactionConfirmed`,
Expand Down Expand Up @@ -2439,8 +2445,8 @@ export class TransactionController extends BaseController<

private async updateGasProperties(transactionMeta: TransactionMeta) {
const isEIP1559Compatible =
(await this.getEIP1559Compatibility(transactionMeta.networkClientId)) &&
transactionMeta.txParams.type !== TransactionEnvelopeType.legacy;
transactionMeta.txParams.type !== TransactionEnvelopeType.legacy &&
(await this.getEIP1559Compatibility(transactionMeta.networkClientId));

const { networkClientId, chainId } = transactionMeta;

Expand Down Expand Up @@ -3358,8 +3364,10 @@ export class TransactionController extends BaseController<
this.onTransactionStatusChange(transactionMeta);

// Intentional given potential duration of process.
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.updatePostBalance(transactionMeta);
this.updatePostBalance(transactionMeta).catch((error) => {
log('Error while updating post balance', error);
throw error;
});
}

private async updatePostBalance(transactionMeta: TransactionMeta) {
Expand Down Expand Up @@ -3710,8 +3718,10 @@ export class TransactionController extends BaseController<
)
) {
log('Updating simulation data due to transaction parameter update');
// eslint-disable-next-line @typescript-eslint/no-floating-promises
this.#updateSimulationData(transactionMeta);
this.#updateSimulationData(transactionMeta).catch((error) => {
log('Error updating simulation data', error);
throw error;
});
}
}

Expand Down
7 changes: 7 additions & 0 deletions packages/transaction-controller/src/utils/validation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ import { validateTxParams } from './validation';

describe('validation', () => {
describe('validateTxParams', () => {
it('should throw if unknown transaction envelope type is specified', () => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
expect(() => validateTxParams({ type: '0x3' } as any)).toThrow(
rpcErrors.invalidParams('Invalid transaction envelope type: 0x3'),
);
});

it('should throw if no from address', () => {
// TODO: Replace `any` with type
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
20 changes: 20 additions & 0 deletions packages/transaction-controller/src/utils/validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export function validateTxParams(
txParams: TransactionParams,
isEIP1559Compatible = true,
) {
validateEnvelopeType(txParams.type);
validateEIP1559Compatibility(txParams, isEIP1559Compatible);
validateParamFrom(txParams.from);
validateParamRecipient(txParams);
Expand All @@ -64,6 +65,25 @@ export function validateTxParams(
validateGasFeeParams(txParams);
}

/**
* Validates type property, ensuring that if it is specified, it is a valid transaction envelope type.
*
* @param type - The transaction envelope type to validate.
* @throws Throws invalid params if the type is not a valid transaction envelope type.
*/
function validateEnvelopeType(type: string | undefined) {
if (
type &&
!Object.values(TransactionEnvelopeType).includes(
type as TransactionEnvelopeType,
)
) {
throw rpcErrors.invalidParams(
`Invalid transaction envelope type: "${type}"`,
);
}
}

/**
* Validates EIP-1559 compatibility for transaction creation.
*
Expand Down

0 comments on commit 0aca7a1

Please sign in to comment.