2018-02-16 19:01:39 +00:00
|
|
|
import { SagaIterator } from 'redux-saga';
|
|
|
|
import { call, put, select, takeLatest } from 'redux-saga/effects';
|
2018-06-18 01:53:00 +00:00
|
|
|
|
2018-02-16 19:01:39 +00:00
|
|
|
import { gasPriceDefaults, gasEstimateCacheTime } from 'config';
|
2018-06-18 01:53:00 +00:00
|
|
|
import { fetchGasEstimates, GasEstimates } from 'api/gas';
|
2018-02-24 18:00:00 +00:00
|
|
|
import { NetworkConfig } from 'types/network';
|
2018-06-18 01:53:00 +00:00
|
|
|
import { AppState } from 'features/reducers';
|
|
|
|
import * as configMetaSelectors from 'features/config/meta/selectors';
|
|
|
|
import * as configSelectors from 'features/config/selectors';
|
|
|
|
import * as types from './types';
|
|
|
|
import * as actions from './actions';
|
|
|
|
import * as selectors from './selectors';
|
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(
|
2018-06-18 01:53:00 +00:00
|
|
|
actions.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
|
2018-06-18 01:53:00 +00:00
|
|
|
const network: NetworkConfig = yield select(configSelectors.getNetworkConfig);
|
2018-02-24 18:00:00 +00:00
|
|
|
if (network.isCustom || !network.shouldEstimateGasPrice) {
|
|
|
|
yield call(setDefaultEstimates, network);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Don't try while offline
|
2018-06-18 01:53:00 +00:00
|
|
|
const isOffline: boolean = yield select(configMetaSelectors.getOffline);
|
2018-02-16 19:01:39 +00:00
|
|
|
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
|
2018-06-18 01:53:00 +00:00
|
|
|
const oldEstimates: AppState['gas']['estimates'] = yield select(selectors.getEstimates);
|
2018-02-24 18:00:00 +00:00
|
|
|
if (
|
|
|
|
oldEstimates &&
|
|
|
|
oldEstimates.chainId === network.chainId &&
|
|
|
|
oldEstimates.time + gasEstimateCacheTime > Date.now()
|
|
|
|
) {
|
2018-06-18 01:53:00 +00:00
|
|
|
yield put(actions.setGasEstimates(oldEstimates));
|
2018-02-16 19:01:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fetch new estimates
|
|
|
|
try {
|
|
|
|
const estimates: GasEstimates = yield call(fetchGasEstimates);
|
2018-06-18 01:53:00 +00:00
|
|
|
yield put(actions.setGasEstimates(estimates));
|
2018-02-16 19:01:39 +00:00
|
|
|
} 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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-18 01:53:00 +00:00
|
|
|
export function* gasSaga(): SagaIterator {
|
|
|
|
yield takeLatest(types.GasActions.FETCH_ESTIMATES, fetchEstimates);
|
2018-02-16 19:01:39 +00:00
|
|
|
}
|