Wallet/src/js/controllers/sellGlidera.js

266 lines
8.5 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',
2016-08-24 11:33:43 -03:00
function($rootScope, $scope, $timeout, $ionicModal, $log, profileService, glideraService, bwcError, lodash, walletService, fingerprintService, configService, ongoingProcess) {
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
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;
2016-08-24 11:33:43 -03:00
var wallet;
2015-09-11 13:11:41 -03:00
2016-08-24 11:33:43 -03:00
var handleEncryptedWallet = function(wallet, cb) {
if (!walletService.isEncrypted(wallet)) return cb();
$rootScope.$emit('Local/NeedsPassword', false, function(err, password) {
if (err) return cb(err);
2016-08-24 11:33:43 -03:00
return cb(walletService.unlock(wallet, password));
});
};
2016-08-24 11:33:43 -03:00
$scope.init = function(accessToken) {
$scope.network = glideraService.getEnvironment();
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) {
$timeout(function() {
2016-08-24 11:33:43 -03:00
self.selectedWalletId = wallet.credentials.walletId;
self.selectedWalletName = wallet.credentials.walletName;
$scope.$apply();
}, 100);
}
};
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;
2016-08-24 11:33:43 -03:00
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;
$scope.type = 'SELL';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.self = self;
$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() {
2016-08-24 11:33:43 -03:00
wallet = profileService.getClient(walletId);
self.selectedWalletId = walletId;
2016-08-24 11:33:43 -03:00
self.selectedWalletName = wallet.credentials.walletName;
$scope.$apply();
}, 100);
$scope.walletsModal.hide();
});
2015-09-11 13:11:41 -03:00
};
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-06-13 15:25:40 -03:00
ongoingProcess.set('Sending 2FA code...', true);
2015-09-01 18:53:47 -03:00
$timeout(function() {
2015-09-07 17:43:55 -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 17:43:55 -03:00
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 = [];
2016-08-24 11:33:43 -03:00
var config = configService.getSync();
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
2016-08-24 11:33:43 -03:00
if (!wallet) {
2016-06-13 08:37:14 -03:00
self.error = 'No wallet selected';
return;
}
2016-06-13 15:25:40 -03:00
ongoingProcess.set('creatingTx', true);
2016-08-24 11:33:43 -03:00
walletService.getAddress(wallet, null, function(err, refundAddress) {
if (!refundAddress) {
2016-06-13 15:25:40 -03:00
ongoingProcess.clear();
2016-07-11 11:46:48 -03:00
self.error = bwcError.msg(err, 'Could not create address');
2016-01-21 14:52:58 -03:00
return;
}
glideraService.getSellAddress(token, function(error, sellAddress) {
if (!sellAddress) {
2016-06-13 15:25:40 -03:00
ongoingProcess.clear();
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
}
};
2016-08-24 11:33:43 -03:00
walletService.createTx(wallet, txp, function(err, createdTxp) {
2016-06-13 15:25:40 -03:00
ongoingProcess.clear();
if (err) {
2016-07-11 11:46:48 -03:00
self.error = err.message ||  bwcError.msg(err);
2015-09-07 17:43:55 -03:00
return;
}
2016-08-24 11:33:43 -03:00
fingerprintService.check(wallet, function(err) {
if (err) {
self.error = err.message ||  bwcError.msg(err);
return;
}
handleEncryptedWallet(wallet, function(err) {
if (err) {
self.error = err.message ||  bwcError.msg(err);
return;
}
ongoingProcess.set('signingTx', true);
walletService.publishTx(wallet, createdTxp, function(err, publishedTxp) {
2015-12-23 18:05:22 -03:00
if (err) {
2016-08-24 11:33:43 -03:00
ongoingProcess.clear();
2016-07-11 11:46:48 -03:00
self.error = err.message ||  bwcError.msg(err);
2015-12-23 18:05:22 -03:00
}
2016-01-21 14:52:58 -03:00
2016-08-24 11:33:43 -03:00
walletService.signTx(wallet, publishedTxp, function(err, signedTxp) {
walletService.lock(wallet);
walletService.removeTx(wallet, signedTxp, function(err) {
if (err) $log.debug(err);
});
ongoingProcess.clear();
2016-01-21 14:52:58 -03:00
if (err) {
2016-07-11 11:46:48 -03:00
self.error = err.message ||  bwcError.msg(err);
return;
}
2016-08-24 11:33:43 -03:00
var rawTx = signedTxp.raw;
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: self.sellPrice.priceUuid,
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
ip: null
};
ongoingProcess.set('Seling Bitcoin', true);
glideraService.sell(token, twoFaCode, data, function(err, data) {
ongoingProcess.clear();
if (err) {
2016-07-11 11:46:48 -03:00
self.error = err.message ||  bwcError.msg(err);
2016-08-24 11:33:43 -03:00
return;
}
2016-08-24 11:33:43 -03:00
self.success = data;
$scope.update();
});
2016-01-21 14:52:58 -03:00
});
2015-12-23 18:05:22 -03:00
});
2016-08-24 11:33:43 -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
});