Skip to content

Commit

Permalink
Rollback actionId changes
Browse files Browse the repository at this point in the history
  • Loading branch information
OGPoyraz committed Oct 16, 2023
1 parent ef89596 commit 1cee089
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 68 deletions.
21 changes: 14 additions & 7 deletions app/scripts/controllers/transactions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -1244,9 +1244,10 @@ export default class TransactionController extends EventEmitter {
*
* @param {number} txId - the tx's Id
* @param {string} rawTx - the hex string of the serialized signed transaction
* @param {string} actionId - actionId passed from UI
* @returns {Promise<void>}
*/
async _publishTransaction(txId, rawTx) {
async _publishTransaction(txId, rawTx, actionId) {
const txMeta = this.txStateManager.getTransaction(txId);
txMeta.rawTx = rawTx;
if (txMeta.type === TransactionType.swap) {
Expand Down Expand Up @@ -1274,6 +1275,7 @@ export default class TransactionController extends EventEmitter {

this.emit('transaction-submitted', {
transactionMeta: txMeta,
actionId,
});
}

Expand Down Expand Up @@ -1798,13 +1800,14 @@ export default class TransactionController extends EventEmitter {
await this._publishTransaction(txId, rawTx, actionId);
this.emit('transaction-approved', {
transactionMeta: txMeta,
actionId,
});
// must set transaction to submitted/failed before releasing lock
nonceLock.releaseLock();
} catch (err) {
// this is try-catch wrapped so that we can guarantee that the nonceLock is released
try {
this._failTransaction(txId, err);
this._failTransaction(txId, err, actionId);
} catch (err2) {
log.error(err2);
}
Expand All @@ -1823,13 +1826,15 @@ export default class TransactionController extends EventEmitter {
* Convenience method for the ui thats sets the transaction to rejected
*
* @param {number} txId - the tx's Id
* @param {string} actionId - actionId passed from UI
* @returns {Promise<void>}
*/
async _cancelTransaction(txId) {
async _cancelTransaction(txId, actionId) {
const txMeta = this.txStateManager.getTransaction(txId);
this.txStateManager.setTxStatusRejected(txId);
this.emit('transaction-rejected', {
transactionMeta: txMeta,
actionId,
});
}

Expand Down Expand Up @@ -2192,12 +2197,13 @@ export default class TransactionController extends EventEmitter {
return gasValuesInGwei;
}

_failTransaction(txId, error) {
_failTransaction(txId, error, actionId) {
this.txStateManager.setTxStatusFailed(txId, error);
const txMeta = this.txStateManager.getTransaction(txId);
this.emit('transaction-finalized', {
transactionMeta: txMeta,
actionId,
error: error.message,
transactionMeta: txMeta,
});
}

Expand All @@ -2220,6 +2226,7 @@ export default class TransactionController extends EventEmitter {
this.emit(`${txMeta.id}:unapproved`, txMeta);
this.emit('transaction-added', {
transactionMeta: txMeta,
actionId: txMeta.actionId,
});
}

Expand Down Expand Up @@ -2300,9 +2307,9 @@ export default class TransactionController extends EventEmitter {

if (transaction && !this._isTransactionCompleted(transaction)) {
if (error.code === errorCodes.provider.userRejectedRequest) {
await this._cancelTransaction(txId);
await this._cancelTransaction(txId, actionId);
} else {
this._failTransaction(txId, error);
this._failTransaction(txId, error, actionId);
}
}

Expand Down
8 changes: 7 additions & 1 deletion app/scripts/lib/transaction-metrics.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,6 @@ describe('Transaction metrics', () => {
securityProviderResponse: {
flagAsDangerous: 0,
},
actionId: mockActionId,
};

jest.clearAllMocks();
Expand All @@ -101,6 +100,7 @@ describe('Transaction metrics', () => {
it('should create event fragment', async () => {
await handleTransactionAdded(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta as any,
actionId: mockActionId,
});

expect(mockTransactionMetricsRequest.createEventFragment).toBeCalledTimes(
Expand Down Expand Up @@ -164,6 +164,7 @@ describe('Transaction metrics', () => {
it('should create, update, finalize event fragment', async () => {
await handleTransactionApproved(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta as any,
actionId: mockActionId,
});

const expectedUniqueId = 'transaction-added-1';
Expand Down Expand Up @@ -258,6 +259,7 @@ describe('Transaction metrics', () => {

await handleTransactionFinalized(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta,
actionId: mockActionId,
} as any);

const expectedUniqueId = 'transaction-submitted-1';
Expand Down Expand Up @@ -332,6 +334,7 @@ describe('Transaction metrics', () => {

await handleTransactionFinalized(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta,
actionId: mockActionId,
error: mockErrorMessage,
} as any);

Expand Down Expand Up @@ -418,6 +421,7 @@ describe('Transaction metrics', () => {
it('should create, update, finalize event fragment', async () => {
await handleTransactionDropped(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta,
actionId: mockActionId,
} as any);

const expectedUniqueId = 'transaction-submitted-1';
Expand Down Expand Up @@ -503,6 +507,7 @@ describe('Transaction metrics', () => {
it('should create, update, finalize event fragment', async () => {
await handleTransactionRejected(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta,
actionId: mockActionId,
} as any);

const expectedUniqueId = 'transaction-added-1';
Expand Down Expand Up @@ -587,6 +592,7 @@ describe('Transaction metrics', () => {
it('should only create event fragment', async () => {
await handleTransactionSubmitted(mockTransactionMetricsRequest, {
transactionMeta: mockTransactionMeta as any,
actionId: mockActionId,
});

expect(mockTransactionMetricsRequest.createEventFragment).toBeCalledTimes(
Expand Down
Loading

0 comments on commit 1cee089

Please sign in to comment.