lbry-desktop/ui/js/redux/selectors/wallet.js

150 lines
3.6 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import { createSelector } from "reselect";
2017-04-22 15:17:01 +02:00
2017-06-06 06:21:55 +02:00
export const _selectState = state => state.wallet || {};
2017-04-22 15:17:01 +02:00
export const selectBalance = createSelector(
_selectState,
state => state.balance
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
export const selectTransactionsById = createSelector(
_selectState,
state => state.transactions
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
export const selectTransactionItems = createSelector(
selectTransactionsById,
2017-06-06 23:19:12 +02:00
byId => {
const items = [];
Object.keys(byId).forEach(txid => {
2017-06-06 23:19:12 +02:00
const tx = byId[txid];
//ignore dust/fees
if (Math.abs(tx.amount) === Math.abs(tx.fee)) {
return;
}
let append = [];
append.push(
...tx.claim_info.map(item =>
2017-09-20 21:27:25 +02:00
Object.assign({}, tx, item, {
type: item.claim_name[0] === "@" ? "channel" : "publish",
})
)
);
append.push(
...tx.support_info.map(item =>
2017-09-20 21:27:25 +02:00
Object.assign({}, tx, item, {
type: !item.is_tip ? "support" : "tip",
})
)
);
append.push(
...tx.update_info.map(item =>
2017-09-20 21:27:25 +02:00
Object.assign({}, tx, item, { type: "update" })
)
);
if (!append.length) {
append.push(
Object.assign({}, tx, {
type: tx.value < 0 ? "spend" : "receive",
})
);
}
items.push(
...append.map(item => {
2017-09-20 16:39:51 +02:00
//value on transaction, amount on outpoint
//amount is always positive, but should match sign of value
const amount = parseFloat(
2017-09-20 21:27:25 +02:00
item.amount ? (item.value < 0 ? -1 : 1) * item.amount : item.value
2017-09-20 16:39:51 +02:00
);
return {
txid: txid,
date: tx.timestamp ? new Date(parseInt(tx.timestamp) * 1000) : null,
amount: amount,
fee: amount < 0 ? -1 * tx.fee / append.length : 0,
claim_id: item.claim_id,
claim_name: item.claim_name,
type: item.type || "send",
nout: item.nout,
};
})
);
2017-06-06 23:19:12 +02:00
});
return items.reverse();
2017-04-22 15:17:01 +02:00
}
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
2017-08-20 23:42:00 +02:00
export const selectRecentTransactions = createSelector(
selectTransactionItems,
transactions => {
let threshold = new Date();
threshold.setDate(threshold.getDate() - 7);
return transactions.filter(transaction => {
return transaction.date > threshold;
});
}
);
export const selectHasTransactions = createSelector(
selectTransactionItems,
transactions => {
return transactions && transactions.length > 0;
}
);
2017-04-22 15:17:01 +02:00
export const selectIsFetchingTransactions = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.fetchingTransactions
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
2017-09-17 22:33:52 +02:00
export const selectIsSendingSupport = createSelector(
_selectState,
state => state.sendingSupport
);
2017-04-22 15:17:01 +02:00
export const selectReceiveAddress = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.receiveAddress
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
export const selectGettingNewAddress = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.gettingNewAddress
2017-06-06 06:21:55 +02:00
);
2017-04-22 15:17:01 +02:00
2017-04-23 07:55:47 +02:00
export const selectDraftTransaction = createSelector(
_selectState,
2017-06-06 23:19:12 +02:00
state => state.draftTransaction || {}
2017-06-06 06:21:55 +02:00
);
2017-04-23 07:55:47 +02:00
export const selectDraftTransactionAmount = createSelector(
selectDraftTransaction,
2017-06-06 23:19:12 +02:00
draft => draft.amount
2017-06-06 06:21:55 +02:00
);
2017-04-23 07:55:47 +02:00
export const selectDraftTransactionAddress = createSelector(
selectDraftTransaction,
2017-06-06 23:19:12 +02:00
draft => draft.address
2017-06-06 06:21:55 +02:00
);
export const selectDraftTransactionError = createSelector(
selectDraftTransaction,
draft => draft.error
);
export const selectBlocks = createSelector(_selectState, state => state.blocks);
export const makeSelectBlockDate = block => {
return createSelector(
selectBlocks,
blocks =>
blocks && blocks[block] ? new Date(blocks[block].time * 1000) : undefined
);
};