2018-02-16 19:01:39 +00:00
|
|
|
import { checkHttpStatus, parseJSON } from './utils';
|
2018-03-07 23:36:05 +00:00
|
|
|
import { Omit } from 'react-redux';
|
2018-02-16 19:01:39 +00:00
|
|
|
|
|
|
|
const MAX_GAS_FAST = 250;
|
|
|
|
|
|
|
|
interface RawGasEstimates {
|
|
|
|
safeLow: number;
|
|
|
|
standard: number;
|
|
|
|
fast: number;
|
|
|
|
fastest: number;
|
|
|
|
block_time: number;
|
|
|
|
blockNum: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface GasEstimates {
|
|
|
|
safeLow: number;
|
|
|
|
standard: number;
|
|
|
|
fast: number;
|
|
|
|
fastest: number;
|
|
|
|
time: number;
|
2018-02-24 18:00:00 +00:00
|
|
|
chainId: number;
|
2018-02-16 19:01:39 +00:00
|
|
|
isDefault: boolean;
|
|
|
|
}
|
|
|
|
|
2018-03-07 23:36:05 +00:00
|
|
|
interface GasExpressResponse {
|
|
|
|
block_time: number;
|
|
|
|
blockNum: number;
|
|
|
|
fast: number;
|
|
|
|
fastest: number;
|
|
|
|
safeLow: number;
|
|
|
|
standard: number;
|
|
|
|
}
|
|
|
|
|
2018-02-16 19:01:39 +00:00
|
|
|
export function fetchGasEstimates(): Promise<GasEstimates> {
|
|
|
|
return fetch('https://dev.blockscale.net/api/gasexpress.json', {
|
|
|
|
mode: 'cors'
|
|
|
|
})
|
|
|
|
.then(checkHttpStatus)
|
|
|
|
.then(parseJSON)
|
2018-03-07 23:36:05 +00:00
|
|
|
.then((res: GasExpressResponse) => {
|
2018-02-16 19:01:39 +00:00
|
|
|
// Make sure it looks like a raw gas estimate, and it has valid values
|
2018-03-07 23:36:05 +00:00
|
|
|
const keys: (keyof Omit<GasExpressResponse, 'block_time' | 'blockNum'>)[] = [
|
|
|
|
'safeLow',
|
|
|
|
'standard',
|
|
|
|
'fast',
|
|
|
|
'fastest'
|
|
|
|
];
|
2018-02-16 19:01:39 +00:00
|
|
|
keys.forEach(key => {
|
|
|
|
if (typeof res[key] !== 'number') {
|
|
|
|
throw new Error(
|
|
|
|
`Gas estimate API has invalid shape: Expected numeric key '${key}' in response, got '${
|
|
|
|
res[key]
|
|
|
|
}' instead`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// Make sure the estimate isn't totally crazy
|
|
|
|
const estimateRes = res as RawGasEstimates;
|
|
|
|
if (estimateRes.fast > MAX_GAS_FAST) {
|
|
|
|
throw new Error(
|
|
|
|
`Gas estimate response estimate too high: Max fast is ${MAX_GAS_FAST}, was given ${
|
|
|
|
estimateRes.fast
|
|
|
|
}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (
|
|
|
|
estimateRes.safeLow > estimateRes.standard ||
|
|
|
|
estimateRes.standard > estimateRes.fast ||
|
|
|
|
estimateRes.fast > estimateRes.fastest
|
|
|
|
) {
|
|
|
|
throw new Error(
|
|
|
|
`Gas esimates are in illogical order: should be safeLow < standard < fast < fastest, received ${
|
|
|
|
estimateRes.safeLow
|
|
|
|
} < ${estimateRes.standard} < ${estimateRes.fast} < ${estimateRes.fastest}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return estimateRes;
|
|
|
|
})
|
|
|
|
.then((res: RawGasEstimates) => ({
|
|
|
|
...res,
|
|
|
|
time: Date.now(),
|
2018-02-24 18:00:00 +00:00
|
|
|
chainId: 1,
|
2018-02-16 19:01:39 +00:00
|
|
|
isDefault: false
|
|
|
|
}));
|
|
|
|
}
|