19421942
19431943 var v5 = v35 ( 'v5' , 0x50 , sha1Browser ) ;
19441944
1945- const { ETH_METHODS } = constants ;
1945+ const { ETH_METHODS } = constants ;
19461946
19471947 /**
19481948 * Builds the endpoint url to pass to .rawQuery(). Checks for non empties and appends
19961996
19971997 const throwNow = message => throwIf ( true , message ) ;
19981998
1999- const onFulfilled = response =>
2000- response . error ? throwNow ( response . message ) : response . payload ;
2001- const onError = error => throwNow ( error . response . data . message ) ;
1999+ /**
2000+ * Handler for all request responses.
2001+ *
2002+ * @param response - The Axios response object.
2003+ * @returns The data from the response.
2004+ * @private
2005+ * @example
2006+ */
2007+ const onFulfilled = function ( response ) {
2008+ throwIf ( response . error , response . message ) ;
2009+ return this && this . formatter
2010+ ? this . formatter ( response . payload )
2011+ : response . payload
2012+ } ;
2013+
2014+ const onError = ( { response : { data} } ) =>
2015+ throwNow ( data . message || data . description ) ;
20022016
20032017 const rejectPromiseIf = ( condition , message ) => {
20042018 if ( condition ) return Promise . reject ( new Error ( message ) )
20052019 } ;
20062020
20072021 const is = ( ) => { } ;
2008-
20092022 is . string = value => typeof value === 'string' ;
20102023 is . bool = value => typeof value === 'boolean' ;
20112024 is . emptyString = value => is . string ( value ) && value . length === 0 ;
20142027 Object . prototype . hasOwnProperty . call ( object , property ) ;
20152028 is . undefined = value => typeof value === 'undefined' ;
20162029 is . null = value => value === null ;
2017-
20182030 is . notHash = hash => is . undefined ( hash ) || is . emptyString ( hash ) ;
20192031 is . notUndefined = value => ! is . undefined ( value ) ;
20202032 is . nonEmptyObject = object => ! is . emptyObject ( object ) ;
20212033 is . nonEmptyString = value => ! is . emptyString ( value ) ;
20222034 is . notInObject = ( object , property ) => ! is . inObject ( object , property ) ;
20232035
20242036 /**
2025- * Generates a uuid see [this gist]() for more details
2037+ * Generates a uuid see [this gist]() for more details.
2038+ *
20262039 * @param data
2040+ * @example
20272041 */
2028- const uuid = data => v5 ( JSON . stringify ( data ) , 'ccfeca02-f0e9-4433-a740-b830cceb3d2d' ) ;
2042+ const uuid = data =>
2043+ v5 ( JSON . stringify ( data ) , 'ccfeca02-f0e9-4433-a740-b830cceb3d2d' ) ;
20292044
20302045 /**
20312046 * Returns an array of methods defined on the object.
21142129 } )
21152130 } ;
21162131
2132+ const defaultFormatter = ( response , field ) => {
2133+ return response [ field ] ? response [ field ] : null
2134+ } ;
2135+
2136+ const recordsFormatter = response => defaultFormatter ( response , 'records' ) ;
2137+
21172138 var utils$1 = {
21182139 buildFilterUrl,
21192140 is,
21272148 onFulfilled,
21282149 onError,
21292150 formatJsonRpc,
2130- getMethods
2151+ getMethods,
2152+ recordsFormatter
21312153 } ;
21322154
21332155 const {
27382760 ERROR_MESSAGE_BLOCK_NO_ID : NO_BLOCK_ID
27392761 } = constants ;
27402762
2741- const { is : is$5 , get : get$5 , throwIf : throwIf$2 , onFulfilled : onFulfilled$4 , onError : onError$4 } = utils$1 ;
2763+ const {
2764+ is : is$5 ,
2765+ get : get$5 ,
2766+ throwIf : throwIf$2 ,
2767+ onFulfilled : onFulfilled$4 ,
2768+ onError : onError$4 ,
2769+ recordsFormatter : recordsFormatter$1
2770+ } = utils$1 ;
27422771
27432772 class Block {
27442773 constructor ( web3data ) {
27502779 return get$5 ( this . web3data , {
27512780 endpoint : ENDPOINT$4 ,
27522781 filterOptions
2753- } ) . then ( onFulfilled$4 , onError$4 )
2782+ } ) . then ( onFulfilled$4 . bind ( { formatter : recordsFormatter$1 } ) , onError$4 )
27542783 }
27552784
27562785 /**
@@ -2770,108 +2799,100 @@
27702799 } ) . then ( onFulfilled$4 , onError$4 )
27712800 }
27722801
2773- async getBlockNumber ( ) {
2774- const block = await this . web3data . block . getBlock ( 'latest' ) ;
2775- throwIf$2 ( block | ! block . number , 'Failed to retrieve block number.' ) ;
2776- return parseInt ( block . number , 10 )
2802+ getBlockNumber ( ) {
2803+ return this . web3data . block . getBlock ( 'latest' ) . then ( block => {
2804+ throwIf$2 ( block | ! block . number , 'Failed to retrieve block number.' ) ;
2805+ return parseInt ( block . number , 10 )
2806+ } )
27772807 }
27782808
2779- async getBlockTransactionCount ( id ) {
2780- const block = await this . web3data . block . getBlock ( id ) ;
2781- throwIf$2 (
2782- ! block || ( ! block . predictions && ! block . numTransactions ) ,
2783- 'Failed to retrieve block transaction count.'
2784- ) ;
2785- return block . predictions ? null : parseInt ( block . numTransactions , 10 )
2809+ getBlockTransactionCount ( id ) {
2810+ return this . web3data . block . getBlock ( id ) . then ( block => {
2811+ throwIf$2 (
2812+ ! block || ( ! block . predictions && ! block . numTransactions ) ,
2813+ 'Failed to retrieve block transaction count.'
2814+ ) ;
2815+ // If 'predictions' field exists then it's a future block thus has no txns
2816+ return block . predictions ? null : parseInt ( block . numTransactions , 10 )
2817+ } )
27862818 }
27872819
2788- async getTransactions ( id , filterOptions ) {
2789- const response = await get$5 ( this . web3data , {
2820+ getTransactions ( id , filterOptions ) {
2821+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
2822+ return get$5 ( this . web3data , {
27902823 pathParam : id ,
27912824 endpoint : ENDPOINT$4 ,
27922825 subendpoint : 'transactions' ,
27932826 filterOptions
2794- } ) ;
2795- throwIf$2 (
2796- ! response ||
2797- response . status !== 200 ||
2798- ! response . payload ||
2799- ! response . payload . records ,
2800- 'Failed to retrieve transactions.'
2801- ) ;
2802- return response . payload . records
2827+ } ) . then ( onFulfilled$4 . bind ( { formatter : recordsFormatter$1 } ) , onError$4 )
28032828 }
28042829
2805- async getTransactionFromBlock ( id , index ) {
2806- const transactions = await this . web3data . block . getTransactions ( id ) ;
2807- return new Promise ( ( resolve , reject ) => {
2808- if ( ! transactions ) {
2809- reject ( new Error ( `Failed to retrieve transaction.` ) ) ;
2810- } else if ( index < transactions . length && index > - 1 ) {
2811- resolve ( transactions [ index ] ) ;
2812- } else {
2813- resolve ( null ) ;
2814- }
2830+ getTransactionFromBlock ( id , index ) {
2831+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
2832+ return this . web3data . block . getTransactions ( id ) . then ( txns => {
2833+ throwIf$2 ( ! txns , 'Failed to retrieve transaction.' ) ;
2834+
2835+ // Check that 'index' is within valid range
2836+ return index < txns . length && index > - 1 ? txns [ index ] : null
28152837 } )
28162838 }
28172839
2818- async getUncle ( id , index ) {
2819- const block = await this . web3data . block . getBlock ( id , {
2820- validationMethod : 'full'
2821- } ) ;
2822- return new Promise ( ( resolve , reject ) => {
2823- if (
2824- ! block ||
2825- ( ! block . predictions && ! block . numTransactions && ! block . validation )
2826- ) {
2827- reject ( new Error ( `Failed to retrieve uncle.` ) ) ;
2828- } else if ( block . predictions || ! block . validation . uncles ) {
2829- resolve ( null ) ;
2830- } else if ( index < block . validation . uncles . length && index > - 1 ) {
2831- resolve ( block . validation . uncles [ index ] ) ;
2832- } else {
2833- resolve ( null ) ;
2834- }
2835- } )
2840+ getUncle ( id , index ) {
2841+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
2842+ throwIf$2 ( is$5 . undefined ( index ) , "Missing required param 'index'" ) ;
2843+ return this . web3data . block
2844+ . getBlock ( id , {
2845+ validationMethod : 'full'
2846+ } )
2847+ . then ( block => {
2848+ throwIf$2 (
2849+ ! block ||
2850+ ( ! block . predictions && ! block . numTransactions && ! block . validation ) ,
2851+ 'Failed to retrieve uncle.'
2852+ ) ;
2853+ // Check that it's...
2854+ // a not a future block
2855+ // and that the block has uncles
2856+ // and 'index' is within the valid range
2857+ return ! block . predictions &&
2858+ block . validation . uncles &&
2859+ index < block . validation . uncles . length &&
2860+ index > - 1
2861+ ? block . validation . uncles [ index ]
2862+ : null
2863+ } )
28362864 }
28372865
28382866 getTokenTransfers ( id , filterOptions ) {
2839- if ( is$5 . undefined ( id ) ) return Promise . reject ( new Error ( NO_BLOCK_ID ) )
2867+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
28402868 return get$5 ( this . web3data , {
28412869 pathParam : id ,
28422870 endpoint : ENDPOINT$4 ,
28432871 subendpoint : 'token-transfers' ,
28442872 filterOptions
2845- } ) . then (
2846- response =>
2847- response . error ? throwIf$2 ( true , response . message ) : response . payload ,
2848- error => throwIf$2 ( true , error . response . data . message )
2849- )
2873+ } ) . then ( onFulfilled$4 , onError$4 )
28502874 }
28512875
2852- // TODO: Needs tests
28532876 getLogs ( id , filterOptions ) {
2854- throwIf$2 ( is$5 . notHash ( id ) , NO_BLOCK_ID ) ;
2877+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
28552878 return get$5 ( this . web3data , {
2856- id,
2879+ pathParam : id ,
28572880 endpoint : ENDPOINT$4 ,
28582881 subendpoint : 'logs' ,
28592882 filterOptions
2860- } ) . then ( onFulfilled$4 , onError$4 )
2883+ } ) . then ( onFulfilled$4 . bind ( { formatter : recordsFormatter$1 } ) , onError$4 )
28612884 }
28622885
2863- // TODO: Needs tests
28642886 getFunctions ( id , filterOptions ) {
2865- throwIf$2 ( is$5 . notHash ( id ) , NO_BLOCK_ID ) ;
2887+ throwIf$2 ( is$5 . undefined ( id ) , NO_BLOCK_ID ) ;
28662888 return get$5 ( this . web3data , {
2867- id,
2889+ pathParam : id ,
28682890 endpoint : ENDPOINT$4 ,
28692891 subendpoint : 'functions' ,
28702892 filterOptions
2871- } ) . then ( onFulfilled$4 , onError$4 )
2893+ } ) . then ( onFulfilled$4 . bind ( { formatter : recordsFormatter$1 } ) , onError$4 )
28722894 }
28732895
2874- // TODO: Needs tests
28752896 getMetrics ( filterOptions ) {
28762897 return get$5 ( this . web3data , {
28772898 endpoint : ENDPOINT$4 ,
37363757 *
37373758 * @param data - The parsed json data sent from the server.
37383759 * @private
3739- * @ignore
37403760 * @example
37413761 */
37423762 _subHandler ( data ) {
0 commit comments