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-06-10 15:16:02 -03:00
|
|
|
|
function($rootScope, $scope, $timeout, $ionicModal, $log, $modal, configService, profileService, addressService, feeService, glideraService, bwsError, lodash, walletService, fingerprintService) {
|
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;
|
|
|
|
|
|
this.loading = null;
|
2015-09-11 13:11:41 -03:00
|
|
|
|
var fc;
|
|
|
|
|
|
|
2015-10-05 15:04:07 -03:00
|
|
|
|
var otherWallets = function(testnet) {
|
2015-09-11 13:11:41 -03:00
|
|
|
|
var network = testnet ? 'testnet' : 'livenet';
|
|
|
|
|
|
return lodash.filter(profileService.getWallets(network), function(w) {
|
|
|
|
|
|
return w.network == network && w.m == 1;
|
|
|
|
|
|
});
|
|
|
|
|
|
};
|
|
|
|
|
|
|
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) {
|
|
|
|
|
|
self.otherWallets = otherWallets(testnet);
|
|
|
|
|
|
// Choose focused wallet
|
|
|
|
|
|
try {
|
|
|
|
|
|
var currentWalletId = profileService.focusedClient.credentials.walletId;
|
|
|
|
|
|
lodash.find(self.otherWallets, function(w) {
|
|
|
|
|
|
if (w.id == currentWalletId) {
|
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
|
self.selectedWalletId = w.id;
|
|
|
|
|
|
self.selectedWalletName = w.name;
|
|
|
|
|
|
fc = profileService.getClient(w.id);
|
|
|
|
|
|
$scope.$apply();
|
|
|
|
|
|
}, 100);
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
2016-01-21 14:52:58 -03:00
|
|
|
|
} catch (e) {
|
2015-10-05 15:04:07 -03:00
|
|
|
|
$log.debug(e);
|
|
|
|
|
|
};
|
|
|
|
|
|
};
|
|
|
|
|
|
|
2015-09-11 13:11:41 -03:00
|
|
|
|
$scope.openWalletsModal = function(wallets) {
|
|
|
|
|
|
self.error = null;
|
|
|
|
|
|
self.selectedWalletId = null;
|
|
|
|
|
|
self.selectedWalletName = 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
|
|
|
|
});
|
|
|
|
|
|
};
|
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-01-21 15:42:59 -03:00
|
|
|
|
self.loading = '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-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-05-09 15:56:44 -03:00
|
|
|
|
addressService.getAddress(fc.credentials.walletId, null, function(err, refundAddress) {
|
|
|
|
|
|
if (!refundAddress) {
|
|
|
|
|
|
self.loading = null;
|
|
|
|
|
|
self.error = bwsError.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) {
|
|
|
|
|
|
self.loading = null;
|
|
|
|
|
|
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
|
|
|
|
|
|
}
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
self.loading = 'Creating transaction...';
|
|
|
|
|
|
walletService.createTx(fc, txp, function(err, createdTxp) {
|
|
|
|
|
|
self.loading = null;
|
|
|
|
|
|
if (err) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.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) {
|
|
|
|
|
|
fingerprintService.check(fc, function(err) {
|
2015-12-23 18:05:22 -03:00
|
|
|
|
if (err) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.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-05-09 15:56:44 -03:00
|
|
|
|
handleEncryptedWallet(fc, function(err) {
|
2016-01-21 14:52:58 -03:00
|
|
|
|
if (err) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.msg(err);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
self.loading = 'Signing transaction...';
|
|
|
|
|
|
|
|
|
|
|
|
walletService.publishTx(fc, createdTxp, function(err, publishedTxp) {
|
|
|
|
|
|
if (err) {
|
2016-01-21 14:52:58 -03:00
|
|
|
|
self.loading = null;
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.msg(err);
|
2016-05-09 15:56:44 -03:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
walletService.signTx(fc, publishedTxp, function(err, signedTxp) {
|
|
|
|
|
|
walletService.lock(fc);
|
|
|
|
|
|
walletService.removeTx(fc, signedTxp, function(err) {
|
|
|
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
|
});
|
2016-01-21 14:52:58 -03:00
|
|
|
|
if (err) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
self.loading = null;
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.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
|
|
|
|
|
|
};
|
|
|
|
|
|
self.loading = 'Selling bitcoin...';
|
|
|
|
|
|
glideraService.sell(token, twoFaCode, data, function(err, data) {
|
|
|
|
|
|
self.loading = null;
|
|
|
|
|
|
if (err) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
self.error = err.message || bwsError.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-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
|
|
|
|
});
|