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 => {
|
|
|
|
encoded.set(key, request[key]);
|
|
|
|
});
|
|
|
|
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',
|
|
|
|
headers: {
|
|
|
|
'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
|
|
|
|
},
|
|
|
|
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
|
|
|
}
|