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

214 lines
6.5 KiB
JavaScript
Raw Normal View History

'use strict';
angular
.module('copayApp.controllers')
.controller('reviewController', reviewController);
2018-08-02 19:51:50 +12:00
function reviewController(addressbookService, configService, profileService, $log, $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-01 14:28:26 +12:00
$scope.$on("$ionicView.beforeEnter", onBeforeEnter);
function onBeforeEnter(event, data) {
2018-08-02 09:48:12 +12:00
originWalletId = data.stateParams.fromWalletId;
satoshis = parseInt(data.stateParams.amount, 10);
2018-08-02 19:51:50 +12:00
toAddress = data.stateParams.toAddr;
2018-08-02 09:48:12 +12:00
var originWallet = profileService.getWallet(originWalletId);
vm.origin.currency = originWallet.coin.toUpperCase();
vm.origin.color = originWallet.color;
vm.origin.name = originWallet.name;
2018-08-02 19:51:50 +12:00
coin = originWallet.coin;
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 19:51:50 +12:00
handleDestinationAsAddress(toAddress, coin);
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;
2018-08-02 19:51:50 +12:00
vm.origin.balanceCurrency = balanceText.currency;
2018-08-02 18:14:54 +12:00
}
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
};
}
2018-08-02 19:51:50 +12:00
function handleDestinationAsAddress(address, originCoin) {
if (!address) {
return;
}
// Check if the recipient is a contact
addressbookService.get(originCoin + address, function(err, contact) {
if (!err && contact) {
console.log('destination is contact');
handleDestinationAsContact(contact);
} else {
console.log('destination is address');
vm.destination.address = address;
vm.destination.kind = 'address';
}
});
}
function handleDestinationAsContact(contact) {
vm.destination.kind = 'contact';
vm.destination.name = contact.name;
vm.destination.color = contact.coin === 'btc' ? config.bitcoinWalletColor : config.bitcoinCashWalletColor;
vm.destination.currency = contact.coin.toUpperCase();
vm.destination.currencyColor = vm.destination.color;
}
2018-08-02 18:14:54 +12:00
function handleDestinationAsWallet(walletId) {
destinationWalletId = walletId;
2018-08-02 19:51:50 +12:00
if (!destinationWalletId) {
return;
}
2018-08-02 18:14:54 +12:00
2018-08-02 19:51:50 +12:00
console.log('destination is wallet');
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;
2018-08-02 09:48:12 +12:00
}
2018-08-02 19:51:50 +12:00
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
});
}
}