-
Notifications
You must be signed in to change notification settings - Fork 5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add verifiedOnBlockchain property to transaction meta #20890
Merged
vinistevam
merged 7 commits into
develop
from
1100-add-verifiedOnBlockchain-property-transaction-meta
Sep 19, 2023
Merged
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
3e8282c
add verifiedOnBlockchain to transaction metadata
vinistevam e1e9827
add migration and tests
vinistevam d258206
Merge branch 'develop' into 1100-add-verifiedOnBlockchain-property-tr…
vinistevam 34c3b8c
Merge branch 'develop' into 1100-add-verifiedOnBlockchain-property-tr…
vinistevam 84ddc22
refactor solution
vinistevam 9a249b0
Merge branch 'develop' into 1100-add-verifiedOnBlockchain-property-tr…
vinistevam 045aa32
Merge branch 'develop' into 1100-add-verifiedOnBlockchain-property-tr…
vinistevam File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
import { migrate, version } from './098'; | ||
|
||
const oldVersion = 97; | ||
describe('migration #98', () => { | ||
it('updates the version metadata', async () => { | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.meta).toStrictEqual({ version }); | ||
}); | ||
|
||
it('handles missing TransactionController', async () => { | ||
const oldState = { | ||
OtherController: {}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles empty transactions', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: {}, | ||
}, | ||
}; | ||
|
||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}); | ||
|
||
expect(transformedState.data).toEqual(oldState); | ||
}); | ||
|
||
it('handles missing state', async () => { | ||
const transformedState = await migrate({ | ||
meta: { version: oldVersion }, | ||
data: {}, | ||
}); | ||
|
||
expect(transformedState.data).toEqual({}); | ||
}); | ||
|
||
it('adds verifiedOnBlockchain in transaction based on the presence of txReceipt', async () => { | ||
const oldState = { | ||
TransactionController: { | ||
transactions: { | ||
tx1: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
}, | ||
tx2: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
otherProp: 'value', | ||
}, | ||
tx3: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
}, | ||
}, | ||
}, | ||
}; | ||
const oldStorage = { | ||
meta: { version: oldVersion }, | ||
data: oldState, | ||
}; | ||
|
||
const newStorage = await migrate(oldStorage); | ||
|
||
expect(newStorage.data).toEqual({ | ||
TransactionController: { | ||
transactions: { | ||
tx1: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
verifiedOnBlockchain: true, | ||
}, | ||
tx2: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
otherProp: 'value', | ||
verifiedOnBlockchain: false, | ||
}, | ||
tx3: { | ||
to: '0x9ef57335bc7d5b6cbc06dca6064a604b75e09ace', | ||
txReceipt: { | ||
blockHash: | ||
'0xafa4e1fd95e429d9c6e6c7c1d282b2bd0bbeb50d0a68743e9392b9c95a06e2eb', | ||
}, | ||
otherProp: 'value', | ||
verifiedOnBlockchain: true, | ||
}, | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { cloneDeep, isEmpty } from 'lodash'; | ||
|
||
type VersionedData = { | ||
meta: { version: number }; | ||
data: Record<string, unknown>; | ||
}; | ||
|
||
export const version = 98; // Increment the version number | ||
|
||
/** | ||
* Add `verifiedOnBlockchain` property to transactions based on the presence of `txReceipt` | ||
* | ||
* @param originalVersionedData - Versioned MetaMask extension state, exactly what we persist to dist. | ||
* @param originalVersionedData.meta - State metadata. | ||
* @param originalVersionedData.meta.version - The current state version. | ||
* @param originalVersionedData.data - The persisted MetaMask state, keyed by controller. | ||
* @returns Updated versioned MetaMask extension state. | ||
*/ | ||
export async function migrate( | ||
originalVersionedData: VersionedData, | ||
): Promise<VersionedData> { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
transformState(versionedData.data); | ||
return versionedData; | ||
} | ||
|
||
function transformState(state: Record<string, any>) { | ||
const transactionControllerState = state?.TransactionController || {}; | ||
const transactions = transactionControllerState?.transactions || {}; | ||
|
||
if (isEmpty(transactions)) { | ||
return; | ||
} | ||
|
||
const newTxs = Object.keys(transactions).reduce((txs, txId) => { | ||
const transaction = transactions[txId]; | ||
|
||
// Add the `verifiedOnBlockchain` property based on the presence of `txReceipt` | ||
transaction.verifiedOnBlockchain = Boolean(transaction.txReceipt); | ||
|
||
return { | ||
...txs, | ||
[txId]: transaction, | ||
}; | ||
}, {}); | ||
|
||
state.TransactionController = { | ||
...transactionControllerState, | ||
transactions: newTxs, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Minor, but is this comment adding much additional detail?