2015-08-31 18:12:04 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-10 15:16:02 -03:00
|
|
|
angular.module('copayApp.controllers').controller('buyGlideraController',
|
2016-07-11 11:46:48 -03:00
|
|
|
function($scope, $timeout, $ionicModal, profileService, addressService, glideraService, bwcError, lodash, ongoingProcess) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
2015-09-11 13:11:41 -03:00
|
|
|
var self = this;
|
2015-09-02 19:16:59 -03:00
|
|
|
this.show2faCodeInput = null;
|
|
|
|
|
this.error = null;
|
|
|
|
|
this.success = null;
|
2015-09-11 13:11:41 -03:00
|
|
|
|
2015-10-05 16:48:04 -03:00
|
|
|
this.init = function(testnet) {
|
2016-06-16 18:29:56 -03:00
|
|
|
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet');
|
2016-06-11 23:55:28 -03:00
|
|
|
|
2016-06-12 16:42:32 -03:00
|
|
|
var client = profileService.focusedClient;
|
2016-06-16 14:57:30 -03:00
|
|
|
if (client) {
|
2016-06-11 23:55:28 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
self.selectedWalletId = client.credentials.walletId;
|
|
|
|
|
self.selectedWalletName = client.credentials.walletName;
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
2015-10-05 16:48:04 -03:00
|
|
|
};
|
|
|
|
|
|
2015-09-11 13:11:41 -03:00
|
|
|
$scope.openWalletsModal = function(wallets) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
|
2016-06-10 15:16:02 -03:00
|
|
|
$scope.type = 'BUY';
|
|
|
|
|
$scope.wallets = wallets;
|
|
|
|
|
$scope.noColor = true;
|
|
|
|
|
$scope.self = self;
|
2015-09-11 13:11:41 -03:00
|
|
|
|
2016-06-10 15:16:02 -03:00
|
|
|
$ionicModal.fromTemplateUrl('views/modals/wallets.html', {
|
|
|
|
|
scope: $scope,
|
|
|
|
|
animation: 'slide-in-up'
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
$scope.walletsModal = modal;
|
|
|
|
|
$scope.walletsModal.show();
|
2015-09-11 13:11:41 -03:00
|
|
|
});
|
2016-06-16 18:29:56 -03:00
|
|
|
|
2016-06-16 21:42:44 -03:00
|
|
|
$scope.$on('walletSelected', function(ev, walletId) {
|
2016-06-16 18:29:56 -03:00
|
|
|
$timeout(function() {
|
2016-06-16 21:42:44 -03:00
|
|
|
var client = profileService.getClient(walletId);
|
|
|
|
|
self.selectedWalletId = walletId;
|
|
|
|
|
self.selectedWalletName = client.credentials.walletName;
|
2016-06-16 18:29:56 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
}, 100);
|
|
|
|
|
$scope.walletsModal.hide();
|
|
|
|
|
});
|
2015-09-11 13:11:41 -03:00
|
|
|
};
|
|
|
|
|
|
2015-08-31 18:12:04 -03:00
|
|
|
this.getBuyPrice = function(token, price) {
|
|
|
|
|
var self = this;
|
2015-09-10 16:19:07 -03:00
|
|
|
this.error = null;
|
2015-09-02 19:16:59 -03:00
|
|
|
if (!price || (price && !price.qty && !price.fiat)) {
|
|
|
|
|
this.buyPrice = null;
|
|
|
|
|
return;
|
|
|
|
|
}
|
2015-09-09 15:24:47 -03:00
|
|
|
this.gettingBuyPrice = true;
|
2015-09-07 13:35:59 -03:00
|
|
|
glideraService.buyPrice(token, price, function(err, buyPrice) {
|
2015-09-09 15:24:47 -03:00
|
|
|
self.gettingBuyPrice = false;
|
2015-09-07 13:35:59 -03:00
|
|
|
if (err) {
|
2015-09-12 23:49:14 -03:00
|
|
|
self.error = 'Could not get exchange information. Please, try again.';
|
2016-05-09 15:56:44 -03:00
|
|
|
return;
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
self.buyPrice = buyPrice;
|
2016-06-10 15:16:02 -03:00
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
|
|
|
|
|
2015-09-02 19:16:59 -03:00
|
|
|
this.get2faCode = function(token) {
|
2015-09-01 18:53:47 -03:00
|
|
|
var self = this;
|
2016-05-09 15:56:44 -03:00
|
|
|
self.error = null;
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Sending 2FA code...', true);
|
2015-09-02 19:16:59 -03:00
|
|
|
$timeout(function() {
|
2015-09-07 13:35:59 -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 13:35:59 -03:00
|
|
|
if (err) {
|
2015-09-12 23:49:14 -03:00
|
|
|
self.error = 'Could not send confirmation code to your phone';
|
2016-05-09 15:56:44 -03:00
|
|
|
return;
|
2015-09-07 13:35:59 -03:00
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
self.show2faCodeInput = sent;
|
2015-09-02 19:16:59 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
|
|
|
|
|
2015-09-07 13:35:59 -03:00
|
|
|
this.sendRequest = function(token, permissions, twoFaCode) {
|
2015-09-01 18:53:47 -03:00
|
|
|
var self = this;
|
2015-09-07 13:35:59 -03:00
|
|
|
self.error = null;
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Buying Bitcoin...', true);
|
2015-09-11 13:11:41 -03:00
|
|
|
$timeout(function() {
|
2015-10-05 16:48:04 -03:00
|
|
|
addressService.getAddress(self.selectedWalletId, false, function(err, walletAddr) {
|
2015-09-11 13:11:41 -03:00
|
|
|
if (err) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Buying Bitcoin...', false);
|
2016-07-11 11:46:48 -03:00
|
|
|
self.error = bwcError.cb(err, 'Could not create address');
|
2015-10-05 16:48:04 -03:00
|
|
|
return;
|
2015-09-11 13:11:41 -03:00
|
|
|
}
|
2015-10-05 16:48:04 -03:00
|
|
|
var data = {
|
|
|
|
|
destinationAddress: walletAddr,
|
|
|
|
|
qty: self.buyPrice.qty,
|
|
|
|
|
priceUuid: self.buyPrice.priceUuid,
|
|
|
|
|
useCurrentPrice: false,
|
2016-06-10 15:16:02 -03:00
|
|
|
ip: null
|
2015-10-05 16:48:04 -03:00
|
|
|
};
|
|
|
|
|
glideraService.buy(token, twoFaCode, data, function(err, data) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Buying Bitcoin...', false);
|
2015-10-05 16:48:04 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
2016-05-09 15:56:44 -03:00
|
|
|
return;
|
2015-10-05 16:48:04 -03:00
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
self.success = data;
|
|
|
|
|
$scope.$emit('Local/GlideraTx');
|
2015-10-05 16:48:04 -03:00
|
|
|
});
|
2015-09-11 13:11:41 -03:00
|
|
|
});
|
|
|
|
|
}, 100);
|
2015-09-01 18:53:47 -03:00
|
|
|
};
|
2015-08-31 18:12:04 -03:00
|
|
|
|
|
|
|
|
});
|