45 lines
1.7 KiB
TypeScript
45 lines
1.7 KiB
TypeScript
import { getNonceSucceeded, getNonceFailed, TypeKeys as TK, inputNonce } from 'actions/transaction';
|
|
import { SagaIterator } from 'redux-saga';
|
|
import { apply, put, select, takeEvery, fork, cancel, take } from 'redux-saga/effects';
|
|
import { INode } from 'libs/nodes/INode';
|
|
import { AppState } from 'reducers';
|
|
import { getNodeLib, getOffline } from 'selectors/config';
|
|
import { getWalletInst } from 'selectors/wallet';
|
|
import { showNotification } from 'actions/notifications';
|
|
import { TypeKeys as WalletTK } from 'actions/wallet';
|
|
import { Nonce } from 'libs/units';
|
|
|
|
export function* handleNonceRequest(): SagaIterator {
|
|
const nodeLib: INode = yield select(getNodeLib);
|
|
const walletInst: AppState['wallet']['inst'] = yield select(getWalletInst);
|
|
const isOffline: boolean = yield select(getOffline);
|
|
try {
|
|
if (isOffline || !walletInst) {
|
|
return;
|
|
}
|
|
|
|
const fromAddress: string = yield apply(walletInst, walletInst.getAddressString);
|
|
|
|
const retrievedNonce: string = yield apply(nodeLib, nodeLib.getTransactionCount, [fromAddress]);
|
|
const base10Nonce = Nonce(retrievedNonce);
|
|
yield put(inputNonce(base10Nonce.toString()));
|
|
yield put(getNonceSucceeded(retrievedNonce));
|
|
} catch {
|
|
yield put(showNotification('warning', 'Your addresses nonce could not be fetched'));
|
|
yield put(getNonceFailed());
|
|
}
|
|
}
|
|
|
|
export function* handleNonceRequestWrapper(): SagaIterator {
|
|
const nonceRequest = yield fork(handleNonceRequest);
|
|
|
|
yield take(WalletTK.WALLET_SET);
|
|
yield cancel(nonceRequest);
|
|
}
|
|
|
|
//leave get nonce requested for nonce refresh later on
|
|
export const nonce = takeEvery(
|
|
[TK.GET_NONCE_REQUESTED, WalletTK.WALLET_SET],
|
|
handleNonceRequestWrapper
|
|
);
|