Daniel Ternyak ab5fa1a799
Support Non-Ethereum Networks (#849)
* Make UnlockHeader a PureComponent

* MVP

* actually disable wallet format if not determined to be valid format for wallet

* default to correct derivation in mnemonic modal

* cleanup

* fix tslint

* use enums for HD wallet getPath

* Add stricter typing

* Fix labels not updating on selector

* Ban hardware wallet support for custom network unsupported chainIds

* Fix type error

* Fix custom node dPath not being saved

* Fix mnemonic modal

* default path bugfixes

* add react-select

* misc fixes; rabbit holing hard.

* fix tslint

* revert identicon changes

* reload on network change :/

* actually reload on network change

* really really reload on network change

* tslint fixes

* Update styles

* set table width

* fix package versioning

* push broken sagas

* Fix saga test

* fix tslint

* address round of review

* move non-selectors out to utilty; adjust reload timer

* cleanup network util comments

* manage wallet disable at WalletDecrypt instead of in both WalletDecrypt and WalletButton

* Separate WalletDecrypt props into ownProps / StateProps

* disable payment requests on non-eth networks

* specialize connect; separate props

* remove unused state prop

* remove bad import

* create tests for networks

* Clarify Lite-Send error on non-ethereum networkS

* remove string option for network config name

* Create concept of always-on 'EXTRA_PATHS'; include SINGULAR_DTV legacy dPath in 'EXTRA_PATHS'

* fix multiple imports

* address PR comments
2018-01-20 14:06:28 -06:00

125 lines
3.3 KiB
TypeScript

import BN from 'bn.js';
import { Token } from 'config';
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';
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;
});
}
}