From e07eedb3ad0439e831db4042ea2c5f304e247fad Mon Sep 17 00:00:00 2001 From: HenryNguyen5 Date: Tue, 5 Sep 2017 12:32:14 -0400 Subject: [PATCH] Refactor BaseNode to be an interface INode (#167) --- common/libs/nodes/INode.js | 13 ++++++++++++ common/libs/nodes/base.js | 30 ---------------------------- common/libs/nodes/index.js | 1 - common/libs/nodes/rpc/index.js | 17 ++++++++-------- common/libs/transaction.js | 4 ++-- common/sagas/deterministicWallets.js | 6 +++--- common/sagas/wallet.js | 8 ++++---- common/selectors/config.js | 4 ++-- 8 files changed, 32 insertions(+), 51 deletions(-) create mode 100644 common/libs/nodes/INode.js delete mode 100644 common/libs/nodes/base.js diff --git a/common/libs/nodes/INode.js b/common/libs/nodes/INode.js new file mode 100644 index 00000000..8ea89bbe --- /dev/null +++ b/common/libs/nodes/INode.js @@ -0,0 +1,13 @@ +// @flow +import Big from 'bignumber.js'; +import type { TransactionWithoutGas } from 'libs/transaction'; +import type { Token } from 'config/data'; + +export interface INode { + getBalance(_address: string): Promise, + getTokenBalance(_address: string, _token: Token): Promise, + getTokenBalances(_address: string, _tokens: Token[]): Promise, + estimateGas(_tx: TransactionWithoutGas): Promise, + getTransactionCount(_address: string): Promise, + sendRawTx(_tx: string): Promise +} diff --git a/common/libs/nodes/base.js b/common/libs/nodes/base.js deleted file mode 100644 index 78d08c17..00000000 --- a/common/libs/nodes/base.js +++ /dev/null @@ -1,30 +0,0 @@ -// @flow -import Big from 'bignumber.js'; -import type { TransactionWithoutGas } from 'libs/transaction'; -import type { Token } from 'config/data'; - -export default class BaseNode { - async getBalance(_address: string): Promise { - throw new Error('Implement me'); - } - - async getTokenBalance(_address: string, _token: Token): Promise { - throw new Error('Implement me'); - } - - async getTokenBalances(_address: string, _tokens: Token[]): Promise { - throw new Error('Implement me'); - } - - async estimateGas(_tx: TransactionWithoutGas): Promise { - throw new Error('Implement me'); - } - - async getTransactionCount(_address: string): Promise { - throw new Error('Implement me'); - } - - async sendRawTx(_tx: string): Promise { - throw new Error('Implement me'); - } -} diff --git a/common/libs/nodes/index.js b/common/libs/nodes/index.js index 4ac70297..2b1acbab 100644 --- a/common/libs/nodes/index.js +++ b/common/libs/nodes/index.js @@ -1,3 +1,2 @@ // @flow -export { default as BaseNode } from './base'; export { default as RPCNode } from './rpc'; diff --git a/common/libs/nodes/rpc/index.js b/common/libs/nodes/rpc/index.js index 432ce972..37bb2561 100644 --- a/common/libs/nodes/rpc/index.js +++ b/common/libs/nodes/rpc/index.js @@ -1,6 +1,6 @@ // @flow import Big from 'bignumber.js'; -import BaseNode from '../base'; +import type { INode } from '../INode'; import type { TransactionWithoutGas } from 'libs/transaction'; import RPCClient, { getBalance, @@ -11,14 +11,13 @@ import RPCClient, { } from './client'; import type { Token } from 'config/data'; -export default class RpcNode extends BaseNode { +export default class RpcNode implements INode { client: RPCClient; constructor(endpoint: string) { - super(); this.client = new RPCClient(endpoint); } - async getBalance(address: string): Promise { + getBalance(address: string): Promise { return this.client.call(getBalance(address)).then(response => { if (response.error) { throw new Error(response.error.message); @@ -27,7 +26,7 @@ export default class RpcNode extends BaseNode { }); } - async estimateGas(transaction: TransactionWithoutGas): Promise { + estimateGas(transaction: TransactionWithoutGas): Promise { return this.client.call(estimateGas(transaction)).then(response => { if (response.error) { throw new Error(response.error.message); @@ -36,7 +35,7 @@ export default class RpcNode extends BaseNode { }); } - async getTokenBalance(address: string, token: Token): Promise { + getTokenBalance(address: string, token: Token): Promise { return this.client.call(getTokenBalance(address, token)).then(response => { if (response.error) { // TODO - Error handling @@ -48,7 +47,7 @@ export default class RpcNode extends BaseNode { }); } - async getTokenBalances(address: string, tokens: Token[]): Promise { + getTokenBalances(address: string, tokens: Token[]): Promise { return this.client .batch(tokens.map(t => getTokenBalance(address, t))) .then(response => { @@ -65,7 +64,7 @@ export default class RpcNode extends BaseNode { // TODO - Error handling } - async getTransactionCount(address: string): Promise { + getTransactionCount(address: string): Promise { return this.client.call(getTransactionCount(address)).then(response => { if (response.error) { throw new Error(response.error.message); @@ -74,7 +73,7 @@ export default class RpcNode extends BaseNode { }); } - async sendRawTx(signedTx: string): Promise { + sendRawTx(signedTx: string): Promise { return this.client.call(sendRawTx(signedTx)).then(response => { if (response.error) { throw new Error(response.error.message); diff --git a/common/libs/transaction.js b/common/libs/transaction.js index 9b649c82..07bd9ab6 100644 --- a/common/libs/transaction.js +++ b/common/libs/transaction.js @@ -6,7 +6,7 @@ import { isValidETHAddress } from 'libs/validators'; import ERC20 from 'libs/erc20'; import { toTokenUnit } from 'libs/units'; import { stripHex } from 'libs/values'; -import type BaseNode from 'libs/nodes/base'; +import type { INode } from 'libs/nodes/INode'; import type { BaseWallet } from 'libs/wallet'; import type { Token } from 'config/data'; import type EthTx from 'ethereumjs-tx'; @@ -72,7 +72,7 @@ export function getTransactionFields(tx: EthTx) { } export async function generateTransaction( - node: BaseNode, + node: INode, tx: Transaction, wallet: BaseWallet, token: ?Token diff --git a/common/sagas/deterministicWallets.js b/common/sagas/deterministicWallets.js index da300ba6..dbae5a64 100644 --- a/common/sagas/deterministicWallets.js +++ b/common/sagas/deterministicWallets.js @@ -27,7 +27,7 @@ import { getWallets, getDesiredToken } from 'selectors/deterministicWallets'; import { getNodeLib } from 'selectors/config'; import { getTokens } from 'selectors/wallet'; -import type { BaseNode } from 'libs/nodes'; +import type { INode } from 'libs/nodes/INode'; import type { Token } from 'config/data'; // TODO: BIP39 for mnemonic wallets? @@ -60,7 +60,7 @@ function* getDeterministicWallets( // Grab each wallet's main network token, and update it with it function* updateWalletValues(): Generator { - const node: BaseNode = yield select(getNodeLib); + const node: INode = yield select(getNodeLib); const wallets: DeterministicWalletData[] = yield select(getWallets); const calls = wallets.map(w => apply(node, node.getBalance, [w.address])); const balances = yield all(calls); @@ -84,7 +84,7 @@ function* updateWalletTokenValues(): Generator { const token = tokens.find(t => t.symbol === desiredToken); if (!token) return; - const node: BaseNode = yield select(getNodeLib); + const node: INode = yield select(getNodeLib); const wallets: DeterministicWalletData[] = yield select(getWallets); const calls = wallets.map(w => { return apply(node, node.getTokenBalance, [w.address, token]); diff --git a/common/sagas/wallet.js b/common/sagas/wallet.js index ccf6ae53..88015488 100644 --- a/common/sagas/wallet.js +++ b/common/sagas/wallet.js @@ -21,7 +21,7 @@ import { PrivKeyWallet, BaseWallet } from 'libs/wallet'; -import { BaseNode } from 'libs/nodes'; +import { INode } from 'libs/nodes/INode'; import { determineKeystoreType } from 'libs/keystore'; import { getNodeLib } from 'selectors/config'; @@ -35,7 +35,7 @@ function* updateAccountBalance(): Generator { if (!wallet) { return; } - const node: BaseNode = yield select(getNodeLib); + const node: INode = yield select(getNodeLib); const address = yield wallet.getAddress(); // network request let balance = yield apply(node, node.getBalance, [address]); @@ -47,7 +47,7 @@ function* updateAccountBalance(): Generator { function* updateTokenBalances(): Generator { try { - const node: BaseNode = yield select(getNodeLib); + const node: INode = yield select(getNodeLib); const wallet: ?BaseWallet = yield select(getWalletInst); const tokens = yield select(getTokens); if (!wallet || !node) { @@ -146,7 +146,7 @@ function* broadcastTx( ): Generator { const signedTx = action.payload.signedTx; try { - const node: BaseNode = yield select(getNodeLib); + const node: INode = yield select(getNodeLib); const txHash = yield apply(node, node.sendRawTx, [signedTx]); yield put( showNotification('success', , 0) diff --git a/common/selectors/config.js b/common/selectors/config.js index 0329d764..590682e2 100644 --- a/common/selectors/config.js +++ b/common/selectors/config.js @@ -1,6 +1,6 @@ // @flow import type { State } from 'reducers'; -import { BaseNode } from 'libs/nodes'; +import type { INode } from 'libs/nodes/INode'; import { NODES, NETWORKS } from 'config/data'; import type { NodeConfig, NetworkConfig, NetworkContract } from 'config/data'; @@ -12,7 +12,7 @@ export function getNodeConfig(state: State): NodeConfig { return NODES[state.config.nodeSelection]; } -export function getNodeLib(state: State): BaseNode { +export function getNodeLib(state: State): INode { return NODES[state.config.nodeSelection].lib; }