2015-08-31 18:12:04 -03:00
|
|
|
|
'use strict';
|
|
|
|
|
|
|
2016-01-21 14:52:58 -03:00
|
|
|
|
angular.module('copayApp.controllers').controller('sellGlideraController',
|
2016-08-31 16:43:52 -03:00
|
|
|
|
function($scope, $timeout, $log, profileService, glideraService, bwcError, lodash, walletService, configService, ongoingProcess, popupService, gettextCatalog) {
|
2015-09-01 18:53:47 -03:00
|
|
|
|
|
2015-09-11 13:11:41 -03:00
|
|
|
|
var self = this;
|
2015-09-01 18:53:47 -03:00
|
|
|
|
this.data = {};
|
|
|
|
|
|
this.show2faCodeInput = null;
|
2015-09-03 02:50:59 -03:00
|
|
|
|
this.success = null;
|
2016-08-24 11:33:43 -03:00
|
|
|
|
var wallet;
|
2016-09-23 13:34:47 -03:00
|
|
|
|
$scope.network = glideraService.getEnvironment();
|
2015-09-11 13:11:41 -03:00
|
|
|
|
|
2016-08-31 16:43:52 -03:00
|
|
|
|
$scope.$on('Wallet/Changed', function(event, w) {
|
|
|
|
|
|
if (lodash.isEmpty(w)) {
|
|
|
|
|
|
$log.debug('No wallet provided');
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
wallet = w;
|
|
|
|
|
|
$log.debug('Wallet changed: ' + w.name);
|
|
|
|
|
|
});
|
2016-05-09 15:56:44 -03:00
|
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
|
$scope.update = function(opts) {
|
|
|
|
|
|
if (!$scope.token || !$scope.permissions) return;
|
|
|
|
|
|
$log.debug('Updating Glidera Account...');
|
|
|
|
|
|
var accessToken = $scope.token;
|
|
|
|
|
|
var permissions = $scope.permissions;
|
2016-06-16 18:29:56 -03:00
|
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
|
opts = opts || {};
|
|
|
|
|
|
|
|
|
|
|
|
glideraService.getStatus(accessToken, function(err, data) {
|
|
|
|
|
|
$scope.status = data;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
glideraService.getLimits(accessToken, function(err, limits) {
|
|
|
|
|
|
$scope.limits = limits;
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (permissions.transaction_history) {
|
|
|
|
|
|
glideraService.getTransactions(accessToken, function(err, data) {
|
|
|
|
|
|
$scope.txs = data;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (permissions.view_email_address && opts.fullUpdate) {
|
|
|
|
|
|
glideraService.getEmail(accessToken, function(err, data) {
|
|
|
|
|
|
$scope.email = data.email;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
if (permissions.personal_info && opts.fullUpdate) {
|
|
|
|
|
|
glideraService.getPersonalInfo(accessToken, function(err, data) {
|
|
|
|
|
|
$scope.personalInfo = data;
|
|
|
|
|
|
});
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
2016-06-11 23:55:28 -03:00
|
|
|
|
|
2015-08-31 18:12:04 -03:00
|
|
|
|
this.getSellPrice = function(token, price) {
|
|
|
|
|
|
var self = this;
|
2015-09-03 02:50:59 -03:00
|
|
|
|
if (!price || (price && !price.qty && !price.fiat)) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.sellPrice = null;
|
2015-09-03 02:50:59 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.gettingSellPrice = true;
|
2015-09-07 17:43:55 -03:00
|
|
|
|
glideraService.sellPrice(token, price, function(err, sellPrice) {
|
2015-09-09 15:24:47 -03:00
|
|
|
|
self.gettingSellPrice = false;
|
2015-09-07 17:43:55 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
2015-09-07 17:43:55 -03:00
|
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.sellPrice = sellPrice;
|
2016-01-21 14:52:58 -03:00
|
|
|
|
});
|
2015-08-31 18:12:04 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2015-09-03 02:50:59 -03:00
|
|
|
|
this.get2faCode = function(token) {
|
2015-09-01 18:53:47 -03:00
|
|
|
|
var self = this;
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('Sending 2FA code...', true);
|
2015-09-01 18:53:47 -03:00
|
|
|
|
$timeout(function() {
|
2015-09-07 17:43:55 -03:00
|
|
|
|
glideraService.get2faCode(token, function(err, sent) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('Sending 2FA code...', false);
|
2015-09-07 17:43:55 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not send confirmation code to your phone'));
|
2016-01-21 14:52:58 -03:00
|
|
|
|
} else {
|
2015-09-07 17:43:55 -03:00
|
|
|
|
self.show2faCodeInput = sent;
|
|
|
|
|
|
}
|
2015-09-03 02:50:59 -03:00
|
|
|
|
});
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
};
|
2015-09-01 18:53:47 -03:00
|
|
|
|
|
2015-12-23 18:15:59 -03:00
|
|
|
|
this.createTx = function(token, permissions, twoFaCode) {
|
2015-09-03 02:50:59 -03:00
|
|
|
|
var self = this;
|
2016-05-09 15:56:44 -03:00
|
|
|
|
var outputs = [];
|
2016-08-24 11:33:43 -03:00
|
|
|
|
var config = configService.getSync();
|
2016-05-09 15:56:44 -03:00
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
|
var walletSettings = configWallet.settings;
|
2016-06-10 15:16:02 -03:00
|
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
|
if (!wallet) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('No wallet selected'));
|
2016-06-13 08:37:14 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('creatingTx', true);
|
2016-08-24 11:33:43 -03:00
|
|
|
|
walletService.getAddress(wallet, null, function(err, refundAddress) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
if (!refundAddress) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err, 'Could not create address'));
|
2016-01-21 14:52:58 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-31 16:43:52 -03:00
|
|
|
|
glideraService.getSellAddress(token, function(err, sellAddress) {
|
|
|
|
|
|
if (!sellAddress || err) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get the destination bitcoin address'));
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var amount = parseInt((self.sellPrice.qty * 100000000).toFixed(0));
|
|
|
|
|
|
var comment = 'Glidera transaction';
|
|
|
|
|
|
|
|
|
|
|
|
outputs.push({
|
|
|
|
|
|
'toAddress': sellAddress,
|
|
|
|
|
|
'amount': amount,
|
|
|
|
|
|
'message': comment
|
|
|
|
|
|
});
|
2016-06-10 15:16:02 -03:00
|
|
|
|
|
2016-05-09 15:56:44 -03:00
|
|
|
|
var txp = {
|
|
|
|
|
|
toAddress: sellAddress,
|
|
|
|
|
|
amount: amount,
|
|
|
|
|
|
outputs: outputs,
|
|
|
|
|
|
message: comment,
|
|
|
|
|
|
payProUrl: null,
|
|
|
|
|
|
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
|
|
|
|
|
feeLevel: walletSettings.feeLevel || 'normal',
|
|
|
|
|
|
customData: {
|
|
|
|
|
|
'glideraToken': token
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
|
walletService.createTx(wallet, txp, function(err, createdTxp) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-05-09 15:56:44 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
|
2015-09-07 17:43:55 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-31 16:43:52 -03:00
|
|
|
|
walletService.prepare(wallet, function(err, password) {
|
2016-08-24 11:33:43 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
ongoingProcess.clear();
|
|
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
|
2016-08-24 11:33:43 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-29 15:49:05 -03:00
|
|
|
|
ongoingProcess.set('signingTx', true);
|
|
|
|
|
|
walletService.publishTx(wallet, createdTxp, function(err, publishedTxp) {
|
2016-08-24 11:33:43 -03:00
|
|
|
|
if (err) {
|
2016-08-29 15:49:05 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
|
|
|
|
|
|
return;
|
2016-08-24 11:33:43 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-08-31 16:43:52 -03:00
|
|
|
|
walletService.signTx(wallet, publishedTxp, password, function(err, signedTxp) {
|
2015-12-23 18:05:22 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
ongoingProcess.clear();
|
|
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
|
|
|
|
|
|
walletService.removeTx(wallet, signedTxp, function(err) {
|
|
|
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
|
});
|
2016-08-29 15:49:05 -03:00
|
|
|
|
return;
|
2015-12-23 18:05:22 -03:00
|
|
|
|
}
|
2016-08-29 15:49:05 -03:00
|
|
|
|
var rawTx = signedTxp.raw;
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
refundAddress: refundAddress,
|
|
|
|
|
|
signedTransaction: rawTx,
|
|
|
|
|
|
priceUuid: self.sellPrice.priceUuid,
|
|
|
|
|
|
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
|
|
|
|
|
|
ip: null
|
|
|
|
|
|
};
|
2016-08-31 16:43:52 -03:00
|
|
|
|
ongoingProcess.set('Selling Bitcoin', true);
|
2016-08-29 15:49:05 -03:00
|
|
|
|
glideraService.sell(token, twoFaCode, data, function(err, data) {
|
2016-08-24 11:33:43 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-01-21 14:52:58 -03:00
|
|
|
|
if (err) {
|
2016-08-31 16:43:52 -03:00
|
|
|
|
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-08-29 15:49:05 -03:00
|
|
|
|
self.success = data;
|
2016-08-31 16:43:52 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$digest();
|
|
|
|
|
|
});
|
2016-01-21 14:52:58 -03:00
|
|
|
|
});
|
2015-12-23 18:05:22 -03:00
|
|
|
|
});
|
2016-08-24 11:33:43 -03:00
|
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2016-05-09 15:56:44 -03:00
|
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
2016-09-22 10:34:00 -03:00
|
|
|
|
|
|
|
|
|
|
$scope.$on("$ionicView.enter", function(event, data){
|
2016-09-23 13:34:47 -03:00
|
|
|
|
$scope.token = null;
|
2016-09-22 10:34:00 -03:00
|
|
|
|
$scope.permissions = null;
|
|
|
|
|
|
$scope.email = null;
|
|
|
|
|
|
$scope.personalInfo = null;
|
|
|
|
|
|
$scope.txs = null;
|
|
|
|
|
|
$scope.status = null;
|
|
|
|
|
|
$scope.limits = null;
|
|
|
|
|
|
|
|
|
|
|
|
ongoingProcess.set('connectingGlidera', true);
|
|
|
|
|
|
glideraService.init($scope.token, function(err, glidera) {
|
|
|
|
|
|
ongoingProcess.set('connectingGlidera');
|
|
|
|
|
|
if (err || !glidera) {
|
|
|
|
|
|
if (err) popupService.showAlert(gettextCatalog.getString('Error'), err);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
$scope.token = glidera.token;
|
|
|
|
|
|
$scope.permissions = glidera.permissions;
|
|
|
|
|
|
$scope.update({fullUpdate: true});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
$scope.wallets = profileService.getWallets({
|
|
|
|
|
|
network: $scope.network,
|
|
|
|
|
|
n: 1,
|
|
|
|
|
|
onlyComplete: true
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
2015-08-31 18:12:04 -03:00
|
|
|
|
});
|