Wallet/src/js/controllers/sellGlidera.js

244 lines
8.3 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
2016-01-21 14:52:58 -03:00
angular.module('copayApp.controllers').controller('sellGlideraController',
function($rootScope, $scope, $timeout, $log, $modal, configService, profileService, addressService, feeService, glideraService, bwsError, lodash, isChromeApp, animationService, walletService, fingerprintService) {
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-11 13:11:41 -03:00
var fc;
window.ignoreMobilePause = true;
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 && w.m == 1;
});
};
var handleEncryptedWallet = function(client, cb) {
if (!walletService.isEncrypted(client)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
return cb(walletService.unlock(client, password));
});
};
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;
fc = profileService.getClient(w.id);
$scope.$apply();
}, 100);
}
});
2016-01-21 14:52:58 -03:00
} 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 = 'SELL';
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()) {
2016-01-21 14:52:58 -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({
2016-01-21 14:52:58 -03:00
'walletId': walletId,
2015-09-11 13:11:41 -03:00
'walletName': walletName,
});
};
};
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',
2016-01-21 14:52:58 -03:00
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
2015-09-11 13:11:41 -03:00
});
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;
self.error = null;
2015-09-03 02:50:59 -03:00
if (!price || (price && !price.qty && !price.fiat)) {
self.sellPrice = null;
2015-09-03 02:50:59 -03:00
return;
}
self.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.';
return;
2015-09-07 17:43:55 -03:00
}
self.sellPrice = sellPrice;
2016-01-21 14:52:58 -03:00
});
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;
2016-01-21 15:42:59 -03:00
self.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';
2016-01-21 14:52:58 -03:00
} else {
2015-09-07 17:43:55 -03:00
self.show2faCodeInput = sent;
}
2015-09-03 02:50:59 -03:00
});
}, 100);
};
2015-09-01 18:53:47 -03:00
2015-12-23 18:15:59 -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;
var outputs = [];
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
if (!refundAddress) {
self.loading = null;
self.error = bwsError.msg(err, 'Could not create address');
2016-01-21 14:52:58 -03:00
return;
}
glideraService.getSellAddress(token, function(error, sellAddress) {
if (!sellAddress) {
self.loading = null;
self.error = 'Could not get the destination bitcoin address';
return;
}
var amount = parseInt((self.sellPrice.qty * 100000000).toFixed(0));
var comment = 'Glidera transaction';
outputs.push({
'toAddress': sellAddress,
'amount': amount,
'message': comment
});
var txp = {
toAddress: sellAddress,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null,
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
feeLevel: walletSettings.feeLevel || 'normal',
customData: {
'glideraToken': token
}
};
self.loading = 'Creating transaction...';
walletService.createTx(fc, txp, function(err, createdTxp) {
self.loading = null;
if (err) {
self.error = err.message || bwsError.msg(err);
2015-09-07 17:43:55 -03:00
return;
}
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) {
fingerprintService.check(fc, function(err) {
2015-12-23 18:05:22 -03:00
if (err) {
self.error = err.message || bwsError.msg(err);
2016-01-21 14:52:58 -03:00
return;
2015-12-23 18:05:22 -03:00
}
2016-01-21 14:52:58 -03:00
handleEncryptedWallet(fc, function(err) {
2016-01-21 14:52:58 -03:00
if (err) {
self.error = err.message || bwsError.msg(err);
return;
}
self.loading = 'Signing transaction...';
walletService.publishTx(fc, createdTxp, function(err, publishedTxp) {
if (err) {
2016-01-21 14:52:58 -03:00
self.loading = null;
self.error = err.message || bwsError.msg(err);
}
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
walletService.lock(fc);
walletService.removeTx(fc, signedTxp, function(err) {
if (err) $log.debug(err);
});
2016-01-21 14:52:58 -03:00
if (err) {
self.loading = null;
self.error = err.message || bwsError.msg(err);
return;
}
var rawTx = signedTxp.raw;
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: self.sellPrice.priceUuid,
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
ip: null
};
self.loading = 'Selling bitcoin...';
glideraService.sell(token, twoFaCode, data, function(err, data) {
self.loading = null;
if (err) {
self.error = err.message || bwsError.msg(err);
2016-01-21 14:52:58 -03:00
$timeout(function() {
$scope.$emit('Local/GlideraError');
}, 100);
return;
}
2016-01-21 14:52:58 -03:00
self.success = data;
$scope.$emit('Local/GlideraTx');
});
2016-01-21 14:52:58 -03:00
});
});
2016-01-21 14:52:58 -03:00
});
2015-12-23 18:05:22 -03:00
});
}
2015-09-01 18:53:47 -03:00
});
});
});
2015-09-01 18:53:47 -03:00
});
};
2015-08-31 18:12:04 -03:00
});