Merged 575.

This commit is contained in:
Brendon Duncan 2018-09-21 14:58:39 -07:00
commit cecad4a072
2 changed files with 172 additions and 157 deletions

View file

@ -1,189 +1,204 @@
'use strict'; 'use strict';
angular.module('copayApp.controllers').controller('walletSelectorController', function($scope, $state, sendFlowService, configService, gettextCatalog, profileService, txFormatService) { (function () {
var fromWalletId = ''; angular
var priceDisplayAsFiat = false; .module('copayApp.controllers')
var unitDecimals = 0; .controller('walletSelectorController', walletSelectorController);
var unitsFromSatoshis = 0;
$scope.$on("$ionicView.beforeEnter", onBeforeEnter); function walletSelectorController ($scope, $state, sendFlowService, configService, gettextCatalog, ongoingProcess, profileService, walletService, txFormatService) {
$scope.$on("$ionicView.enter", onEnter); var fromWalletId = '';
var priceDisplayAsFiat = false;
var unitDecimals = 0;
var unitsFromSatoshis = 0;
function onBeforeEnter(event, data) { $scope.$on("$ionicView.beforeEnter", onBeforeEnter);
if (data.direction == "back") { $scope.$on("$ionicView.enter", onEnter);
sendFlowService.state.pop();
}
$scope.params = sendFlowService.state.getClone(); function onBeforeEnter(event, data) {
if (data.direction == "back") {
sendFlowService.state.pop();
}
console.log('walletSelector onBeforeEnter after back sendflow', $scope.params); $scope.params = sendFlowService.state.getClone();
var config = configService.getSync().wallet.settings; console.log('walletSelector onBeforeEnter after back sendflow', $scope.params);
priceDisplayAsFiat = config.priceDisplay === 'fiat';
unitDecimals = config.unitDecimals;
unitsFromSatoshis = 1 / config.unitToSatoshi;
if ($scope.params.isWalletTransfer) { var config = configService.getSync().wallet.settings;
$scope.sendFlowTitle = gettextCatalog.getString('Transfer between wallets'); priceDisplayAsFiat = config.priceDisplay === 'fiat';
} else if (!$scope.params.thirdParty) { unitDecimals = config.unitDecimals;
$scope.sendFlowTitle = gettextCatalog.getString('Send'); unitsFromSatoshis = 1 / config.unitToSatoshi;
}
$scope.coin = false; // Wallets to show (for destination screen or contacts) if ($scope.params.isWalletTransfer) {
$scope.type = $scope.params['fromWalletId'] ? 'destination' : 'origin'; // origin || destination $scope.sendFlowTitle = gettextCatalog.getString('Transfer between wallets');
fromWalletId = $scope.params['fromWalletId']; } else if (!$scope.params.thirdParty) {
$scope.sendFlowTitle = gettextCatalog.getString('Send');
}
if ($scope.type === 'destination' && $scope.params.toAddress) { $scope.coin = false; // Wallets to show (for destination screen or contacts)
$state.transitionTo(getNextStep($scope.params)); $scope.type = $scope.params['fromWalletId'] ? 'destination' : 'origin'; // origin || destination
} fromWalletId = $scope.params['fromWalletId'];
if ($scope.params.coin) { if ($scope.type === 'destination' && $scope.params.toAddress) {
$scope.coin = $scope.params.coin; // Contacts have a coin embedded $state.transitionTo(getNextStep($scope.params));
} }
if ($scope.params.amount) { // There is an amount, so presume that it is a payment request if ($scope.params.coin) {
$scope.sendFlowTitle = gettextCatalog.getString('Payment Request'); $scope.coin = $scope.params.coin; // Contacts have a coin embedded
$scope.specificAmount = $scope.specificAlternativeAmount = ''; }
$scope.isPaymentRequest = true;
}
if ($scope.params.thirdParty) {
$scope.thirdParty = $scope.params.thirdParty;
}
};
function onEnter (event, data) { if ($scope.params.amount) { // There is an amount, so presume that it is a payment request
configService.whenAvailable(function(config) { $scope.sendFlowTitle = gettextCatalog.getString('Payment Request');
$scope.selectedPriceDisplay = config.wallet.settings.priceDisplay; $scope.specificAmount = $scope.specificAlternativeAmount = '';
}); $scope.isPaymentRequest = true;
}
if ($scope.params.thirdParty) {
$scope.thirdParty = $scope.params.thirdParty;
}
};
if ($scope.thirdParty) { function onEnter (event, data) {
// Third party services specific logic configService.whenAvailable(function(config) {
handleThirdPartyIfShapeshift(); $scope.selectedPriceDisplay = config.wallet.settings.priceDisplay;
}
prepareWalletLists();
formatRequestedAmount();
};
function formatRequestedAmount() {
if ($scope.params.amount) {
var cryptoAmount = (unitsFromSatoshis * $scope.params.amount).toFixed(unitDecimals);
var cryptoCoin = $scope.coin.toUpperCase();
txFormatService.formatAlternativeStr($scope.coin, $scope.params.amount, function onFormatAlternativeStr(formatted){
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;
}
$scope.$apply();
}
}); });
}
}
function handleThirdPartyIfShapeshift() { if ($scope.thirdParty) {
console.log($scope.thirdParty, $scope.coin); // Third party services specific logic
if ($scope.thirdParty.id === 'shapeshift' && $scope.type === 'destination') { // Shapeshift wants to know the handleThirdPartyIfShapeshift();
$scope.coin = profileService.getWallet(fromWalletId).coin;
if ($scope.coin === 'bch') {
$scope.coin = 'btc';
} else {
$scope.coin = 'bch';
} }
}
}
function prepareWalletLists() { prepareWalletLists();
var walletsAll = []; formatRequestedAmount();
var walletsSufficientFunds = []; };
$scope.walletsInsufficientFunds = []; // For origin screen
if ($scope.type === 'origin') {
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send from');
function formatRequestedAmount() {
if ($scope.params.amount) { if ($scope.params.amount) {
var cryptoAmount = (unitsFromSatoshis * $scope.params.amount).toFixed(unitDecimals);
var cryptoCoin = $scope.coin.toUpperCase();
walletsAll = profileService.getWallets({coin: $scope.coin}); txFormatService.formatAlternativeStr($scope.coin, $scope.params.amount, function onFormatAlternativeStr(formatted){
if (formatted) {
var fiatParts = formatted.split(' ');
var fiatAmount = fiatParts[0];
var fiatCurrrency = fiatParts.length > 1 ? fiatParts[1] : '';
walletsAll.forEach(function forWallet(wallet){ if (priceDisplayAsFiat) {
if (wallet.status.availableBalanceSat > $scope.params.amount) { $scope.requestAmount = fiatAmount;
walletsSufficientFunds.push(wallet); $scope.requestCurrency = fiatCurrrency;
} else {
$scope.walletsInsufficientFunds.push(wallet); $scope.requestAmountSecondary = cryptoAmount;
$scope.requestCurrencySecondary = cryptoCoin;
} else {
$scope.requestAmount = cryptoAmount;
$scope.requestCurrency = cryptoCoin;
$scope.requestAmountSecondary = fiatAmount;
$scope.requestCurrencySecondary = fiatCurrrency;
}
$scope.$apply();
} }
}); });
}
}
if ($scope.coin === 'btc') { function handleThirdPartyIfShapeshift() {
$scope.walletsBtc = walletsSufficientFunds; console.log($scope.thirdParty, $scope.coin);
if ($scope.thirdParty.id === 'shapeshift' && $scope.type === 'destination') { // Shapeshift wants to know the
$scope.coin = profileService.getWallet(fromWalletId).coin;
if ($scope.coin === 'bch') {
$scope.coin = 'btc';
} else { } else {
$scope.walletsBch = walletsSufficientFunds; $scope.coin = 'bch';
} }
}
}
} else if ($scope.coin) { function prepareWalletLists() {
walletsAll = profileService.getWallets({coin: $scope.coin}); var walletsAll = [];
walletsAll.forEach(function forWallet(wallet){ var walletsSufficientFunds = [];
if (wallet.status.availableBalanceSat > 0) { $scope.walletsInsufficientFunds = []; // For origin screen
walletsSufficientFunds.push(wallet);
if ($scope.type === 'origin') {
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send from');
if ($scope.params.amount || $scope.coin) {
walletsAll = profileService.getWallets({coin: $scope.coin});
ongoingProcess.set('scanning', true);
walletsAll.forEach(function forWallet(wallet) {
if (!wallet.status && !wallet.cachedStatus) {
walletService.getStatus(wallet, {}, function(err, status) {
wallet.status = status;
if (status.availableBalanceSat > ($scope.params.amount ? $scope.params.amount : 0)) {
walletsSufficientFunds.push(wallet);
} else {
$scope.walletsInsufficientFunds.push(wallet);
}
if ($scope.coin === 'btc') { // As this is a promise
$scope.walletsBtc = walletsSufficientFunds;
} else {
$scope.walletsBch = walletsSufficientFunds;
}
ongoingProcess.set('scanning', false);
});
} else {
var walletStatus = null;
if (wallet.status && wallet.status.isValid) {
walletStatus = wallet.status;
} else if (wallet.cachedStatus && wallet.status.isValid) {
walletStatus = wallet.cachedStatus;
}
if (walletStatus && walletStatus.availableBalanceSat > ($scope.params.amount ? $scope.params.amount : 0)) {
walletsSufficientFunds.push(wallet);
} else {
$scope.walletsInsufficientFunds.push(wallet);
}
ongoingProcess.set('scanning', false);
}
});
if ($scope.coin === 'btc') {
$scope.walletsBtc = walletsSufficientFunds;
} else { } else {
$scope.walletsInsufficientFunds.push(wallet); $scope.walletsBch = walletsSufficientFunds;
} }
});
if ($scope.coin === 'btc') {
$scope.walletsBtc = walletsSufficientFunds;
} else { } else {
$scope.walletsBch = walletsSufficientFunds; $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 {
$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') { } else if ($scope.type === 'destination') {
if (!$scope.coin) { // Allow for the coin to be set by a third party if (!$scope.coin) { // Allow for the coin to be set by a third party
$scope.fromWallet = profileService.getWallet(fromWalletId); $scope.fromWallet = profileService.getWallet(fromWalletId);
$scope.coin = $scope.fromWallet.coin; // Only show wallets with the select origin wallet coin $scope.coin = $scope.fromWallet.coin; // Only show wallets with the select origin wallet coin
} }
$scope.headerTitle = gettextCatalog.getString('Choose a wallet to send to'); $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 if ($scope.coin === 'btc') { // if no specific coin is set or coin is set btc
$scope.walletsBtc = profileService.getWallets({coin: $scope.coin}); $scope.walletsBtc = profileService.getWallets({coin: $scope.coin});
} else { } else {
$scope.walletsBch = profileService.getWallets({coin: $scope.coin}); $scope.walletsBch = profileService.getWallets({coin: $scope.coin});
}
} }
} }
}
$scope.useWallet = function(wallet) { $scope.useWallet = function(wallet) {
var params = sendFlowService.state.getClone(); var params = sendFlowService.state.getClone();
if ($scope.type === 'origin') { // we're on the origin screen, set wallet to send from if ($scope.type === 'origin') { // we're on the origin screen, set wallet to send from
params.fromWalletId = wallet.id; params.fromWalletId = wallet.id;
} else { // we're on the destination screen, set wallet to send to } else { // we're on the destination screen, set wallet to send to
params.toWalletId = wallet.id; params.toWalletId = wallet.id;
}
sendFlowService.goNext(params);
};
$scope.goBack = function() {
sendFlowService.router.goBack();
} }
sendFlowService.goNext(params);
};
$scope.goBack = function() {
sendFlowService.router.goBack();
} }
})();
});

View file

@ -13,7 +13,7 @@
<div class="header--request__amount-alt" ng-show="requestAmountSecondary" translate>{{requestAmountSecondary}} {{requestCurrencySecondary}}</div> <div class="header--request__amount-alt" ng-show="requestAmountSecondary" translate>{{requestAmountSecondary}} {{requestCurrencySecondary}}</div>
</div> </div>
<div class="wallets-header"> <div class="wallets-header">
<div class="title"> <div class="title" ng-if="walletsBch.length > 0 || walletsBtc.length > 0 || walletsInsufficientFunds.length > 0">
{{headerTitle}} {{headerTitle}}
</div> </div>
</div> </div>