173 lines
5.3 KiB
TypeScript
173 lines
5.3 KiB
TypeScript
import { SagaIterator, buffers, delay } from 'redux-saga';
|
|
import {
|
|
apply,
|
|
put,
|
|
select,
|
|
take,
|
|
actionChannel,
|
|
call,
|
|
fork,
|
|
race,
|
|
takeEvery
|
|
} from 'redux-saga/effects';
|
|
import BN from 'bn.js';
|
|
import { INode } from 'libs/nodes/INode';
|
|
import { getNodeLib, getOffline, getAutoGasLimitEnabled } from 'selectors/config';
|
|
import { getWalletInst } from 'selectors/wallet';
|
|
import { getTransaction, IGetTransaction, getCurrentToAddressMessage } from 'selectors/transaction';
|
|
import {
|
|
EstimateGasRequestedAction,
|
|
setGasLimitField,
|
|
estimateGasTimedout,
|
|
estimateGasSucceeded,
|
|
TypeKeys,
|
|
estimateGasRequested,
|
|
SetToFieldAction,
|
|
SetDataFieldAction,
|
|
SetValueFieldAction,
|
|
SwapEtherToTokenAction,
|
|
SwapTokenToTokenAction,
|
|
SwapTokenToEtherAction,
|
|
estimateGasFailed
|
|
} from 'actions/transaction';
|
|
import { TypeKeys as ConfigTypeKeys, ToggleAutoGasLimitAction } from 'actions/config';
|
|
import { IWallet } from 'libs/wallet';
|
|
import { makeTransaction, getTransactionFields, IHexStrTransaction } from 'libs/transaction';
|
|
import { AddressMessage } from 'config';
|
|
import { isSchedulingEnabled } from 'selectors/schedule/fields';
|
|
import { setScheduleGasLimitField } from 'actions/schedule';
|
|
|
|
export function* shouldEstimateGas(): SagaIterator {
|
|
while (true) {
|
|
const action:
|
|
| SetToFieldAction
|
|
| SetDataFieldAction
|
|
| SwapEtherToTokenAction
|
|
| SwapTokenToTokenAction
|
|
| SwapTokenToEtherAction
|
|
| ToggleAutoGasLimitAction
|
|
| SetValueFieldAction = yield take([
|
|
TypeKeys.TO_FIELD_SET,
|
|
TypeKeys.VALUE_FIELD_SET,
|
|
TypeKeys.DATA_FIELD_SET,
|
|
TypeKeys.ETHER_TO_TOKEN_SWAP,
|
|
TypeKeys.TOKEN_TO_TOKEN_SWAP,
|
|
TypeKeys.TOKEN_TO_ETHER_SWAP,
|
|
ConfigTypeKeys.CONFIG_TOGGLE_AUTO_GAS_LIMIT
|
|
]);
|
|
|
|
const isOffline: boolean = yield select(getOffline);
|
|
const autoGasLimitEnabled: boolean = yield select(getAutoGasLimitEnabled);
|
|
const message: AddressMessage | undefined = yield select(getCurrentToAddressMessage);
|
|
|
|
if (isOffline || !autoGasLimitEnabled || (message && message.gasLimit)) {
|
|
continue;
|
|
}
|
|
|
|
// invalid field is a field that the value is null and the input box isnt empty
|
|
// reason being is an empty field is valid because it'll be null
|
|
const invalidField =
|
|
(action.type === TypeKeys.TO_FIELD_SET ||
|
|
action.type === TypeKeys.DATA_FIELD_SET ||
|
|
action.type === TypeKeys.VALUE_FIELD_SET) &&
|
|
!action.payload.value &&
|
|
action.payload.raw !== '';
|
|
|
|
if (invalidField) {
|
|
continue;
|
|
}
|
|
const { transaction }: IGetTransaction = yield select(getTransaction);
|
|
|
|
const { gasLimit, gasPrice, nonce, chainId, ...rest }: IHexStrTransaction = yield call(
|
|
getTransactionFields,
|
|
transaction
|
|
);
|
|
|
|
// gas estimation calls with
|
|
// '0x' as an address (contract creation)
|
|
// fail, so instead we set it as undefined
|
|
// interestingly, the transaction itself as '0x' as the
|
|
// to address works fine.
|
|
if (rest.to === '0x') {
|
|
rest.to = undefined as any;
|
|
}
|
|
|
|
yield put(estimateGasRequested(rest));
|
|
}
|
|
}
|
|
|
|
export function* estimateGas(): SagaIterator {
|
|
const requestChan = yield actionChannel(TypeKeys.ESTIMATE_GAS_REQUESTED, buffers.sliding(1));
|
|
|
|
while (true) {
|
|
const autoGasLimitEnabled: boolean = yield select(getAutoGasLimitEnabled);
|
|
const isOffline = yield select(getOffline);
|
|
|
|
if (isOffline || !autoGasLimitEnabled) {
|
|
continue;
|
|
}
|
|
|
|
const { payload }: EstimateGasRequestedAction = yield take(requestChan);
|
|
// debounce 250 ms
|
|
yield call(delay, 250);
|
|
const node: INode = yield select(getNodeLib);
|
|
const walletInst: IWallet = yield select(getWalletInst);
|
|
try {
|
|
const from: string = yield apply(walletInst, walletInst.getAddressString);
|
|
const txObj = { ...payload, from };
|
|
|
|
const { gasLimit } = yield race({
|
|
gasLimit: apply(node, node.estimateGas, [txObj]),
|
|
timeout: call(delay, 10000)
|
|
});
|
|
if (gasLimit) {
|
|
const gasSetOptions = {
|
|
raw: gasLimit.toString(),
|
|
value: gasLimit
|
|
};
|
|
|
|
const scheduling: boolean = yield select(isSchedulingEnabled);
|
|
|
|
if (scheduling) {
|
|
yield put(setScheduleGasLimitField(gasSetOptions));
|
|
} else {
|
|
yield put(setGasLimitField(gasSetOptions));
|
|
}
|
|
|
|
yield put(estimateGasSucceeded());
|
|
} else {
|
|
yield put(estimateGasTimedout());
|
|
yield call(localGasEstimation, payload);
|
|
}
|
|
} catch (e) {
|
|
yield put(estimateGasFailed());
|
|
yield call(localGasEstimation, payload);
|
|
}
|
|
}
|
|
}
|
|
|
|
export function* localGasEstimation(payload: EstimateGasRequestedAction['payload']) {
|
|
const tx = yield call(makeTransaction, payload);
|
|
const gasLimit = yield apply(tx, tx.getBaseFee);
|
|
yield put(setGasLimitField({ raw: gasLimit.toString(), value: gasLimit }));
|
|
}
|
|
|
|
export function* setAddressMessageGasLimit() {
|
|
const autoGasLimitEnabled: boolean = yield select(getAutoGasLimitEnabled);
|
|
const message: AddressMessage | undefined = yield select(getCurrentToAddressMessage);
|
|
if (autoGasLimitEnabled && message && message.gasLimit) {
|
|
yield put(
|
|
setGasLimitField({
|
|
raw: message.gasLimit.toString(),
|
|
value: new BN(message.gasLimit)
|
|
})
|
|
);
|
|
}
|
|
}
|
|
|
|
export const gas = [
|
|
fork(shouldEstimateGas),
|
|
fork(estimateGas),
|
|
takeEvery(TypeKeys.TO_FIELD_SET, setAddressMessageGasLimit)
|
|
];
|