feat: granular balances

+ We were not using the total balances stuff I removed -that was an attempt by Akin at one point to work around the SDK cross account balance issue.
+ Decided to keep `balance` as is so we don't have to change it everywhere else...still makes sense I think.
This commit is contained in:
Thomas Zarebczan 2019-09-23 18:56:53 -04:00
parent 52bca60b3e
commit 45e585c9cf
6 changed files with 86 additions and 87 deletions

77
dist/bundle.es.js vendored
View file

@ -754,7 +754,7 @@ const Lbry = {
blob_list: (params = {}) => daemonCallWithResult('blob_list', params),
// Wallet utilities
account_balance: () => daemonCallWithResult('account_balance'),
account_balance: (params = {}) => daemonCallWithResult('account_balance', params),
account_decrypt: () => daemonCallWithResult('account_decrypt', {}),
account_encrypt: (params = {}) => daemonCallWithResult('account_encrypt', params),
account_unlock: (params = {}) => daemonCallWithResult('account_unlock', params),
@ -1317,6 +1317,14 @@ const selectBalance = reselect.createSelector(selectState$1, state => state.bala
const selectTotalBalance = reselect.createSelector(selectState$1, state => state.totalBalance);
const selectReservedBalance = reselect.createSelector(selectState$1, state => state.reservedBalance);
const selectClaimsBalance = reselect.createSelector(selectState$1, state => state.claimsBalance);
const selectSupportsBalance = reselect.createSelector(selectState$1, state => state.supportsBalance);
const selectTipsBalance = reselect.createSelector(selectState$1, state => state.tipsBalance);
const selectTransactionsById = reselect.createSelector(selectState$1, state => state.transactions || {});
const selectSupportsByOutpoint = reselect.createSelector(selectState$1, state => state.supports || {});
@ -1879,37 +1887,22 @@ function creditsToString(amount) {
function doUpdateBalance() {
return (dispatch, getState) => {
const {
wallet: { balance: balanceInStore }
wallet: { total: totalInStore }
} = getState();
lbryProxy.account_balance().then(response => {
const { available } = response;
const balance = parseFloat(available);
if (balanceInStore !== balance) {
lbryProxy.account_balance({ reserved_subtotals: true }).then(response => {
const { available, reserved, reserved_subtotals, total } = response;
const { claims, supports, tips } = reserved_subtotals;
const totalFloat = parseFloat(total);
if (totalInStore !== totalFloat) {
dispatch({
type: UPDATE_BALANCE,
data: {
balance
}
});
}
});
};
}
function doUpdateTotalBalance() {
return (dispatch, getState) => {
const {
wallet: { totalBalance: totalBalanceInStore }
} = getState();
lbryProxy.account_list().then(accountList => {
const { lbc_mainnet: accounts } = accountList;
const totalSatoshis = accounts.length === 1 ? accounts[0].satoshis : accounts.reduce((a, b) => a.satoshis + b.satoshis);
const totalBalance = (Number.isNaN(totalSatoshis) ? 0 : totalSatoshis) / Math.pow(10, 8);
if (totalBalanceInStore !== totalBalance) {
dispatch({
type: UPDATE_TOTAL_BALANCE,
data: {
totalBalance
totalBalance: totalFloat,
balance: parseFloat(available),
reservedBalance: parseFloat(reserved),
claimsBalance: parseFloat(claims),
supportsBalance: parseFloat(supports),
tipsBalance: parseFloat(tips)
}
});
}
@ -1924,13 +1917,6 @@ function doBalanceSubscribe() {
};
}
function doTotalBalanceSubscribe() {
return dispatch => {
dispatch(doUpdateTotalBalance());
setInterval(() => dispatch(doUpdateTotalBalance()), 5000);
};
}
function doFetchTransactions() {
return dispatch => {
dispatch(doFetchSupports());
@ -4624,6 +4610,10 @@ const buildDraftTransaction = () => ({
const defaultState$a = {
balance: undefined,
totalBalance: undefined,
reservedBalance: undefined,
claimsBalance: undefined,
supportsBalance: undefined,
tipsBalance: undefined,
latestBlock: undefined,
transactions: {},
fetchingTransactions: false,
@ -4720,11 +4710,12 @@ const walletReducer = handleActions({
},
[UPDATE_BALANCE]: (state, action) => _extends$e({}, state, {
balance: action.data.balance
}),
[UPDATE_TOTAL_BALANCE]: (state, action) => _extends$e({}, state, {
totalBalance: action.data.totalBalance
totalBalance: action.data.totalBalance,
balance: action.data.balance,
reservedBalance: action.data.reservedBalance,
claimsBalance: action.data.claimsBalance,
supportsBalance: action.data.supportsBalance,
tipsBalance: action.data.tipsBalance
}),
[CHECK_ADDRESS_IS_MINE_STARTED]: state => _extends$e({}, state, {
@ -5034,14 +5025,12 @@ exports.doSetTransactionListFilter = doSetTransactionListFilter;
exports.doToast = doToast;
exports.doToggleBlockChannel = doToggleBlockChannel;
exports.doToggleTagFollow = doToggleTagFollow;
exports.doTotalBalanceSubscribe = doTotalBalanceSubscribe;
exports.doUpdateBalance = doUpdateBalance;
exports.doUpdateBlockHeight = doUpdateBlockHeight;
exports.doUpdateChannel = doUpdateChannel;
exports.doUpdatePublishForm = doUpdatePublishForm;
exports.doUpdateSearchOptions = doUpdateSearchOptions;
exports.doUpdateSearchQuery = doUpdateSearchQuery;
exports.doUpdateTotalBalance = doUpdateTotalBalance;
exports.doUploadThumbnail = doUploadThumbnail;
exports.doWalletDecrypt = doWalletDecrypt;
exports.doWalletEncrypt = doWalletEncrypt;
@ -5121,6 +5110,7 @@ exports.selectChannelImportPending = selectChannelImportPending;
exports.selectChannelIsBlocked = selectChannelIsBlocked;
exports.selectClaimSearchByQuery = selectClaimSearchByQuery;
exports.selectClaimSearchByQueryLastPageReached = selectClaimSearchByQueryLastPageReached;
exports.selectClaimsBalance = selectClaimsBalance;
exports.selectClaimsById = selectClaimsById;
exports.selectClaimsByUri = selectClaimsByUri;
exports.selectCreateChannelError = selectCreateChannelError;
@ -5170,6 +5160,7 @@ exports.selectPurchaseUriErrorMessage = selectPurchaseUriErrorMessage;
exports.selectPurchasedUris = selectPurchasedUris;
exports.selectReceiveAddress = selectReceiveAddress;
exports.selectRecentTransactions = selectRecentTransactions;
exports.selectReservedBalance = selectReservedBalance;
exports.selectResolvingUris = selectResolvingUris;
exports.selectSearchBarFocused = selectSearchBarFocused;
exports.selectSearchOptions = selectSearchOptions;
@ -5177,8 +5168,10 @@ exports.selectSearchState = selectState;
exports.selectSearchSuggestions = selectSearchSuggestions;
exports.selectSearchUrisByQuery = selectSearchUrisByQuery;
exports.selectSearchValue = selectSearchValue;
exports.selectSupportsBalance = selectSupportsBalance;
exports.selectSupportsByOutpoint = selectSupportsByOutpoint;
exports.selectTakeOverAmount = selectTakeOverAmount;
exports.selectTipsBalance = selectTipsBalance;
exports.selectToast = selectToast;
exports.selectTotalBalance = selectTotalBalance;
exports.selectTotalDownloadProgress = selectTotalDownloadProgress;

View file

@ -91,8 +91,6 @@ export { savePosition } from 'redux/actions/content';
export {
doUpdateBalance,
doBalanceSubscribe,
doUpdateTotalBalance,
doTotalBalanceSubscribe,
doFetchTransactions,
doGetNewAddress,
doCheckAddressIsMine,
@ -258,6 +256,10 @@ export {
export {
selectBalance,
selectTotalBalance,
selectReservedBalance,
selectClaimsBalance,
selectSupportsBalance,
selectTipsBalance,
selectTransactionsById,
selectSupportsByOutpoint,
selectTotalSupports,

View file

@ -93,7 +93,7 @@ const Lbry: LbryTypes = {
blob_list: (params = {}) => daemonCallWithResult('blob_list', params),
// Wallet utilities
account_balance: () => daemonCallWithResult('account_balance'),
account_balance: (params = {}) => daemonCallWithResult('account_balance', params),
account_decrypt: () => daemonCallWithResult('account_decrypt', {}),
account_encrypt: (params = {}) => daemonCallWithResult('account_encrypt', params),
account_unlock: (params = {}) => daemonCallWithResult('account_unlock', params),

View file

@ -8,40 +8,22 @@ import { selectMyClaimsRaw } from 'redux/selectors/claims';
export function doUpdateBalance() {
return (dispatch, getState) => {
const {
wallet: { balance: balanceInStore },
wallet: { total: totalInStore },
} = getState();
Lbry.account_balance().then(response => {
const { available } = response;
const balance = parseFloat(available);
if (balanceInStore !== balance) {
Lbry.account_balance({reserved_subtotals: true}).then((response: BalanceResponse) => {
const { available, reserved, reserved_subtotals, total } = response;
const { claims, supports, tips } = reserved_subtotals;
const totalFloat = parseFloat(total);
if (totalInStore !== totalFloat) {
dispatch({
type: ACTIONS.UPDATE_BALANCE,
data: {
balance,
},
});
}
});
};
}
export function doUpdateTotalBalance() {
return (dispatch, getState) => {
const {
wallet: { totalBalance: totalBalanceInStore },
} = getState();
Lbry.account_list().then(accountList => {
const { lbc_mainnet: accounts } = accountList;
const totalSatoshis =
accounts.length === 1
? accounts[0].satoshis
: accounts.reduce((a, b) => a.satoshis + b.satoshis);
const totalBalance = (Number.isNaN(totalSatoshis) ? 0 : totalSatoshis) / 10 ** 8;
if (totalBalanceInStore !== totalBalance) {
dispatch({
type: ACTIONS.UPDATE_TOTAL_BALANCE,
data: {
totalBalance,
totalBalance: totalFloat,
balance: parseFloat(available),
reservedBalance: parseFloat(reserved),
claimsBalance: parseFloat(claims),
supportsBalance: parseFloat(supports),
tipsBalance: parseFloat(tips),
},
});
}
@ -56,13 +38,6 @@ export function doBalanceSubscribe() {
};
}
export function doTotalBalanceSubscribe() {
return dispatch => {
dispatch(doUpdateTotalBalance());
setInterval(() => dispatch(doUpdateTotalBalance()), 5000);
};
}
export function doFetchTransactions() {
return dispatch => {
dispatch(doFetchSupports());

View file

@ -16,6 +16,11 @@ type ActionResult = {
type WalletState = {
balance: any,
totalBalance: any,
reservedBalance: any,
claimsBalance: any,
supportsBalance: any,
tipsBalance: any,
latestBlock: ?number,
transactions: { [string]: Transaction },
supports: { [string]: Support },
@ -42,6 +47,10 @@ type WalletState = {
const defaultState = {
balance: undefined,
totalBalance: undefined,
reservedBalance: undefined,
claimsBalance: undefined,
supportsBalance: undefined,
tipsBalance: undefined,
latestBlock: undefined,
transactions: {},
fetchingTransactions: false,
@ -145,13 +154,13 @@ export const walletReducer = handleActions(
},
[ACTIONS.UPDATE_BALANCE]: (state: WalletState, action) => ({
...state,
balance: action.data.balance,
}),
[ACTIONS.UPDATE_TOTAL_BALANCE]: (state: WalletState, action) => ({
...state,
totalBalance: action.data.totalBalance,
balance: action.data.balance,
reservedBalance: action.data.reservedBalance,
claimsBalance: action.data.claimsBalance,
supportsBalance: action.data.supportsBalance,
tipsBalance: action.data.tipsBalance,
}),
[ACTIONS.CHECK_ADDRESS_IS_MINE_STARTED]: (state: WalletState) => ({

View file

@ -80,6 +80,26 @@ export const selectTotalBalance = createSelector(
state => state.totalBalance
);
export const selectReservedBalance = createSelector(
selectState,
state => state.reservedBalance
);
export const selectClaimsBalance = createSelector(
selectState,
state => state.claimsBalance
);
export const selectSupportsBalance = createSelector(
selectState,
state => state.supportsBalance
);
export const selectTipsBalance = createSelector(
selectState,
state => state.tipsBalance
);
export const selectTransactionsById = createSelector(
selectState,
state => state.transactions || {}