@@ -9,25 +9,27 @@ export default class Infura {
99 this . bigNumberUtils = diContainer . bigNumberUtils ;
1010
1111 this . network = this . appConfig . NETWORK ;
12- this . apiUrl = this . appConfig . INFURA_API_URL ;
13- this . apiKey = this . appConfig . INFURA_API_KEY ;
12+ this . url = this . appConfig . INFURA_URL ;
13+ this . projectId = this . appConfig . INFURA_PROJECT_ID ;
14+ this . projectSecret = this . appConfig . INFURA_PROJECT_SECRET ;
1415 }
1516
16- get ( params ) {
17+ request ( jsonRpcObject ) {
1718 let requestOptions = {
18- method : 'GET' ,
19- uri : `${ this . apiUrl } /${ this . network } /${ params . method } ?token=${ this . apiKey } ` ,
20- json : true
19+ method : 'POST' ,
20+ headers : {
21+ Authorization : `Basic ${ Buffer . from ( ':' + this . projectSecret ) . toString ( 'base64' ) } `
22+ } ,
23+ uri : `https://${ this . network } .${ this . url } /${ this . projectId } ` ,
24+ json : false ,
25+ body : JSON . stringify ( jsonRpcObject )
2126 } ;
2227
23- if ( params . params ) {
24- requestOptions . uri += `¶ms=${ params . params } ` ;
25- }
26-
2728 this . log . debug ( `Infura request: ${ JSON . stringify ( requestOptions ) } ` ) ;
2829
2930 return this . requestPromise ( requestOptions ) . then ( requestResult => {
3031 let result = null ;
32+ requestResult = JSON . parse ( requestResult ) ;
3133
3234 if ( requestResult . error ) {
3335 this . log . error ( `Infura => ${ requestResult . error . message } ` ) ;
@@ -37,25 +39,29 @@ export default class Infura {
3739
3840 return result ;
3941 } ) . catch ( error => {
40- let errorMessage = this . lodash . isObject ( error . error ) ? ( ( error . error . body === undefined ) ? error . error : error . error . body . errors [ 0 ] ) : error . message ;
41- this . log . error ( `Infura => ${ errorMessage } ` ) ;
42+ this . log . error ( `Infura => ${ JSON . stringify ( error ) } ` ) ;
4243 } ) ;
4344 }
4445
4546 getBlockByNumber ( number ) {
46- let requestParams = {
47+ let jsonRpcObject = {
48+ jsonrpc : '2.0' ,
4749 method : 'eth_getBlockByNumber' ,
48- params : `["${ this . bigNumberUtils . getHex ( number , true ) } ",true]`
50+ params : [ this . bigNumberUtils . getHex ( number , true ) , false ] ,
51+ id : 1
4952 } ;
5053
51- return this . get ( requestParams ) ;
54+ return this . request ( jsonRpcObject ) ;
5255 }
5356
5457 getLastBlockNumber ( ) {
55- let requestParams = {
56- method : 'eth_blockNumber'
58+ let jsonRpcObject = {
59+ jsonrpc : '2.0' ,
60+ method : 'eth_blockNumber' ,
61+ params : [ ] ,
62+ id : 1
5763 } ;
5864
59- return this . get ( requestParams ) ;
65+ return this . request ( jsonRpcObject ) ;
6066 }
6167}
0 commit comments