Wallet/src/js/controllers/buyGlidera.js

116 lines
3.5 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('buyGlideraController',
2016-06-13 15:25:40 -03:00
function($scope, $timeout, $modal, $ionicModal, profileService, addressService, glideraService, bwsError, lodash, ongoingProcess) {
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) {
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet');
var client = profileService.focusedClient;
if (client) {
$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;
$scope.type = 'BUY';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.self = self;
2015-09-11 13:11:41 -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
});
$scope.$on('walletSelected', function(ev, walletId) {
$timeout(function() {
var client = profileService.getClient(walletId);
self.selectedWalletId = walletId;
self.selectedWalletName = client.credentials.walletName;
$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;
}
this.gettingBuyPrice = true;
2015-09-07 13:35:59 -03:00
glideraService.buyPrice(token, price, function(err, buyPrice) {
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.';
return;
2015-09-07 13:35:59 -03:00
}
self.buyPrice = buyPrice;
});
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;
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';
return;
2015-09-07 13:35:59 -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);
2015-10-05 16:48:04 -03:00
self.error = bwsError.cb(err, 'Could not create address');
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,
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;
return;
2015-10-05 16:48:04 -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
});