From 1f5d4b7ef1d41a1ef3e27b370f71a6c747b55db1 Mon Sep 17 00:00:00 2001 From: katspaugh <381895+katspaugh@users.noreply.github.com> Date: Mon, 13 Dec 2021 12:49:00 +0100 Subject: [PATCH] Fix: legacy storage keys (#3170) * Fix: legacy storage keys * Use CHAIN_ID --- src/logic/safe/utils/mocks/remoteConfig.json | 105 ++++++++++--------- src/logic/safe/utils/safeStorage.ts | 4 +- src/utils/storage/__tests__/index.test.ts | 13 +++ src/utils/storage/index.ts | 24 ++++- 4 files changed, 89 insertions(+), 57 deletions(-) create mode 100644 src/utils/storage/__tests__/index.test.ts diff --git a/src/logic/safe/utils/mocks/remoteConfig.json b/src/logic/safe/utils/mocks/remoteConfig.json index b5373cff7d..8226002560 100644 --- a/src/logic/safe/utils/mocks/remoteConfig.json +++ b/src/logic/safe/utils/mocks/remoteConfig.json @@ -65,119 +65,119 @@ ] }, { - "transactionService": "https://safe-transaction-polygon.staging.gnosisdev.com", - "chainId": "137", - "chainName": "Polygon", - "shortName": "matic", + "transactionService": "https://safe-transaction.avalanche.gnosis.io", + "chainId": "43114", + "chainName": "Avalanche", + "shortName": "Avalanche", "l2": true, - "description": "Test", + "description": "", "rpcUri": { - "authentication": "API_KEY_PATH", - "value": "https://polygon-mainnet.infura.io/v3/" + "authentication": "NO_AUTHENTICATION", + "value": "https://api.avax.network/ext/bc/C/rpc" }, "safeAppsRpcUri": { - "authentication": "API_KEY_PATH", - "value": "https://polygon-mainnet.infura.io/v3/" + "authentication": "NO_AUTHENTICATION", + "value": "https://api.avax.network/ext/bc/C/rpc" }, "publicRpcUri": { - "authentication": "API_KEY_PATH", - "value": "https://polygon-mainnet.infura.io/v3/" + "authentication": "NO_AUTHENTICATION", + "value": "https://api.avax.network/ext/bc/C/rpc" }, "blockExplorerUriTemplate": { - "address": "https://polygonscan.com/address/{{address}}", - "txHash": "https://polygonscan.com/tx/{{txHash}}", - "api": "https://api.polygonscan.com/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" + "address": "https://snowtrace.io/address/{{address}}", + "txHash": "https://snowtrace.io/tx/{{txHash}}", + "api": "https://api.snowtrace.io/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" }, "nativeCurrency": { - "name": "Matic", - "symbol": "MATIC", + "name": "Avalanche", + "symbol": "AVAX", "decimals": 18, - "logoUri": "https://safe-transaction-assets.staging.gnosisdev.com/chains/137/currency_logo.png" + "logoUri": "https://safe-transaction-assets.staging.gnosisdev.com/chains/43114/currency_logo.png" }, "theme": { "textColor": "#ffffff", - "backgroundColor": "#8248E5" + "backgroundColor": "#e84142" }, - "gasPrice": [ - { - "type": "ORACLE", - "uri": "https://gasstation-mainnet.matic.network", - "gasParameter": "standard", - "gweiFactor": "1000000000.000000000" - } - ], + "gasPrice": [], "disabledWallets": [ "authereum", - "coinbase", "fortmatic", "keystone", "lattice", - "ledger", "opera", "operaTouch", "portis", "torus", "trezor", - "trust", - "walletLink" + "trust" ], "features": [ "CONTRACT_INTERACTION", + "DOMAIN_LOOKUP", "ERC721", "SAFE_APPS", "SAFE_TX_GAS_OPTIONAL" ] }, { - "transactionService": "https://safe-transaction.avalanche.gnosis.io", - "chainId": "43114", - "chainName": "Avalanche", - "shortName": "Avalanche", + "transactionService": "https://safe-transaction-polygon.staging.gnosisdev.com", + "chainId": "137", + "chainName": "Polygon", + "shortName": "matic", "l2": true, - "description": "", + "description": "Test", "rpcUri": { - "authentication": "NO_AUTHENTICATION", - "value": "https://api.avax.network/ext/bc/C/rpc" + "authentication": "API_KEY_PATH", + "value": "https://polygon-mainnet.infura.io/v3/" }, "safeAppsRpcUri": { - "authentication": "NO_AUTHENTICATION", - "value": "https://api.avax.network/ext/bc/C/rpc" + "authentication": "API_KEY_PATH", + "value": "https://polygon-mainnet.infura.io/v3/" }, "publicRpcUri": { "authentication": "NO_AUTHENTICATION", - "value": "https://api.avax.network/ext/bc/C/rpc" + "value": "https://polygon-rpc.com/" }, "blockExplorerUriTemplate": { - "address": "https://snowtrace.io/address/{{address}}", - "txHash": "https://snowtrace.io/tx/{{txHash}}", - "api": "https://api.snowtrace.io/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" + "address": "https://polygonscan.com/address/{{address}}", + "txHash": "https://polygonscan.com/tx/{{txHash}}", + "api": "https://api.polygonscan.com/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" }, "nativeCurrency": { - "name": "Avalanche", - "symbol": "AVAX", + "name": "Matic", + "symbol": "MATIC", "decimals": 18, - "logoUri": "https://safe-transaction-assets.staging.gnosisdev.com/chains/43114/currency_logo.png" + "logoUri": "https://safe-transaction-assets.staging.gnosisdev.com/chains/137/currency_logo.png" }, "theme": { "textColor": "#ffffff", - "backgroundColor": "#333333" + "backgroundColor": "#8248E5" }, - "gasPrice": [], + "gasPrice": [ + { + "type": "ORACLE", + "uri": "https://gasstation-mainnet.matic.network", + "gasParameter": "standard", + "gweiFactor": "1000000000.000000000" + } + ], "disabledWallets": [ "authereum", + "coinbase", "fortmatic", "keystone", "lattice", + "ledger", "opera", "operaTouch", "portis", "torus", "trezor", - "trust" + "trust", + "walletLink" ], "features": [ "CONTRACT_INTERACTION", - "EIP1559", "ERC721", "SAFE_APPS", "SAFE_TX_GAS_OPTIONAL" @@ -205,7 +205,7 @@ "blockExplorerUriTemplate": { "address": "https://optimistic.etherscan.io/address/{{address}}", "txHash": "https://optimistic.etherscan.io/tx/{{txHash}}", - "api": "https://api.optimistic.etherscan.io/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" + "api": "https://api-optimistic.etherscan.io/api?module={{module}}&action={{action}}&address={{address}}&apiKey={{apiKey}}" }, "nativeCurrency": { "name": "Ether", @@ -234,6 +234,9 @@ "walletLink" ], "features": [ + "CONTRACT_INTERACTION", + "ERC721", + "SAFE_APPS", "SAFE_TX_GAS_OPTIONAL" ] }, diff --git a/src/logic/safe/utils/safeStorage.ts b/src/logic/safe/utils/safeStorage.ts index 20641d9bc2..708c730d35 100644 --- a/src/logic/safe/utils/safeStorage.ts +++ b/src/logic/safe/utils/safeStorage.ts @@ -1,6 +1,5 @@ import { getStoragePrefix, loadFromStorage, saveToStorage } from 'src/utils/storage' import { SafeRecordProps } from 'src/logic/safe/store/models/safe' -import { getChainById } from 'src/config' import { ChainId } from 'src/config/chain.d' export const SAFES_KEY = 'SAFES' @@ -12,8 +11,7 @@ export const loadStoredSafes = (): Promise => { } export const loadStoredNetworkSafeById = (id: ChainId): Promise => { - const chain = getChainById(id) - return loadFromStorage(SAFES_KEY, getStoragePrefix(chain.chainName)) + return loadFromStorage(SAFES_KEY, getStoragePrefix(id)) } export const saveSafes = async (safes: StoredSafes): Promise => { diff --git a/src/utils/storage/__tests__/index.test.ts b/src/utils/storage/__tests__/index.test.ts new file mode 100644 index 0000000000..7080533b16 --- /dev/null +++ b/src/utils/storage/__tests__/index.test.ts @@ -0,0 +1,13 @@ +import { getStoragePrefix } from '..' + +describe('getStoragePrefix', () => { + it('saves a stringified value', () => { + expect(getStoragePrefix('137')).toBe('_immortal|v2_POLYGON__') + expect(getStoragePrefix('4')).toBe('_immortal|v2_RINKEBY__') + expect(getStoragePrefix('1')).toBe('_immortal|v2_MAINNET__') + expect(getStoragePrefix('100')).toBe('_immortal|v2_XDAI__') + expect(getStoragePrefix('246')).toBe('_immortal|v2_ENERGY_WEB_CHAIN__') + expect(getStoragePrefix('9358392457')).toBe('_immortal|v2_9358392457__') + expect(getStoragePrefix()).toBe('_immortal|v2_RINKEBY__') + }) +}) diff --git a/src/utils/storage/index.ts b/src/utils/storage/index.ts index 2f083be08e..656108332d 100644 --- a/src/utils/storage/index.ts +++ b/src/utils/storage/index.ts @@ -1,10 +1,28 @@ -import { getChainName } from 'src/config' +import { _getChainId } from 'src/config' +import { ChainId, CHAIN_ID } from 'src/config/chain.d' import Storage from './Storage' +// Legacy storage keys. New chains will use the chain id as prefix. +// @TODO: migrate them to chain ids. +const STORAGE_KEYS: Record = { + [CHAIN_ID.ETHEREUM]: 'MAINNET', + [CHAIN_ID.RINKEBY]: 'RINKEBY', + [CHAIN_ID.BSC]: 'BSC', + [CHAIN_ID.XDAI]: 'XDAI', + [CHAIN_ID.POLYGON]: 'POLYGON', + [CHAIN_ID.ENERGY_WEB_CHAIN]: 'ENERGY_WEB_CHAIN', + [CHAIN_ID.ARBITRUM]: 'ARBITRUM', + [CHAIN_ID.VOLTA]: 'VOLTA', +} + export const storage = new Storage(window.localStorage, '') -// We need this to update on run time depending on selected network name -export const getStoragePrefix = (chainName = getChainName()): string => `_immortal|v2_${chainName}__` +export const getStoragePrefix = (id = _getChainId()): string => { + const name = STORAGE_KEYS[id] || id + // Legacy ImmortalDB prefix + // @TODO: migrate it + return `_immortal|v2_${name}__` +} export const loadFromStorage = async ( key: string,