Wallet/src/js/controllers/sellGlidera.js

215 lines
7.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',
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;
var client;
2015-09-11 13:11:41 -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));
});
};
this.init = function(testnet) {
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1);
client = profileService.focusedClient;
if (client && client.credentials.m == 1) {
$timeout(function() {
self.selectedWalletId = client.credentials.walletId;
self.selectedWalletName = client.credentials.walletName;
$scope.$apply();
}, 100);
}
};
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() {
client = profileService.getClient(walletId);
self.selectedWalletId = walletId;
self.selectedWalletName = client.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 = [];
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
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);
addressService.getAddress(client.credentials.walletId, 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
}
};
walletService.createTx(client, 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;
}
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
if (accept) {
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
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);
return;
}
2016-06-13 15:25:40 -03:00
ongoingProcess.set('signingTx', true);
walletService.publishTx(client, createdTxp, function(err, publishedTxp) {
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);
}
walletService.signTx(client, publishedTxp, function(err, signedTxp) {
walletService.lock(client);
walletService.removeTx(client, signedTxp, function(err) {
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);
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);
glideraService.sell(token, twoFaCode, data, function(err, data) {
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);
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
});
} else {
go.path('glidera');
}
2015-09-01 18:53:47 -03:00
});
});
});
2015-09-01 18:53:47 -03:00
});
};
2015-08-31 18:12:04 -03:00
});