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

add ability to remove tokens #655

Merged
merged 3 commits into from
Dec 2, 2021
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
1 change: 0 additions & 1 deletion app/App/Panel/Notify/AddToken/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React from 'react'
import Restore from 'react-restore'
import svg from '../../../../../resources/svg'
import link from '../../../../../resources/link'

class AddToken extends React.Component {
Expand Down
5 changes: 4 additions & 1 deletion app/App/Panel/Notify/CustomTokens/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import React from 'react'
import Restore from 'react-restore'
import svg from '../../../../../resources/svg'
import link from '../../../../../resources/link'

class CustomTokens extends React.Component {
constructor (props, context) {
Expand Down Expand Up @@ -69,7 +70,9 @@ class CustomTokens extends React.Component {
{token.decimals}
</div>
</div>
<div className='customTokensListItemRemoveButton'>
<div className='customTokensListItemRemoveButton'
onClick={() => link.send('tray:removeToken', token)}
>
{'Remove Token'}
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions main/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,10 @@ ipcMain.on('tray:addToken', (e, token, req) => {
accounts.resolveRequest(req)
})

ipcMain.on('tray:removeToken', (e, token) => {
if (token) store.removeCustomTokens([token])
})

ipcMain.on('tray:adjustNonce', (e, handlerId, nonceAdjust) => {
accounts.adjustNonce(handlerId, nonceAdjust)
})
Expand Down
18 changes: 12 additions & 6 deletions main/store/actions/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import log from 'electron-log'
import BigNumber from 'bignumber.js'

const panelActions = require('./panel')

Expand All @@ -20,6 +19,12 @@ function validateNetworkSettings (network) {
return networkId
}

function includesToken (tokens, token) {
const existingAddress = token.address.toLowerCase()
return tokens.some(t =>
t.address.toLowerCase() === existingAddress && t.chainId === token.chainId
)
}

module.exports = {
...panelActions,
Expand Down Expand Up @@ -475,15 +480,16 @@ module.exports = {
u('main.tokens', existing => {
// remove any tokens that have been overwritten by one with
// the same address and chain ID
const existingTokens = existing.filter(token => {
return !tokens.some(t =>
t.address === token.address && t.chainId === token.chainId
)
})
const existingTokens = existing.filter(token => !includesToken(tokens, token))

return [...existingTokens, ...tokens]
})
},
removeCustomTokens: (u, tokens) => {
u('main.tokens', existing => {
return existing.filter(token => !includesToken(tokens, token))
})
},
setColorway: (u, colorway) => {
u('main.colorway', () => {
return colorway
Expand Down
160 changes: 130 additions & 30 deletions test/main/store/actions/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import log from 'electron-log'
import {
addNetwork as addNetworkAction,
setBalances as setBalancesAction,
addCustomTokens as addTokensAction,
removeCustomTokens as removeTokensAction,
setScanning as setScanningAction
} from '../../../../main/store/actions'

Expand All @@ -17,6 +19,21 @@ afterAll(() => {

const owner = '0xa8be0f701d0f37088600164e71bffc0ad652c251'

const testTokens = {
zrx: {
chainId: 1,
address: '0xe41d2489571d322189246dafa5ebde1f4699f498',
symbol: 'ZRX',
decimals: 18
},
badger: {
chainId: 42161,
address: '0xbfa641051ba0a0ad1b0acf549a89536a0d76472e',
symbol: 'BADGER',
decimals: 18
}
}

describe('#addNetwork', () => {
const polygonNetwork = {
id: 137,
Expand Down Expand Up @@ -218,20 +235,6 @@ describe('#addNetwork', () => {
})

describe('#setBalances', () => {
const zrxToken = {
chainId: 1,
address: '0xe41d2489571d322189246dafa5ebde1f4699f498',
symbol: 'ZRX',
decimals: 18
}

const badgerDaoToken = {
chainId: 42161,
address: '0xbfa641051ba0a0ad1b0acf549a89536a0d76472e',
symbol: 'BADGER',
decimals: 18
}

const updaterFn = (node, netId, address, update) => {
expect(node).toBe('main.balances')
expect(netId).toBe(1)
Expand All @@ -246,66 +249,163 @@ describe('#setBalances', () => {

beforeEach(() => {
balances = {
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(30.5)
}
}
})

it('adds a new balance', () => {
setBalances({
[zrxToken.address]: {
...zrxToken,
[testTokens.zrx.address]: {
...testTokens.zrx,
balance: new BigNumber(7983.2332)
}
})

expect(balances).toStrictEqual({
[zrxToken.address]: {
...zrxToken,
[testTokens.zrx.address]: {
...testTokens.zrx,
balance: new BigNumber(7983.2332)
},
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(30.5)
}
})
})

it('updates an existing balance to a positive amount', () => {
setBalances({
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(41.9)
}
})

expect(balances).toStrictEqual({
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(41.9)
}
})
})

it('updates an existing balance to zero', () => {
setBalances({
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(0)
}
})

expect(balances).toStrictEqual({
[badgerDaoToken.address]: {
...badgerDaoToken,
[testTokens.badger.address]: {
...testTokens.badger,
balance: new BigNumber(0)
}
})
})
})

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

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

tokens = update(tokens)
}

const addTokens = tokensToAdd => addTokensAction(updaterFn, tokensToAdd)

it('adds a token', () => {
tokens = [testTokens.zrx]

addTokens([testTokens.badger])

expect(tokens).toStrictEqual([testTokens.zrx, testTokens.badger])
})

it('overwrites a token', () => {
tokens = [testTokens.zrx, testTokens.badger]

const updatedBadgerToken = {
...testTokens.badger,
symbol: 'BAD'
}

addTokens([updatedBadgerToken])

expect(tokens).toHaveLength(2)
expect(tokens[0]).toEqual(testTokens.zrx)
expect(tokens[1].symbol).toBe('BAD')
})
})

describe('#removeCustomTokens', () => {
let tokens = []

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

tokens = update(tokens)
}

const removeTokens = tokensToRemove => removeTokensAction(updaterFn, tokensToRemove)

it('removes a token', () => {
tokens = [testTokens.zrx, testTokens.badger]

const tokenToRemove = { ...testTokens.zrx }

removeTokens([tokenToRemove])

expect(tokens).toStrictEqual([testTokens.badger])
})

it('does not modify tokens if they cannot be found', () => {
tokens = [testTokens.zrx, testTokens.badger]

const tokenToRemove = {
chainId: 1,
address: '0x383518188c0c6d7730d91b2c03a03c837814a899',
symbol: 'OHM'
}

removeTokens([tokenToRemove])

expect(tokens).toStrictEqual([testTokens.zrx, testTokens.badger])
})

it('does not remove a token with the same address but different chain id', () => {
const tokenToRemove = {
...testTokens.badger,
chainId: 1
}

tokens = [testTokens.zrx, testTokens.badger, tokenToRemove]

removeTokens([tokenToRemove])

expect(tokens).toStrictEqual([testTokens.zrx, testTokens.badger])
})

it('does not remove a token with the same chain id but different address', () => {
const tokenToRemove = {
...testTokens.zrx,
address: '0xa7a82dd06901f29ab14af63faf3358ad101724a8'
}

tokens = [testTokens.zrx, testTokens.badger, tokenToRemove]

removeTokens([tokenToRemove])

expect(tokens).toStrictEqual([testTokens.zrx, testTokens.badger])
})
})

describe('#setScanning', () => {
let isScanning

Expand Down