HenryNguyen5 01fc5f1a89 Move Nodes/Networks to Redux (#961)
* Start splitting networks into their own reducers

* Split out nodes and networks into their own reducers

* Cleanup file structure

* Make selectors for new state

* Change custom network typing

* re-type repo

* Fix up components to use selectors, work on fixing sagas

* Provide consistency in naming, fix more sagas

* Get non web3 node switching working

* Split config rehydration off into a different file for store

* Inline auth for custom nodes

* Include typing for app state

* moar selectors

* Get web3 working + cleanup sagas

* Cleanup tsc errors

* Use forof loop instead of foreach for clearing pruning custom networks

* Add reducer tests for new redux state

* Export needed variables

* Add console error

* Remove old comment

* Work on saga tests

* Get passing existing saga tests

* Fix more tests

* Remove irrlevant tests

* add console error

* Get rest of tests passing

* Fix merge errors

* Remove random text

* Fix store saving

* Fix selector lib only grabbing from static nodes

* Fix custom node removal crashing app

* Infer selected network via node

* Prune custom networks properly on node removal

* Infer network name from chainid from selecting state

* Cleanup tsc errors

* Remove MEW nodes for main and testnet
2018-02-12 14:43:07 -06:00

125 lines
3.3 KiB
TypeScript

import BN from 'bn.js';
import { IHexStrTransaction } from 'libs/transaction';
import { Wei, TokenValue } from 'libs/units';
import { stripHexPrefix } from 'libs/values';
import { INode, TxObj } from '../INode';
import RPCClient from './client';
import RPCRequests from './requests';
import {
isValidGetBalance,
isValidEstimateGas,
isValidCallRequest,
isValidTokenBalance,
isValidTransactionCount,
isValidCurrentBlock,
isValidRawTxApi
} from '../../validators';
import { Token } from 'types/network';
export default class RpcNode implements INode {
public client: RPCClient;
public requests: RPCRequests;
constructor(endpoint: string) {
this.client = new RPCClient(endpoint);
this.requests = new RPCRequests();
}
public ping(): Promise<boolean> {
return this.client
.call(this.requests.getNetVersion())
.then(() => true)
.catch(() => false);
}
public sendCallRequest(txObj: TxObj): Promise<string> {
return this.client
.call(this.requests.ethCall(txObj))
.then(isValidCallRequest)
.then(response => response.result);
}
public getBalance(address: string): Promise<Wei> {
return this.client
.call(this.requests.getBalance(address))
.then(isValidGetBalance)
.then(({ result }) => Wei(result));
}
public estimateGas(transaction: Partial<IHexStrTransaction>): Promise<Wei> {
// Timeout after 10 seconds
return this.client
.call(this.requests.estimateGas(transaction))
.then(isValidEstimateGas)
.then(({ result }) => Wei(result))
.catch(error => {
throw new Error(error.message);
});
}
public getTokenBalance(
address: string,
token: Token
): Promise<{ balance: TokenValue; error: string | null }> {
return this.client
.call(this.requests.getTokenBalance(address, token))
.then(isValidTokenBalance)
.then(({ result }) => {
return {
balance: TokenValue(result),
error: null
};
})
.catch(err => ({
balance: TokenValue('0'),
error: 'Caught error:' + err
}));
}
public getTokenBalances(
address: string,
tokens: Token[]
): Promise<{ balance: TokenValue; error: string | null }[]> {
return this.client
.batch(tokens.map(t => this.requests.getTokenBalance(address, t)))
.then(response =>
response.map(item => {
if (isValidTokenBalance(item)) {
return {
balance: TokenValue(item.result),
error: null
};
} else {
return {
balance: TokenValue('0'),
error: 'Invalid object shape'
};
}
})
);
}
public getTransactionCount(address: string): Promise<string> {
return this.client
.call(this.requests.getTransactionCount(address))
.then(isValidTransactionCount)
.then(({ result }) => result);
}
public getCurrentBlock(): Promise<string> {
return this.client
.call(this.requests.getCurrentBlock())
.then(isValidCurrentBlock)
.then(({ result }) => new BN(stripHexPrefix(result)).toString());
}
public sendRawTx(signedTx: string): Promise<string> {
return this.client
.call(this.requests.sendRawTx(signedTx))
.then(isValidRawTxApi)
.then(({ result }) => {
return result;
});
}
}