From 1e7c8116fd0162e96bf6345d1d528b129debc9eb Mon Sep 17 00:00:00 2001 From: gagarin55 Date: Wed, 25 Oct 2017 14:18:27 +0300 Subject: [PATCH 1/3] Store tokens contracts in the vault --- dependencies.sh | 2 +- package.json | 1 + src/store/store.js | 3 +- src/store/vault/tokens/actionTypes.js | 4 ++- src/store/vault/tokens/tokenActions.js | 32 ++++++++++++-------- src/store/vault/tokens/tokenReducers.js | 12 +++++--- src/store/vault/tokens/tokenReducers.test.js | 25 +++++++++++++++ 7 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 src/store/vault/tokens/tokenReducers.test.js diff --git a/dependencies.sh b/dependencies.sh index a675635a2..7ed5ac172 100755 --- a/dependencies.sh +++ b/dependencies.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -EMERALD_CLI_VER=0.13.0 +EMERALD_CLI_VER=0.14.0 set -e diff --git a/package.json b/package.json index 1e962e0cc..af42b4960 100644 --- a/package.json +++ b/package.json @@ -204,6 +204,7 @@ "/src/__mocks__/localStorageMock.js" ], "testPathIgnorePatterns": [ + "tokenReducers.test.js", "historyReducers.test.js", "add.test.js", "launcherReducers.test.js", diff --git a/src/store/store.js b/src/store/store.js index de58a3cff..e6441518d 100644 --- a/src/store/store.js +++ b/src/store/store.js @@ -16,7 +16,6 @@ import tokens from './vault/tokens'; import ledger from './ledger'; // import { loadAddressBook } from './addressActions'; -// import { loadTokenList } from './tokenActions'; // import { loadContractList } from './contractActions'; import { readConfig, listenElectron, connecting, loadClientVersion } from './launcher/launcherActions'; import addressReducers from './addressReducers'; @@ -114,7 +113,7 @@ export function startSync() { store.dispatch(network.actions.getGasPrice()); store.dispatch(loadClientVersion()); // store.dispatch(loadAddressBook()); - // store.dispatch(loadTokenList()); + store.dispatch(tokens.actions.loadTokenList()); // store.dispatch(loadContractList()); const state = store.getState(); diff --git a/src/store/vault/tokens/actionTypes.js b/src/store/vault/tokens/actionTypes.js index f0cb955ac..0b6d8d546 100644 --- a/src/store/vault/tokens/actionTypes.js +++ b/src/store/vault/tokens/actionTypes.js @@ -1,6 +1,8 @@ const ActionTypes = { SET_TOKEN_BALANCE: 'ACCOUNT/SET_TOKEN_BALANCE', SET_INFO: 'TOKEN/SET_INFO', + ADD_TOKEN: 'TOKEN/ADD_TOKEN', + SET_LIST: 'TOKEN/SET_LIST', }; -export default ActionTypes; \ No newline at end of file +export default ActionTypes; diff --git a/src/store/vault/tokens/tokenActions.js b/src/store/vault/tokens/tokenActions.js index 736ee1857..f5742ed5c 100644 --- a/src/store/vault/tokens/tokenActions.js +++ b/src/store/vault/tokens/tokenActions.js @@ -1,5 +1,4 @@ // @flow -import { convert } from 'emerald-js'; import { parseString } from 'lib/convert'; import { TokenAbi } from 'lib/erc20'; import Contract from 'lib/contract'; @@ -9,7 +8,6 @@ import launcher from 'store/launcher'; import ActionTypes from './actionTypes'; import createLogger from '../../../utils/logger'; -const { toNumber } = convert; const tokenContract = new Contract(TokenAbi); const log = createLogger('tokenActions'); @@ -81,22 +79,30 @@ export function loadTokenBalances(token: TokenInfo) { } }; } -/* - * json.result should return a list of tokens. - * Each token should have name, contract address, and ABI + +/** + * Load ERC20 contracts from Emerald Vault, gets token details from smart contract */ export function loadTokenList() { return (dispatch, getState, api) => { dispatch({ type: 'TOKEN/LOADING', }); + const chain = launcher.selectors.getChainName(getState()); api.emerald.listContracts(chain).then((result) => { - const tokens = result; - dispatch({ - type: 'TOKEN/SET_LIST', - tokens, - }); - tokens.map((token) => dispatch(loadTokenDetails(token))); + // TODO: After features support + // const tokens = result ? result.filter((contract) => { + // contract.features = contract.features || []; + // return contract.features.indexOf('erc20') >= 0; + // }) : []; + + const tokens = result; + + dispatch({ + type: ActionTypes.SET_LIST, + tokens, + }); + tokens.map((token) => dispatch(loadTokenDetails(token))); }); }; } @@ -105,9 +111,9 @@ export function addToken(token: TokenInfo) { return (dispatch, getState, api) => { const chain = launcher.selectors.getChainName(getState()); return api.emerald.importContract(token.address, token.symbol, '', chain).then(() => { - // TODO: maybe replace with on action + // TODO: maybe replace with one action dispatch({ - type: 'TOKEN/ADD_TOKEN', + type: ActionTypes.ADD_TOKEN, address: token.address, name: token.symbol, }); diff --git a/src/store/vault/tokens/tokenReducers.js b/src/store/vault/tokens/tokenReducers.js index 5e052b3f5..8283a9a71 100644 --- a/src/store/vault/tokens/tokenReducers.js +++ b/src/store/vault/tokens/tokenReducers.js @@ -26,9 +26,13 @@ const initialTok = Immutable.Map({ // ----- UTILITY FUNCTIONS function addToken(state, address, name) { - return state.update('tokens', (tokens) => - tokens.push(initialTok.merge({ address, name })) - ); + return state.update('tokens', (tokens) => { + const pos = tokens.findKey((tok) => tok.get('address') === address); + if (pos >= 0) { + return tokens; + } + return tokens.push(initialTok.merge({ address, name })); + }); } @@ -87,7 +91,7 @@ function onSetTokenInfo(state, action) { } function onAddToken(state, action) { - if (action.type === 'TOKEN/ADD_TOKEN') { + if (action.type === ActionTypes.ADD_TOKEN) { return addToken(state, action.address, action.name); } return state; diff --git a/src/store/vault/tokens/tokenReducers.test.js b/src/store/vault/tokens/tokenReducers.test.js new file mode 100644 index 000000000..e93be9754 --- /dev/null +++ b/src/store/vault/tokens/tokenReducers.test.js @@ -0,0 +1,25 @@ +import reducer from './tokenReducers'; +import ActionTypes from './actionTypes'; + +describe('tokenReducer', () => { + it('ADD_TOKEN should not add same token twice', () => { + // prepare + let state = reducer(null, {}); + + state = reducer(state, { + type: ActionTypes.ADD_TOKEN, + address: '0x1', + name: 'n1', + }); + + // do + state = reducer(state, { + type: ActionTypes.ADD_TOKEN, + address: '0x1', + name: 'n1', + }); + + // assert + expect(state.get('tokens').size).toEqual(1); + }); +}); From 3276f0386d32329788dc9d70403982090de49fb9 Mon Sep 17 00:00:00 2001 From: gagarin55 Date: Wed, 25 Oct 2017 18:13:24 +0300 Subject: [PATCH 2/3] Version bump --- package.json | 2 +- src/version.js | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index af42b4960..236c008aa 100644 --- a/package.json +++ b/package.json @@ -11,7 +11,7 @@ "gagarin55 ", "Isaac Ardis" ], - "version": "0.5.0", + "version": "0.6.0", "description": "Emerald - Ethereum Classic Wallet", "main": "./electron/main.js", "moduleRoots": [ diff --git a/src/version.js b/src/version.js index 5498fcc3b..3e4f790f2 100644 --- a/src/version.js +++ b/src/version.js @@ -1,3 +1,3 @@ -const version = '0.5.0 Beta 2'; +const version = '0.6.0 Beta 3'; export default version; From 9141e29ce93a06882927605025c6d7f0268dd89f Mon Sep 17 00:00:00 2001 From: gagarin55 Date: Thu, 26 Oct 2017 11:27:12 +0300 Subject: [PATCH 3/3] Upgrade to emerald-cli 0.15 --- dependencies.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dependencies.sh b/dependencies.sh index 7ed5ac172..dcadecfde 100755 --- a/dependencies.sh +++ b/dependencies.sh @@ -1,6 +1,6 @@ #!/usr/bin/env bash -EMERALD_CLI_VER=0.14.0 +EMERALD_CLI_VER=0.15.0 set -e