2016-07-07 16:55:04 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
angular.module('copayApp.controllers').controller('amountController', amountController);
|
|
|
|
|
|
2018-08-02 15:15:23 +02:00
|
|
|
function amountController(configService, $filter, gettextCatalog, $ionicHistory, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window) {
|
2018-07-24 12:37:51 -07:00
|
|
|
var vm = this;
|
|
|
|
|
|
|
|
|
|
vm.allowSend = false;
|
|
|
|
|
vm.altCurrencyList = [];
|
|
|
|
|
vm.alternativeAmount = '';
|
|
|
|
|
vm.alternativeUnit = '';
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = '0';
|
2018-07-28 20:24:29 -07:00
|
|
|
vm.availableFunds = '';
|
|
|
|
|
// Use insufficient for logic, as when the amount is invalid, funds being
|
|
|
|
|
// either sufficent or insufficient doesn't make sense.
|
|
|
|
|
vm.fundsAreInsufficient = false;
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.globalResult = '';
|
|
|
|
|
vm.isRequestingSpecificAmount = false;
|
|
|
|
|
vm.listComplete = false;
|
|
|
|
|
vm.lastUsedPopularList = [];
|
2018-08-02 16:40:00 +12:00
|
|
|
vm.maxAmount = 0;
|
|
|
|
|
vm.minAmount = 0;
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.shapeshiftOrderId = '';
|
2018-08-02 15:15:23 +02:00
|
|
|
vm.thirdParty = false;
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.unit = '';
|
|
|
|
|
|
|
|
|
|
vm.changeUnit = changeUnit;
|
|
|
|
|
vm.close = close;
|
|
|
|
|
vm.findCurrency = findCurrency;
|
|
|
|
|
vm.finish = finish;
|
|
|
|
|
vm.goBack = goBack;
|
|
|
|
|
vm.loadMore = loadMore;
|
|
|
|
|
vm.openPopup = openPopup;
|
|
|
|
|
vm.pushDigit = pushDigit;
|
|
|
|
|
vm.removeDigit = removeDigit;
|
|
|
|
|
vm.save = save;
|
|
|
|
|
vm.sendMax = sendMax;
|
2018-08-02 16:40:00 +12:00
|
|
|
vm.errorMessage = '';
|
2018-08-02 15:12:40 +02:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
$scope.$on('$ionicView.beforeEnter', onBeforeEnter);
|
2018-08-02 15:12:40 +02:00
|
|
|
$scope.$on('$ionicView.leave', onLeave);
|
2017-12-12 17:15:39 +09:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
var LENGTH_EXPRESSION_LIMIT = 19;
|
2018-05-09 17:25:28 +09:00
|
|
|
var LENGTH_BEFORE_COMMA_EXPRESSION_LIMIT = 8;
|
|
|
|
|
var LENGTH_AFTER_COMMA_EXPRESSION_LIMIT = 8;
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
var altCurrencyModal = null;
|
2017-08-31 17:56:28 -03:00
|
|
|
var altUnitIndex = 0;
|
2018-07-28 20:24:29 -07:00
|
|
|
var availableFundsInCrypto = '';
|
|
|
|
|
var availableFundsInFiat = '';
|
|
|
|
|
var availableSatoshis = null;
|
2017-08-31 17:56:28 -03:00
|
|
|
var availableUnits = [];
|
|
|
|
|
var fiatCode;
|
2018-07-24 12:37:51 -07:00
|
|
|
var isNW = platformInfo.isNW;
|
|
|
|
|
var isAndroid = platformInfo.isAndroid;
|
|
|
|
|
var isIos = platformInfo.isIOS;
|
|
|
|
|
var lastUsedAltCurrencyList = [];
|
2018-08-02 15:44:59 +12:00
|
|
|
var passthroughParams = {};
|
2018-07-24 12:37:51 -07:00
|
|
|
var satToUnit;
|
|
|
|
|
var unitDecimals;
|
|
|
|
|
var unitIndex = 0;
|
2018-08-02 15:44:59 +12:00
|
|
|
var unitToSatoshi;
|
2018-07-24 12:37:51 -07:00
|
|
|
var useSendMax = false;
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function onLeave() {
|
2017-08-31 17:56:28 -03:00
|
|
|
angular.element($window).off('keydown');
|
2018-07-24 12:37:51 -07:00
|
|
|
}
|
2017-12-12 17:15:39 +09:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function onBeforeEnter(event, data) {
|
2017-12-20 17:36:19 +00:00
|
|
|
|
2018-05-08 16:17:19 +09:00
|
|
|
initCurrencies();
|
2018-08-02 16:40:00 +12:00
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
passthroughParams = data.stateParams;
|
2018-08-02 16:40:00 +12:00
|
|
|
console.log('stateParams:', data.stateParams);
|
2018-01-26 12:02:48 -04:00
|
|
|
|
2018-08-02 15:15:23 +02:00
|
|
|
vm.fromWalletId = data.stateParams.fromWalletId;
|
|
|
|
|
vm.toWalletId = data.stateParams.toWalletId;
|
2018-08-02 17:10:58 +12:00
|
|
|
vm.minAmount = parseFloat(data.stateParams.minAmount);
|
|
|
|
|
vm.maxAmount = parseFloat(data.stateParams.maxAmount);
|
2018-08-02 15:15:23 +02:00
|
|
|
|
|
|
|
|
if (passthroughParams.thirdParty) {
|
|
|
|
|
vm.thirdParty = JSON.parse(passthroughParams.thirdParty); // Parse stringified JSON-object
|
|
|
|
|
if (vm.thirdParty) {
|
|
|
|
|
if (vm.thirdParty.id === 'shapeshift') {
|
|
|
|
|
if (!vm.thirdParty.data) {
|
|
|
|
|
vm.thirdParty.data = {};
|
|
|
|
|
}
|
|
|
|
|
vm.thirdParty.data['fromWalletId'] = vm.fromWalletId;
|
|
|
|
|
|
|
|
|
|
vm.fromWallet = profileService.getWallet(vm.fromWalletId);
|
|
|
|
|
vm.toWallet = profileService.getWallet(vm.toWalletId);
|
|
|
|
|
|
|
|
|
|
shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) {
|
|
|
|
|
console.log(data);
|
|
|
|
|
vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum);
|
|
|
|
|
vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// if (vm.thirdParty.data['shapeshiftOrderId'] && data.stateParams.shapeshiftOrderId.length > 0) {
|
|
|
|
|
// vm.shapeshiftOrderId = vm.thirdParty.data['shapeshiftOrderId'];
|
|
|
|
|
// }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// vm.shapeshiftOrderId = data.stateParams.thirdPartyOrderId;
|
2018-08-02 17:10:58 +12:00
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
vm.isRequestingSpecificAmount = !data.stateParams.fromWalletId;
|
2018-08-02 15:15:23 +02:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
var config = configService.getSync().wallet.settings;
|
2017-08-31 17:13:54 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
setAvailableUnits();
|
|
|
|
|
updateUnitUI();
|
2017-12-12 17:15:39 +09:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
var reNr = /^[1234567890\.]$/;
|
|
|
|
|
var reOp = /^[\*\+\-\/]$/;
|
|
|
|
|
|
|
|
|
|
if (!isAndroid && !isIos) {
|
|
|
|
|
var disableKeys = angular.element($window).on('keydown', function(e) {
|
|
|
|
|
if (!e.key) return;
|
|
|
|
|
if (e.which === 8) { // you can add others here inside brackets.
|
|
|
|
|
if (!altCurrencyModal) {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
vm.removeDigit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (e.key.match(reNr)) {
|
|
|
|
|
vm.pushDigit(e.key);
|
|
|
|
|
} else if (e.key.match(reOp)) {
|
|
|
|
|
pushOperator(e.key);
|
|
|
|
|
} else if (e.keyCode === 86) {
|
|
|
|
|
if (e.ctrlKey || e.metaKey) processClipboard();
|
|
|
|
|
} else if (e.keyCode === 13) vm.finish();
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unitToSatoshi = config.unitToSatoshi;
|
|
|
|
|
satToUnit = 1 / unitToSatoshi;
|
|
|
|
|
unitDecimals = config.unitDecimals;
|
|
|
|
|
|
|
|
|
|
resetAmount();
|
|
|
|
|
|
|
|
|
|
processAmount();
|
|
|
|
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$ionicScrollDelegate.resize();
|
|
|
|
|
}, 10);
|
|
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
function setAvailableUnits() {
|
2017-11-01 16:26:42 +09:00
|
|
|
var defaults = configService.getDefaults();
|
|
|
|
|
var configCache = configService.getSync();
|
2017-08-31 17:56:28 -03:00
|
|
|
availableUnits = [];
|
2017-08-31 17:13:54 -03:00
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
var coinFromWallet = '';
|
|
|
|
|
if (passthroughParams.fromWalletId) {
|
|
|
|
|
var fromWallet = profileService.getWallet(passthroughParams.fromWalletId);
|
|
|
|
|
coinFromWallet = fromWallet.coin;
|
|
|
|
|
} else {
|
|
|
|
|
var toWallet = profileService.getWallet(passthroughParams.toWalletId);
|
|
|
|
|
coinFromWallet = toWallet.coin;
|
|
|
|
|
}
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
if (coinFromWallet === 'bch') {
|
2017-08-31 17:56:28 -03:00
|
|
|
availableUnits.push({
|
|
|
|
|
name: 'Bitcoin Cash',
|
|
|
|
|
id: 'bch',
|
2017-11-01 16:26:42 +09:00
|
|
|
shortName: (configCache.bitcoinCashAlias || defaults.bitcoinCashAlias).toUpperCase(),
|
2017-08-31 17:56:28 -03:00
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
if (coinFromWallet === 'btc') {
|
2017-11-15 18:24:48 +09:00
|
|
|
availableUnits.push({
|
|
|
|
|
name: 'Bitcoin',
|
|
|
|
|
id: 'btc',
|
|
|
|
|
shortName: (configCache.bitcoinAlias || defaults.bitcoinAlias).toUpperCase(),
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
unitIndex = 0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// currency have preference
|
|
|
|
|
var fiatName;
|
|
|
|
|
if (data.stateParams.currency) {
|
|
|
|
|
fiatCode = data.stateParams.currency;
|
|
|
|
|
altUnitIndex = unitIndex
|
|
|
|
|
unitIndex = availableUnits.length;
|
|
|
|
|
} else {
|
|
|
|
|
fiatCode = config.alternativeIsoCode || 'USD';
|
|
|
|
|
fiatName = config.alternanativeName || fiatCode;
|
|
|
|
|
altUnitIndex = availableUnits.length;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
availableUnits.push({
|
|
|
|
|
name: fiatName || fiatCode,
|
|
|
|
|
// TODO
|
|
|
|
|
id: fiatCode,
|
|
|
|
|
shortName: fiatCode,
|
|
|
|
|
isFiat: true,
|
|
|
|
|
});
|
|
|
|
|
|
2018-01-26 16:58:48 -04:00
|
|
|
unitIndex = lodash.findIndex(availableUnits, {
|
|
|
|
|
isFiat: true
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
altUnitIndex = 0;
|
2018-07-28 20:24:29 -07:00
|
|
|
|
2018-08-02 15:44:59 +12:00
|
|
|
if (passthroughParams.fromWalletId) {
|
|
|
|
|
var fromWallet = profileService.getWallet(passthroughParams.fromWalletId);
|
2018-07-28 20:24:29 -07:00
|
|
|
updateAvailableFundsFromWallet(fromWallet);
|
|
|
|
|
}
|
2017-08-31 17:13:54 -03:00
|
|
|
};
|
2018-07-24 12:37:51 -07:00
|
|
|
};
|
2017-08-31 17:13:54 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function goBack() {
|
|
|
|
|
if (vm.shapeshiftOrderId) {
|
2017-12-14 17:56:47 +09:00
|
|
|
$state.go('tabs.send').then(function() {
|
|
|
|
|
$ionicHistory.clearHistory();
|
|
|
|
|
$state.go('tabs.home').then(function() {
|
|
|
|
|
$state.transitionTo('tabs.shapeshift');
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
} else {
|
|
|
|
|
$ionicHistory.goBack();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
function paste(value) {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = value;
|
2018-07-24 12:37:51 -07:00
|
|
|
processAmount();
|
2017-08-31 17:56:28 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
2016-11-03 17:53:32 -03:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
function processClipboard() {
|
|
|
|
|
if (!isNW) return;
|
|
|
|
|
var value = nodeWebkitService.readFromClipboard();
|
|
|
|
|
if (value && evaluate(value) > 0) paste(evaluate(value));
|
|
|
|
|
};
|
2016-11-03 17:53:32 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function sendMax() {
|
|
|
|
|
useSendMax = true;
|
|
|
|
|
finish();
|
2017-08-31 17:56:28 -03:00
|
|
|
};
|
2016-07-07 16:55:04 -03:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
function updateUnitUI() {
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.unit = availableUnits[unitIndex].shortName;
|
|
|
|
|
vm.alternativeUnit = availableUnits[altUnitIndex].shortName;
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
processAmount();
|
|
|
|
|
$log.debug('Update unit coin @amount unit:' + vm.unit + " alternativeUnit:" + vm.alternativeUnit);
|
2017-08-31 17:56:28 -03:00
|
|
|
};
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function changeUnit() {
|
2017-12-27 21:38:39 +00:00
|
|
|
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = '0';
|
2017-12-27 21:38:39 +00:00
|
|
|
|
2018-01-26 16:58:48 -04:00
|
|
|
if (!(availableUnits[unitIndex].isFiat && availableUnits.length > 2 && altUnitIndex == 0)) {
|
|
|
|
|
unitIndex++;
|
|
|
|
|
if (unitIndex >= availableUnits.length) unitIndex = 0;
|
|
|
|
|
}
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
if (availableUnits[unitIndex].isFiat) {
|
2018-01-26 16:58:48 -04:00
|
|
|
altUnitIndex = altUnitIndex == 0 && availableUnits.length > 2 ? 1 : 0;
|
2017-08-31 17:56:28 -03:00
|
|
|
} else {
|
|
|
|
|
altUnitIndex = lodash.findIndex(availableUnits, {
|
|
|
|
|
isFiat: true
|
|
|
|
|
});
|
|
|
|
|
}
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-30 18:28:27 +12:00
|
|
|
updateAvailableFundsStringIfNeeded();
|
2017-08-31 17:56:28 -03:00
|
|
|
updateUnitUI();
|
|
|
|
|
};
|
2017-08-29 10:52:26 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function pushDigit(digit) {
|
2018-07-31 20:18:27 +12:00
|
|
|
if (vm.amount && digit != '.') {
|
|
|
|
|
var amountSplitByComma = vm.amount.split('.');
|
2018-05-09 17:25:28 +09:00
|
|
|
if (amountSplitByComma.length > 1 && amountSplitByComma[1].length >= LENGTH_AFTER_COMMA_EXPRESSION_LIMIT) return;
|
|
|
|
|
if (amountSplitByComma.length == 1 && amountSplitByComma[0].length >= LENGTH_BEFORE_COMMA_EXPRESSION_LIMIT) return;
|
2018-05-09 17:01:05 +09:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 20:18:27 +12:00
|
|
|
if (vm.amount && vm.amount.length >= LENGTH_EXPRESSION_LIMIT) return;
|
|
|
|
|
if (vm.amount.indexOf('.') > -1 && digit == '.') return;
|
|
|
|
|
if (vm.amount == '0' && digit == '0') return;
|
|
|
|
|
if (availableUnits[unitIndex].isFiat && vm.amount.indexOf('.') > -1 && vm.amount[vm.amount.indexOf('.') + 2]) return;
|
2018-05-08 00:28:44 +09:00
|
|
|
|
2018-08-02 15:12:40 +02:00
|
|
|
if (vm.amount == '0' && digit != '.') {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = '';
|
2018-05-21 12:31:59 +09:00
|
|
|
}
|
|
|
|
|
|
2018-08-02 15:12:40 +02:00
|
|
|
if (vm.amount == '' && digit == '.') {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = '0';
|
2018-05-21 12:31:59 +09:00
|
|
|
}
|
|
|
|
|
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = (vm.amount + digit).replace('..', '.');
|
2018-07-24 12:37:51 -07:00
|
|
|
processAmount();
|
2017-08-31 17:56:28 -03:00
|
|
|
};
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function pushOperator(operator) {
|
2018-07-31 20:18:27 +12:00
|
|
|
if (!vm.amount || vm.amount.length == 0) return;
|
|
|
|
|
vm.amount = pushOperator(vm.amount);
|
2017-08-31 17:56:28 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function pushOperator(val) {
|
2017-08-31 17:56:28 -03:00
|
|
|
if (!isOperator(lodash.last(val))) {
|
|
|
|
|
return val + operator;
|
|
|
|
|
} else {
|
|
|
|
|
return val.slice(0, -1) + operator;
|
|
|
|
|
}
|
2016-07-07 16:55:04 -03:00
|
|
|
};
|
2017-08-31 17:56:28 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function isOperator(val) {
|
|
|
|
|
var regex = /[\/\-\+\x\*]/;
|
|
|
|
|
return regex.test(val);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function isExpression(val) {
|
|
|
|
|
var regex = /^\.?\d+(\.?\d+)?([\/\-\+\*x]\d?\.?\d+)+$/;
|
|
|
|
|
return regex.test(val);
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function removeDigit() {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = (vm.amount).toString().slice(0, -1);
|
2018-07-24 12:37:51 -07:00
|
|
|
processAmount();
|
|
|
|
|
}
|
2017-08-31 17:56:28 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function resetAmount() {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.amount = vm.alternativeAmount = vm.globalResult = '0';
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.allowSend = false;
|
|
|
|
|
}
|
2018-05-08 16:17:19 +09:00
|
|
|
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function openPopup() {
|
2018-05-08 16:17:19 +09:00
|
|
|
$ionicModal.fromTemplateUrl('views/modals/altCurrency.html', {
|
|
|
|
|
scope: $scope
|
|
|
|
|
}).then(function(modal) {
|
2018-07-24 12:37:51 -07:00
|
|
|
altCurrencyModal = modal;
|
|
|
|
|
altCurrencyModal.show();
|
2018-05-08 16:17:19 +09:00
|
|
|
});
|
2018-07-24 12:37:51 -07:00
|
|
|
}
|
2018-05-08 16:17:19 +09:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function close() {
|
|
|
|
|
altCurrencyModal.remove();
|
|
|
|
|
altCurrencyModal = null;
|
2018-05-08 16:17:19 +09:00
|
|
|
};
|
2017-08-31 17:56:28 -03:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function processAmount() {
|
2018-07-31 20:18:27 +12:00
|
|
|
var formatedValue = format(vm.amount);
|
2017-08-31 17:56:28 -03:00
|
|
|
var result = evaluate(formatedValue);
|
2017-12-12 17:15:39 +09:00
|
|
|
|
2018-08-02 17:10:58 +12:00
|
|
|
var amountInCrypto = 0;
|
2018-08-02 16:40:00 +12:00
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
if (lodash.isNumber(result)) {
|
2018-07-31 20:18:27 +12:00
|
|
|
vm.globalResult = isExpression(vm.amount) ? '= ' + processResult(result) : '';
|
2017-08-31 17:56:28 -03:00
|
|
|
|
|
|
|
|
if (availableUnits[unitIndex].isFiat) {
|
2017-08-31 18:16:50 -03:00
|
|
|
|
|
|
|
|
var a = fromFiat(result);
|
|
|
|
|
if (a) {
|
2018-08-02 17:10:58 +12:00
|
|
|
amountInCrypto = a;
|
|
|
|
|
var amountInSatoshis = a * unitToSatoshi;
|
2018-08-02 15:15:23 +02:00
|
|
|
vm.fundsAreInsufficient = !!passthroughParams.fromWalletId
|
|
|
|
|
&& availableSatoshis !== null
|
2018-07-28 20:24:29 -07:00
|
|
|
&& availableSatoshis < amountInSatoshis;
|
|
|
|
|
|
|
|
|
|
vm.alternativeAmount = txFormatService.formatAmount(amountInSatoshis, true);
|
2018-08-02 15:12:40 +02:00
|
|
|
vm.allowSend = lodash.isNumber(a)
|
2018-07-28 20:24:29 -07:00
|
|
|
&& a > 0
|
2018-07-24 12:37:51 -07:00
|
|
|
&& (!vm.shapeshiftOrderId
|
2018-08-02 16:40:00 +12:00
|
|
|
|| (a >= vm.minAmount && a <= vm.maxAmount))
|
2018-08-02 15:12:40 +02:00
|
|
|
&& !vm.fundsAreInsufficient;
|
2017-08-31 18:16:50 -03:00
|
|
|
} else {
|
2017-09-15 12:33:17 -03:00
|
|
|
if (result) {
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.alternativeAmount = 'N/A';
|
2017-09-15 12:33:17 -03:00
|
|
|
} else {
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.alternativeAmount = null;
|
2017-09-15 12:33:17 -03:00
|
|
|
}
|
2018-07-28 20:24:29 -07:00
|
|
|
vm.fundsAreInsufficient = false;
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.allowSend = false;
|
2017-08-31 18:16:50 -03:00
|
|
|
}
|
2017-08-31 17:56:28 -03:00
|
|
|
} else {
|
2018-08-02 17:10:58 +12:00
|
|
|
amountInCrypto = result;
|
2018-08-02 15:15:23 +02:00
|
|
|
vm.fundsAreInsufficient = passthroughParams.fromWalletId
|
|
|
|
|
&& availableSatoshis !== null
|
2018-07-28 20:24:29 -07:00
|
|
|
&& availableSatoshis < result * unitToSatoshi;
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.alternativeAmount = $filter('formatFiatAmount')(toFiat(result));
|
2018-08-02 15:12:40 +02:00
|
|
|
vm.allowSend = lodash.isNumber(result)
|
2018-07-28 20:24:29 -07:00
|
|
|
&& result > 0
|
2018-07-24 12:37:51 -07:00
|
|
|
&& (!vm.shapeshiftOrderId
|
2018-08-02 16:40:00 +12:00
|
|
|
|| (result >= vm.minAmount && result <= vm.maxAmount))
|
2018-08-02 15:12:40 +02:00
|
|
|
&& !vm.fundsAreInsufficient;
|
2017-08-31 17:56:28 -03:00
|
|
|
}
|
2018-07-28 20:24:29 -07:00
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
vm.fundsAreInsufficient = false;
|
2017-08-31 17:56:28 -03:00
|
|
|
}
|
2018-08-02 16:40:00 +12:00
|
|
|
|
|
|
|
|
if (vm.fundsAreInsufficient) {
|
|
|
|
|
vm.errorMessage = gettextCatalog.getString('Not enough available funds');
|
2018-08-02 17:10:58 +12:00
|
|
|
|
|
|
|
|
} else if (amountInCrypto && vm.shapeshiftOrderId) {
|
|
|
|
|
if (amountInCrypto < vm.minAmount) {
|
2018-08-02 16:40:00 +12:00
|
|
|
vm.errorMessage = gettextCatalog.getString('Amount is below minimum');
|
2018-08-02 17:10:58 +12:00
|
|
|
|
|
|
|
|
} else if (amountInCrypto > vm.maxAmount) {
|
2018-08-02 16:40:00 +12:00
|
|
|
vm.errorMessage = gettextCatalog.getString('Amount is above maximum');
|
2018-08-02 15:12:40 +02:00
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
} else {
|
|
|
|
|
vm.errorMessage = '';
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
vm.errorMessage = '';
|
|
|
|
|
}
|
2017-08-31 17:56:28 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function processResult(val) {
|
|
|
|
|
if (availableUnits[unitIndex].isFiat) return $filter('formatFiatAmount')(val);
|
|
|
|
|
else return txFormatService.formatAmount(val.toFixed(unitDecimals) * unitToSatoshi, true);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function fromFiat(val) {
|
|
|
|
|
return parseFloat((rateService.fromFiat(val, fiatCode, availableUnits[altUnitIndex].id) * satToUnit).toFixed(unitDecimals));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function toFiat(val) {
|
2017-08-31 18:16:50 -03:00
|
|
|
if (!rateService.getRate(fiatCode)) return;
|
|
|
|
|
|
2017-08-31 17:56:28 -03:00
|
|
|
return parseFloat((rateService.toFiat(val * unitToSatoshi, fiatCode, availableUnits[unitIndex].id)).toFixed(2));
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function evaluate(val) {
|
|
|
|
|
var result;
|
|
|
|
|
try {
|
|
|
|
|
result = $scope.$eval(val);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
if (!lodash.isFinite(result)) return 0;
|
|
|
|
|
return result;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
function format(val) {
|
|
|
|
|
if (!val) return;
|
|
|
|
|
|
|
|
|
|
var result = val.toString();
|
|
|
|
|
|
|
|
|
|
if (isOperator(lodash.last(val))) result = result.slice(0, -1);
|
|
|
|
|
|
|
|
|
|
return result.replace('x', '*');
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function finish() {
|
2018-08-02 16:40:00 +12:00
|
|
|
var unit = availableUnits[unitIndex];
|
|
|
|
|
var uiAmount = evaluate(format(vm.amount));
|
2017-08-31 17:56:28 -03:00
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
var satoshis = 0;
|
|
|
|
|
if (unit.isFiat) {
|
|
|
|
|
satoshis = (fromFiat(uiAmount) * unitToSatoshi).toFixed(0);
|
|
|
|
|
} else {
|
|
|
|
|
satoshis = (uiAmount * unitToSatoshi).toFixed(0);
|
|
|
|
|
}
|
2018-08-02 15:44:59 +12:00
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
var confirmData = {
|
|
|
|
|
amount: useSendMax ? undefined : satoshis,
|
|
|
|
|
fromWalletId: passthroughParams.fromWalletId,
|
|
|
|
|
sendMax: useSendMax,
|
|
|
|
|
toAddr: passthroughParams.toAddress,
|
|
|
|
|
toWalletId: passthroughParams.toWalletId
|
|
|
|
|
};
|
2018-01-26 15:01:18 -04:00
|
|
|
|
2018-08-02 15:15:23 +02:00
|
|
|
if (vm.thirdParty) {
|
|
|
|
|
confirmData['thirdParty'] = JSON.stringify(this.thirdParty);
|
|
|
|
|
}
|
|
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
console.log('confirmData:', confirmData);
|
2018-01-26 15:01:18 -04:00
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
if (!confirmData.fromWalletId) {
|
|
|
|
|
$state.transitionTo('tabs.paymentRequest.confirm', confirmData);
|
|
|
|
|
} else {
|
2018-08-02 15:44:59 +12:00
|
|
|
|
2018-08-02 16:40:00 +12:00
|
|
|
if (vm.shapeshiftOrderId) {
|
|
|
|
|
var shapeshiftOrderUrl = 'https://www.shapeshift.io/#/status/';
|
|
|
|
|
shapeshiftOrderUrl += vm.shapeshiftOrderId;
|
|
|
|
|
confirmData.description = shapeshiftOrderUrl;
|
|
|
|
|
|
|
|
|
|
if (confirmData.sendMax) {
|
|
|
|
|
var wallet = lodash.find(profileService.getWallets({ coin: coin }),
|
|
|
|
|
function(w) {
|
2018-08-02 17:10:58 +12:00
|
|
|
return w.id == passthroughParams.fromWalletId;
|
2018-08-02 16:40:00 +12:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
var balance = parseFloat(wallet.cachedBalance.substring(0, wallet.cachedBalance.length-4));
|
|
|
|
|
if (balance < vm.minAmount * 1.04) {
|
|
|
|
|
confirmData.sendMax = false;
|
|
|
|
|
confirmData.amount = vm.minAmount * unitToSatoshi;
|
|
|
|
|
} else if (balance > vm.maxAmount) {
|
|
|
|
|
confirmData.sendMax = false;
|
|
|
|
|
confirmData.amount = vm.maxAmount * unitToSatoshi * 0.99;
|
2018-01-03 14:41:59 +00:00
|
|
|
}
|
2018-08-02 15:44:59 +12:00
|
|
|
}
|
2017-12-17 11:48:00 +00:00
|
|
|
}
|
|
|
|
|
|
2018-08-01 11:33:24 +12:00
|
|
|
$state.transitionTo('tabs.send.review', confirmData);
|
2017-12-17 11:48:00 +00:00
|
|
|
}
|
2018-01-26 15:01:18 -04:00
|
|
|
$scope.useSendMax = null;
|
2017-08-31 17:56:28 -03:00
|
|
|
}
|
|
|
|
|
};
|
2018-05-08 16:17:19 +09:00
|
|
|
|
|
|
|
|
|
|
|
|
|
// Currency
|
|
|
|
|
|
|
|
|
|
var next = 10;
|
2018-05-08 16:44:55 +09:00
|
|
|
var completeAlternativeList = [];
|
|
|
|
|
|
|
|
|
|
var popularCurrencyList = [
|
|
|
|
|
{isoCode: 'USD', order: 0},
|
|
|
|
|
{isoCode: 'EUR', order: 1},
|
|
|
|
|
{isoCode: 'JPY', order: 2},
|
|
|
|
|
{isoCode: 'GBP', order: 3},
|
|
|
|
|
{isoCode: 'AUD', order: 4},
|
|
|
|
|
{isoCode: 'CAD', order: 5},
|
|
|
|
|
{isoCode: 'CHF', order: 6},
|
|
|
|
|
{isoCode: 'CNY', order: 7},
|
|
|
|
|
{isoCode: 'KRW', order: 8},
|
|
|
|
|
{isoCode: 'HKD', order: 9},
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
function initCurrencies() {
|
|
|
|
|
var unusedCurrencyList = [{
|
|
|
|
|
isoCode: 'LTL'
|
|
|
|
|
}, {
|
|
|
|
|
isoCode: 'BTC'
|
|
|
|
|
}, {
|
|
|
|
|
isoCode: 'BCC'
|
|
|
|
|
}, {
|
|
|
|
|
isoCode: 'BCH_BTC'
|
|
|
|
|
}, {
|
|
|
|
|
isoCode: 'BCH'
|
|
|
|
|
}];
|
|
|
|
|
rateService.whenAvailable(function() {
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.listComplete = false;
|
2018-05-08 16:44:55 +09:00
|
|
|
|
|
|
|
|
var idx = lodash.indexBy(unusedCurrencyList, 'isoCode');
|
2018-07-24 12:37:51 -07:00
|
|
|
var idx2 = lodash.indexBy(lastUsedAltCurrencyList, 'isoCode');
|
2018-05-08 16:44:55 +09:00
|
|
|
var idx3 = lodash.indexBy(popularCurrencyList, 'isoCode');
|
|
|
|
|
var alternatives = rateService.listAlternatives(true);
|
|
|
|
|
|
|
|
|
|
lodash.each(alternatives, function(c) {
|
|
|
|
|
if (idx3[c.isoCode]) {
|
|
|
|
|
idx3[c.isoCode].name = c.name;
|
|
|
|
|
}
|
|
|
|
|
if (!idx[c.isoCode] && !idx2[c.isoCode] && !idx3[c.isoCode]) {
|
|
|
|
|
completeAlternativeList.push(c);
|
|
|
|
|
}
|
2018-05-08 16:17:19 +09:00
|
|
|
});
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.altCurrencyList = completeAlternativeList.slice(0, 10);
|
|
|
|
|
vm.lastUsedPopularList = lodash.unique(lodash.union(lastUsedAltCurrencyList, popularCurrencyList), 'isoCode');
|
2018-05-08 16:17:19 +09:00
|
|
|
|
2018-07-28 20:24:29 -07:00
|
|
|
rateService.updateRates();
|
|
|
|
|
|
2018-05-08 16:17:19 +09:00
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
2018-05-08 16:44:55 +09:00
|
|
|
});
|
|
|
|
|
}
|
2018-05-08 16:17:19 +09:00
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function loadMore() {
|
2018-05-08 16:44:55 +09:00
|
|
|
$timeout(function() {
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.altCurrencyList = completeAlternativeList.slice(0, next);
|
2018-05-08 16:44:55 +09:00
|
|
|
next += 10;
|
2018-07-24 12:37:51 -07:00
|
|
|
vm.listComplete = vm.altCurrencyList.length >= completeAlternativeList.length;
|
2018-05-08 16:44:55 +09:00
|
|
|
$scope.$broadcast('scroll.infiniteScrollComplete');
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function findCurrency(search) {
|
2018-05-08 16:44:55 +09:00
|
|
|
if (!search) initCurrencies();
|
2018-07-24 12:37:51 -07:00
|
|
|
var list = lodash.unique(lodash.union(completeAlternativeList, lodash.union(lastUsedAltCurrencyList, popularCurrencyList)), 'isoCode');
|
|
|
|
|
vm.altCurrencyList = lodash.filter(list, function(item) {
|
2018-05-08 16:44:55 +09:00
|
|
|
var val = item.name
|
|
|
|
|
var val2 = item.isoCode;
|
|
|
|
|
return lodash.includes(val.toLowerCase(), search.toLowerCase()) || lodash.includes(val2.toLowerCase(), search.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
function save(newAltCurrency) {
|
2018-05-08 16:44:55 +09:00
|
|
|
var opts = {
|
|
|
|
|
wallet: {
|
|
|
|
|
settings: {
|
|
|
|
|
alternativeName: newAltCurrency.name,
|
|
|
|
|
alternativeIsoCode: newAltCurrency.isoCode,
|
2018-05-08 16:17:19 +09:00
|
|
|
}
|
2018-05-08 16:44:55 +09:00
|
|
|
}
|
|
|
|
|
};
|
2018-05-08 16:17:19 +09:00
|
|
|
|
2018-05-08 16:44:55 +09:00
|
|
|
configService.set(opts, function(err) {
|
|
|
|
|
if (err) $log.warn(err);
|
|
|
|
|
walletService.updateRemotePreferences(profileService.getWallets());
|
|
|
|
|
var altUnitIndex = lodash.findIndex(availableUnits, {
|
|
|
|
|
isFiat: true
|
2018-05-08 16:17:19 +09:00
|
|
|
});
|
2018-05-08 16:44:55 +09:00
|
|
|
availableUnits[altUnitIndex].id = newAltCurrency.isoCode;
|
|
|
|
|
availableUnits[altUnitIndex].name = newAltCurrency.isoCode;
|
|
|
|
|
availableUnits[altUnitIndex].shortName = newAltCurrency.isoCode;
|
|
|
|
|
fiatCode = newAltCurrency.isoCode;
|
2018-07-30 18:28:27 +12:00
|
|
|
updateAvailableFundsStringIfNeeded();
|
2018-05-08 16:44:55 +09:00
|
|
|
updateUnitUI();
|
2018-07-24 12:37:51 -07:00
|
|
|
close();
|
2018-05-08 16:44:55 +09:00
|
|
|
});
|
2018-07-28 20:24:29 -07:00
|
|
|
};
|
2018-08-02 15:12:40 +02:00
|
|
|
|
2018-07-30 18:28:27 +12:00
|
|
|
function updateAvailableFundsStringIfNeeded() {
|
2018-08-02 17:10:58 +12:00
|
|
|
if (passthroughParams.fromWalletId && availableSatoshis !== null) {
|
2018-07-28 20:24:29 -07:00
|
|
|
availableFundsInFiat = '';
|
|
|
|
|
vm.availableFunds = availableFundsInCrypto;
|
|
|
|
|
var coin = availableUnits[altUnitIndex].isFiat ? availableUnits[unitIndex].id : availableUnits[altUnitIndex].id;
|
|
|
|
|
txFormatService.formatAlternativeStr(coin, availableSatoshis, function formatCallback(formatted){
|
|
|
|
|
if (formatted) {
|
|
|
|
|
availableFundsInFiat = formatted;
|
2018-07-31 13:25:56 +12:00
|
|
|
|
|
|
|
|
$scope.$apply(function() {
|
|
|
|
|
if (availableUnits[unitIndex].isFiat) {
|
|
|
|
|
vm.availableFunds = availableFundsInFiat;
|
|
|
|
|
} else {
|
|
|
|
|
vm.availableFunds = availableFundsInCrypto;
|
|
|
|
|
}
|
|
|
|
|
});
|
2018-07-28 20:24:29 -07:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function updateAvailableFundsFromWallet(wallet) {
|
|
|
|
|
if (wallet.status && wallet.status.isValid) {
|
|
|
|
|
availableFundsInCrypto = wallet.status.spendableBalanceStr;
|
|
|
|
|
availableSatoshis = wallet.status.spendableAmount;
|
|
|
|
|
if (wallet.status.alternativeBalanceAvailable) {
|
|
|
|
|
availableFundsInFiat = wallet.status.spendableBalanceAlternative + ' ' + wallet.status.alternativeIsoCode;
|
|
|
|
|
} else {
|
|
|
|
|
availableFundsInFiat = '';
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} else if (wallet.cachedStatus && wallet.status.isValid) {
|
|
|
|
|
|
|
|
|
|
if (wallet.cachedStatus.alternativeBalanceAvailable) {
|
|
|
|
|
availableFundsInFiat = wallet.cachedStatus.spendableBalanceAlternative + ' ' + wallet.cachedStatus.alternativeIsoCode;
|
|
|
|
|
} else {
|
|
|
|
|
availableFundsInFiat = '';
|
|
|
|
|
}
|
|
|
|
|
availableFundsInCrypto = wallet.cachedStatus.spendableBalanceStr;
|
|
|
|
|
availableSatoshis = wallet.cachedStatus.spendableAmount;
|
|
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
|
|
availableFundsInFiat = '';
|
|
|
|
|
availableFundsInCrypto = '';
|
|
|
|
|
availableSatoshis = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (availableUnits[unitIndex].isFiat) {
|
|
|
|
|
vm.availableFunds = availableFundsInFiat || availableFundsInCrypto;
|
|
|
|
|
} else {
|
|
|
|
|
vm.availableFunds = availableFundsInCrypto;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-24 12:37:51 -07:00
|
|
|
}
|