Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update balances when custom token details change #1412

Merged
merged 1 commit into from
Feb 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions main/store/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -623,6 +623,26 @@ module.exports = {

return [...existingTokens, ...tokensToAdd]
})

u('main.balances', (balances) => {
// update the balances for any custom tokens that changed
Object.values(balances).forEach((accountBalances) => {
tokens.forEach((token) => {
const tokenAddress = token.address.toLowerCase()
const matchingBalance = accountBalances.find(
(b) => b.address.toLowerCase() === tokenAddress && b.chainId === token.chainId
)

if (matchingBalance) {
matchingBalance.logoURI = token.logoURI || matchingBalance.logoURI
matchingBalance.symbol = token.symbol || matchingBalance.symbol
matchingBalance.name = token.name || matchingBalance.symbol
}
})
})

return balances
})
},
removeCustomTokens: (u, tokens) => {
u('main.tokens.custom', (existing) => {
Expand Down
45 changes: 42 additions & 3 deletions test/main/store/actions/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,17 @@ describe('#removeBalance', () => {
})

describe('#addCustomTokens', () => {
let tokens = []
let tokens = [],
balances = {}

const updaterFn = (node, update) => {
expect(node).toBe('main.tokens.custom')
if (node === 'main.tokens.custom') {
tokens = update(tokens)
}

tokens = update(tokens)
if (node === 'main.balances') {
balances = update(balances)
}
}

const addTokens = (tokensToAdd) => addCustomTokensAction(updaterFn, tokensToAdd)
Expand All @@ -407,6 +412,40 @@ describe('#addCustomTokens', () => {
expect(tokens[0]).toEqual(testTokens.zrx)
expect(tokens[1].symbol).toBe('BAD')
})

it('updates an existing balance for a custom token', () => {
const account = '0xd0e3872f5fa8ecb49f1911f605c0da90689a484e'

balances = {
[account]: [
{
address: testTokens.badger.address,
chainId: testTokens.badger.chainId,
symbol: 'BDG',
name: 'Old Badger',
logoURI: 'http://logo.io'
}
]
}

const updatedBadgerToken = {
...testTokens.badger,
symbol: 'BADGER',
name: 'Badger Token'
}

addTokens([updatedBadgerToken])

expect(balances[account]).toStrictEqual([
{
address: testTokens.badger.address,
chainId: testTokens.badger.chainId,
symbol: 'BADGER',
name: 'Badger Token',
logoURI: 'http://logo.io'
}
])
})
})

describe('#removeCustomTokens', () => {
Expand Down