|
| 1 | +const version = 39 |
| 2 | +const clone = require('clone') |
| 3 | +const ethUtil = require('ethereumjs-util') |
| 4 | + |
| 5 | +const DAI_V1_CONTRACT_ADDRESS = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359' |
| 6 | +const DAI_V1_TOKEN_SYMBOL = 'DAI' |
| 7 | +const SAI_TOKEN_SYMBOL = 'SAI' |
| 8 | + |
| 9 | +function isOldDai (token = {}) { |
| 10 | + return token && typeof token === 'object' && |
| 11 | + token.symbol === DAI_V1_TOKEN_SYMBOL && |
| 12 | + ethUtil.toChecksumAddress(token.address) === DAI_V1_CONTRACT_ADDRESS |
| 13 | +} |
| 14 | + |
| 15 | +/** |
| 16 | + * This migration renames the Dai token to Sai. |
| 17 | + * |
| 18 | + * As of 2019-11-18 Dai is now called Sai (refs https://git.io/JeooP) to facilitate |
| 19 | + * Maker's upgrade to Multi-Collateral Dai and this migration renames the token |
| 20 | + * at the old address. |
| 21 | + */ |
| 22 | +module.exports = { |
| 23 | + version, |
| 24 | + migrate: async function (originalVersionedData) { |
| 25 | + const versionedData = clone(originalVersionedData) |
| 26 | + versionedData.meta.version = version |
| 27 | + const state = versionedData.data |
| 28 | + versionedData.data = transformState(state) |
| 29 | + return versionedData |
| 30 | + }, |
| 31 | +} |
| 32 | + |
| 33 | +function transformState (state) { |
| 34 | + const { PreferencesController } = state |
| 35 | + |
| 36 | + if (PreferencesController) { |
| 37 | + const tokens = PreferencesController.tokens || [] |
| 38 | + if (Array.isArray(tokens)) { |
| 39 | + for (const token of tokens) { |
| 40 | + if (isOldDai(token)) { |
| 41 | + token.symbol = SAI_TOKEN_SYMBOL |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + const accountTokens = PreferencesController.accountTokens || {} |
| 47 | + if (accountTokens && typeof accountTokens === 'object') { |
| 48 | + for (const address of Object.keys(accountTokens)) { |
| 49 | + const networkTokens = accountTokens[address] |
| 50 | + if (networkTokens && typeof networkTokens === 'object') { |
| 51 | + for (const network of Object.keys(networkTokens)) { |
| 52 | + const tokensOnNetwork = networkTokens[network] |
| 53 | + if (Array.isArray(tokensOnNetwork)) { |
| 54 | + for (const token of tokensOnNetwork) { |
| 55 | + if (isOldDai(token)) { |
| 56 | + token.symbol = SAI_TOKEN_SYMBOL |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + } |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return state |
| 67 | +} |
0 commit comments