2017-09-20 00:47:08 +00:00
|
|
|
import RPCClient from '../rpc/client';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { JsonRpcResponse } from '../rpc/types';
|
|
|
|
import { EtherscanRequest } from './types';
|
2017-09-20 00:47:08 +00:00
|
|
|
|
|
|
|
export default class EtherscanClient extends RPCClient {
|
2017-09-25 02:06:28 +00:00
|
|
|
public encodeRequest(request: EtherscanRequest): string {
|
2017-09-20 00:47:08 +00:00
|
|
|
const encoded = new URLSearchParams();
|
|
|
|
Object.keys(request).forEach(key => {
|
2017-11-30 05:35:17 +00:00
|
|
|
if (request[key]) {
|
|
|
|
encoded.set(key, request[key]);
|
|
|
|
}
|
2017-09-20 00:47:08 +00:00
|
|
|
});
|
|
|
|
return encoded.toString();
|
|
|
|
}
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public call = (request: EtherscanRequest): Promise<JsonRpcResponse> =>
|
|
|
|
fetch(this.endpoint, {
|
2017-09-20 00:47:08 +00:00
|
|
|
method: 'POST',
|
2017-11-30 05:35:17 +00:00
|
|
|
headers: new Headers({
|
2017-09-20 00:47:08 +00:00
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
2017-11-30 05:35:17 +00:00
|
|
|
}),
|
2017-09-20 00:47:08 +00:00
|
|
|
body: this.encodeRequest(request)
|
|
|
|
}).then(r => r.json());
|
|
|
|
|
2017-09-25 02:06:28 +00:00
|
|
|
public batch = (requests: EtherscanRequest[]): Promise<JsonRpcResponse[]> => {
|
2017-09-20 00:47:08 +00:00
|
|
|
const promises = requests.map(req => this.call(req));
|
|
|
|
return Promise.all(promises);
|
2017-09-25 02:06:28 +00:00
|
|
|
};
|
2017-09-20 00:47:08 +00:00
|
|
|
}
|