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

128 lines
2.8 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 } from "actions/app";
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
2017-06-06 23:19:12 +02:00
lbry.call("transaction_list", {}, 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-06 23:19:12 +02:00
lbry.checkAddressIsMine(address, isMine => {
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 < 1) {
2017-06-06 23:19:12 +02:00
return dispatch(doOpenModal("insufficientBalance"));
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(doOpenModal("transactionSuccessful"));
} 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 },
});
dispatch(doOpenModal("transactionFailed"));
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 },
});
dispatch(doOpenModal("transactionFailed"));
};
lbry.sendToAddress(
draftTx.amount,
draftTx.address,
successCallback,
errorCallback
);
};
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 },
};
}