Wallet/src/js/controllers/review.controller.js

185 lines
5.6 KiB
JavaScript
Raw Normal View History

'use strict';
angular
.module('copayApp.controllers')
.controller('reviewController', reviewController);
2018-08-02 09:48:12 +12:00
function reviewController(configService, gettextCatalog, profileService, $scope, txFormatService) {
var vm = this;
2018-08-01 14:28:26 +12:00
2018-08-02 11:48:41 +12:00
vm.destination = {
address: '',
balanceAmount: '',
2018-08-02 18:14:54 +12:00
balanceCurrency: '',
coin: '',
2018-08-02 11:48:41 +12:00
color: '',
2018-08-02 18:14:54 +12:00
currency: '',
currencyColor: '',
kind: '', // 'address', 'contact', 'wallet'
2018-08-02 11:48:41 +12:00
name: ''
};
2018-08-02 10:20:03 +12:00
vm.feeCrypto = '';
vm.feeFiat = '';
2018-08-02 09:48:12 +12:00
vm.origin = {
balanceAmount: '',
balanceCurrency: '',
color: '',
currency: '',
2018-08-02 10:07:25 +12:00
currencyColor: '',
2018-08-02 09:48:12 +12:00
name: '',
};
2018-08-01 17:17:34 +12:00
vm.primaryAmount = '';
2018-08-01 14:28:26 +12:00
vm.primaryCurrency = '';
vm.secondaryAmount = '';
vm.secondaryCurrency = '';
2018-08-02 18:14:54 +12:00
var config = null;
2018-08-01 17:17:34 +12:00
var coin = '';
2018-08-02 09:48:12 +12:00
var originWalletId = '';
var priceDisplayIsFiat = true;
2018-08-01 17:17:34 +12:00
var satoshis = null;
2018-08-02 09:48:12 +12:00
var toAddress = '';
2018-08-02 11:48:41 +12:00
var destinationWalletId = '';
2018-08-02 09:48:12 +12:00
2018-08-01 17:17:34 +12:00
2018-08-02 09:48:12 +12:00
2018-08-01 14:28:26 +12:00
$scope.$on("$ionicView.beforeEnter", onBeforeEnter);
function onBeforeEnter(event, data) {
2018-08-01 17:17:34 +12:00
coin = data.stateParams.coin;
2018-08-02 09:48:12 +12:00
originWalletId = data.stateParams.fromWalletId;
satoshis = parseInt(data.stateParams.amount, 10);
toAddress = data.stateParams.toAddress;
var originWallet = profileService.getWallet(originWalletId);
vm.origin.currency = originWallet.coin.toUpperCase();
vm.origin.color = originWallet.color;
vm.origin.name = originWallet.name;
2018-08-01 14:28:26 +12:00
2018-08-02 18:14:54 +12:00
configService.get(function onConfig(err, configCache) {
2018-08-01 14:28:26 +12:00
if (err) {
$log.err('Error getting config.', err);
} else {
2018-08-02 18:14:54 +12:00
config = configCache;
2018-08-02 10:07:25 +12:00
priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat';
vm.origin.currencyColor = originWallet.coin === 'btc' ? config.bitcoinWalletColor : config.bitcoinCashWalletColor;
2018-08-01 14:28:26 +12:00
}
2018-08-02 09:48:12 +12:00
updateSendAmounts();
getOriginWalletBalance(originWallet);
2018-08-02 18:14:54 +12:00
handleDestinationAsWallet(data.stateParams.toWalletId);
2018-08-01 14:28:26 +12:00
});
}
2018-08-02 09:48:12 +12:00
function getOriginWalletBalance(originWallet) {
2018-08-02 18:14:54 +12:00
var balanceText = getWalletBalanceDisplayText(originWallet);
vm.origin.balanceAmount = balanceText.amount;
vm.origin.balanceCurrecny = balanceText.currency;
}
function getWalletBalanceDisplayText(wallet) {
2018-08-02 09:48:12 +12:00
var balanceCryptoAmount = '';
var balanceCryptoCurrencyCode = '';
var balanceFiatAmount = '';
var balanceFiatCurrency = ''
2018-08-02 18:14:54 +12:00
var displayAmount = '';
var displayCurrency = '';
var walletStatus = null;
if (wallet.status.isValid) {
walletStatus = wallet.status;
} else if (wallet.cachedStatus.isValid) {
walletStatus = wallet.cachedStatus;
2018-08-02 09:48:12 +12:00
}
2018-08-02 18:14:54 +12:00
if (walletStatus) {
var cryptoBalanceParts = walletStatus.spendableBalanceStr.split(' ');
2018-08-02 09:48:12 +12:00
balanceCryptoAmount = cryptoBalanceParts[0];
balanceCryptoCurrencyCode = cryptoBalanceParts.length > 1 ? cryptoBalanceParts[1] : '';
2018-08-02 18:14:54 +12:00
if (walletStatus.alternativeBalanceAvailable) {
balanceFiatAmount = walletStatus.spendableBalanceAlternative;
balanceFiatCurrency = walletStatus.alternativeIsoCode;
2018-08-02 09:48:12 +12:00
}
}
if (priceDisplayIsFiat) {
2018-08-02 18:14:54 +12:00
displayAmount = balanceFiatAmount ? balanceFiatAmount : balanceCryptoAmount;
displayCurrency = balanceFiatAmount ? balanceFiatCurrency : balanceCryptoCurrencyCode;
2018-08-02 09:48:12 +12:00
} else {
2018-08-02 18:14:54 +12:00
displayAmount = balanceCryptoAmount;
displayCurrency = balanceCryptoCurrencyCode;
}
return {
amount: displayAmount,
currency: displayCurrency
};
}
function handleDestinationAsWallet(walletId) {
destinationWalletId = walletId;
if (destinationWalletId) {
var destinationWallet = profileService.getWallet(destinationWalletId);
vm.destination.coin = destinationWallet.coin;
vm.destination.color = destinationWallet.color;
vm.destination.currency = destinationWallet.coin.toUpperCase();
vm.destination.kind = 'wallet';
vm.destination.name = destinationWallet.name;
if (config) {
vm.destination.currencyColor = vm.destination.coin === 'btc' ? config.bitcoinWalletColor : config.bitcoinCashWalletColor;
}
var balanceText = getWalletBalanceDisplayText(destinationWallet);
vm.destination.balanceAmount = balanceText.amount;
vm.destination.balanceCurrency = balanceText.currency;
2018-08-02 09:48:12 +12:00
}
}
function updateSendAmounts() {
2018-08-01 17:17:34 +12:00
if (typeof satoshis !== 'number') {
return;
}
2018-08-02 09:48:12 +12:00
var cryptoAmount = '';
var cryptoCurrencyCode = '';
2018-08-01 17:17:34 +12:00
var amountStr = txFormatService.formatAmountStr(coin, satoshis);
2018-08-02 09:48:12 +12:00
if (amountStr) {
var amountParts = amountStr.split(' ');
cryptoAmount = amountParts[0];
cryptoCurrencyCode = amountParts.length > 1 ? amountParts[1] : '';
}
// Want to avoid flashing of amount strings so do all formatting after this has returned.
2018-08-01 17:17:34 +12:00
txFormatService.formatAlternativeStr(coin, satoshis, function(v) {
if (!v) {
2018-08-02 09:48:12 +12:00
vm.primaryAmount = cryptoAmount;
vm.primaryCurrency = cryptoCurrencyCode;
2018-08-01 17:17:34 +12:00
vm.secondaryAmount = '';
vm.secondaryCurrency = '';
return;
}
vm.secondaryAmount = vm.primaryAmount;
vm.secondaryCurrency = vm.primaryCurrency;
var fiatParts = v.split(' ');
2018-08-02 09:48:12 +12:00
var fiatAmount = fiatParts[0];
var fiatCurrency = fiatParts.length > 1 ? fiatParts[1] : '';
if (priceDisplayIsFiat) {
vm.primaryAmount = fiatAmount;
vm.primaryCurrency = fiatCurrency;
vm.secondaryAmount = cryptoAmount;
vm.secondaryCurrency = cryptoCurrencyCode;
} else {
vm.primaryAmount = cryptoAmount;
vm.primaryCurrency = cryptoCurrencyCode;
vm.secondaryAmount = fiatAmount;
vm.secondaryCurrency = fiatCurrency;
}
2018-08-01 17:17:34 +12:00
});
}
}