Skip to content

Commit

Permalink
Merge pull request #348 from gagarin55/import-contract
Browse files Browse the repository at this point in the history
Store tokens contract in the vault
  • Loading branch information
gagarin55 authored Oct 26, 2017
2 parents cadde05 + 9141e29 commit 44bf101
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 23 deletions.
2 changes: 1 addition & 1 deletion dependencies.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

EMERALD_CLI_VER=0.13.0
EMERALD_CLI_VER=0.15.0

set -e

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"gagarin55 <[email protected]>",
"Isaac Ardis"
],
"version": "0.5.0",
"version": "0.6.0",
"description": "Emerald - Ethereum Classic Wallet",
"main": "./electron/main.js",
"moduleRoots": [
Expand Down Expand Up @@ -204,6 +204,7 @@
"<rootDir>/src/__mocks__/localStorageMock.js"
],
"testPathIgnorePatterns": [
"tokenReducers.test.js",
"historyReducers.test.js",
"add.test.js",
"launcherReducers.test.js",
Expand Down
3 changes: 1 addition & 2 deletions src/store/store.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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();
Expand Down
4 changes: 3 additions & 1 deletion src/store/vault/tokens/actionTypes.js
Original file line number Diff line number Diff line change
@@ -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;
export default ActionTypes;
32 changes: 19 additions & 13 deletions src/store/vault/tokens/tokenActions.js
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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');
Expand Down Expand Up @@ -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)));
});
};
}
Expand All @@ -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,
});
Expand Down
12 changes: 8 additions & 4 deletions src/store/vault/tokens/tokenReducers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }));
});
}


Expand Down Expand Up @@ -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;
Expand Down
25 changes: 25 additions & 0 deletions src/store/vault/tokens/tokenReducers.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
2 changes: 1 addition & 1 deletion src/version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const version = '0.5.0 Beta 2';
const version = '0.6.0 Beta 3';

export default version;

0 comments on commit 44bf101

Please sign in to comment.