lbry-desktop/ui/js/actions/wallet.js

128 lines
2.6 KiB
JavaScript
Raw Normal View History

2017-06-06 06:21:55 +02:00
import * as types from 'constants/action_types';
import lbry from 'lbry';
2017-04-23 07:55:47 +02:00
import {
2017-06-06 06:21:55 +02:00
selectDraftTransaction,
selectDraftTransactionAmount,
selectBalance
} from 'selectors/wallet';
import { doOpenModal } from 'actions/app';
2017-04-22 15:17:01 +02:00
export function doUpdateBalance(balance) {
2017-06-06 06:21:55 +02:00
return {
type: types.UPDATE_BALANCE,
data: {
balance: balance
}
};
2017-04-22 15:17:01 +02:00
}
export function doFetchTransactions() {
2017-06-06 06:21:55 +02:00
return function(dispatch, getState) {
dispatch({
type: types.FETCH_TRANSACTIONS_STARTED
});
lbry.call('transaction_list', {}, results => {
dispatch({
type: types.FETCH_TRANSACTIONS_COMPLETED,
data: {
transactions: results
}
});
});
};
2017-04-22 15:17:01 +02:00
}
export function doGetNewAddress() {
2017-06-06 06:21:55 +02:00
return function(dispatch, getState) {
dispatch({
type: types.GET_NEW_ADDRESS_STARTED
});
lbry.wallet_new_address().then(function(address) {
localStorage.setItem('wallet_address', address);
dispatch({
type: types.GET_NEW_ADDRESS_COMPLETED,
data: { address }
});
});
};
2017-04-22 15:17:01 +02:00
}
export function doCheckAddressIsMine(address) {
2017-06-06 06:21:55 +02:00
return function(dispatch, getState) {
dispatch({
type: types.CHECK_ADDRESS_IS_MINE_STARTED
});
lbry.checkAddressIsMine(address, isMine => {
if (!isMine) dispatch(doGetNewAddress());
dispatch({
type: types.CHECK_ADDRESS_IS_MINE_COMPLETED
});
});
};
2017-04-22 15:17:01 +02:00
}
2017-04-23 07:55:47 +02:00
export function doSendDraftTransaction() {
2017-06-06 06:21:55 +02:00
return function(dispatch, getState) {
const state = getState();
const draftTx = selectDraftTransaction(state);
const balance = selectBalance(state);
const amount = selectDraftTransactionAmount(state);
if (balance - amount < 1) {
return dispatch(doOpenModal('insufficientBalance'));
}
dispatch({
type: types.SEND_TRANSACTION_STARTED
});
const successCallback = results => {
if (results === true) {
dispatch({
type: types.SEND_TRANSACTION_COMPLETED
});
dispatch(doOpenModal('transactionSuccessful'));
} else {
dispatch({
type: types.SEND_TRANSACTION_FAILED,
data: { error: results }
});
dispatch(doOpenModal('transactionFailed'));
}
};
const errorCallback = error => {
dispatch({
type: types.SEND_TRANSACTION_FAILED,
data: { error: error.message }
});
dispatch(doOpenModal('transactionFailed'));
};
lbry.sendToAddress(
draftTx.amount,
draftTx.address,
successCallback,
errorCallback
);
};
2017-04-23 07:55:47 +02:00
}
export function doSetDraftTransactionAmount(amount) {
2017-06-06 06:21:55 +02:00
return {
type: types.SET_DRAFT_TRANSACTION_AMOUNT,
data: { amount }
};
2017-04-23 07:55:47 +02:00
}
export function doSetDraftTransactionAddress(address) {
2017-06-06 06:21:55 +02:00
return {
type: types.SET_DRAFT_TRANSACTION_ADDRESS,
data: { address }
};
}