mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-01-10 02:55:41 +00:00
1d235cf67a
* Basic reducer / action / saga setup, rendering wallets. * Better address rows, with values. * Styling + back and next buttons. * Formatting, dpath changing. * Derived -> Deterministic * Set wallet on confirm. * Flesh out Trezor wallet, add transaction signing. * Custom dpath, better handling of canceled switches and over-rendering / prop calling. * Token empty string value. * Move DPaths to config file. * Clarifying comments.
70 lines
2.0 KiB
JavaScript
70 lines
2.0 KiB
JavaScript
// @flow
|
|
import Big from 'bignumber.js';
|
|
import BaseNode from '../base';
|
|
import type { TransactionWithoutGas } from 'libs/transaction';
|
|
import RPCClient, {
|
|
getBalance,
|
|
estimateGas,
|
|
getTransactionCount,
|
|
getTokenBalance
|
|
} from './client';
|
|
import type { Token } from 'config/data';
|
|
|
|
export default class RpcNode extends BaseNode {
|
|
client: RPCClient;
|
|
constructor(endpoint: string) {
|
|
super();
|
|
this.client = new RPCClient(endpoint);
|
|
}
|
|
|
|
async getBalance(address: string): Promise<Big> {
|
|
return this.client.call(getBalance(address)).then(response => {
|
|
if (response.error) {
|
|
throw new Error('getBalance error');
|
|
}
|
|
return new Big(Number(response.result));
|
|
});
|
|
}
|
|
|
|
async estimateGas(transaction: TransactionWithoutGas): Promise<Big> {
|
|
return this.client.call(estimateGas(transaction)).then(response => {
|
|
if (response.error) {
|
|
throw new Error('estimateGas error');
|
|
}
|
|
return new Big(Number(response.result));
|
|
});
|
|
}
|
|
|
|
async getTokenBalance(address: string, token: Token): Promise<Big> {
|
|
return this.client.call(getTokenBalance(address, token)).then(response => {
|
|
if (response.error) {
|
|
return Big(0);
|
|
}
|
|
return new Big(response.result).div(new Big(10).pow(token.decimal));
|
|
});
|
|
}
|
|
|
|
async getTokenBalances(address: string, tokens: Token[]): Promise<Big[]> {
|
|
return this.client
|
|
.batch(tokens.map(t => getTokenBalance(address, t)))
|
|
.then(response => {
|
|
return response.map((item, idx) => {
|
|
// FIXME wrap in maybe-like
|
|
if (item.error) {
|
|
return new Big(0);
|
|
}
|
|
return new Big(item.result).div(new Big(10).pow(tokens[idx].decimal));
|
|
});
|
|
});
|
|
}
|
|
|
|
async getTransactionCount(address: string): Promise<string> {
|
|
return this.client.call(getTransactionCount(address)).then(response => {
|
|
if (response.error) {
|
|
throw new Error('getTransactionCount error');
|
|
}
|
|
return response.result;
|
|
});
|
|
}
|
|
}
|