Skip to content
This repository was archived by the owner on Oct 11, 2024. It is now read-only.

Commit c230474

Browse files
authored
Merge pull request #380 from 0xProject/feat/update-gas-source
Updating to use 0x Gas API
2 parents f0eac3c + f2a412d commit c230474

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

ts/types.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -821,17 +821,19 @@ export interface GasInfo {
821821
estimatedTimeMs: number;
822822
}
823823

824-
export interface WebsiteBackendGasInfo {
825-
average: number;
824+
export interface WebsiteBackendGasWaitTimeInfo {
826825
fastestWait: number;
827826
fastWait: number;
828-
fast: number;
829827
safeLowWait: number;
830828
blockNum: number;
831829
avgWait: number;
832830
block_time: number;
833-
speed: number;
831+
}
832+
833+
export interface WebsiteBackendGasInfo {
834834
fastest: number;
835+
fast: number;
836+
average: number;
835837
safeLow: number;
836838
}
837839

ts/utils/backend_client.ts

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {
66
MailchimpSubscriberInfo,
77
WebsiteBackendCFLMetricsData,
88
WebsiteBackendGasInfo,
9+
WebsiteBackendGasWaitTimeInfo,
910
WebsiteBackendJobInfo,
1011
WebsiteBackendPriceInfo,
1112
WebsiteBackendRelayerInfo,
@@ -17,6 +18,8 @@ import { constants } from 'ts/utils/constants';
1718
import { fetchUtils } from 'ts/utils/fetch_utils';
1819
import { utils } from 'ts/utils/utils';
1920

21+
const ZEROEX_GAS_API = 'https://gas.api.0x.org/';
22+
2023
const ETH_GAS_STATION_ENDPOINT = '/eth_gas_station';
2124
const JOBS_ENDPOINT = '/jobs';
2225
const PRICES_ENDPOINT = '/prices';
@@ -29,17 +32,26 @@ const STAKING_POOLS_ENDPOINT = '/staking-pools';
2932

3033
export const backendClient = {
3134
async getGasInfoAsync(): Promise<GasInfo> {
32-
const gasInfo: WebsiteBackendGasInfo = await fetchUtils.requestAsync(
33-
utils.getBackendBaseUrl(),
34-
ETH_GAS_STATION_ENDPOINT,
35-
);
35+
// Median gas prices across 0x api gas oracles
36+
// Defaulting to average/standard gas. Using eth gas station for time estimates
37+
const gasApiPath = 'source/median?output=eth_gas_station';
38+
const gasInfoReq = fetchUtils.requestAsync(ZEROEX_GAS_API, gasApiPath);
39+
const gasWaitTimesReq = fetchUtils.requestAsync(utils.getBackendBaseUrl(), ETH_GAS_STATION_ENDPOINT);
40+
41+
const res: [WebsiteBackendGasInfo, WebsiteBackendGasWaitTimeInfo] = await Promise.all([
42+
gasInfoReq,
43+
gasWaitTimesReq,
44+
]);
45+
const gasInfo = res[0];
46+
const gasWaitTimes = res[1];
3647

3748
// Eth Gas Station result is gwei * 10
38-
const gasPriceInGwei = new BigNumber(gasInfo.fast / 10);
49+
const gasPriceInGwei = new BigNumber(gasInfo.average / 10);
3950
// Time is in minutes
40-
const estimatedTimeMs = gasInfo.fastWait * 60 * 1000; // Minutes to MS
51+
const estimatedTimeMs = gasWaitTimes.avgWait * 60 * 1000; // Minutes to MS
4152
return { gasPriceInWei: gasPriceInGwei.multipliedBy(constants.GWEI_IN_WEI), estimatedTimeMs };
4253
},
54+
4355
async getJobInfosAsync(): Promise<WebsiteBackendJobInfo[]> {
4456
const result = await fetchUtils.requestAsync(utils.getBackendBaseUrl(), JOBS_ENDPOINT);
4557
return result;

0 commit comments

Comments
 (0)