Daniel Ternyak a4ec6f6139 Finalize send (broadcast signedTx, loading indicators, error handling, form validation) (#141)
* hide buttons during send loading state

* fix transaction succeeded not clickable; provide error in action

* move BroadcastStatusTransaction into 'libs/transaction'

* use more succint Array.prototype.find

* rename resetState -> resetTransaction

* refactor and component componentDidUpdate logic

* rename disabled -> generateDisabled; comment componentDidUpdate

* add size to Spinner, use in ConfirmationModal; disable instead of hide buttons in Modal

* fix flow not understanding that an object wouldn't be null in this case anyway. silly flow

* various refactors; send entire balance working
2017-08-30 21:00:31 -07:00

113 lines
2.3 KiB
JavaScript

// @flow
import { randomBytes } from 'crypto';
import ERC20 from 'libs/erc20';
import { hexEncodeData } from './utils';
import type {
RPCRequest,
JsonRpcResponse,
CallRequest,
GetBalanceRequest,
GetTokenBalanceRequest,
EstimateGasRequest,
GetTransactionCountRequest,
SendRawTxRequest
} from './types';
import type { Token } from 'config/data';
// FIXME is it safe to generate that much entropy?
function id(): string {
return randomBytes(16).toString('hex');
}
export function sendRawTx(signedTx: string): SendRawTxRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_sendRawTransaction',
params: [signedTx]
};
}
export function estimateGas<T: *>(transaction: T): EstimateGasRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_estimateGas',
params: [transaction]
};
}
export function getBalance(address: string): GetBalanceRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_getBalance',
params: [hexEncodeData(address), 'pending']
};
}
export function ethCall<T: *>(transaction: T): CallRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_call',
params: [transaction, 'pending']
};
}
export function getTransactionCount(
address: string
): GetTransactionCountRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_getTransactionCount',
params: [address, 'pending']
};
}
export function getTokenBalance(
address: string,
token: Token
): GetTokenBalanceRequest {
return {
id: id(),
jsonrpc: '2.0',
method: 'eth_call',
params: [
{
to: token.address,
data: ERC20.balanceOf(address)
},
'pending'
]
};
}
export default class RPCClient {
endpoint: string;
constructor(endpoint: string) {
this.endpoint = endpoint;
}
async call(request: RPCRequest): Promise<JsonRpcResponse> {
return fetch(this.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(request)
}).then(r => r.json());
}
async batch(requests: RPCRequest[]): Promise<JsonRpcResponse[]> {
return fetch(this.endpoint, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(requests)
}).then(r => r.json());
}
}