Skip to content

Commit 0998a0e

Browse files
vinistevamMajorLift
authored andcommitted
Rename transactionHash property to hash in the persisted transaction metadata (#1620)
## Explanation Rename the `transactionHash` property to `hash` in the persisted transaction metadata as part of the unification initiative between the core and extension. The following task will take place to handle the migration and adoption of the patch/release. ## References Fixes MetaMask/MetaMask-planning#1077 ## Changelog ### `@metamask/transaction-controller` - **BREAKING**: rename the `transactionHash` property to `hash` in the persisted `TransactionMeta`.
1 parent c76b253 commit 0998a0e

File tree

7 files changed

+38
-55
lines changed

7 files changed

+38
-55
lines changed

packages/transaction-controller/src/EtherscanRemoteTransactionSource.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ const ETHERSCAN_TOKEN_TRANSACTION_RESPONSE_EMPTY_MOCK: EtherscanTransactionRespo
8989
const EXPECTED_NORMALISED_TRANSACTION_BASE = {
9090
blockNumber: ETHERSCAN_TRANSACTION_SUCCESS_MOCK.blockNumber,
9191
chainId: undefined,
92+
hash: ETHERSCAN_TRANSACTION_SUCCESS_MOCK.hash,
9293
id: ID_MOCK,
9394
networkID: undefined,
9495
status: TransactionStatus.confirmed,
@@ -102,7 +103,6 @@ const EXPECTED_NORMALISED_TRANSACTION_BASE = {
102103
to: ETHERSCAN_TRANSACTION_SUCCESS_MOCK.to,
103104
value: '0xb1a2bc2ec50000',
104105
},
105-
transactionHash: ETHERSCAN_TRANSACTION_SUCCESS_MOCK.hash,
106106
verifiedOnBlockchain: false,
107107
};
108108

packages/transaction-controller/src/EtherscanRemoteTransactionSource.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ export class EtherscanRemoteTransactionSource
141141
return {
142142
blockNumber: txMeta.blockNumber,
143143
chainId: currentChainId,
144+
hash: txMeta.hash,
144145
id: random({ msecs: time }),
145146
networkID: currentNetworkId,
146147
status: TransactionStatus.confirmed,
@@ -154,7 +155,6 @@ export class EtherscanRemoteTransactionSource
154155
to: txMeta.to,
155156
value: BNToHex(new BN(txMeta.value)),
156157
},
157-
transactionHash: txMeta.hash,
158158
verifiedOnBlockchain: false,
159159
};
160160
}

packages/transaction-controller/src/IncomingTransactionHelper.test.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,18 +49,18 @@ const CONTROLLER_ARGS_MOCK = {
4949
const TRANSACTION_MOCK: TransactionMeta = {
5050
blockNumber: '123',
5151
chainId: '0x1',
52+
hash: '0x1',
5253
status: TransactionStatus.submitted,
5354
time: 0,
5455
transaction: { to: '0x1', gasUsed: '0x1' },
55-
transactionHash: '0x1',
5656
} as unknown as TransactionMeta;
5757

5858
const TRANSACTION_MOCK_2: TransactionMeta = {
5959
blockNumber: '234',
60+
hash: '0x2',
6061
chainId: '0x1',
6162
time: 1,
6263
transaction: { to: '0x1' },
63-
transactionHash: '0x2',
6464
} as unknown as TransactionMeta;
6565

6666
const createRemoteTransactionSourceMock = (

packages/transaction-controller/src/IncomingTransactionHelper.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -185,10 +185,7 @@ export class IncomingTransactionHelper {
185185
localTxs: TransactionMeta[],
186186
): TransactionMeta[] {
187187
return remoteTxs.filter(
188-
(tx) =>
189-
!localTxs.some(
190-
({ transactionHash }) => transactionHash === tx.transactionHash,
191-
),
188+
(tx) => !localTxs.some(({ hash }) => hash === tx.hash),
192189
);
193190
}
194191

@@ -199,7 +196,7 @@ export class IncomingTransactionHelper {
199196
return remoteTxs.filter((remoteTx) =>
200197
localTxs.some(
201198
(localTx) =>
202-
remoteTx.transactionHash === localTx.transactionHash &&
199+
remoteTx.hash === localTx.hash &&
203200
this.#isTransactionOutdated(remoteTx, localTx),
204201
),
205202
);

packages/transaction-controller/src/TransactionController.test.ts

Lines changed: 22 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,10 @@ jest.mock('@metamask/eth-query', () =>
8484
},
8585
getTransactionByHash: (_hash: string, callback: any) => {
8686
const txs: any = [
87-
{ transactionHash: '1337', blockNumber: '0x1' },
88-
{ transactionHash: '1338', blockNumber: null },
87+
{ blockNumber: '0x1', hash: '1337' },
88+
{ blockNumber: null, hash: '1338' },
8989
];
90-
const tx: any = txs.find(
91-
(element: any) => element.transactionHash === _hash,
92-
);
90+
const tx: any = txs.find((element: any) => element.hash === _hash);
9391
callback(undefined, tx);
9492
},
9593
getTransactionCount: (_from: any, _to: any, callback: any) => {
@@ -101,30 +99,28 @@ jest.mock('@metamask/eth-query', () =>
10199
getTransactionReceipt: (_hash: any, callback: any) => {
102100
const txs: any = [
103101
{
104-
transactionHash: '1337',
102+
blockHash: '1337',
105103
gasUsed: '0x5208',
104+
hash: '1337',
106105
status: '0x1',
107106
transactionIndex: 1337,
108-
blockHash: '1337',
109107
},
110108
{
111-
transactionHash: '1111',
112109
gasUsed: '0x1108',
110+
hash: '1111',
113111
status: '0x0',
114112
transactionIndex: 1111,
115113
},
116114
];
117-
const tx: any = txs.find(
118-
(element: any) => element.transactionHash === _hash,
119-
);
115+
const tx: any = txs.find((element: any) => element.hash === _hash);
120116
callback(undefined, tx);
121117
},
122118
getBlockByHash: (_blockHash: any, callback: any) => {
123119
const blocks: any = [
124120
{
121+
baseFeePerGas: '0x14',
125122
hash: '1337',
126123
number: '0x1',
127-
baseFeePerGas: '0x14',
128124
timestamp: '628dc0c8',
129125
},
130126
{ hash: '1338', number: '0x2' },
@@ -399,21 +395,21 @@ const NONCE_MOCK = 12;
399395
const ACTION_ID_MOCK = '123456';
400396

401397
const TRANSACTION_META_MOCK = {
398+
hash: '0x1',
402399
status: TransactionStatus.confirmed,
400+
time: 123456789,
403401
transaction: {
404402
from: ACCOUNT_MOCK,
405403
},
406-
transactionHash: '0x1',
407-
time: 123456789,
408404
} as TransactionMeta;
409405

410406
const TRANSACTION_META_2_MOCK = {
407+
hash: '0x2',
411408
status: TransactionStatus.confirmed,
409+
time: 987654321,
412410
transaction: {
413411
from: '0x3',
414412
},
415-
transactionHash: '0x2',
416-
time: 987654321,
417413
} as TransactionMeta;
418414

419415
describe('TransactionController', () => {
@@ -1383,10 +1379,10 @@ describe('TransactionController', () => {
13831379

13841380
controller.state.transactions.push({
13851381
from: MOCK_PREFERENCES.state.selectedAddress,
1382+
hash: '1337',
13861383
id: 'foo',
13871384
networkID: '5',
13881385
status: TransactionStatus.submitted,
1389-
transactionHash: '1337',
13901386
} as any);
13911387

13921388
controller.wipeTransactions();
@@ -1462,12 +1458,12 @@ describe('TransactionController', () => {
14621458
const controller = newController();
14631459

14641460
controller.state.transactions.push({
1461+
chainId: toHex(5),
14651462
from: MOCK_PREFERENCES.state.selectedAddress,
1463+
hash: '1337',
14661464
id: 'foo',
14671465
networkID: '5',
1468-
chainId: toHex(5),
14691466
status: TransactionStatus.submitted,
1470-
transactionHash: '1337',
14711467
} as any);
14721468

14731469
controller.state.transactions.push({} as any);
@@ -1489,10 +1485,10 @@ describe('TransactionController', () => {
14891485

14901486
controller.state.transactions.push({
14911487
from: MOCK_PREFERENCES.state.selectedAddress,
1488+
hash: '1337',
14921489
id: 'foo',
14931490
networkID: '5',
14941491
status: TransactionStatus.submitted,
1495-
transactionHash: '1337',
14961492
} as any);
14971493

14981494
controller.state.transactions.push({} as any);
@@ -1515,7 +1511,7 @@ describe('TransactionController', () => {
15151511
id: 'foo',
15161512
networkID: '5',
15171513
status: TransactionStatus.submitted,
1518-
transactionHash: '1338',
1514+
hash: '1338',
15191515
} as any);
15201516

15211517
await controller.queryTransactionStatuses();
@@ -1528,16 +1524,16 @@ describe('TransactionController', () => {
15281524
const controller = newController();
15291525

15301526
controller.state.transactions.push({
1527+
chainId: toHex(5),
15311528
from: MOCK_PREFERENCES.state.selectedAddress,
1529+
hash: '1337',
15321530
id: 'foo',
15331531
networkID: '5',
1534-
chainId: toHex(5),
15351532
status: TransactionStatus.confirmed,
1536-
transactionHash: '1337',
1537-
verifiedOnBlockchain: false,
15381533
transaction: {
15391534
gasUsed: undefined,
15401535
},
1536+
verifiedOnBlockchain: false,
15411537
} as any);
15421538

15431539
await controller.queryTransactionStatuses();
@@ -1785,17 +1781,17 @@ describe('TransactionController', () => {
17851781
it('creates approvals for all unapproved transaction', async () => {
17861782
const transaction = {
17871783
from: ACCOUNT_MOCK,
1784+
hash: '1337',
17881785
id: 'mocked',
17891786
networkID: '5',
17901787
status: TransactionStatus.unapproved,
1791-
transactionHash: '1337',
17921788
};
17931789
const controller = newController();
17941790
controller.state.transactions.push(transaction as any);
17951791
controller.state.transactions.push({
17961792
...transaction,
17971793
id: 'mocked1',
1798-
transactionHash: '1338',
1794+
hash: '1338',
17991795
} as any);
18001796

18011797
controller.initApprovals();

packages/transaction-controller/src/TransactionController.ts

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,13 @@ export class TransactionController extends BaseController<
709709
transactionMeta.transaction.from,
710710
);
711711
const rawTx = bufferToHex(signedTx.serialize());
712-
const transactionHash = await query(this.ethQuery, 'sendRawTransaction', [
713-
rawTx,
714-
]);
712+
const hash = await query(this.ethQuery, 'sendRawTransaction', [rawTx]);
715713
const baseTransactionMeta = {
716714
...transactionMeta,
717715
estimatedBaseFee,
718716
id: random(),
719717
time: Date.now(),
720-
transactionHash,
718+
hash,
721719
actionId,
722720
originalGasEstimate: transactionMeta.transaction.gas,
723721
};
@@ -1031,7 +1029,7 @@ export class TransactionController extends BaseController<
10311029

10321030
case TransactionStatus.submitted:
10331031
resultCallbacks?.success();
1034-
return finalMeta.transactionHash as string;
1032+
return finalMeta.hash as string;
10351033

10361034
default:
10371035
const internalError = ethErrors.rpc.internal(
@@ -1122,10 +1120,8 @@ export class TransactionController extends BaseController<
11221120

11231121
transactionMeta.rawTx = rawTx;
11241122
this.updateTransaction(transactionMeta);
1125-
const transactionHash = await query(this.ethQuery, 'sendRawTransaction', [
1126-
rawTx,
1127-
]);
1128-
transactionMeta.transactionHash = transactionHash;
1123+
const hash = await query(this.ethQuery, 'sendRawTransaction', [rawTx]);
1124+
transactionMeta.hash = hash;
11291125
transactionMeta.status = TransactionStatus.submitted;
11301126
transactionMeta.submittedTime = new Date().getTime();
11311127
this.updateTransaction(transactionMeta);
@@ -1249,11 +1245,11 @@ export class TransactionController extends BaseController<
12491245
private async blockchainTransactionStateReconciler(
12501246
meta: TransactionMeta,
12511247
): Promise<[TransactionMeta, boolean]> {
1252-
const { status, transactionHash } = meta;
1248+
const { status, hash } = meta;
12531249
switch (status) {
12541250
case TransactionStatus.confirmed:
12551251
const txReceipt = await query(this.ethQuery, 'getTransactionReceipt', [
1256-
transactionHash,
1252+
hash,
12571253
]);
12581254

12591255
if (!txReceipt) {
@@ -1283,12 +1279,12 @@ export class TransactionController extends BaseController<
12831279
return [meta, true];
12841280
case TransactionStatus.submitted:
12851281
const txObj = await query(this.ethQuery, 'getTransactionByHash', [
1286-
transactionHash,
1282+
hash,
12871283
]);
12881284

12891285
if (!txObj) {
12901286
const receiptShowsFailedStatus =
1291-
await this.checkTxReceiptStatusIsFailed(transactionHash);
1287+
await this.checkTxReceiptStatusIsFailed(hash);
12921288

12931289
// Case the txObj is evaluated as false, a second check will
12941290
// determine if the tx failed or it is pending or confirmed
@@ -1445,8 +1441,7 @@ export class TransactionController extends BaseController<
14451441
...added,
14461442
...currentTransactions.map((originalTransaction) => {
14471443
const updatedTransaction = updated.find(
1448-
({ transactionHash }) =>
1449-
transactionHash === originalTransaction.transactionHash,
1444+
({ hash }) => hash === originalTransaction.hash,
14501445
);
14511446

14521447
return updatedTransaction ?? originalTransaction;

packages/transaction-controller/src/types.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -118,11 +118,6 @@ type TransactionMetaBase = {
118118
*/
119119
transaction: Transaction;
120120

121-
/**
122-
* Hash of a successful transaction.
123-
*/
124-
transactionHash?: string;
125-
126121
/**
127122
* Additional transfer information.
128123
*/

0 commit comments

Comments
 (0)