Wallet/src/js/controllers/sellGlidera.js

158 lines
5.3 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('sellGlideraController',
2015-09-08 13:58:24 -03:00
function($scope, $timeout, $log, gettext, gettextCatalog, configService, profileService, addressService, feeService, glideraService, bwsError) {
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-01 18:53:47 -03:00
this.currentSpendUnconfirmed = config.wallet.spendUnconfirmed;
this.currentFeeLevel = config.wallet.settings.feeLevel || 'normal';
2015-08-31 18:12:04 -03:00
this.getSellPrice = function(token, price) {
var self = this;
2015-09-03 02:50:59 -03:00
if (!price || (price && !price.qty && !price.fiat)) {
2015-09-07 17:43:55 -03:00
this.error = null;
2015-09-03 02:50:59 -03:00
this.sellPrice = null;
return;
}
2015-09-07 17:43:55 -03:00
glideraService.sellPrice(token, price, function(err, sellPrice) {
if (err) {
2015-09-08 13:58:24 -03:00
self.error = gettext('Could not get exchange information. Please, try again.');
2015-09-07 17:43:55 -03:00
}
else {
self.error = null;
self.sellPrice = sellPrice;
}
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;
2015-09-07 17:43:55 -03:00
this.loading = gettext('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-08 13:58:24 -03:00
self.error = gettext('Could not send confirmation code to your phone');
2015-09-07 17:43:55 -03:00
}
else {
self.show2faCodeInput = sent;
}
2015-09-03 02:50:59 -03:00
});
}, 100);
};
2015-09-01 18:53:47 -03:00
2015-09-07 17:43:55 -03:00
this.createTx = function(token, permissions, twoFaCode) {
2015-09-03 02:50:59 -03:00
var self = this;
var fc = profileService.focusedClient;
2015-09-07 17:43:55 -03:00
self.error = null;
2015-09-01 18:53:47 -03:00
2015-09-07 17:43:55 -03:00
this.loading = gettext('Selling Bitcoin...');
2015-09-03 02:50:59 -03:00
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
2015-09-07 17:43:55 -03:00
if (!refundAddress) {
self.loading = null;
2015-09-08 13:58:24 -03:00
self.error = bwsError.msg(err);
2015-09-07 17:43:55 -03:00
return;
}
2015-09-03 02:50:59 -03:00
glideraService.getSellAddress(token, function(error, sellAddress) {
2015-09-07 17:43:55 -03:00
if (!sellAddress) {
self.loading = null;
self.error = gettext('Could not get the destination bitcoin address');
return;
}
2015-09-03 02:50:59 -03:00
var amount = parseInt((self.sellPrice.qty * 100000000).toFixed(0));
feeService.getCurrentFeeValue(self.currentFeeLevel, function(err, feePerKb) {
if (err) $log.debug(err);
fc.sendTxProposal({
toAddress: sellAddress,
amount: amount,
2015-09-08 15:42:55 -03:00
message: 'Glidera transaction',
2015-09-07 17:43:55 -03:00
customData: {'glideraToken': token},
2015-09-03 02:50:59 -03:00
payProUrl: null,
feePerKb: feePerKb,
excludeUnconfirmedUtxos: self.currentSpendUnconfirmed ? false : true
}, function(err, txp) {
if (err) {
profileService.lockFC();
$log.error(err);
2015-09-07 17:43:55 -03:00
$timeout(function() {
self.loading = null;
2015-09-08 13:58:24 -03:00
self.error = bwsError.msg(err, gettextCatalog.getString('Error'));
2015-09-07 17:43:55 -03:00
}, 1);
2015-09-03 02:50:59 -03:00
return;
}
if (!fc.canSign()) {
2015-09-07 17:43:55 -03:00
self.loading = null;
2015-09-03 02:50:59 -03:00
$log.info('No signing proposal: No private key');
return;
}
2015-09-07 17:43:55 -03:00
_signTx(txp, function(err, txp, rawTx) {
2015-09-03 02:50:59 -03:00
profileService.lockFC();
if (err) {
2015-09-07 17:43:55 -03:00
self.loading = null;
self.error = err;
$scope.$apply();
2015-09-03 02:50:59 -03:00
}
else {
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: self.sellPrice.priceUuid,
2015-09-07 17:43:55 -03:00
useCurrentPrice: self.sellPrice.priceUuid ? false : true,
2015-09-03 02:50:59 -03:00
ip: null
};
2015-09-07 17:43:55 -03:00
glideraService.sell(token, twoFaCode, data, function(err, data) {
self.loading = null;
if (err) {
2015-09-08 10:17:59 -03:00
self.error = err;
2015-09-07 17:43:55 -03:00
fc.removeTxProposal(txp, function(err, txpb) {
$timeout(function() {
$scope.$emit('Local/GlideraError');
}, 100);
});
}
else {
self.success = data;
$scope.$emit('Local/GlideraUpdated', token, permissions);
}
2015-09-03 02:50:59 -03:00
});
}
});
});
2015-09-01 18:53:47 -03:00
});
});
});
}, 100);
};
2015-09-03 02:50:59 -03:00
var _signTx = function(txp, cb) {
2015-09-01 18:53:47 -03:00
var self = this;
var fc = profileService.focusedClient;
fc.signTxProposal(txp, function(err, signedTx) {
profileService.lockFC();
if (err) {
2015-09-08 13:58:24 -03:00
err = bwsError.msg(err, gettextCatalog.getString('Could not accept payment'));
2015-09-01 18:53:47 -03:00
return cb(err);
}
2015-09-07 17:43:55 -03:00
else {
if (signedTx.status == 'accepted') {
return cb(null, txp, signedTx.raw);
2015-09-01 18:53:47 -03:00
2015-09-07 17:43:55 -03:00
} else {
return cb(gettext('The transaction could not be signed'));
}
2015-09-01 18:53:47 -03:00
}
});
};
2015-08-31 18:12:04 -03:00
});