Wallet/src/js/controllers/buyGlidera.js

152 lines
4.6 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('buyGlideraController',
2015-10-01 02:13:33 -03:00
function($scope, $timeout, $modal, profileService, addressService, 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-02 19:16:59 -03:00
this.show2faCodeInput = null;
this.error = null;
this.success = null;
2015-09-08 12:04:27 -03:00
this.loading = null;
2015-09-01 18:53:47 -03:00
window.ignoreMobilePause = true;
2015-10-05 16:48:04 -03:00
var otherWallets = function(testnet) {
2015-09-11 13:11:41 -03:00
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network;
});
};
2015-10-05 16:48:04 -03:00
this.init = function(testnet) {
self.otherWallets = otherWallets(testnet);
// Choose focused wallet
try {
var currentWalletId = profileService.focusedClient.credentials.walletId;
lodash.find(self.otherWallets, function(w) {
if (w.id == currentWalletId) {
$timeout(function() {
self.selectedWalletId = w.id;
self.selectedWalletName = w.name;
$scope.$apply();
}, 100);
}
});
} catch(e) {
$log.debug(e);
};
};
2015-09-11 13:11:41 -03:00
$scope.openWalletsModal = function(wallets) {
self.error = null;
self.selectedWalletId = null;
self.selectedWalletName = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'BUY';
2015-09-11 13:11:41 -03:00
$scope.wallets = wallets;
2015-12-01 17:16:39 -03:00
$scope.noColor = true;
2015-09-11 13:11:41 -03:00
$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;
}
2015-10-05 16:48:04 -03:00
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
2015-09-11 13:11:41 -03:00
});
};
};
var modalInstance = $modal.open({
Feat/coinbase integration (#4012) * Oauth2 and first view * Connect with Coinbase using mobile * Buy and Sell through Coinbase * Fix buy * Receive and send bitcoin to Coinbase account * Receive bitcoin from Coinbase to Copay * Complete user and account information. Connection errors * Improves error handler * Removes console.log * Coinbase background color. Send to Coinbase form validation * Fix send from different wallet * Send and receive using Coinbase * Pagination activity * Fix Buy and Sell * One option in the sidebar to Buy and Sell * Native balance on Coinbase homepage * Rename receive and send * Auto-close window after authenticate * Reorder * Get payment methods * Fix when token expired * Fix token expired * Integration: sell and send to Coinbase * Store pending transaction before sell * Sell flow completed * Removing files * Fix sell * Fix sell * Fix sell * Sell completed * Buy bitcoin through coinbase * Buy auto * Currency set to USD * Select payment methods. Limits * Removes payment methods from preferences * Fix signs. Tx ordered by updated. Minor fixes * Removes console.log * Improving ux-language things * Fix selectedpaymentmethod if not verified * Set error if tx not found * Price sensitivity. Minor fixes * Adds coinbase api key to gitignore * Coinbase production ready * Fix sell in usd * Bug fixes * New Sensitivity step * Refresh token with a simple click * Refresh token * Refactor * Fix auto reconnect if token expired Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com> * Fix calls if token expired
2016-04-13 14:08:03 -03:00
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;
$scope.$apply();
}, 100);
});
};
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.';
2015-09-07 13:35:59 -03:00
}
else {
self.buyPrice = buyPrice;
}
2015-08-31 18:12:04 -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;
2015-09-12 23:49:14 -03:00
this.loading = 'Sending 2FA code...';
2015-09-02 19:16:59 -03:00
$timeout(function() {
2015-09-07 13:35:59 -03:00
glideraService.get2faCode(token, function(err, sent) {
2015-09-07 17:43:55 -03:00
self.loading = null;
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';
2015-09-07 13:35:59 -03:00
}
else {
2015-09-07 17:43:55 -03:00
self.error = null;
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;
2015-09-12 23:49:14 -03:00
self.loading = 'Buying bitcoin...';
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) {
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
};
glideraService.buy(token, twoFaCode, data, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
}
else {
self.success = data;
$scope.$emit('Local/GlideraTx');
}
});
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
});