2017-06-29 23:03:11 +00:00
|
|
|
// @flow
|
2017-07-13 21:02:39 +00:00
|
|
|
import { takeEvery, call, apply, put, select, fork } from 'redux-saga/effects';
|
2017-07-03 23:59:27 +00:00
|
|
|
import type { Effect } from 'redux-saga/effects';
|
2017-07-13 21:02:39 +00:00
|
|
|
import { setWallet, setBalance, setTokenBalances } from 'actions/wallet';
|
2017-06-29 23:03:11 +00:00
|
|
|
import type { UnlockPrivateKeyAction } from 'actions/wallet';
|
2017-07-04 12:19:04 +00:00
|
|
|
import { showNotification } from 'actions/notifications';
|
|
|
|
import translate from 'translations';
|
2017-07-13 21:02:39 +00:00
|
|
|
import { PrivKeyWallet, BaseWallet } from 'libs/wallet';
|
|
|
|
import { BaseNode } from 'libs/nodes';
|
|
|
|
import { getNodeLib } from 'selectors/config';
|
|
|
|
import { getWalletInst, getTokens } from 'selectors/wallet';
|
|
|
|
|
|
|
|
function* updateAccountBalance() {
|
|
|
|
const node: BaseNode = yield select(getNodeLib);
|
|
|
|
const wallet: ?BaseWallet = yield select(getWalletInst);
|
|
|
|
if (!wallet) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let balance = yield apply(node, node.getBalance, [wallet.getAddress()]);
|
|
|
|
yield put(setBalance(balance));
|
|
|
|
}
|
|
|
|
|
|
|
|
function* updateTokenBalances() {
|
2017-08-08 03:45:08 +00:00
|
|
|
const node: BaseNode = yield select(getNodeLib);
|
2017-07-13 21:02:39 +00:00
|
|
|
const wallet: ?BaseWallet = yield select(getWalletInst);
|
|
|
|
const tokens = yield select(getTokens);
|
2017-08-08 03:45:08 +00:00
|
|
|
if (!wallet || !node) {
|
2017-07-13 21:02:39 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
// FIXME handle errors
|
2017-08-08 03:45:08 +00:00
|
|
|
const tokenBalances = yield apply(node, node.getTokenBalances, [
|
|
|
|
wallet.getAddress(),
|
|
|
|
tokens
|
|
|
|
]);
|
2017-07-13 21:02:39 +00:00
|
|
|
yield put(
|
|
|
|
setTokenBalances(
|
|
|
|
tokens.reduce((acc, t, i) => {
|
2017-08-08 03:45:08 +00:00
|
|
|
acc[t.symbol] = tokenBalances[i];
|
2017-07-13 21:02:39 +00:00
|
|
|
return acc;
|
|
|
|
}, {})
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
function* updateBalances() {
|
|
|
|
yield fork(updateAccountBalance);
|
|
|
|
yield fork(updateTokenBalances);
|
2017-07-03 23:59:27 +00:00
|
|
|
}
|
|
|
|
|
2017-07-16 21:02:13 +00:00
|
|
|
export function* unlockPrivateKey(
|
|
|
|
action?: UnlockPrivateKeyAction
|
|
|
|
): Generator<Effect, void, any> {
|
2017-07-04 03:21:19 +00:00
|
|
|
if (!action) return;
|
2017-07-04 12:19:04 +00:00
|
|
|
let wallet = null;
|
2017-07-13 21:02:39 +00:00
|
|
|
|
2017-07-04 12:19:04 +00:00
|
|
|
try {
|
2017-07-16 21:02:13 +00:00
|
|
|
wallet = new PrivKeyWallet(Buffer.from(action.payload.key, 'hex'));
|
2017-07-04 12:19:04 +00:00
|
|
|
} catch (e) {
|
|
|
|
yield put(showNotification('danger', translate('INVALID_PKEY')));
|
|
|
|
return;
|
|
|
|
}
|
2017-07-13 21:02:39 +00:00
|
|
|
yield put(setWallet(wallet));
|
|
|
|
yield call(updateBalances);
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|
|
|
|
|
2017-07-13 21:02:39 +00:00
|
|
|
export default function* walletSaga(): Generator<Effect | Effect[], void, any> {
|
|
|
|
// useful for development
|
|
|
|
yield call(updateBalances);
|
|
|
|
yield [
|
|
|
|
takeEvery('WALLET_UNLOCK_PRIVATE_KEY', unlockPrivateKey),
|
|
|
|
takeEvery('CUSTOM_TOKEN_ADD', updateTokenBalances)
|
|
|
|
];
|
2017-06-29 23:03:11 +00:00
|
|
|
}
|