Wallet/src/js/controllers/sellGlidera.js

211 lines
6.9 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('sellGlideraController',
2015-10-01 02:13:33 -03:00
function($scope, $timeout, $log, $modal, configService, profileService, addressService, feeService, glideraService, bwsError, lodash, isChromeApp, animationService) {
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
var config = configService.getSync();
this.data = {};
this.show2faCodeInput = null;
2015-09-03 02:50:59 -03:00
this.success = null;
2015-09-07 17:43:55 -03:00
this.error = null;
this.loading = null;
2015-09-01 18:53:47 -03:00
this.currentSpendUnconfirmed = config.wallet.spendUnconfirmed;
this.currentFeeLevel = config.wallet.settings.feeLevel || 'normal';
2015-09-11 13:11:41 -03:00
var fc;
this.otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
self.selectedWalletId = null;
self.selectedWalletName = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.wallets = wallets;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
2015-09-12 23:49:14 -03:00
self.error = bwsError.msg({'code': 'WALLET_NOT_COMPLETE'}, 'Could not choose the wallet');
2015-09-11 13:11:41 -03:00
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
2015-10-01 02:13:33 -03:00
windowClass: animationService.modalAnimated.slideUp,
2015-09-11 13:11:41 -03:00
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
2015-10-01 02:13:33 -03:00
m.addClass(animationService.modalAnimated.slideOutDown);
2015-09-11 13:11:41 -03:00
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
2015-09-01 18:53:47 -03:00
2015-08-31 18:12:04 -03:00
this.getSellPrice = function(token, price) {
var self = this;
2015-09-10 16:19:07 -03:00
this.error = null;
2015-09-03 02:50:59 -03:00
if (!price || (price && !price.qty && !price.fiat)) {
this.sellPrice = null;
return;
}
this.gettingSellPrice = true;
2015-09-07 17:43:55 -03:00
glideraService.sellPrice(token, price, function(err, sellPrice) {
self.gettingSellPrice = false;
2015-09-07 17:43:55 -03:00
if (err) {
2015-09-12 23:49:14 -03:00
self.error = 'Could not get exchange information. Please, try again.';
2015-09-07 17:43:55 -03:00
}
else {
self.error = null;
self.sellPrice = sellPrice;
}
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;
2015-09-12 23:49:14 -03:00
this.loading = 'Sending 2FA code...';
2015-09-01 18:53:47 -03:00
$timeout(function() {
2015-09-07 17:43:55 -03:00
glideraService.get2faCode(token, function(err, sent) {
self.loading = null;
if (err) {
2015-09-12 23:49:14 -03:00
self.error = 'Could not send confirmation code to your phone';
2015-09-07 17:43:55 -03:00
}
else {
self.show2faCodeInput = sent;
}
2015-09-03 02:50:59 -03:00
});
}, 100);
};
2015-09-01 18:53:47 -03:00
2015-09-07 17:43:55 -03:00
this.createTx = function(token, permissions, twoFaCode) {
2015-09-03 02:50:59 -03:00
var self = this;
2015-09-07 17:43:55 -03:00
self.error = null;
2015-09-01 18:53:47 -03:00
2015-09-12 23:49:14 -03:00
this.loading = 'Selling Bitcoin...';
2015-09-03 02:50:59 -03:00
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
2015-09-07 17:43:55 -03:00
if (!refundAddress) {
self.loading = null;
2015-09-12 23:49:14 -03:00
self.error = bwsError.msg(err, 'Could not create address');
2015-09-07 17:43:55 -03:00
return;
}
2015-09-03 02:50:59 -03:00
glideraService.getSellAddress(token, function(error, sellAddress) {
2015-09-07 17:43:55 -03:00
if (!sellAddress) {
self.loading = null;
2015-09-12 23:49:14 -03:00
self.error = 'Could not get the destination bitcoin address';
2015-09-07 17:43:55 -03:00
return;
}
2015-09-03 02:50:59 -03:00
var amount = parseInt((self.sellPrice.qty * 100000000).toFixed(0));
feeService.getCurrentFeeValue(self.currentFeeLevel, function(err, feePerKb) {
if (err) $log.debug(err);
fc.sendTxProposal({
toAddress: sellAddress,
amount: amount,
2015-09-08 15:42:55 -03:00
message: 'Glidera transaction',
2015-09-07 17:43:55 -03:00
customData: {'glideraToken': token},
2015-09-03 02:50:59 -03:00
payProUrl: null,
feePerKb: feePerKb,
excludeUnconfirmedUtxos: self.currentSpendUnconfirmed ? false : true
}, function(err, txp) {
if (err) {
profileService.lockFC();
$log.error(err);
2015-09-07 17:43:55 -03:00
$timeout(function() {
self.loading = null;
2015-09-12 23:49:14 -03:00
self.error = bwsError.msg(err, 'Error');
2015-09-07 17:43:55 -03:00
}, 1);
2015-09-03 02:50:59 -03:00
return;
}
if (!fc.canSign()) {
2015-09-07 17:43:55 -03:00
self.loading = null;
2015-09-03 02:50:59 -03:00
$log.info('No signing proposal: No private key');
return;
}
2015-09-07 17:43:55 -03:00
_signTx(txp, function(err, txp, rawTx) {
2015-09-03 02:50:59 -03:00
profileService.lockFC();
if (err) {
2015-09-07 17:43:55 -03:00
self.loading = null;
self.error = err;
$scope.$apply();
2015-09-03 02:50:59 -03:00
}
else {
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: self.sellPrice.priceUuid,
2015-09-07 17:43:55 -03:00
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
2015-09-03 02:50:59 -03:00
ip: null
};
2015-09-07 17:43:55 -03:00
glideraService.sell(token, twoFaCode, data, function(err, data) {
self.loading = null;
if (err) {
2015-09-08 10:17:59 -03:00
self.error = err;
2015-09-07 17:43:55 -03:00
fc.removeTxProposal(txp, function(err, txpb) {
$timeout(function() {
$scope.$emit('Local/GlideraError');
}, 100);
});
}
else {
self.success = data;
$scope.$emit('Local/GlideraTx');
2015-09-07 17:43:55 -03:00
}
2015-09-03 02:50:59 -03:00
});
}
});
});
2015-09-01 18:53:47 -03:00
});
});
});
}, 100);
};
2015-09-03 02:50:59 -03:00
var _signTx = function(txp, cb) {
2015-09-01 18:53:47 -03:00
var self = this;
fc.signTxProposal(txp, function(err, signedTx) {
profileService.lockFC();
if (err) {
2015-09-12 23:49:14 -03:00
err = bwsError.msg(err, 'Could not accept payment');
2015-09-01 18:53:47 -03:00
return cb(err);
}
2015-09-07 17:43:55 -03:00
else {
if (signedTx.status == 'accepted') {
return cb(null, txp, signedTx.raw);
2015-09-01 18:53:47 -03:00
2015-09-07 17:43:55 -03:00
} else {
2015-09-12 23:49:14 -03:00
return cb('The transaction could not be signed');
2015-09-07 17:43:55 -03:00
}
2015-09-01 18:53:47 -03:00
}
});
};
2015-08-31 18:12:04 -03:00
});