-
Notifications
You must be signed in to change notification settings - Fork 5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixing incorrectly typed token decimal attribute
- Loading branch information
Showing
4 changed files
with
176 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { cloneDeep } from 'lodash'; | ||
|
||
const version = 54; | ||
|
||
/** | ||
* Migrates preference tokens with 0 decimals typed as 'string' to 'number' | ||
*/ | ||
export default { | ||
version, | ||
async migrate(originalVersionedData) { | ||
const versionedData = cloneDeep(originalVersionedData); | ||
versionedData.meta.version = version; | ||
const state = versionedData.data; | ||
const newState = transformState(state); | ||
versionedData.data = newState; | ||
return versionedData; | ||
}, | ||
}; | ||
|
||
function transformState(state) { | ||
const newState = state; | ||
|
||
if (newState.PreferencesController) { | ||
const { tokens } = newState.PreferencesController; | ||
if (Array.isArray(tokens)) { | ||
for (const token of tokens) { | ||
if (token.decimals === '0') { | ||
token.decimals = Number('0'); | ||
} | ||
} | ||
} | ||
newState.PreferencesController.tokens = tokens; | ||
} | ||
|
||
return newState; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
import { strict as assert } from 'assert'; | ||
import migration54 from './054'; | ||
|
||
describe('migration #54', function () { | ||
it('should update the version metadata', async function () { | ||
const oldStorage = { | ||
meta: { | ||
version: 53, | ||
}, | ||
data: {}, | ||
}; | ||
|
||
const newStorage = await migration54.migrate(oldStorage); | ||
assert.deepEqual(newStorage.meta, { | ||
version: 54, | ||
}); | ||
}); | ||
|
||
it('should retype instance of 0 decimal values to numbers', async function () { | ||
const oldStorage = { | ||
meta: {}, | ||
data: { | ||
PreferencesController: { | ||
tokens: [ | ||
{ | ||
address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', | ||
decimals: '0', | ||
symbol: 'CK', | ||
}, | ||
{ | ||
address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', | ||
decimals: 18, | ||
symbol: 'BAT', | ||
}, | ||
{ | ||
address: '0x514910771af9ca656af840dff83e8264ecf986ca', | ||
decimals: 18, | ||
symbol: 'LINK', | ||
}, | ||
{ | ||
address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', | ||
decimals: '0', | ||
symbol: 'SOR', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migration54.migrate(oldStorage); | ||
assert.deepEqual(newStorage.data, { | ||
PreferencesController: { | ||
tokens: [ | ||
{ | ||
address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', | ||
decimals: 0, | ||
symbol: 'CK', | ||
}, | ||
{ | ||
address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', | ||
decimals: 18, | ||
symbol: 'BAT', | ||
}, | ||
{ | ||
address: '0x514910771af9ca656af840dff83e8264ecf986ca', | ||
decimals: 18, | ||
symbol: 'LINK', | ||
}, | ||
{ | ||
address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', | ||
decimals: 0, | ||
symbol: 'SOR', | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
|
||
it('should do nothing if all decimal value typings are correct', async function () { | ||
const oldStorage = { | ||
meta: {}, | ||
data: { | ||
PreferencesController: { | ||
tokens: [ | ||
{ | ||
address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', | ||
decimals: 0, | ||
symbol: 'CK', | ||
}, | ||
{ | ||
address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', | ||
decimals: 18, | ||
symbol: 'BAT', | ||
}, | ||
{ | ||
address: '0x514910771af9ca656af840dff83e8264ecf986ca', | ||
decimals: 18, | ||
symbol: 'LINK', | ||
}, | ||
{ | ||
address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', | ||
decimals: 0, | ||
symbol: 'SOR', | ||
}, | ||
], | ||
}, | ||
}, | ||
}; | ||
|
||
const newStorage = await migration54.migrate(oldStorage); | ||
assert.deepEqual(newStorage.data, { | ||
PreferencesController: { | ||
tokens: [ | ||
{ | ||
address: '0x06012c8cf97bead5deae237070f9587f8e7a266d', | ||
decimals: 0, | ||
symbol: 'CK', | ||
}, | ||
{ | ||
address: '0x0d8775f648430679a709e98d2b0cb6250d2887ef', | ||
decimals: 18, | ||
symbol: 'BAT', | ||
}, | ||
{ | ||
address: '0x514910771af9ca656af840dff83e8264ecf986ca', | ||
decimals: 18, | ||
symbol: 'LINK', | ||
}, | ||
{ | ||
address: '0x629a673a8242c2ac4b7b8c5d8735fbeac21a6205', | ||
decimals: 0, | ||
symbol: 'SOR', | ||
}, | ||
], | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters