2018-07-23 16:58:32 +02:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-08-08 17:10:47 +02:00
|
|
|
angular.module('copayApp.controllers').controller('walletSelectorController', function($scope, $rootScope, $state, $log, $ionicHistory, sendFlowService, configService, gettextCatalog, profileService, txFormatService) {
|
2018-08-06 13:39:03 +12:00
|
|
|
|
2018-08-06 16:45:00 +12:00
|
|
|
var fromWalletId = '';
|
2018-08-06 13:39:03 +12:00
|
|
|
var priceDisplayAsFiat = false;
|
|
|
|
|
var unitDecimals = 0;
|
|
|
|
|
var unitsFromSatoshis = 0;
|
2018-07-23 16:58:32 +02:00
|
|
|
|
2018-08-09 13:14:44 +09:00
|
|
|
$scope.$on("$ionicView.beforeEnter", onBeforeEnter);
|
|
|
|
|
$scope.$on("$ionicView.enter", onEnter);
|
|
|
|
|
|
|
|
|
|
function onBeforeEnter(event, data) {
|
|
|
|
|
if (data.direction == "back") {
|
|
|
|
|
sendFlowService.popState();
|
|
|
|
|
}
|
2018-08-13 10:00:39 +12:00
|
|
|
console.log('walletSelector onBeforeEnter after back sendflow', sendFlowService.state);
|
2018-08-09 13:14:44 +09:00
|
|
|
|
2018-08-13 10:00:39 +12:00
|
|
|
$scope.params = sendFlowService.getStateClone();
|
2018-08-09 12:35:39 +09:00
|
|
|
|
2018-07-23 16:58:32 +02:00
|
|
|
var config = configService.getSync().wallet.settings;
|
2018-08-06 13:39:03 +12:00
|
|
|
priceDisplayAsFiat = config.priceDisplay === 'fiat';
|
|
|
|
|
unitDecimals = config.unitDecimals;
|
|
|
|
|
unitsFromSatoshis = 1 / config.unitToSatoshi;
|
2018-07-25 17:25:38 +02:00
|
|
|
|
2018-08-03 08:48:24 +12:00
|
|
|
switch($state.current.name) {
|
|
|
|
|
case 'tabs.send.wallet-to-wallet':
|
2018-08-14 11:38:24 +09:00
|
|
|
$scope.sendFlowTitle = gettextCatalog.getString('Transfer between wallets');
|
2018-08-03 08:48:24 +12:00
|
|
|
break;
|
|
|
|
|
case 'tabs.send.destination':
|
2018-08-13 10:00:39 +12:00
|
|
|
if ($scope.params.fromWalletId && !$scope.params.thirdParty) {
|
2018-08-14 11:38:24 +09:00
|
|
|
$scope.sendFlowTitle = gettextCatalog.getString('Transfer between wallets');
|
2018-08-03 08:48:24 +12:00
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
default:
|
2018-08-10 19:04:53 +09:00
|
|
|
if (!$scope.params.thirdParty) {
|
2018-08-10 14:05:46 +09:00
|
|
|
$scope.sendFlowTitle = gettextCatalog.getString('Send');
|
|
|
|
|
}
|
2018-08-03 08:48:24 +12:00
|
|
|
// nop
|
2018-07-25 17:25:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$scope.coin = false; // Wallets to show (for destination screen or contacts)
|
2018-08-08 17:10:47 +02:00
|
|
|
$scope.type = $scope.params['fromWalletId'] ? 'destination' : 'origin'; // origin || destination
|
|
|
|
|
fromWalletId = $scope.params['fromWalletId'];
|
|
|
|
|
|
|
|
|
|
if ($scope.type === 'destination' && $scope.params.toAddress) {
|
2018-08-13 10:00:39 +12:00
|
|
|
$state.transitionTo(getNextStep($scope.params));
|
2018-08-08 17:10:47 +02:00
|
|
|
}
|
2018-07-25 17:25:38 +02:00
|
|
|
|
|
|
|
|
if ($scope.params.coin) {
|
|
|
|
|
$scope.coin = $scope.params.coin; // Contacts have a coin embedded
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:04:14 +12:00
|
|
|
if ($scope.params.amount) { // There is an amount, so presume that it is a payment request
|
|
|
|
|
$scope.sendFlowTitle = gettextCatalog.getString('Payment Request');
|
2018-07-25 11:26:33 +02:00
|
|
|
$scope.specificAmount = $scope.specificAlternativeAmount = '';
|
2018-07-23 16:58:32 +02:00
|
|
|
$scope.isPaymentRequest = true;
|
|
|
|
|
}
|
2018-07-25 17:25:38 +02:00
|
|
|
if ($scope.params.thirdParty) {
|
2018-08-09 12:35:39 +09:00
|
|
|
$scope.thirdParty = $scope.params.thirdParty;
|
2018-07-25 17:25:38 +02:00
|
|
|
}
|
2018-08-09 13:14:44 +09:00
|
|
|
};
|
2018-07-23 16:58:32 +02:00
|
|
|
|
2018-08-09 13:14:44 +09:00
|
|
|
function onEnter (event, data) {
|
2018-07-25 11:26:33 +02:00
|
|
|
configService.whenAvailable(function(config) {
|
|
|
|
|
$scope.selectedPriceDisplay = config.wallet.settings.priceDisplay;
|
|
|
|
|
});
|
|
|
|
|
|
2018-07-31 17:21:56 +02:00
|
|
|
if ($scope.thirdParty) {
|
|
|
|
|
// Third party services specific logic
|
2018-08-06 12:04:14 +12:00
|
|
|
handleThirdPartyIfShapeshift();
|
2018-07-31 17:21:56 +02:00
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:45:00 +12:00
|
|
|
prepareWalletLists();
|
2018-08-06 13:39:03 +12:00
|
|
|
formatRequestedAmount();
|
2018-08-09 13:14:44 +09:00
|
|
|
};
|
2018-07-23 16:58:32 +02:00
|
|
|
|
2018-08-06 13:39:03 +12:00
|
|
|
function formatRequestedAmount() {
|
2018-08-06 20:31:06 +12:00
|
|
|
if ($scope.params.amount) {
|
|
|
|
|
var cryptoAmount = (unitsFromSatoshis * $scope.params.amount).toFixed(unitDecimals);
|
2018-08-06 13:39:03 +12:00
|
|
|
var cryptoCoin = $scope.coin.toUpperCase();
|
|
|
|
|
|
2018-08-06 20:31:06 +12:00
|
|
|
txFormatService.formatAlternativeStr($scope.coin, $scope.params.amount, function onFormatAlternativeStr(formatted){
|
2018-08-06 13:39:03 +12:00
|
|
|
if (formatted) {
|
|
|
|
|
var fiatParts = formatted.split(' ');
|
|
|
|
|
var fiatAmount = fiatParts[0];
|
|
|
|
|
var fiatCurrrency = fiatParts.length > 1 ? fiatParts[1] : '';
|
|
|
|
|
|
|
|
|
|
if (priceDisplayAsFiat) {
|
|
|
|
|
$scope.requestAmount = fiatAmount;
|
|
|
|
|
$scope.requestCurrency = fiatCurrrency;
|
|
|
|
|
|
|
|
|
|
$scope.requestAmountSecondary = cryptoAmount;
|
|
|
|
|
$scope.requestCurrencySecondary = cryptoCoin;
|
|
|
|
|
} else {
|
|
|
|
|
$scope.requestAmount = cryptoAmount;
|
|
|
|
|
$scope.requestCurrency = cryptoCoin;
|
|
|
|
|
|
|
|
|
|
$scope.requestAmountSecondary = fiatAmount;
|
|
|
|
|
$scope.requestCurrencySecondary = fiatCurrrency;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-13 10:00:39 +12:00
|
|
|
function getNextStep(params) {
|
|
|
|
|
if (!params.toWalletId && !params.toAddress) { // If we have no toAddress or fromWallet
|
2018-07-24 14:24:22 +02:00
|
|
|
return 'tabs.send.destination';
|
2018-08-13 10:00:39 +12:00
|
|
|
} else if (!params.amount) { // If we have no amount
|
2018-07-24 14:24:22 +02:00
|
|
|
return 'tabs.send.amount';
|
2018-07-25 11:26:33 +02:00
|
|
|
} else { // If we do have them
|
2018-08-06 16:45:00 +12:00
|
|
|
return 'tabs.send.review';
|
2018-07-24 14:24:22 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:04:14 +12:00
|
|
|
function handleThirdPartyIfShapeshift() {
|
2018-08-06 18:11:37 +02:00
|
|
|
console.log($scope.thirdParty, $scope.coin);
|
2018-08-06 12:04:14 +12:00
|
|
|
if ($scope.thirdParty.id === 'shapeshift' && $scope.type === 'destination') { // Shapeshift wants to know the
|
2018-08-06 18:06:10 +02:00
|
|
|
$scope.coin = profileService.getWallet(fromWalletId).coin;
|
2018-08-06 12:04:14 +12:00
|
|
|
if ($scope.coin === 'bch') {
|
|
|
|
|
$scope.coin = 'btc';
|
|
|
|
|
} else {
|
|
|
|
|
$scope.coin = 'bch';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 16:45:00 +12:00
|
|
|
function prepareWalletLists() {
|
|
|
|
|
var walletsAll = [];
|
|
|
|
|
var walletsSufficientFunds = [];
|
|
|
|
|
$scope.walletsInsufficientFunds = []; // For origin screen
|
|
|
|
|
|
|
|
|
|
if ($scope.type === 'origin') {
|
|
|
|
|
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send from');
|
|
|
|
|
|
|
|
|
|
if ($scope.params.amount) {
|
|
|
|
|
|
|
|
|
|
walletsAll = profileService.getWallets({coin: $scope.coin});
|
|
|
|
|
|
|
|
|
|
walletsAll.forEach(function forWallet(wallet){
|
|
|
|
|
if (wallet.status.availableBalanceSat > $scope.params.amount) {
|
|
|
|
|
walletsSufficientFunds.push(wallet);
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsInsufficientFunds.push(wallet);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($scope.coin === 'btc') {
|
|
|
|
|
$scope.walletsBtc = walletsSufficientFunds;
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsBch = walletsSufficientFunds;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if ($scope.coin) {
|
|
|
|
|
walletsAll = profileService.getWallets({coin: $scope.coin});
|
|
|
|
|
walletsAll.forEach(function forWallet(wallet){
|
|
|
|
|
if (wallet.status.availableBalanceSat > 0) {
|
|
|
|
|
walletsSufficientFunds.push(wallet);
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsInsufficientFunds.push(wallet);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
if ($scope.coin === 'btc') {
|
|
|
|
|
$scope.walletsBtc = walletsSufficientFunds;
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsBch = walletsSufficientFunds;
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsBch = profileService.getWallets({coin: 'bch', hasFunds: true});
|
|
|
|
|
$scope.walletsBtc = profileService.getWallets({coin: 'btc', hasFunds: true});
|
|
|
|
|
$scope.walletsInsufficientFunds = profileService.getWallets({coin: $scope.coin, hasNoFunds: true});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if ($scope.type === 'destination') {
|
2018-08-06 20:39:59 +12:00
|
|
|
if (!$scope.coin) { // Allow for the coin to be set by a third party
|
|
|
|
|
$scope.fromWallet = profileService.getWallet(fromWalletId);
|
|
|
|
|
$scope.coin = $scope.fromWallet.coin; // Only show wallets with the select origin wallet coin
|
|
|
|
|
}
|
2018-08-06 16:45:00 +12:00
|
|
|
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send to');
|
|
|
|
|
|
|
|
|
|
if ($scope.coin === 'btc') { // if no specific coin is set or coin is set btc
|
|
|
|
|
$scope.walletsBtc = profileService.getWallets({coin: $scope.coin});
|
|
|
|
|
} else {
|
|
|
|
|
$scope.walletsBch = profileService.getWallets({coin: $scope.coin});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-06 12:04:14 +12:00
|
|
|
|
|
|
|
|
|
2018-07-23 16:58:32 +02:00
|
|
|
$scope.useWallet = function(wallet) {
|
2018-08-13 10:00:39 +12:00
|
|
|
var params = sendFlowService.getStateClone();
|
2018-07-25 11:26:33 +02:00
|
|
|
if ($scope.type === 'origin') { // we're on the origin screen, set wallet to send from
|
2018-08-09 13:01:48 +12:00
|
|
|
params.fromWalletId = wallet.id;
|
2018-07-25 11:26:33 +02:00
|
|
|
} else { // we're on the destination screen, set wallet to send to
|
2018-08-09 13:01:48 +12:00
|
|
|
params.toWalletId = wallet.id;
|
2018-07-23 16:58:32 +02:00
|
|
|
}
|
2018-08-09 13:01:48 +12:00
|
|
|
sendFlowService.pushState(params);
|
2018-08-13 10:00:39 +12:00
|
|
|
var nextStep = getNextStep(params);
|
|
|
|
|
console.log('walletSelector nextStep', nextStep);
|
|
|
|
|
$state.transitionTo(nextStep, $scope.params);
|
2018-07-23 16:58:32 +02:00
|
|
|
};
|
2018-07-25 16:05:07 +02:00
|
|
|
|
|
|
|
|
$scope.goBack = function() {
|
|
|
|
|
$ionicHistory.goBack();
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-23 16:58:32 +02:00
|
|
|
});
|