From efa0593dc992ba09c2f49274c2fd7f73677e7e17 Mon Sep 17 00:00:00 2001 From: Matt Holtzman Date: Fri, 24 Feb 2023 16:55:11 -0500 Subject: [PATCH] allow migration when chains were not previously present, bump version (#1458) --- main/store/migrations/index.js | 136 +++++++++++++---------- package-lock.json | 4 +- package.json | 2 +- test/main/store/migrations/index.test.js | 32 ++++++ 4 files changed, 111 insertions(+), 63 deletions(-) diff --git a/main/store/migrations/index.js b/main/store/migrations/index.js index eef50ba19..d4c00ae95 100644 --- a/main/store/migrations/index.js +++ b/main/store/migrations/index.js @@ -548,51 +548,54 @@ const migrations = { } } - // we removed support for the following goerli RPCs so reset the connections - // to defaults when the user was previously connecting to them - const removedGoerliRPCs = ['mudit', 'slockit', 'prylabs'] - const goerli = initial.main.networks.ethereum[5] - const goerliPrimaryConnection = goerli.connection.primary.current - const goerliSecondaryConnection = goerli.connection.secondary.current - - if (removedGoerliRPCs.includes(goerliPrimaryConnection)) { - initial.main.networks.ethereum[5] = { - ...goerli, - connection: { - ...goerli.connection, - primary: { - on: false, - current: 'custom', - status: 'loading', - connected: false, - type: '', - network: '', - custom: '' + if ('5' in initial.main.networks.ethereum) { + // we removed support for the following goerli RPCs so reset the connections + // to defaults when the user was previously connecting to them + const removedGoerliRPCs = ['mudit', 'slockit', 'prylabs'] + const goerli = initial.main.networks.ethereum[5] + const goerliPrimaryConnection = goerli.connection.primary.current + const goerliSecondaryConnection = goerli.connection.secondary.current + + if (removedGoerliRPCs.includes(goerliPrimaryConnection)) { + initial.main.networks.ethereum[5] = { + ...goerli, + connection: { + ...goerli.connection, + primary: { + on: false, + current: 'custom', + status: 'loading', + connected: false, + type: '', + network: '', + custom: '' + } } } } - } - if (removedGoerliRPCs.includes(goerliSecondaryConnection)) { - initial.main.networks.ethereum[5] = { - ...goerli, - connection: { - ...goerli.connection, - secondary: { - on: false, - current: 'custom', - status: 'loading', - connected: false, - type: '', - network: '', - custom: '' + + if (removedGoerliRPCs.includes(goerliSecondaryConnection)) { + initial.main.networks.ethereum[5] = { + ...goerli, + connection: { + ...goerli.connection, + secondary: { + on: false, + current: 'custom', + status: 'loading', + connected: false, + type: '', + network: '', + custom: '' + } } } } - } - // if neither primary nor secondary is enabled then we switch the overall connection off - initial.main.networks.ethereum[5].connection.on = - goerli.connection.primary.on || goerli.connection.secondary.on + // if neither primary nor secondary is enabled then we switch the overall connection off + initial.main.networks.ethereum[5].connection.on = + goerli.connection.primary.on || goerli.connection.secondary.on + } return initial }, @@ -650,19 +653,24 @@ const migrations = { return initial }, 25: (initial) => { - const optimism = initial.main.networks.ethereum[10] - const removeOptimismConnection = (connection) => ({ - ...connection, - current: connection.current === 'optimism' ? 'infura' : connection.current - }) + // remove Optimism RPC connection presets and use Infura instead + if ('10' in initial.main.networks.ethereum) { + const removeOptimismConnection = (connection) => ({ + ...connection, + current: connection.current === 'optimism' ? 'infura' : connection.current + }) - initial.main.networks.ethereum[10] = { - ...optimism, - connection: { - primary: removeOptimismConnection(optimism.connection.primary), - secondary: removeOptimismConnection(optimism.connection.secondary) + const optimism = initial.main.networks.ethereum[10] + + initial.main.networks.ethereum[10] = { + ...optimism, + connection: { + primary: removeOptimismConnection(optimism.connection.primary), + secondary: removeOptimismConnection(optimism.connection.secondary) + } } } + return initial }, 26: (initial) => { @@ -691,20 +699,28 @@ const migrations = { return initial }, 28: (initial) => { - const networkMeta = initial.main.networksMeta.ethereum - const { - 5: { - nativeCurrency: { symbol: goerliSymbol } - }, - 11155111: { - nativeCurrency: { symbol: sepoliaSymbol } + const getUpdatedSymbol = (symbol, chainId) => { + return parseInt(chainId) === 5 ? 'görETH' : parseInt(chainId) === 11155111 ? 'sepETH' : symbol + } + + const updatedMeta = Object.entries(initial.main.networksMeta.ethereum).map(([id, chainMeta]) => { + const { symbol, decimals } = chainMeta.nativeCurrency + const updatedSymbol = (symbol || '').toLowerCase() !== 'eth' ? symbol : getUpdatedSymbol(symbol, id) + + const updatedChainMeta = { + ...chainMeta, + nativeCurrency: { + ...chainMeta.nativeCurrency, + symbol: updatedSymbol, + decimals: decimals || 18 + } } - } = networkMeta - goerliSymbol === 'ETH' && (initial.main.networksMeta.ethereum[5].nativeCurrency.symbol = 'görETH') - sepoliaSymbol === 'ETH' && (initial.main.networksMeta.ethereum[11155111].nativeCurrency.symbol = 'sepETH') - Object.values(initial.main.networksMeta.ethereum).forEach((metadata) => { - metadata.nativeCurrency.decimals = metadata.nativeCurrency.decimals || 18 + + return [id, updatedChainMeta] }) + + initial.main.networksMeta.ethereum = Object.fromEntries(updatedMeta) + return initial }, 29: (initial) => { diff --git a/package-lock.json b/package-lock.json index 0a94edee7..ce2abd1ab 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "frame", - "version": "0.6.0", + "version": "0.6.1", "lockfileVersion": 2, "requires": true, "packages": { "": { "name": "frame", - "version": "0.6.0", + "version": "0.6.1", "hasInstallScript": true, "license": "GPL-3.0", "dependencies": { diff --git a/package.json b/package.json index a59f6516b..86285b864 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "frame", - "version": "0.6.0", + "version": "0.6.1", "description": "System-wide web3", "main": "compiled/main", "scripts": { diff --git a/test/main/store/migrations/index.test.js b/test/main/store/migrations/index.test.js index e5f3a3155..8c9cff6eb 100644 --- a/test/main/store/migrations/index.test.js +++ b/test/main/store/migrations/index.test.js @@ -785,6 +785,14 @@ describe('migration 21', () => { expect(goerli.on).toBe(false) }) + + it('takes no action on goerli if the chain is not present', () => { + delete state.main.networks.ethereum[5] + + const updatedState = migrations.apply(state, 21) + + expect(Object.keys(updatedState.main.networks.ethereum)).toEqual(['11155111']) + }) }) describe('migration 22', () => { @@ -967,6 +975,14 @@ describe('migration 25', () => { expect(optimism.connection[priority].current).toBe('custom') }) + + it('takes no action if no Optimism chain is present', () => { + delete state.main.networks.ethereum[10] + + const updatedState = migrations.apply(state, 25) + + expect(updatedState.main.networks).toStrictEqual({ ethereum: {} }) + }) }) }) @@ -1100,6 +1116,22 @@ describe('migration 28', () => { const metadata = updatedState.main.networksMeta.ethereum expect(metadata[5].nativeCurrency.decimals).toBe(18) }) + + it('takes no action on Goerli if the chain is not present', () => { + delete state.main.networksMeta.ethereum[5] + + const updatedState = migrations.apply(state, 28) + + expect(Object.keys(updatedState.main.networksMeta.ethereum)).toEqual(['11155111']) + }) + + it('takes no action on Sepolia if the chain is not present', () => { + delete state.main.networksMeta.ethereum[11155111] + + const updatedState = migrations.apply(state, 28) + + expect(Object.keys(updatedState.main.networksMeta.ethereum)).toEqual(['5']) + }) }) describe('migration 29', () => {