Skip to content

Commit 92c2923

Browse files
authored
update balances when custom token details change (#1412)
1 parent 52d25f1 commit 92c2923

File tree

2 files changed

+62
-3
lines changed

2 files changed

+62
-3
lines changed

main/store/actions/index.js

+20
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,26 @@ module.exports = {
623623

624624
return [...existingTokens, ...tokensToAdd]
625625
})
626+
627+
u('main.balances', (balances) => {
628+
// update the balances for any custom tokens that changed
629+
Object.values(balances).forEach((accountBalances) => {
630+
tokens.forEach((token) => {
631+
const tokenAddress = token.address.toLowerCase()
632+
const matchingBalance = accountBalances.find(
633+
(b) => b.address.toLowerCase() === tokenAddress && b.chainId === token.chainId
634+
)
635+
636+
if (matchingBalance) {
637+
matchingBalance.logoURI = token.logoURI || matchingBalance.logoURI
638+
matchingBalance.symbol = token.symbol || matchingBalance.symbol
639+
matchingBalance.name = token.name || matchingBalance.symbol
640+
}
641+
})
642+
})
643+
644+
return balances
645+
})
626646
},
627647
removeCustomTokens: (u, tokens) => {
628648
u('main.tokens.custom', (existing) => {

test/main/store/actions/index.test.js

+42-3
Original file line numberDiff line numberDiff line change
@@ -375,12 +375,17 @@ describe('#removeBalance', () => {
375375
})
376376

377377
describe('#addCustomTokens', () => {
378-
let tokens = []
378+
let tokens = [],
379+
balances = {}
379380

380381
const updaterFn = (node, update) => {
381-
expect(node).toBe('main.tokens.custom')
382+
if (node === 'main.tokens.custom') {
383+
tokens = update(tokens)
384+
}
382385

383-
tokens = update(tokens)
386+
if (node === 'main.balances') {
387+
balances = update(balances)
388+
}
384389
}
385390

386391
const addTokens = (tokensToAdd) => addCustomTokensAction(updaterFn, tokensToAdd)
@@ -407,6 +412,40 @@ describe('#addCustomTokens', () => {
407412
expect(tokens[0]).toEqual(testTokens.zrx)
408413
expect(tokens[1].symbol).toBe('BAD')
409414
})
415+
416+
it('updates an existing balance for a custom token', () => {
417+
const account = '0xd0e3872f5fa8ecb49f1911f605c0da90689a484e'
418+
419+
balances = {
420+
[account]: [
421+
{
422+
address: testTokens.badger.address,
423+
chainId: testTokens.badger.chainId,
424+
symbol: 'BDG',
425+
name: 'Old Badger',
426+
logoURI: 'http://logo.io'
427+
}
428+
]
429+
}
430+
431+
const updatedBadgerToken = {
432+
...testTokens.badger,
433+
symbol: 'BADGER',
434+
name: 'Badger Token'
435+
}
436+
437+
addTokens([updatedBadgerToken])
438+
439+
expect(balances[account]).toStrictEqual([
440+
{
441+
address: testTokens.badger.address,
442+
chainId: testTokens.badger.chainId,
443+
symbol: 'BADGER',
444+
name: 'Badger Token',
445+
logoURI: 'http://logo.io'
446+
}
447+
])
448+
})
410449
})
411450

412451
describe('#removeCustomTokens', () => {

0 commit comments

Comments
 (0)