1- import { BigNumber } from '@0x/utils' ;
21import * as _ from 'lodash' ;
32
43import { Web3Wrapper } from '@0x/web3-wrapper' ;
54import { ZeroExProvider } from 'ethereum-types' ;
65
76import {
7+ EIP1559GasInfo ,
88 GasInfo ,
99 MailchimpSubscriberInfo ,
1010 WebsiteBackendCFLMetricsData ,
@@ -17,7 +17,6 @@ import {
1717 WebsiteBackendTokenInfo ,
1818 WebsiteBackendTradingPairs ,
1919} from 'ts/types' ;
20- import { constants } from 'ts/utils/constants' ;
2120import { fetchUtils } from 'ts/utils/fetch_utils' ;
2221import { utils } from 'ts/utils/utils' ;
2322
@@ -41,11 +40,18 @@ export interface GasInfoSelection {
4140 fast : number ;
4241}
4342
44- const speedToSelectionMap : { [ key : string ] : string } = {
45- standard : 'average' ,
46- fast : 'fast' ,
47- instant : 'fastest' ,
48- } ;
43+ interface GasApiSingleSourceResponse {
44+ result : {
45+ source : string ;
46+ timestamp : number ;
47+ instant : EIP1559GasInfo ;
48+ fast : EIP1559GasInfo ;
49+ standard : EIP1559GasInfo ;
50+ low : EIP1559GasInfo ;
51+ } ;
52+ }
53+
54+ type GasSpeedSelectors = 'instant' | 'fast' | 'standard' | 'low' ;
4955
5056const speedToWaitTimeMap : { [ key : string ] : string } = {
5157 standard : 'avgWait' ,
@@ -199,29 +205,22 @@ export const backendClient = {
199205 return data ;
200206 } ,
201207
202- async getGasInfoAsync ( speed ?: string ) : Promise < GasInfo > {
203- // Median gas prices across 0x api gas oracles
204- // Defaulting to average/standard gas. Using eth gas station for time estimates
205- const gasApiPath = 'source/gas_now?output=eth_gas_station' ;
206- const gasInfoReq = fetchUtils . requestAsync ( ZEROEX_GAS_API , gasApiPath ) ;
207- const speedInput = speed || 'standard' ;
208+ async getGasInfoAsync ( speed ?: GasSpeedSelectors ) : Promise < GasInfo > {
209+ const gasApiPath = 'v2/source/block_native' ;
210+ const gasInfoReq : Promise < GasApiSingleSourceResponse > = fetchUtils . requestAsync ( ZEROEX_GAS_API , gasApiPath ) ;
211+ const speedInput : GasSpeedSelectors = speed || 'standard' ;
208212
209- const gasSpeed = speedToSelectionMap [ speedInput ] ;
210- const waitTime = speedToWaitTimeMap [ speedInput ] ;
211213 const gasWaitTimesReq = fetchUtils . requestAsync ( utils . getBackendBaseUrl ( ) , ETH_GAS_STATION_ENDPOINT ) ;
212214
213- const res : [ WebsiteBackendGasInfo , WebsiteBackendGasWaitTimeInfo ] = await Promise . all ( [
215+ const [ gasInfo , gasWaitTimes ] : [ GasApiSingleSourceResponse , WebsiteBackendGasWaitTimeInfo ] = await Promise . all ( [
214216 gasInfoReq ,
215217 gasWaitTimesReq ,
216218 ] ) ;
217- const gasInfo = res [ 0 ] ;
218- const gasWaitTimes = res [ 1 ] ;
219- // Eth Gas Station result is gwei * 10
220- const gasPriceInGwei = new BigNumber ( ( gasInfo as any ) [ gasSpeed ] / 10 ) ;
221219 // Time is in minutes
220+ const waitTime = speedToWaitTimeMap [ speedInput ] ;
222221 const estimatedTimeMs = ( gasWaitTimes as any ) [ waitTime ] * 60 * 1000 ; // Minutes to MS
223222
224- return { gasPriceInWei : gasPriceInGwei . multipliedBy ( constants . GWEI_IN_WEI ) , estimatedTimeMs } ;
223+ return { ... gasInfo . result [ speedInput ] , estimatedTimeMs } ;
225224 } ,
226225
227226 async getGasInfoSelectionAsync ( ) : Promise < GasInfoSelection > {
0 commit comments