1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/app/scripts/migrations/039.js
Whymarrh Whitby 86b165ea83
Add migration notification for users with Sai (#7450)
Maker has upgraded its Dai token to "Multi-Collateral Dai" (MCD) and requires
all users interacting with Dai migrate their tokens to the new version. Dai
now exclusively refers to Multi-Collateral Dai and what was previouly called
Dai is now Sai (Single Collateral Dai).

In this description, Sai refers to what was (prior to the 2019-11-18) known as Dai.
Dai is the _new_ token.

This changeset:

1. Only affects users who had non-zero Sai at the old contract address
2. Displays a persistent notification for users with Sai
3. Updates the token symbol for users already tracking the Sai token
4. Bumps our direct and indirect eth-contract-metadata dependencies

The notification copy:

> A message from Maker: The new Multi-Collateral Dai token has been released. Your old tokens are now called Sai. Please upgrade your Sai tokens to the new Dai.

The copy is from the Maker team.
2019-11-18 18:16:28 -03:30

68 lines
2.0 KiB
JavaScript

const version = 39
const clone = require('clone')
const ethUtil = require('ethereumjs-util')
const DAI_V1_CONTRACT_ADDRESS = '0x89d24A6b4CcB1B6fAA2625fE562bDD9a23260359'
const DAI_V1_TOKEN_SYMBOL = 'DAI'
const SAI_TOKEN_SYMBOL = 'SAI'
function isOldDai (token = {}) {
return token && typeof token === 'object' &&
token.symbol === DAI_V1_TOKEN_SYMBOL &&
ethUtil.toChecksumAddress(token.address) === DAI_V1_CONTRACT_ADDRESS
}
/**
* This migration renames the Dai token to Sai.
*
* As of 2019-11-18 Dai is now called Sai (refs https://git.io/JeooP) to facilitate
* Maker's upgrade to Multi-Collateral Dai and this migration renames the token
* at the old address.
*/
module.exports = {
version,
migrate: async function (originalVersionedData) {
const versionedData = clone(originalVersionedData)
versionedData.meta.version = version
const state = versionedData.data
versionedData.data = transformState(state)
return versionedData
},
}
function transformState (state) {
const { PreferencesController } = state
if (PreferencesController) {
const tokens = PreferencesController.tokens || []
if (Array.isArray(tokens)) {
for (const token of tokens) {
if (isOldDai(token)) {
token.symbol = SAI_TOKEN_SYMBOL
}
}
}
const accountTokens = PreferencesController.accountTokens || {}
if (accountTokens && typeof accountTokens === 'object') {
for (const address of Object.keys(accountTokens)) {
const networkTokens = accountTokens[address]
if (networkTokens && typeof networkTokens === 'object') {
for (const network of Object.keys(networkTokens)) {
const tokensOnNetwork = networkTokens[network]
if (Array.isArray(tokensOnNetwork)) {
for (const token of tokensOnNetwork) {
if (isOldDai(token)) {
token.symbol = SAI_TOKEN_SYMBOL
}
}
}
}
}
}
}
}
return state
}