Skip to content
This repository has been archived by the owner on Oct 28, 2022. It is now read-only.

Commit

Permalink
Add: fc by default
Browse files Browse the repository at this point in the history
  • Loading branch information
yqrashawn committed Apr 16, 2020
1 parent 0ada8e7 commit 397a929
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 19 deletions.
5 changes: 3 additions & 2 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import ObservableStore from 'obs-store'
import { addInternalMethodPrefix } from './permissions'
import { normalize as normalizeAddress } from 'cfx-sig-util'
import { isValidContractAddress, keccak, bufferToHex } from 'cfx-util'
import { FC_TOKEN_MAP } from '../lib/fc-helper'

class PreferencesController {
/**
Expand Down Expand Up @@ -32,7 +33,7 @@ class PreferencesController {
currentAccountTab: 'history',
accountTokens: {},
assetImages: {},
tokens: [],
tokens: [FC_TOKEN_MAP],
suggestedTokens: {},
useBlockie: false,
useNonceField: false,
Expand Down Expand Up @@ -764,7 +765,7 @@ class PreferencesController {
accountTokens[selectedAddress] = {}
}
if (!(providerType in accountTokens[selectedAddress])) {
accountTokens[selectedAddress][providerType] = []
accountTokens[selectedAddress][providerType] = [FC_TOKEN_MAP]
}
const tokens = accountTokens[selectedAddress][providerType]
return { tokens, accountTokens, providerType, selectedAddress }
Expand Down
13 changes: 13 additions & 0 deletions app/scripts/lib/fc-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import contractMap from '@yqrashawn/cfx-contract-metadata'

export const FC_TOKEN_ADDRESS = Object.keys(contractMap).find((address) => {
return contractMap[address].symbol === 'FC'
})

export const FC = contractMap[FC_TOKEN_ADDRESS]

export const FC_TOKEN_MAP = {
address: FC_TOKEN_ADDRESS,
decimals: FC.decimals,
symbol: FC.symbol,
}
3 changes: 3 additions & 0 deletions test/unit/app/controllers/detect-tokens-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import ObservableStore from 'obs-store'
import DetectTokensController from '../../../../app/scripts/controllers/detect-tokens'
import NetworkController from '../../../../app/scripts/controllers/network/network'
import PreferencesController from '../../../../app/scripts/controllers/preferences'
import { FC_TOKEN_MAP } from '../../../../app/scripts/lib/fc-helper'

describe('DetectTokensController', function () {
const sandbox = sinon.createSandbox()
Expand Down Expand Up @@ -116,6 +117,7 @@ describe('DetectTokensController', function () {

await controller.detectNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [
FC_TOKEN_MAP,
{
address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4',
decimals: 8,
Expand Down Expand Up @@ -160,6 +162,7 @@ describe('DetectTokensController', function () {

await controller.detectNewTokens()
assert.deepEqual(preferences.store.getState().tokens, [
FC_TOKEN_MAP,
{
address: '0x0d262e5dc4a06a0f1c90ce79c7a60c09dfc884e4',
decimals: 8,
Expand Down
39 changes: 22 additions & 17 deletions test/unit/app/controllers/preferences-controller-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import ObservableStore from 'obs-store'
import PreferencesController from '../../../../app/scripts/controllers/preferences'
import { addInternalMethodPrefix } from '../../../../app/scripts/controllers/permissions'
import sinon from 'sinon'
import { FC_TOKEN_MAP } from '../../../../app/scripts/lib/fc-helper'

describe('preferences controller', function () {
let preferencesController
Expand Down Expand Up @@ -120,7 +121,7 @@ describe('preferences controller', function () {
await preferencesController.setSelectedAddress('0x7e57e2')

const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 0, 'empty list of tokens')
assert.equal(tokens.length, 1, 'fc should in the list')
})
})

Expand All @@ -134,9 +135,9 @@ describe('preferences controller', function () {
await preferencesController.addToken(address, symbol, decimals)

const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token added')
assert.equal(tokens.length, 2, 'one token added')

const added = tokens[0]
const added = tokens[1]
assert.equal(added.address, address, 'set address correctly')
assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, decimals, 'set decimals correctly')
Expand All @@ -154,9 +155,10 @@ describe('preferences controller', function () {
await preferencesController.addToken(address, symbol, newDecimals)

const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token added')
assert.equal(tokens.length, 2, 'one token added')

const added = tokens[0]
assert.equal(tokens[0].symbol, FC_TOKEN_MAP.symbol, 'fist token is FC')
const added = tokens[1]
assert.equal(added.address, address, 'set address correctly')
assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, newDecimals, 'updated decimals correctly')
Expand All @@ -171,15 +173,15 @@ describe('preferences controller', function () {
await preferencesController.addToken(address, symbol, decimals)
assert.equal(
preferencesController.getTokens().length,
1,
2,
'one token added for 1st address'
)

await preferencesController.setSelectedAddress('0xda22le')
await preferencesController.addToken(address, symbol, decimals)
assert.equal(
preferencesController.getTokens().length,
1,
2,
'one token added for 2nd address'
)
})
Expand Down Expand Up @@ -238,13 +240,13 @@ describe('preferences controller', function () {
})

describe('removeToken', function () {
it('should remove the only token from its state', async function () {
it('should remove the only none FC token from its state', async function () {
await preferencesController.setSelectedAddress('0x7e57e2')
await preferencesController.addToken('0xa', 'A', 5)
await preferencesController.removeToken('0xa')

const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 0, 'one token removed')
assert.equal(tokens.length, 1, 'one token removed')
})

it('should remove a token from its state', async function () {
Expand All @@ -254,9 +256,10 @@ describe('preferences controller', function () {
await preferencesController.removeToken('0xa')

const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token removed')
assert.equal(tokens.length, 2, 'one token removed')

const [token1] = tokens
const [token0, token1] = tokens
assert.deepEqual(token0, FC_TOKEN_MAP)
assert.deepEqual(token1, { address: '0xb', symbol: 'B', decimals: 5 })
})

Expand All @@ -272,9 +275,10 @@ describe('preferences controller', function () {
await preferencesController.removeToken('0xa')

const tokensFirst = preferencesController.getTokens()
assert.equal(tokensFirst.length, 1, 'one token removed in account')
assert.equal(tokensFirst.length, 2, 'one token removed in account')

const [token1] = tokensFirst
const [token0, token1] = tokensFirst
assert.deepEqual(token0, FC_TOKEN_MAP)
assert.deepEqual(token1, { address: '0xb', symbol: 'B', decimals: 5 })

await preferencesController.setSelectedAddress('0x7e57e3')
Expand All @@ -298,9 +302,10 @@ describe('preferences controller', function () {
await preferencesController.removeToken('0xa')

const tokensFirst = preferencesController.getTokens()
assert.equal(tokensFirst.length, 1, 'one token removed in network')
assert.equal(tokensFirst.length, 2, 'one token removed in network')

const [token1] = tokensFirst
const [token0, token1] = tokensFirst
assert.deepEqual(token0, FC_TOKEN_MAP)
assert.deepEqual(token1, { address: '0xb', symbol: 'B', decimals: 5 })

network.providerStore.updateState({ type: 'rinkeby' })
Expand Down Expand Up @@ -503,8 +508,8 @@ describe('preferences controller', function () {

await preferencesController._handleWatchAssetERC20(req.params.options)
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, `one token added`)
const added = tokens[0]
assert.equal(tokens.length, 2, `two token added`)
const added = tokens[1]
assert.equal(added.address, address, 'set address correctly')
assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, decimals, 'set decimals correctly')
Expand Down

0 comments on commit 397a929

Please sign in to comment.