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-07-11 11:46:48 -03:00
|
|
|
|
function($rootScope, $scope, $timeout, $ionicModal, $log, configService, profileService, addressService, feeService, glideraService, bwcError, lodash, walletService, fingerprintService, ongoingProcess, go) {
|
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;
|
2016-06-11 23:55:28 -03:00
|
|
|
|
var client;
|
2015-09-11 13:11:41 -03:00
|
|
|
|
|
2016-05-09 15:56:44 -03:00
|
|
|
|
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));
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-10-05 15:04:07 -03:00
|
|
|
|
this.init = function(testnet) {
|
2016-06-16 18:29:56 -03:00
|
|
|
|
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1);
|
2016-06-11 23:55:28 -03:00
|
|
|
|
|
|
|
|
|
|
client = profileService.focusedClient;
|
2016-06-16 18:29:56 -03:00
|
|
|
|
if (client && client.credentials.m == 1) {
|
2016-06-11 23:55:28 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
self.selectedWalletId = client.credentials.walletId;
|
|
|
|
|
|
self.selectedWalletName = client.credentials.walletName;
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
}
|
2015-10-05 15:04:07 -03:00
|
|
|
|
};
|
|
|
|
|
|
|
2016-06-16 18:29:56 -03:00
|
|
|
|
|
2016-06-11 23:55:28 -03:00
|
|
|
|
|
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 = '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
|
|
|
|
});
|
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-06-16 21:42:44 -03:00
|
|
|
|
client = profileService.getClient(walletId);
|
|
|
|
|
|
self.selectedWalletId = walletId;
|
|
|
|
|
|
self.selectedWalletName = client.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-09-01 18:53:47 -03:00
|
|
|
|
|
2015-08-31 18:12:04 -03:00
|
|
|
|
this.getSellPrice = function(token, price) {
|
|
|
|
|
|
var self = this;
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.error = null;
|
2015-09-03 02:50:59 -03:00
|
|
|
|
if (!price || (price && !price.qty && !price.fiat)) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.sellPrice = null;
|
2015-09-03 02:50:59 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.gettingSellPrice = true;
|
2015-09-07 17:43:55 -03:00
|
|
|
|
glideraService.sellPrice(token, price, function(err, sellPrice) {
|
2015-09-09 15:24:47 -03:00
|
|
|
|
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.';
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
2015-09-07 17:43:55 -03:00
|
|
|
|
}
|
2016-05-09 15:56:44 -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;
|
2016-05-09 15:56:44 -03:00
|
|
|
|
var outputs = [];
|
|
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
|
var walletSettings = configWallet.settings;
|
2016-06-10 15:16:02 -03:00
|
|
|
|
|
2016-06-13 08:37:14 -03:00
|
|
|
|
if (!client) {
|
|
|
|
|
|
self.error = 'No wallet selected';
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('creatingTx', true);
|
2016-06-11 23:55:28 -03:00
|
|
|
|
addressService.getAddress(client.credentials.walletId, null, function(err, refundAddress) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
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;
|
|
|
|
|
|
}
|
2016-05-09 15:56:44 -03:00
|
|
|
|
glideraService.getSellAddress(token, function(error, sellAddress) {
|
|
|
|
|
|
if (!sellAddress) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-05-09 15:56:44 -03:00
|
|
|
|
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
|
|
|
|
|
|
});
|
2016-06-10 15:16:02 -03:00
|
|
|
|
|
2016-05-09 15:56:44 -03:00
|
|
|
|
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-06-11 23:55:28 -03:00
|
|
|
|
walletService.createTx(client, txp, function(err, createdTxp) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-05-09 15:56:44 -03:00
|
|
|
|
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-05-09 15:56:44 -03:00
|
|
|
|
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
|
|
|
|
|
if (accept) {
|
2016-06-11 23:55:28 -03:00
|
|
|
|
fingerprintService.check(client, function(err) {
|
2015-12-23 18:05:22 -03:00
|
|
|
|
if (err) {
|
2016-07-11 11:46:48 -03:00
|
|
|
|
self.error = err.message || bwcError.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
|
|
|
|
|
2016-06-11 23:55:28 -03:00
|
|
|
|
handleEncryptedWallet(client, function(err) {
|
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);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('signingTx', true);
|
2016-06-11 23:55:28 -03:00
|
|
|
|
walletService.publishTx(client, createdTxp, function(err, publishedTxp) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
if (err) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-07-11 11:46:48 -03:00
|
|
|
|
self.error = err.message || bwcError.msg(err);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
2016-06-11 23:55:28 -03:00
|
|
|
|
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
|
|
|
|
|
|
walletService.lock(client);
|
|
|
|
|
|
walletService.removeTx(client, signedTxp, function(err) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
|
});
|
2016-06-13 15:25:40 -03:00
|
|
|
|
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);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
var rawTx = signedTxp.raw;
|
|
|
|
|
|
var data = {
|
|
|
|
|
|
refundAddress: refundAddress,
|
|
|
|
|
|
signedTransaction: rawTx,
|
|
|
|
|
|
priceUuid: self.sellPrice.priceUuid,
|
|
|
|
|
|
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
|
|
|
|
|
|
ip: null
|
|
|
|
|
|
};
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.set('Seling Bitcoin', true);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
glideraService.sell(token, twoFaCode, data, function(err, data) {
|
2016-06-13 15:25:40 -03:00
|
|
|
|
ongoingProcess.clear();
|
2016-05-09 15:56:44 -03:00
|
|
|
|
if (err) {
|
2016-07-11 11:46:48 -03:00
|
|
|
|
self.error = err.message || bwcError.msg(err);
|
2016-01-21 14:52:58 -03:00
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
$scope.$emit('Local/GlideraError');
|
|
|
|
|
|
}, 100);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
2016-01-21 14:52:58 -03:00
|
|
|
|
self.success = data;
|
|
|
|
|
|
$scope.$emit('Local/GlideraTx');
|
2016-05-09 15:56:44 -03:00
|
|
|
|
});
|
2016-01-21 14:52:58 -03:00
|
|
|
|
});
|
2016-05-09 15:56:44 -03:00
|
|
|
|
});
|
2016-01-21 14:52:58 -03:00
|
|
|
|
});
|
2015-12-23 18:05:22 -03:00
|
|
|
|
});
|
2016-06-16 18:29:56 -03:00
|
|
|
|
} else {
|
|
|
|
|
|
go.path('glidera');
|
2016-05-09 15:56:44 -03:00
|
|
|
|
}
|
2015-09-01 18:53:47 -03:00
|
|
|
|
});
|
|
|
|
|
|
});
|
2016-05-09 15:56:44 -03:00
|
|
|
|
});
|
2015-09-01 18:53:47 -03:00
|
|
|
|
});
|
|
|
|
|
|
};
|
2015-08-31 18:12:04 -03:00
|
|
|
|
});
|