mirror of
https://github.com/status-im/MyCrypto.git
synced 2025-03-03 20:20:54 +00:00
* remove 'transate' property and ng-scopes * use bigs (surprised flow did not catch this) * fix dropdown not expanding -- switch to simpledropdown * Don't use generics for no real reason * Create Ether, Wei, and GWei types, and annotate. Also contains refactors and UX improvements 1. clear previously generated rawTX/signedTx when changes to transaction inputs are made. 2. reset generated rawTx/signedTx while new generateTx is loading * add toString helper method and use in place of .amount.toString() * support optional base in toString helper method and use * incorporate PR suggestions (destructure, resolve via callback)
89 lines
2.4 KiB
JavaScript
89 lines
2.4 KiB
JavaScript
// @flow
|
|
import Big from 'bignumber.js';
|
|
import type { INode } from '../INode';
|
|
import type { TransactionWithoutGas } from 'libs/messages';
|
|
import RPCClient, {
|
|
getBalance,
|
|
estimateGas,
|
|
getTransactionCount,
|
|
getTokenBalance,
|
|
sendRawTx
|
|
} from './client';
|
|
import type { Token } from 'config/data';
|
|
import { Wei } from 'libs/units';
|
|
|
|
export default class RpcNode implements INode {
|
|
client: RPCClient;
|
|
constructor(endpoint: string) {
|
|
this.client = new RPCClient(endpoint);
|
|
}
|
|
|
|
getBalance(address: string): Promise<Wei> {
|
|
return this.client.call(getBalance(address)).then(response => {
|
|
if (response.error) {
|
|
throw new Error(response.error.message);
|
|
}
|
|
return new Wei(String(response.result));
|
|
});
|
|
}
|
|
|
|
estimateGas(transaction: TransactionWithoutGas): Promise<Big> {
|
|
return this.client.call(estimateGas(transaction)).then(response => {
|
|
if (response.error) {
|
|
throw new Error(response.error.message);
|
|
}
|
|
return new Big(String(response.result));
|
|
});
|
|
}
|
|
|
|
getTokenBalance(address: string, token: Token): Promise<Big> {
|
|
return this.client.call(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)
|
|
);
|
|
});
|
|
}
|
|
|
|
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(String(item.result)).div(
|
|
new Big(10).pow(tokens[idx].decimal)
|
|
);
|
|
});
|
|
});
|
|
// TODO - Error handling
|
|
}
|
|
|
|
getTransactionCount(address: string): Promise<string> {
|
|
return this.client.call(getTransactionCount(address)).then(response => {
|
|
if (response.error) {
|
|
throw new Error(response.error.message);
|
|
}
|
|
return response.result;
|
|
});
|
|
}
|
|
|
|
sendRawTx(signedTx: string): Promise<string> {
|
|
return this.client.call(sendRawTx(signedTx)).then(response => {
|
|
if (response.error) {
|
|
throw new Error(response.error.message);
|
|
}
|
|
if (response.errorMessage) {
|
|
throw new Error(response.errorMessage);
|
|
}
|
|
return response.result;
|
|
});
|
|
}
|
|
}
|