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

203 lines
6.1 KiB
JavaScript
Raw Normal View History

2016-05-20 11:37:13 -03:00
'use strict';
2017-05-23 09:46:42 -03:00
angular.module('copayApp.controllers').controller('txDetailsController', function($rootScope, $log, $ionicHistory, $scope, $timeout, walletService, lodash, gettextCatalog, profileService, externalLinkService, popupService, ongoingProcess, txFormatService, txConfirmNotification) {
2016-11-04 17:03:20 -03:00
var txId;
2017-03-08 10:28:55 -03:00
var listeners = [];
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;
$scope.isShared = $scope.wallet.credentials.n > 1;
2017-05-23 09:46:42 -03:00
txConfirmNotification.checkIfEnabled(txId, function(res) {
$scope.txNotification = { value: res };
});
});
$scope.$on("$ionicView.afterEnter", function(event) {
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-05-18 18:51:49 -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
2016-10-21 17:16:35 -04:00
function getDisplayAmount(amountStr) {
return amountStr.split(' ')[0];
}
function getDisplayUnit(amountStr) {
return amountStr.split(' ')[1];
}
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'));
}
$scope.btx = txFormatService.processTx(tx);
2017-03-07 16:50:44 -03:00
txFormatService.formatAlternativeStr(tx.fees, function(v) {
$scope.feeFiatStr = v;
});
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');
}
$scope.displayAmount = getDisplayAmount($scope.btx.amountStr);
$scope.displayUnit = getDisplayUnit($scope.btx.amountStr);
updateMemo();
initActionList();
getFiatRate();
$timeout(function() {
$scope.$apply();
});
});
};
2017-05-18 18:51:49 -03:00
var updateTxDebounced = lodash.debounce(updateTx, 5000);
$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;
var url = 'https://' + ($scope.getShortNetworkName() == 'test' ? 'test-' : '') + 'insight.bitpay.com/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) {
txConfirmNotification.subscribe($scope.wallet, { txid: txId });
} else {
txConfirmNotification.unsubscribe($scope.wallet, txId);
}
};
});