diff --git a/app/scripts/controllers/incoming-transactions.js b/app/scripts/controllers/incoming-transactions.js index 7e8aa87d0..fadc1aaa4 100644 --- a/app/scripts/controllers/incoming-transactions.js +++ b/app/scripts/controllers/incoming-transactions.js @@ -240,7 +240,7 @@ export default class IncomingTransactionsController { return { blockNumber: txMeta.blockNumber, id: createId(), - metamaskNetworkId: currentNetworkID, + metamaskNetworkId: currentNetworkID.toString(), status, time, txParams: { diff --git a/app/scripts/migrations/047.js b/app/scripts/migrations/047.js new file mode 100644 index 000000000..daece3b49 --- /dev/null +++ b/app/scripts/migrations/047.js @@ -0,0 +1,30 @@ +const version = 47 +import { cloneDeep } from 'lodash' + +/** + * Stringify numbers: + * - PreferencesController.frequentRpcListDetail item chainId + * - TransactionsController.transactions item metamaskNetworkId + */ +export default { + version, + migrate: async function (originalVersionedData) { + const versionedData = cloneDeep(originalVersionedData) + versionedData.meta.version = version + const state = versionedData.data + versionedData.data = transformState(state) + return versionedData + }, +} + +function transformState (state) { + const transactions = state?.TransactionsController?.transactions + if (Array.isArray(transactions)) { + transactions.forEach((transaction) => { + if (typeof transaction.metamaskNetworkId === 'number') { + transaction.metamaskNetworkId = transaction.metamaskNetworkId.toString() + } + }) + } + return state +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index a5bf0206b..d90c5dc86 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -57,6 +57,7 @@ const migrations = [ require('./044').default, require('./045').default, require('./046').default, + require('./047').default, ] export default migrations diff --git a/test/unit/migrations/047-test.js b/test/unit/migrations/047-test.js new file mode 100644 index 000000000..31b1db77c --- /dev/null +++ b/test/unit/migrations/047-test.js @@ -0,0 +1,109 @@ +import { strict as assert } from 'assert' +import migration47 from '../../../app/scripts/migrations/047' + +describe('migration #47', function () { + it('should update the version metadata', async function () { + const oldStorage = { + 'meta': { + 'version': 46, + }, + 'data': {}, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(newStorage.meta, { + 'version': 47, + }) + }) + + it('should stringify transactions metamaskNetworkId values', async function () { + const oldStorage = { + meta: {}, + data: { + TransactionsController: { + transactions: [ + { foo: 'bar', metamaskNetworkId: 2 }, + { foo: 'bar' }, + { foo: 'bar', metamaskNetworkId: 0 }, + { foo: 'bar', metamaskNetworkId: 42 }, + ], + }, + foo: 'bar', + }, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(newStorage.data, { + TransactionsController: { + transactions: [ + { foo: 'bar', metamaskNetworkId: '2' }, + { foo: 'bar' }, + { foo: 'bar', metamaskNetworkId: '0' }, + { foo: 'bar', metamaskNetworkId: '42' }, + ], + }, + foo: 'bar', + }) + }) + + it('should do nothing if transactions metamaskNetworkId values are already strings', async function () { + const oldStorage = { + meta: {}, + data: { + TransactionsController: { + transactions: [ + { foo: 'bar', metamaskNetworkId: '2' }, + { foo: 'bar' }, + { foo: 'bar', metamaskNetworkId: '0' }, + { foo: 'bar', metamaskNetworkId: '42' }, + ], + }, + foo: 'bar', + }, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(oldStorage.data, newStorage.data) + }) + + it('should do nothing if transactions state does not exist', async function () { + const oldStorage = { + meta: {}, + data: { + TransactionsController: { + bar: 'baz', + }, + foo: 'bar', + }, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(oldStorage.data, newStorage.data) + }) + + it('should do nothing if transactions state is empty', async function () { + const oldStorage = { + meta: {}, + data: { + TransactionsController: { + transactions: [], + bar: 'baz', + }, + foo: 'bar', + }, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(oldStorage.data, newStorage.data) + }) + + it('should do nothing if state is empty', async function () { + const oldStorage = { + meta: {}, + data: {}, + } + + const newStorage = await migration47.migrate(oldStorage) + assert.deepEqual(oldStorage.data, newStorage.data) + }) +})