skubakdj c54453729c Web3 Future Proof, Refactor, Bugfixes (#481)
* correct web3 hide/show logic

* refactor web3 node lib

* refactor web3 wallet to use new web3 node

* update web3 node init to use new lib

* update web3 wallet saga to use new lib, address unlock flow bug

* remove comments

* add validators for web3 methods

* update web3 node to use latest standards

* remove legacy function

* update lib call, add account unlock check

* add & use new flavor of unsetWeb3Node

* address PR feedback

* add test, update tests, update snapshot
2017-12-01 10:32:29 -06:00

44 lines
1.2 KiB
TypeScript

import { JsonRpcResponse, RPCRequest } from '../rpc/types';
import { IWeb3Provider } from './types';
import RPCClient from '../rpc/client';
export default class Web3Client extends RPCClient {
private provider: IWeb3Provider;
constructor() {
super('web3'); // initialized with fake endpoint
this.provider = (window as any).web3.currentProvider;
}
public decorateRequest = (req: RPCRequest) => ({
...req,
id: this.id(),
jsonrpc: '2.0',
params: req.params || [] // default to empty array so MetaMask doesn't error
});
public call = (request: RPCRequest | any): Promise<JsonRpcResponse> =>
this.sendAsync(this.decorateRequest(request)) as Promise<JsonRpcResponse>;
public batch = (requests: RPCRequest[] | any): Promise<JsonRpcResponse[]> =>
this.sendAsync(requests.map(this.decorateRequest)) as Promise<
JsonRpcResponse[]
>;
private sendAsync = (
request: any
): Promise<JsonRpcResponse | JsonRpcResponse[]> => {
return new Promise((resolve, reject) => {
this.provider.sendAsync(
request,
(error, result: JsonRpcResponse | JsonRpcResponse[]) => {
if (error) {
return reject(error);
}
resolve(result);
}
);
});
};
}