Wallet/src/js/controllers/sellGlidera.js

115 lines
3.8 KiB
JavaScript
Raw Normal View History

2015-08-31 18:12:04 -03:00
'use strict';
angular.module('copayApp.controllers').controller('sellGlideraController',
2015-09-01 18:53:47 -03:00
function($scope, $timeout, $log, gettext, configService, profileService, addressService, feeService, glideraService) {
var config = configService.getSync();
this.data = {};
this.show2faCodeInput = null;
2015-09-03 02:50:59 -03:00
this.success = 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)) {
this.sellPrice = null;
return;
}
2015-08-31 18:12:04 -03:00
glideraService.sellPrice(token, price, function(error, sellPrice) {
self.sellPrice = sellPrice;
});
};
2015-09-03 02:50:59 -03:00
this.get2faCode = function(token) {
2015-09-01 18:53:47 -03:00
var self = this;
$timeout(function() {
2015-09-03 02:50:59 -03:00
glideraService.get2faCode(token, function(error, sent) {
self.show2faCodeInput = sent;
});
}, 100);
};
2015-09-01 18:53:47 -03:00
2015-09-03 02:50:59 -03:00
this.createTx = function(token, twoFaCode) {
var self = this;
var fc = profileService.focusedClient;
2015-09-01 18:53:47 -03:00
2015-09-03 02:50:59 -03:00
$timeout(function() {
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
if (!refundAddress) return;
glideraService.getSellAddress(token, function(error, sellAddress) {
if (!sellAddress) return;
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,
message: 'Glidera',
payProUrl: null,
feePerKb: feePerKb,
excludeUnconfirmedUtxos: self.currentSpendUnconfirmed ? false : true
}, function(err, txp) {
if (err) {
profileService.lockFC();
$log.error(err);
return;
}
if (!fc.canSign()) {
$log.info('No signing proposal: No private key');
return;
}
_signTx(txp, function(err, rawTx) {
profileService.lockFC();
if (err) {
self.error = err.message ? err.message : gettext('The payment was created but could not be completed. Please try again from home screen');
$timeout(function() {
$scope.$digest();
}, 1);
}
else {
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: self.sellPrice.priceUuid,
useCurrentPrice: false,
ip: null
};
glideraService.sell(token, twoFaCode, data, function(error, data) {
self.success = data
});
}
});
});
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) {
err.message = bwsError.msg(err, gettextCatalog.getString('The payment was created but could not be signed. Please try again from home screen'));
return cb(err);
}
if (signedTx.status == 'accepted') {
2015-09-03 02:50:59 -03:00
return cb(null, signedTx.raw);
2015-09-01 18:53:47 -03:00
} else {
2015-09-03 02:50:59 -03:00
return cb(true);
2015-09-01 18:53:47 -03:00
console.log('NO ACCEPTED TX');
}
});
};
2015-08-31 18:12:04 -03:00
});