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-08-24 11:33:43 -03:00
|
|
|
function($scope, $timeout, $log, $ionicModal, profileService, walletService, glideraService, bwcError, lodash, ongoingProcess) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
var wallet;
|
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
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
$scope.init = function(accessToken) {
|
|
|
|
|
$scope.network = glideraService.getEnvironment();
|
2016-06-11 23:55:28 -03:00
|
|
|
|
2016-08-24 11:33:43 -03:00
|
|
|
$scope.token = accessToken;
|
|
|
|
|
$scope.error = null;
|
|
|
|
|
$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) {
|
|
|
|
|
$scope.error = err;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
$scope.token = glidera.token;
|
|
|
|
|
$scope.permissions = glidera.permissions;
|
|
|
|
|
$scope.update({fullUpdate: true});
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
self.allWallets = profileService.getWallets({
|
|
|
|
|
network: $scope.network,
|
|
|
|
|
n: 1,
|
|
|
|
|
onlyComplete: true
|
|
|
|
|
});
|
|
|
|
|
if (lodash.isEmpty(self.allWallets)) return;
|
|
|
|
|
|
|
|
|
|
wallet = self.allWallets[0];
|
|
|
|
|
if (wallet) {
|
2016-06-11 23:55:28 -03:00
|
|
|
$timeout(function() {
|
2016-08-24 11:33:43 -03:00
|
|
|
self.selectedWalletId = wallet.credentials.walletId;
|
|
|
|
|
self.selectedWalletName = wallet.credentials.walletName;
|
2016-06-11 23:55:28 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
}, 100);
|
|
|
|
|
}
|
2015-10-05 16:48:04 -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;
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
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-08-24 11:33:43 -03:00
|
|
|
wallet = profileService.getClient(walletId);
|
2016-06-16 21:42:44 -03:00
|
|
|
self.selectedWalletId = walletId;
|
2016-08-24 11:33:43 -03:00
|
|
|
self.selectedWalletName = wallet.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() {
|
2016-08-24 11:33:43 -03:00
|
|
|
walletService.getAddress(wallet, 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
|
|
|
|
|
|
|
|
});
|