2017-09-25 02:06:28 +00:00
|
|
|
import Big, { BigNumber } from 'bignumber.js';
|
|
|
|
import { Token } from 'config/data';
|
|
|
|
import { TransactionWithoutGas } from 'libs/messages';
|
|
|
|
import { Wei } from 'libs/units';
|
2017-10-16 23:48:03 +00:00
|
|
|
import { INode, TxObj } from '../INode';
|
2017-09-20 00:47:08 +00:00
|
|
|
import RPCClient from './client';
|
|
|
|
import RPCRequests from './requests';
|
2017-08-08 03:45:08 +00:00
|
|
|
|
2017-09-05 16:32:14 +00:00
|
|
|
export default class RpcNode implements INode {
|
2017-09-25 02:06:28 +00:00
|
|
|
public client: RPCClient;
|
|
|
|
public requests: RPCRequests;
|
|
|
|
|
2017-08-08 03:45:08 +00:00
|
|
|
constructor(endpoint: string) {
|
|
|
|
this.client = new RPCClient(endpoint);
|
2017-09-20 00:47:08 +00:00
|
|
|
this.requests = new RPCRequests();
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-10-16 23:48:03 +00:00
|
|
|
public sendCallRequest(txObj: TxObj): Promise<string> {
|
|
|
|
return this.client.call(this.requests.ethCall(txObj)).then(r => {
|
|
|
|
if (r.error) {
|
|
|
|
throw Error(r.error.message);
|
|
|
|
}
|
|
|
|
return r.result;
|
|
|
|
});
|
|
|
|
}
|
2017-09-25 02:06:28 +00:00
|
|
|
public getBalance(address: string): Promise<Wei> {
|
2017-09-20 00:47:08 +00:00
|
|
|
return this.client
|
|
|
|
.call(this.requests.getBalance(address))
|
|
|
|
.then(response => {
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error.message);
|
|
|
|
}
|
|
|
|
return new Wei(String(response.result));
|
|
|
|
});
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public estimateGas(transaction: TransactionWithoutGas): Promise<BigNumber> {
|
2017-09-20 00:47:08 +00:00
|
|
|
return this.client
|
|
|
|
.call(this.requests.estimateGas(transaction))
|
|
|
|
.then(response => {
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error.message);
|
|
|
|
}
|
|
|
|
return new Big(String(response.result));
|
|
|
|
});
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public getTokenBalance(address: string, token: Token): Promise<BigNumber> {
|
2017-09-20 00:47:08 +00:00
|
|
|
return this.client
|
|
|
|
.call(this.requests.getTokenBalance(address, token))
|
|
|
|
.then(response => {
|
|
|
|
if (response.error) {
|
|
|
|
// TODO - Error handling
|
|
|
|
return new Big(0);
|
|
|
|
}
|
|
|
|
return new Big(String(response.result)).div(
|
|
|
|
new Big(10).pow(token.decimal)
|
|
|
|
);
|
|
|
|
});
|
2017-08-23 06:57:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public getTokenBalances(
|
|
|
|
address: string,
|
|
|
|
tokens: Token[]
|
|
|
|
): Promise<BigNumber[]> {
|
2017-08-08 03:45:08 +00:00
|
|
|
return this.client
|
2017-09-20 00:47:08 +00:00
|
|
|
.batch(tokens.map(t => this.requests.getTokenBalance(address, t)))
|
2017-08-08 03:45:08 +00:00
|
|
|
.then(response => {
|
|
|
|
return response.map((item, idx) => {
|
|
|
|
// FIXME wrap in maybe-like
|
|
|
|
if (item.error) {
|
|
|
|
return new Big(0);
|
|
|
|
}
|
2017-08-31 04:00:31 +00:00
|
|
|
return new Big(String(item.result)).div(
|
|
|
|
new Big(10).pow(tokens[idx].decimal)
|
|
|
|
);
|
2017-08-08 03:45:08 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-31 04:00:31 +00:00
|
|
|
// TODO - Error handling
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|
2017-08-11 21:54:10 +00:00
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public getTransactionCount(address: string): Promise<string> {
|
2017-09-20 00:47:08 +00:00
|
|
|
return this.client
|
|
|
|
.call(this.requests.getTransactionCount(address))
|
|
|
|
.then(response => {
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error.message);
|
|
|
|
}
|
|
|
|
return response.result;
|
|
|
|
});
|
2017-08-31 04:00:31 +00:00
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public sendRawTx(signedTx: string): Promise<string> {
|
2017-09-20 00:47:08 +00:00
|
|
|
return this.client
|
|
|
|
.call(this.requests.sendRawTx(signedTx))
|
|
|
|
.then(response => {
|
|
|
|
if (response.error) {
|
|
|
|
throw new Error(response.error.message);
|
|
|
|
}
|
|
|
|
return response.result;
|
|
|
|
});
|
2017-08-11 21:54:10 +00:00
|
|
|
}
|
2017-08-08 03:45:08 +00:00
|
|
|
}
|