2016-04-13 14:08:03 -03:00
|
|
|
'use strict';
|
|
|
|
|
|
2016-06-10 15:16:02 -03:00
|
|
|
angular.module('copayApp.controllers').controller('sellCoinbaseController',
|
2016-07-11 11:46:48 -03:00
|
|
|
function($rootScope, $scope, $log, $timeout, $ionicModal, lodash, profileService, coinbaseService, configService, walletService, fingerprintService, ongoingProcess, go) {
|
2016-06-10 15:16:02 -03:00
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
var self = this;
|
2016-06-11 23:55:28 -03:00
|
|
|
var client;
|
2016-04-13 14:08:03 -03:00
|
|
|
|
|
|
|
|
$scope.priceSensitivity = [
|
|
|
|
|
{
|
2016-06-10 15:16:02 -03:00
|
|
|
value: 0.5,
|
2016-04-13 14:08:03 -03:00
|
|
|
name: '0.5%'
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-06-10 15:16:02 -03:00
|
|
|
value: 1,
|
2016-04-13 14:08:03 -03:00
|
|
|
name: '1%'
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-06-10 15:16:02 -03:00
|
|
|
value: 2,
|
2016-04-13 14:08:03 -03:00
|
|
|
name: '2%'
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-06-10 15:16:02 -03:00
|
|
|
value: 5,
|
2016-04-13 14:08:03 -03:00
|
|
|
name: '5%'
|
|
|
|
|
},
|
|
|
|
|
{
|
2016-06-10 15:16:02 -03:00
|
|
|
value: 10,
|
2016-04-13 14:08:03 -03:00
|
|
|
name: '10%'
|
|
|
|
|
}
|
|
|
|
|
];
|
|
|
|
|
$scope.selectedPriceSensitivity = $scope.priceSensitivity[1];
|
|
|
|
|
|
|
|
|
|
this.init = function(testnet) {
|
2016-06-11 23:55:28 -03:00
|
|
|
self.allWallets = profileService.getWallets(testnet ? 'testnet' : 'livenet', 1);
|
|
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
}
|
2016-04-13 14:08:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.getPaymentMethods = function(token) {
|
|
|
|
|
coinbaseService.getPaymentMethods(token, function(err, p) {
|
|
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.paymentMethods = [];
|
|
|
|
|
lodash.each(p.data, function(pm) {
|
|
|
|
|
if (pm.allow_sell) {
|
|
|
|
|
self.paymentMethods.push(pm);
|
|
|
|
|
}
|
|
|
|
|
if (pm.allow_sell && pm.primary_sell) {
|
|
|
|
|
$scope.selectedPaymentMethod = pm;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.getPrice = function(token) {
|
|
|
|
|
var currency = 'USD';
|
|
|
|
|
coinbaseService.sellPrice(token, currency, function(err, s) {
|
|
|
|
|
if (err) return;
|
|
|
|
|
self.sellPrice = s.data || null;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
$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;
|
2016-04-13 14:08:03 -03:00
|
|
|
|
2016-06-10 15:16:02 -03:00
|
|
|
$ionicModal.fromTemplateUrl('views/modals/wallets.html', {
|
|
|
|
|
scope: $scope,
|
|
|
|
|
animation: 'slide-in-up'
|
|
|
|
|
}).then(function(modal) {
|
|
|
|
|
$scope.walletsModal = modal;
|
|
|
|
|
$scope.walletsModal.show();
|
2016-04-13 14:08:03 -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();
|
|
|
|
|
});
|
2016-04-13 14:08:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.depositFunds = function(token, account) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
if ($scope.amount) {
|
|
|
|
|
this.createTx(token, account, $scope.amount)
|
|
|
|
|
} else if ($scope.fiat) {
|
|
|
|
|
var btcValue = ($scope.fiat / self.sellPrice.amount).toFixed(8);
|
|
|
|
|
this.createTx(token, account, btcValue);
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.sellRequest = function(token, account, ctx) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
if (!ctx.amount) return;
|
|
|
|
|
var accountId = account.id;
|
|
|
|
|
var data = ctx.amount;
|
|
|
|
|
data['payment_method'] = $scope.selectedPaymentMethod.id || null;
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Sending request...', true);
|
2016-04-13 14:08:03 -03:00
|
|
|
coinbaseService.sellRequest(token, accountId, data, function(err, sell) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Sending request...', false);
|
2016-04-13 14:08:03 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.sellInfo = sell.data;
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.confirmSell = function(token, account, sell) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
var accountId = account.id;
|
|
|
|
|
var sellId = sell.id;
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Selling Bitcoin...', true);
|
2016-04-13 14:08:03 -03:00
|
|
|
coinbaseService.sellCommit(token, accountId, sellId, function(err, data) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Selling Bitcoin...', false);
|
2016-04-13 14:08:03 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = err;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
self.success = data.data;
|
|
|
|
|
$scope.$emit('Local/CoinbaseTx');
|
|
|
|
|
});
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.createTx = function(token, account, amount) {
|
|
|
|
|
self.error = null;
|
|
|
|
|
|
2016-06-13 08:37:14 -03:00
|
|
|
if (!client) {
|
|
|
|
|
self.error = 'No wallet selected';
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-13 14:08:03 -03:00
|
|
|
var accountId = account.id;
|
2016-06-10 15:16:02 -03:00
|
|
|
var dataSrc = {
|
|
|
|
|
name: 'Received from Copay: ' + self.selectedWalletName
|
|
|
|
|
};
|
2016-04-13 14:08:03 -03:00
|
|
|
var outputs = [];
|
2016-05-09 15:56:44 -03:00
|
|
|
var config = configService.getSync();
|
|
|
|
|
var configWallet = config.wallet;
|
|
|
|
|
var walletSettings = configWallet.settings;
|
2016-04-13 14:08:03 -03:00
|
|
|
|
|
|
|
|
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Creating Transaction...', true);
|
2016-04-13 14:08:03 -03:00
|
|
|
$timeout(function() {
|
|
|
|
|
|
|
|
|
|
coinbaseService.createAddress(token, accountId, dataSrc, function(err, data) {
|
|
|
|
|
if (err) {
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Creating Transaction...', false);
|
2016-04-13 14:08:03 -03:00
|
|
|
self.error = err;
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var address, comment;
|
|
|
|
|
|
|
|
|
|
address = data.data.address;
|
|
|
|
|
amount = parseInt((amount * 100000000).toFixed(0));
|
|
|
|
|
comment = 'Send funds to Coinbase Account: ' + account.name;
|
|
|
|
|
|
|
|
|
|
outputs.push({
|
|
|
|
|
'toAddress': address,
|
|
|
|
|
'amount': amount,
|
|
|
|
|
'message': comment
|
|
|
|
|
});
|
2016-06-10 15:16:02 -03:00
|
|
|
|
2016-05-09 15:56:44 -03:00
|
|
|
var txp = {
|
2016-04-13 14:08:03 -03:00
|
|
|
toAddress: address,
|
|
|
|
|
amount: amount,
|
|
|
|
|
outputs: outputs,
|
|
|
|
|
message: comment,
|
2016-05-09 15:56:44 -03:00
|
|
|
payProUrl: null,
|
|
|
|
|
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
|
|
|
|
|
feeLevel: walletSettings.feeLevel || 'normal'
|
2016-04-13 14:08:03 -03:00
|
|
|
};
|
|
|
|
|
|
2016-06-11 23:55:28 -03:00
|
|
|
walletService.createTx(client, txp, function(err, createdTxp) {
|
2016-04-13 14:08:03 -03:00
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Creating Transaction...', false);
|
2016-06-10 15:16:02 -03:00
|
|
|
self.error = {
|
|
|
|
|
errors: [{
|
|
|
|
|
message: 'Could not create transaction: ' + err.message
|
|
|
|
|
}]
|
|
|
|
|
};
|
2016-04-13 14:08:03 -03:00
|
|
|
$scope.$apply();
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Creating Transaction...', false);
|
2016-05-09 15:56:44 -03:00
|
|
|
$scope.$emit('Local/NeedsConfirmation', createdTxp, function(accept) {
|
2016-06-10 15:16:02 -03:00
|
|
|
if (accept) {
|
2016-05-09 15:56:44 -03:00
|
|
|
self.confirmTx(createdTxp, function(err, tx) {
|
2016-06-16 18:29:56 -03:00
|
|
|
ongoingProcess.clear();
|
2016-06-10 15:16:02 -03:00
|
|
|
if (err) {
|
|
|
|
|
self.error = {
|
|
|
|
|
errors: [{
|
|
|
|
|
message: 'Could not create transaction: ' + err.message
|
|
|
|
|
}]
|
|
|
|
|
};
|
2016-04-13 14:08:03 -03:00
|
|
|
return;
|
|
|
|
|
}
|
2016-06-13 15:25:40 -03:00
|
|
|
ongoingProcess.set('Checking Transaction...', false);
|
2016-04-13 14:08:03 -03:00
|
|
|
coinbaseService.getTransactions(token, accountId, function(err, ctxs) {
|
|
|
|
|
if (err) {
|
|
|
|
|
$log.debug(err);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
lodash.each(ctxs.data, function(ctx) {
|
|
|
|
|
if (ctx.type == 'send' && ctx.from) {
|
2016-06-16 18:29:56 -03:00
|
|
|
ongoingProcess.clear();
|
2016-04-13 14:08:03 -03:00
|
|
|
if (ctx.status == 'completed') {
|
|
|
|
|
self.sellRequest(token, account, ctx);
|
|
|
|
|
} else {
|
|
|
|
|
// Save to localstorage
|
|
|
|
|
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity;
|
2016-06-16 18:29:56 -03:00
|
|
|
ctx['sell_price_amount'] = self.sellPrice ? self.sellPrice.amount : '';
|
|
|
|
|
ctx['sell_price_currency'] = self.sellPrice ? self.sellPrice.currency : 'USD';
|
2016-06-11 23:55:28 -03:00
|
|
|
ctx['description'] = 'Copay Wallet: ' + client.credentials.walletName;
|
2016-04-13 14:08:03 -03:00
|
|
|
coinbaseService.savePendingTransaction(ctx, null, function(err) {
|
|
|
|
|
if (err) $log.debug(err);
|
|
|
|
|
self.sendInfo = ctx;
|
|
|
|
|
$timeout(function() {
|
|
|
|
|
$scope.$emit('Local/CoinbaseTx');
|
|
|
|
|
}, 1000);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
2016-06-16 18:29:56 -03:00
|
|
|
} else {
|
|
|
|
|
go.path('coinbase');
|
2016-04-13 14:08:03 -03:00
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}, 100);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
this.confirmTx = function(txp, cb) {
|
2016-05-09 15:56:44 -03:00
|
|
|
|
2016-08-29 15:24:46 -03:00
|
|
|
// TODO see walletService createAndPublish
|
2016-04-13 14:08:03 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
});
|