2018-02-16 19:01:39 +00:00
|
|
|
import { setGasEstimates, TypeKeys } from 'actions/gas';
|
|
|
|
import { SagaIterator } from 'redux-saga';
|
|
|
|
import { call, put, select, takeLatest } from 'redux-saga/effects';
|
|
|
|
import { AppState } from 'reducers';
|
|
|
|
import { fetchGasEstimates, GasEstimates } from 'api/gas';
|
|
|
|
import { gasPriceDefaults, gasEstimateCacheTime } from 'config';
|
|
|
|
import { getEstimates } from 'selectors/gas';
|
2018-02-24 18:00:00 +00:00
|
|
|
import { getOffline, getNetworkConfig } from 'selectors/config';
|
|
|
|
import { NetworkConfig } from 'types/network';
|
2018-02-16 19:01:39 +00:00
|
|
|
|
2018-02-24 18:00:00 +00:00
|
|
|
export function* setDefaultEstimates(network: NetworkConfig): SagaIterator {
|
2018-02-16 19:01:39 +00:00
|
|
|
// Must yield time for testability
|
|
|
|
const time = yield call(Date.now);
|
2018-02-24 18:00:00 +00:00
|
|
|
const gasSettings = network.isCustom ? gasPriceDefaults : network.gasPriceSettings;
|
2018-02-16 19:01:39 +00:00
|
|
|
|
|
|
|
yield put(
|
|
|
|
setGasEstimates({
|
2018-02-24 18:00:00 +00:00
|
|
|
safeLow: gasSettings.min,
|
|
|
|
standard: gasSettings.initial,
|
|
|
|
fast: gasSettings.initial,
|
|
|
|
fastest: gasSettings.max,
|
2018-02-16 19:01:39 +00:00
|
|
|
isDefault: true,
|
2018-02-24 18:00:00 +00:00
|
|
|
chainId: network.chainId,
|
2018-02-16 19:01:39 +00:00
|
|
|
time
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
export function* fetchEstimates(): SagaIterator {
|
2018-02-24 18:00:00 +00:00
|
|
|
// Don't try on non-estimating network
|
|
|
|
const network: NetworkConfig = yield select(getNetworkConfig);
|
|
|
|
if (network.isCustom || !network.shouldEstimateGasPrice) {
|
|
|
|
yield call(setDefaultEstimates, network);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't try while offline
|
2018-02-16 19:01:39 +00:00
|
|
|
const isOffline: boolean = yield select(getOffline);
|
|
|
|
if (isOffline) {
|
2018-02-24 18:00:00 +00:00
|
|
|
yield call(setDefaultEstimates, network);
|
2018-02-16 19:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache estimates for a bit
|
|
|
|
const oldEstimates: AppState['gas']['estimates'] = yield select(getEstimates);
|
2018-02-24 18:00:00 +00:00
|
|
|
if (
|
|
|
|
oldEstimates &&
|
|
|
|
oldEstimates.chainId === network.chainId &&
|
|
|
|
oldEstimates.time + gasEstimateCacheTime > Date.now()
|
|
|
|
) {
|
2018-02-16 19:01:39 +00:00
|
|
|
yield put(setGasEstimates(oldEstimates));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fetch new estimates
|
|
|
|
try {
|
|
|
|
const estimates: GasEstimates = yield call(fetchGasEstimates);
|
|
|
|
yield put(setGasEstimates(estimates));
|
|
|
|
} catch (err) {
|
|
|
|
console.warn('Failed to fetch gas estimates:', err);
|
2018-02-24 18:00:00 +00:00
|
|
|
yield call(setDefaultEstimates, network);
|
2018-02-16 19:01:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default function* gas(): SagaIterator {
|
|
|
|
yield takeLatest(TypeKeys.GAS_FETCH_ESTIMATES, fetchEstimates);
|
|
|
|
}
|