Wallet/src/js/controllers/tx-details.js

241 lines
7.7 KiB
JavaScript
Raw Normal View History

2016-05-20 11:37:13 -03:00
'use strict';
angular.module('copayApp.controllers').controller('txDetailsController', function($rootScope, $log, $ionicHistory, $scope, $timeout, walletService, lodash, gettextCatalog, profileService, externalLinkService, popupService, ongoingProcess, txFormatService, txConfirmNotification, feeService, configService, bitcoinCashJsService) {
2016-11-04 17:03:20 -03:00
var txId;
2017-03-08 10:28:55 -03:00
var listeners = [];
var config = configService.getSync();
2017-11-20 16:02:54 +09:00
var defaults = configService.getDefaults();
2017-08-31 14:42:43 -03:00
var blockexplorerUrl;
2016-11-04 17:03:20 -03:00
$scope.$on("$ionicView.beforeEnter", function(event, data) {
txId = data.stateParams.txid;
2016-11-04 17:03:20 -03:00
$scope.title = gettextCatalog.getString('Transaction');
$scope.wallet = profileService.getWallet(data.stateParams.walletId);
$scope.color = $scope.wallet.color;
$scope.copayerId = $scope.wallet.credentials.copayerId;
2017-06-21 11:11:40 -03:00
$scope.isShared = $scope.wallet.credentials.n > 1;
2017-07-14 15:45:18 -03:00
$scope.txsUnsubscribedForNotifications = config.confirmedTxsNotifications ? !config.confirmedTxsNotifications.enabled : true;
2017-05-23 09:46:42 -03:00
2017-11-20 16:02:54 +09:00
blockexplorerUrl = defaults.blockExplorer[$scope.wallet.coin];
2017-08-31 14:42:43 -03:00
2017-05-23 09:46:42 -03:00
txConfirmNotification.checkIfEnabled(txId, function(res) {
2017-06-21 11:11:40 -03:00
$scope.txNotification = {
value: res
};
2017-05-23 09:46:42 -03:00
});
2017-03-08 10:28:55 -03:00
updateTx();
listeners = [
$rootScope.$on('bwsEvent', function(e, walletId, type, n) {
if (type == 'NewBlock' && n && n.data && n.data.network == 'livenet') {
2017-06-21 11:11:40 -03:00
updateTxDebounced({
hideLoading: true
});
2017-03-08 10:28:55 -03:00
}
})
];
});
$scope.$on("$ionicView.leave", function(event, data) {
lodash.each(listeners, function(x) {
x();
});
2016-11-04 17:03:20 -03:00
});
2016-09-21 10:49:56 -03:00
2017-08-30 10:25:53 -03:00
$scope.readMore = function() {
var url = 'https://github.com/bitpay/copay/wiki/COPAY---FAQ#amount-too-low-to-spend';
var optIn = true;
var title = null;
var message = gettextCatalog.getString('Read more in our Wiki');
var okText = gettextCatalog.getString('Open');
var cancelText = gettextCatalog.getString('Go Back');
externalLinkService.open(url, optIn, title, message, okText, cancelText);
};
2016-09-21 10:49:56 -03:00
function updateMemo() {
2016-11-04 17:03:20 -03:00
walletService.getTxNote($scope.wallet, $scope.btx.txid, function(err, note) {
2016-10-12 10:19:27 -03:00
if (err) {
2016-10-17 11:15:36 -03:00
$log.warn('Could not fetch transaction note: ' + err);
2016-09-21 10:49:56 -03:00
return;
}
2016-10-12 10:19:27 -03:00
if (!note) return;
2016-10-11 16:46:06 -03:00
$scope.btx.note = note;
2016-11-04 17:03:20 -03:00
$scope.$apply();
2016-09-21 10:49:56 -03:00
});
2016-10-21 18:06:51 -04:00
}
2016-09-19 17:52:01 -03:00
function initActionList() {
$scope.actionList = [];
if ($scope.btx.action != 'sent' || !$scope.isShared) return;
var actionDescriptions = {
created: gettextCatalog.getString('Proposal Created'),
accept: gettextCatalog.getString('Accepted'),
reject: gettextCatalog.getString('Rejected'),
broadcasted: gettextCatalog.getString('Broadcasted'),
};
$scope.actionList.push({
type: 'created',
time: $scope.btx.createdOn,
description: actionDescriptions['created'],
by: $scope.btx.creatorName
});
lodash.each($scope.btx.actions, function(action) {
$scope.actionList.push({
type: action.type,
time: action.createdOn,
description: actionDescriptions[action.type],
by: action.copayerName
});
});
$scope.actionList.push({
type: 'broadcasted',
time: $scope.btx.time,
description: actionDescriptions['broadcasted'],
});
2017-01-06 15:45:36 -03:00
$timeout(function() {
$scope.actionList.reverse();
}, 10);
2016-10-21 19:13:14 -04:00
}
2016-06-23 18:46:48 -03:00
var updateTx = function(opts) {
opts = opts || {};
if (!opts.hideLoading) ongoingProcess.set('loadingTxInfo', true);
walletService.getTx($scope.wallet, txId, function(err, tx) {
if (!opts.hideLoading) ongoingProcess.set('loadingTxInfo', false);
if (err) {
2017-05-18 18:51:49 -03:00
$log.warn('Error getting transaction: ' + err);
$ionicHistory.goBack();
2017-03-08 15:55:30 -03:00
return popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Transaction not available at this time'));
}
2017-08-28 15:51:13 -03:00
$scope.btx = txFormatService.processTx($scope.wallet.coin, tx);
var cashaddrDate = new Date(2018, 0, 16);
var txDate = new Date(($scope.btx.createdOn || $scope.btx.time) * 1000);
if ($scope.wallet.coin == 'bch' && txDate >= cashaddrDate) {
var bchAddresses = bitcoinCashJsService.translateAddresses($scope.btx.addressTo);
$scope.btx.displayAddress = bchAddresses.cashaddr;
$scope.btx.copyAddress = 'bitcoincash:' + $scope.btx.displayAddress;
} else {
$scope.btx.displayAddress = $scope.btx.addressTo;
$scope.btx.copyAddress = $scope.btx.displayAddress;
}
2017-08-28 15:51:13 -03:00
txFormatService.formatAlternativeStr($scope.wallet.coin, tx.fees, function(v) {
2017-06-21 11:11:40 -03:00
$scope.btx.feeFiatStr = v;
$scope.btx.feeRateStr = ($scope.btx.fees / ($scope.btx.amount + $scope.btx.fees) * 100).toFixed(2) + '%';
2017-03-07 16:50:44 -03:00
});
if ($scope.btx.action != 'invalid') {
if ($scope.btx.action == 'sent') $scope.title = gettextCatalog.getString('Sent Funds');
if ($scope.btx.action == 'received') $scope.title = gettextCatalog.getString('Received Funds');
if ($scope.btx.action == 'moved') $scope.title = gettextCatalog.getString('Moved Funds');
}
updateMemo();
initActionList();
getFiatRate();
2017-06-23 17:05:00 -03:00
$timeout(function() {
$scope.$digest();
});
2017-06-22 11:38:13 -03:00
2017-08-29 15:47:39 -03:00
feeService.getFeeLevels($scope.wallet.coin, function(err, levels) {
2017-06-23 12:20:51 -03:00
if (err) return;
walletService.getLowAmount($scope.wallet, levels, function(err, amount) {
if (err) return;
2017-12-04 14:28:37 +09:00
if ($scope.wallet.coin == 'bch') return;
2017-06-23 12:20:51 -03:00
$scope.btx.lowAmount = tx.amount < amount;
$timeout(function() {
$scope.$apply();
});
2017-06-22 11:38:13 -03:00
2017-06-23 12:20:51 -03:00
});
});
});
};
2017-05-18 18:51:49 -03:00
var updateTxDebounced = lodash.debounce(updateTx, 5000);
2017-06-21 11:11:40 -03:00
$scope.showCommentPopup = function() {
2016-09-21 14:55:56 -03:00
var opts = {};
2016-10-22 17:43:05 -03:00
if ($scope.btx.message) {
2016-10-21 17:16:35 -04:00
opts.defaultText = $scope.btx.message;
}
2016-09-21 14:55:56 -03:00
if ($scope.btx.note && $scope.btx.note.body) opts.defaultText = $scope.btx.note.body;
2016-11-04 17:03:20 -03:00
popupService.showPrompt($scope.wallet.name, gettextCatalog.getString('Memo'), opts, function(text) {
2016-09-21 14:55:56 -03:00
if (typeof text == "undefined") return;
2016-11-08 09:58:48 -03:00
$scope.btx.note = {
body: text
};
2016-09-19 12:00:44 -03:00
$log.debug('Saving memo');
2016-05-27 15:06:41 -03:00
2016-06-04 17:52:34 -03:00
var args = {
txid: $scope.btx.txid,
2016-09-21 10:49:56 -03:00
body: text
2016-06-04 17:52:34 -03:00
};
2016-11-04 17:03:20 -03:00
walletService.editTxNote($scope.wallet, args, function(err, res) {
if (err) {
2016-10-12 10:19:27 -03:00
$log.debug('Could not save tx comment ' + err);
}
});
2016-09-19 12:00:44 -03:00
});
};
2016-10-21 18:40:37 -04:00
$scope.viewOnBlockchain = function() {
var btx = $scope.btx;
2017-08-31 14:42:43 -03:00
var url = 'https://' + ($scope.getShortNetworkName() == 'test' ? 'test-' : '') + blockexplorerUrl + '/tx/' + btx.txid;
2016-12-12 17:29:11 -03:00
var optIn = true;
2016-12-28 16:29:45 -03:00
var title = null;
var message = gettextCatalog.getString('View Transaction on Insight');
2016-12-12 17:29:11 -03:00
var okText = gettextCatalog.getString('Open Insight');
var cancelText = gettextCatalog.getString('Go Back');
externalLinkService.open(url, optIn, title, message, okText, cancelText);
};
$scope.getShortNetworkName = function() {
2016-11-04 17:03:20 -03:00
var n = $scope.wallet.credentials.network;
return n.substring(0, 4);
};
var getFiatRate = function() {
$scope.alternativeIsoCode = $scope.wallet.status.alternativeIsoCode;
2016-11-09 10:40:18 -03:00
$scope.wallet.getFiatRate({
code: $scope.alternativeIsoCode,
2016-11-09 10:40:18 -03:00
ts: $scope.btx.time * 1000
}, function(err, res) {
if (err) {
$log.debug('Could not get historic rate');
return;
}
if (res && res.rate) {
$scope.rateDate = res.fetchedOn;
$scope.rate = res.rate;
2016-11-09 10:40:18 -03:00
}
});
};
2017-05-23 09:46:42 -03:00
$scope.txConfirmNotificationChange = function() {
if ($scope.txNotification.value) {
2017-06-21 11:11:40 -03:00
txConfirmNotification.subscribe($scope.wallet, {
txid: txId
});
2017-05-23 09:46:42 -03:00
} else {
txConfirmNotification.unsubscribe($scope.wallet, txId);
}
};
});