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

135 lines
3.1 KiB
JavaScript
Raw Normal View History

2017-06-06 23:19:12 +02:00
import * as types from "constants/action_types";
import lbry from "lbry";
2017-04-23 07:55:47 +02:00
import {
selectDraftTransaction,
selectDraftTransactionAmount,
selectBalance,
2017-06-06 23:19:12 +02:00
} from "selectors/wallet";
import { doOpenModal, doShowSnackBar } from "actions/app";
2017-07-27 22:15:13 +02:00
import * as modals from "constants/modal_types";
2017-04-22 15:17:01 +02:00
export function doUpdateBalance(balance) {
return {
type: types.UPDATE_BALANCE,
data: {
2017-06-06 23:19:12 +02:00
balance: balance,
},
};
2017-04-22 15:17:01 +02:00
}
export function doFetchTransactions() {
return function(dispatch, getState) {
dispatch({
2017-06-06 23:19:12 +02:00
type: types.FETCH_TRANSACTIONS_STARTED,
});
2017-04-22 15:17:01 +02:00
lbry.transaction_list({ include_tip_info: true }).then(results => {
2017-04-22 15:17:01 +02:00
dispatch({
type: types.FETCH_TRANSACTIONS_COMPLETED,
data: {
2017-06-06 23:19:12 +02:00
transactions: results,
},
});
});
};
2017-04-22 15:17:01 +02:00
}
export function doGetNewAddress() {
return function(dispatch, getState) {
dispatch({
2017-06-06 23:19:12 +02:00
type: types.GET_NEW_ADDRESS_STARTED,
});
2017-04-22 15:17:01 +02:00
lbry.wallet_new_address().then(function(address) {
2017-06-06 23:19:12 +02:00
localStorage.setItem("wallet_address", address);
2017-04-22 15:17:01 +02:00
dispatch({
type: types.GET_NEW_ADDRESS_COMPLETED,
2017-06-06 23:19:12 +02:00
data: { address },
});
});
};
2017-04-22 15:17:01 +02:00
}
export function doCheckAddressIsMine(address) {
return function(dispatch, getState) {
dispatch({
2017-06-06 23:19:12 +02:00
type: types.CHECK_ADDRESS_IS_MINE_STARTED,
});
2017-04-22 15:17:01 +02:00
2017-06-15 02:21:31 +02:00
lbry.wallet_is_address_mine({ address }).then(isMine => {
2017-06-06 23:19:12 +02:00
if (!isMine) dispatch(doGetNewAddress());
2017-04-22 15:17:01 +02:00
dispatch({
2017-06-06 23:19:12 +02:00
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() {
return function(dispatch, getState) {
2017-06-06 23:19:12 +02:00
const state = getState();
const draftTx = selectDraftTransaction(state);
const balance = selectBalance(state);
const amount = selectDraftTransactionAmount(state);
2017-04-23 07:55:47 +02:00
if (balance - amount <= 0) {
2017-07-27 22:15:13 +02:00
return dispatch(doOpenModal(modals.INSUFFICIENT_BALANCE));
2017-04-23 07:55:47 +02:00
}
dispatch({
type: types.SEND_TRANSACTION_STARTED,
2017-06-06 23:19:12 +02:00
});
2017-04-23 07:55:47 +02:00
2017-06-06 23:19:12 +02:00
const successCallback = results => {
if (results === true) {
2017-04-23 07:55:47 +02:00
dispatch({
type: types.SEND_TRANSACTION_COMPLETED,
2017-06-06 23:19:12 +02:00
});
dispatch(
doShowSnackBar({
message: __(`You sent ${amount} LBC`),
linkText: __("History"),
linkTarget: __("/wallet"),
})
);
2017-06-06 23:19:12 +02:00
} else {
2017-04-23 07:55:47 +02:00
dispatch({
type: types.SEND_TRANSACTION_FAILED,
2017-06-06 23:19:12 +02:00
data: { error: results },
});
2017-07-27 22:15:13 +02:00
dispatch(doOpenModal(modals.TRANSACTION_FAILED));
2017-04-23 07:55:47 +02:00
}
2017-06-06 23:19:12 +02:00
};
2017-04-23 07:55:47 +02:00
2017-06-06 23:19:12 +02:00
const errorCallback = error => {
2017-04-23 07:55:47 +02:00
dispatch({
type: types.SEND_TRANSACTION_FAILED,
2017-06-06 23:19:12 +02:00
data: { error: error.message },
});
2017-07-27 22:15:13 +02:00
dispatch(doOpenModal(modals.TRANSACTION_FAILED));
2017-06-06 23:19:12 +02:00
};
2017-06-15 02:21:31 +02:00
lbry
2017-08-15 23:04:09 +02:00
.wallet_send({
2017-06-15 02:21:31 +02:00
amount: draftTx.amount,
address: draftTx.address,
})
.then(successCallback, errorCallback);
2017-06-06 23:19:12 +02:00
};
2017-04-23 07:55:47 +02:00
}
export function doSetDraftTransactionAmount(amount) {
return {
type: types.SET_DRAFT_TRANSACTION_AMOUNT,
2017-06-06 23:19:12 +02:00
data: { amount },
};
2017-04-23 07:55:47 +02:00
}
export function doSetDraftTransactionAddress(address) {
return {
type: types.SET_DRAFT_TRANSACTION_ADDRESS,
2017-06-06 23:19:12 +02:00
data: { address },
};
}