@@ -3,84 +3,74 @@ const {
33 ERROR_MESSAGE_TRANSACTION_NO_HASH : NO_HASH
44} = require ( './constants' )
55
6- const { is, get, onFulfilled, onError} = require ( './utils' )
6+ const {
7+ is,
8+ get,
9+ onFulfilled,
10+ onError,
11+ throwIf,
12+ recordsFormatter
13+ } = require ( './utils' )
714
815class Transaction {
916 constructor ( web3data ) {
1017 this . web3data = web3data
1118 }
1219
13- async getTransactions ( filterOptions ) {
14- const response = await get ( this . web3data , {
20+ getTransactions ( filterOptions ) {
21+ return get ( this . web3data , {
1522 endpoint : ENDPOINT ,
1623 filterOptions
17- } )
18- return new Promise ( ( resolve , reject ) => {
19- if (
20- ! response ||
21- response . status !== 200 ||
22- ! response . payload ||
23- ! response . payload . records
24- ) {
25- reject ( new Error ( 'Failed to retrieve transactions.' ) )
26- } else {
27- resolve ( response . payload . records )
28- }
29- } )
24+ } ) . then ( onFulfilled . bind ( { formatter : recordsFormatter } ) , onError )
3025 }
3126
32- async getTransaction ( hash , filterOptions ) {
33- if ( ! hash ) return Promise . reject ( new Error ( NO_HASH ) )
34- const response = await get ( this . web3data , {
27+ getTransaction ( hash , filterOptions ) {
28+ throwIf ( is . notHash ( hash ) , NO_HASH )
29+ return get ( this . web3data , {
3530 pathParam : hash ,
3631 endpoint : ENDPOINT ,
3732 filterOptions
38- } )
39- return new Promise ( ( resolve , reject ) => {
40- if ( ! response || response . status !== 200 || ! response . payload ) {
41- reject ( new Error ( 'Failed to retrieve transaction.' ) )
42- } else {
43- resolve ( response . payload )
44- }
45- } )
33+ } ) . then ( onFulfilled , onError )
4634 }
4735
48- async getPendingTransactions ( ) {
49- const pendingTransactions = await this . getTransactions ( { status : 'pending' } )
50- return new Promise ( ( resolve , reject ) => {
51- if ( is . undefined ( pendingTransactions ) || is . null ( pendingTransactions ) ) {
52- reject ( new Error ( 'Failed to retrieve pending transactions.' ) )
53- } else {
54- resolve ( pendingTransactions )
55- }
56- } )
36+ getPendingTransactions ( ) {
37+ return this . getTransactions ( { status : 'pending' } ) . then (
38+ pendingTransactions => {
39+ throwIf (
40+ is . undefined ( pendingTransactions ) || is . null ( pendingTransactions ) ,
41+ 'Failed to retrieve pending transactions.'
42+ )
43+ return pendingTransactions
44+ } ,
45+ console . error
46+ )
5747 }
5848
5949 getGasPrediction ( ) {
6050 return get ( this . web3data , {
6151 endpoint : ENDPOINT ,
6252 subendpoint : 'gas/predictions'
63- } )
53+ } ) . then ( onFulfilled , onError )
54+ }
55+
56+ getGasPercentiles ( filterOptions ) {
57+ return get ( this . web3data , {
58+ endpoint : ENDPOINT ,
59+ subendpoint : 'gas/percentiles' ,
60+ filterOptions
61+ } ) . then ( onFulfilled , onError )
6462 }
6563
66- async getGasPrice ( ) {
67- const response = await this . getGasPrediction ( )
68- return new Promise ( ( resolve , reject ) => {
69- if (
70- is . null ( response ) ||
71- is . undefined ( response ) ||
72- response . status !== 200
73- ) {
74- reject ( new Error ( 'Failed to retrieve gas price.' ) )
75- } else if ( ! response . payload || ! response . payload . average ) {
76- reject ( new Error ( 'Failed to retrieve gas price.' ) )
77- } else {
78- resolve ( `${ response . payload . average . gasPrice } ` )
79- }
64+ getGasPrice ( ) {
65+ return this . getGasPrediction ( ) . then ( gasPrediction => {
66+ throwIf (
67+ ! gasPrediction . average || ! gasPrediction . average . gasPrice ,
68+ 'Failed to retrieve gas price.'
69+ )
70+ return `${ gasPrediction . average . gasPrice } `
8071 } )
8172 }
8273
83- // TODO: Needs tests
8474 getVolume ( filterOptions ) {
8575 return get ( this . web3data , {
8676 endpoint : ENDPOINT ,
@@ -89,7 +79,6 @@ class Transaction {
8979 } ) . then ( onFulfilled , onError )
9080 }
9181
92- // TODO: Needs tests
9382 getMetrics ( filterOptions ) {
9483 return get ( this . web3data , {
9584 endpoint : ENDPOINT ,
0 commit comments