Feat/coinbase integration (#4012)

* Oauth2 and first view

* Connect with Coinbase using mobile

* Buy and Sell through Coinbase

* Fix buy

* Receive and send bitcoin to Coinbase account

* Receive bitcoin from Coinbase to Copay

* Complete user and account information. Connection errors

* Improves error handler

* Removes console.log

* Coinbase background color. Send to Coinbase form validation

* Fix send from different wallet

* Send and receive using Coinbase

* Pagination activity

* Fix Buy and Sell

* One option in the sidebar to Buy and Sell

* Native balance on Coinbase homepage

* Rename receive and send

* Auto-close window after authenticate

* Reorder

* Get payment methods

* Fix when token expired

* Fix token expired

* Integration: sell and send to Coinbase

* Store pending transaction before sell

* Sell flow completed

* Removing files

* Fix sell

* Fix sell

* Fix sell

* Sell completed

* Buy bitcoin through coinbase

* Buy auto

* Currency set to USD

* Select payment methods. Limits

* Removes payment methods from preferences

* Fix signs. Tx ordered by updated. Minor fixes

* Removes console.log

* Improving ux-language things

* Fix selectedpaymentmethod if not verified

* Set error if tx not found

* Price sensitivity. Minor fixes

* Adds coinbase api key to gitignore

* Coinbase production ready

* Fix sell in usd

* Bug fixes

* New Sensitivity step

* Refresh token with a simple click

* Refresh token

* Refactor

* Fix auto reconnect if token expired

Signed-off-by: Gustavo Maximiliano Cortez <cmgustavo83@gmail.com>

* Fix calls if token expired
This commit is contained in:
Gustavo Maximiliano Cortez 2016-04-13 14:08:03 -03:00 committed by Matias Alejo Garcia
commit d0dbd85711
39 changed files with 2365 additions and 55 deletions

View file

@ -74,7 +74,7 @@ angular.module('copayApp.controllers').controller('backupController',
$scope.$apply();
}, 1);
profileService.unlockFC(function(err) {
profileService.unlockFC({}, function(err) {
if (err) {
self.error = bwsError.msg(err, gettext('Could not decrypt'));
$log.warn('Error decrypting credentials:', self.error); //TODO

View file

@ -0,0 +1,206 @@
'use strict';
angular.module('copayApp.controllers').controller('buyCoinbaseController',
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, txService, bwsError, addressService) {
window.ignoreMobilePause = true;
var self = this;
var fc;
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
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);
}
});
} catch (e) {
$log.debug(e);
};
};
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_buy) {
self.paymentMethods.push(pm);
}
if (pm.allow_buy && pm.primary_buy) {
$scope.selectedPaymentMethod = pm;
}
});
});
};
this.getPrice = function(token) {
var currency = 'USD';
coinbaseService.buyPrice(token, currency, function(err, b) {
if (err) return;
self.buyPrice = b.data || null;
});
};
$scope.openWalletsModal = function(wallets) {
self.error = null;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'BUY';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg({
'code': 'WALLET_NOT_COMPLETE'
}, 'Could not choose the wallet');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
this.buyRequest = function(token, account) {
self.error = null;
var accountId = account.id;
var amount = $scope.amount ? $scope.amount : $scope.fiat;
var currency = $scope.amount ? 'BTC' : 'USD';
if (!amount) return;
var dataSrc = {
amount: amount,
currency: currency,
payment_method: $scope.selectedPaymentMethod.id || null
};
this.loading = 'Sending request...';
coinbaseService.buyRequest(token, accountId, dataSrc, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.buyInfo = data.data;
});
};
this.confirmBuy = function(token, account, buy) {
self.error = null;
var accountId = account.id;
var buyId = buy.id;
this.loading = 'Buying bitcoin...';
coinbaseService.buyCommit(token, accountId, buyId, function(err, b) {
self.loading = null;
if (err) {
self.error = err;
return;
} else {
var tx = b.data.transaction;
if (!tx) return;
self.loading = 'Getting transaction...';
coinbaseService.getTransaction(token, accountId, tx.id, function(err, updatedTx) {
if (err) $log.debug(err);
addressService.getAddress(self.selectedWalletId, false, function(err, addr) {
if (err) {
self.loading = null;
self.error = {errors: [{ message: 'Could not create address' }]};
return;
}
coinbaseService.savePendingTransaction(updatedTx.data, {toAddr: addr}, function(err) {
self.loading = null;
if (err) $log.debug(err);
updatedTx.data['toAddr'] = addr;
if (updatedTx.data.status == 'completed') {
self.sendToCopay(token, account, updatedTx.data);
} else {
self.success = updatedTx.data;
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
}
});
});
});
}
});
};
this.sendToCopay = function(token, account, tx) {
self.error = null;
var accountId = account.id;
self.loading = 'Sending funds to Copay...';
var data = {
to: tx.toAddr,
amount: tx.amount.amount,
currency: tx.amount.currency,
description: 'Copay Wallet: ' + self.selectedWalletName
};
coinbaseService.sendTo(token, accountId, data, function(err, res) {
self.loading = null;
if (err) {
self.error = err;
} else {
self.receiveInfo = res.data;
if (!res.data.id) return;
coinbaseService.getTransaction(token, accountId, res.data.id, function(err, sendTx) {
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.savePendingTransaction(sendTx.data, {}, function(err) {
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
});
});
});
}
});
};
});

View file

@ -63,7 +63,7 @@ angular.module('copayApp.controllers').controller('buyGlideraController',
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/glidera-wallets.html',
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});

View file

@ -0,0 +1,103 @@
'use strict';
angular.module('copayApp.controllers').controller('coinbaseController',
function($rootScope, $scope, $timeout, $modal, profileService, configService, storageService, coinbaseService, isChromeApp, animationService, lodash, nodeWebkit) {
this.openAuthenticateWindow = function() {
var oauthUrl = this.getAuthenticateUrl();
if (!nodeWebkit.isDefined()) {
$rootScope.openExternalLink(oauthUrl, '_system');
} else {
var self = this;
var gui = require('nw.gui');
var win = gui.Window.open(oauthUrl, {
focus: true,
position: 'center'
});
win.on ('loaded', function(){
var title = win.title;
if (title.indexOf('Coinbase') == -1) {
$scope.code = title;
self.submitOauthCode(title);
win.close();
}
});
}
}
this.getAuthenticateUrl = function() {
return coinbaseService.getOauthCodeUrl();
};
this.submitOauthCode = function(code) {
var self = this;
var coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = coinbaseTestnet ? 'testnet' : 'livenet';
this.loading = true;
this.error = null;
$timeout(function() {
coinbaseService.getToken(code, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
$timeout(function() {
$scope.$apply();
}, 100);
}
else if (data && data.access_token && data.refresh_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
storageService.setCoinbaseRefreshToken(network, data.refresh_token, function() {
$scope.$emit('Local/CoinbaseUpdated', data.access_token);
$timeout(function() {
$scope.$apply();
}, 100);
});
});
}
});
}, 100);
};
this.openTxModal = function(tx) {
$rootScope.modalOpened = true;
var self = this;
var config = configService.getSync().wallet.settings;
var fc = profileService.focusedClient;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.tx = tx;
$scope.settings = config;
$scope.color = fc.backgroundColor;
$scope.noColor = true;
$scope.remove = function() {
coinbaseService.savePendingTransaction($scope.tx, {remove: true}, function(err) {
$rootScope.$emit('Local/CoinbaseTx');
$scope.cancel();
});
};
$scope.cancel = lodash.debounce(function() {
$modalInstance.dismiss('cancel');
}, 0, 1000);
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/coinbase-tx-details.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl,
});
var disableCloseModal = $rootScope.$on('closeModal', function() {
modalInstance.dismiss('cancel');
});
modalInstance.result.finally(function() {
$rootScope.modalOpened = false;
disableCloseModal();
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
};
});

View file

@ -0,0 +1,38 @@
'use strict';
angular.module('copayApp.controllers').controller('coinbaseUriController',
function($scope, $stateParams, $timeout, profileService, configService, coinbaseService, storageService, go) {
this.submitOauthCode = function(code) {
var self = this;
var coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = coinbaseTestnet ? 'testnet' : 'livenet';
this.loading = true;
this.error = null;
$timeout(function() {
coinbaseService.getToken(code, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
$timeout(function() {
$scope.$apply();
}, 100);
}
else if (data && data.access_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
$scope.$emit('Local/CoinbaseUpdated', data.access_token);
$timeout(function() {
go.path('coinbase');
$scope.$apply();
}, 100);
});
}
});
}, 100);
};
this.checkCode = function() {
this.code = $stateParams.code;
this.submitOauthCode(this.code);
};
});

View file

@ -1,6 +1,6 @@
'use strict';
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, latestReleaseService, bwcService, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, isMobile, addressbookService) {
angular.module('copayApp.controllers').controller('indexController', function($rootScope, $scope, $log, $filter, $timeout, latestReleaseService, bwcService, pushNotificationsService, lodash, go, profileService, configService, isCordova, rateService, storageService, addressService, gettext, gettextCatalog, amMoment, nodeWebkit, addonManager, isChromeApp, bwsError, txFormatService, uxLanguage, $state, glideraService, coinbaseService, isMobile, addressbookService) {
var self = this;
var SOFT_CONFIRMATION_LIMIT = 12;
var errors = bwcService.getErrors();
@ -147,6 +147,7 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.setAddressbook();
self.initGlidera();
self.initCoinbase();
self.setCustomBWSFlag();
@ -1265,6 +1266,246 @@ angular.module('copayApp.controllers').controller('indexController', function($r
};
self.initCoinbase = function(accessToken) {
self.coinbaseEnabled = configService.getSync().coinbase.enabled;
self.coinbaseTestnet = configService.getSync().coinbase.testnet;
var network = self.coinbaseTestnet ? 'testnet' : 'livenet';
self.coinbaseToken = null;
self.coinbaseError = null;
self.coinbasePermissions = null;
self.coinbaseEmail = null;
self.coinbasePersonalInfo = null;
self.coinbaseTxs = null;
self.coinbaseStatus = null;
if (!self.coinbaseEnabled) return;
coinbaseService.setCredentials(network);
var getToken = function(cb) {
if (accessToken) {
cb(null, accessToken);
} else {
storageService.getCoinbaseToken(network, cb);
}
};
getToken(function(err, accessToken) {
if (err || !accessToken) return;
else {
self.coinbaseLoading = 'Getting primary account...';
coinbaseService.getAccounts(accessToken, function(err, a) {
self.coinbaseLoading = null;
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
} else {
self.coinbaseToken = accessToken;
lodash.each(a.data, function(account) {
if (account.primary && account.type == 'wallet') {
self.coinbaseAccount = account;
self.updateCoinbase();
}
});
}
});
}
});
};
self.updateCoinbase = lodash.debounce(function(opts) {
if (!self.coinbaseToken || !self.coinbaseAccount) return;
var accessToken = self.coinbaseToken;
var accountId = self.coinbaseAccount.id;
opts = opts || {};
if (opts.updateAccount) {
coinbaseService.getAccount(accessToken, accountId, function(err, a) {
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
return;
}
self.coinbaseAccount = a.data;
});
}
coinbaseService.getCurrentUser(accessToken, function(err, u) {
if (err) {
self.coinbaseError = err;
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
}
return;
}
self.coinbaseUser = u.data;
});
coinbaseService.getPendingTransactions(function(err, txs) {
self.coinbasePendingTransactions = lodash.isEmpty(txs) ? null : txs;
lodash.forEach(txs, function(dataFromStorage, txId) {
if ((dataFromStorage.type == 'sell' && dataFromStorage.status == 'completed') ||
(dataFromStorage.type == 'buy' && dataFromStorage.status == 'completed') ||
dataFromStorage.status == 'error' ||
(dataFromStorage.type == 'send' && dataFromStorage.status == 'completed')) return;
coinbaseService.getTransaction(accessToken, accountId, txId, function(err, tx) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
_updateCoinbasePendingTransactions(dataFromStorage, tx.data);
self.coinbasePendingTransactions[txId] = dataFromStorage;
if (tx.data.type == 'send' && tx.data.status == 'completed' && tx.data.from) {
coinbaseService.sellPrice(accessToken, dataFromStorage.sell_price_currency, function(err, s) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
var newSellPrice = s.data.amount;
var variance = Math.abs((newSellPrice - dataFromStorage.sell_price_amount) / dataFromStorage.sell_price_amount * 100);
if (variance < dataFromStorage.price_sensitivity.value) {
self.sellPending(tx.data);
} else {
var error = {errors: [{ message: 'Price falls over the selected percentage' }]};
coinbaseService.savePendingTransaction(dataFromStorage, {status: 'error', error: error}, function(err) {
if (err) $log.debug(err);
});
}
});
} else if (tx.data.type == 'buy' && tx.data.status == 'pending' && tx.data.buy) {
self.buyPending(tx.data);
} else {
coinbaseService.savePendingTransaction(dataFromStorage, {}, function(err) {
if (err) $log.debug(err);
});
}
});
});
});
}, 1000);
var _updateCoinbasePendingTransactions = function (obj/*, …*/) {
for (var i=1; i<arguments.length; i++) {
for (var prop in arguments[i]) {
var val = arguments[i][prop];
if (typeof val == "object")
_updateCoinbasePendingTransactions(obj[prop], val);
else
obj[prop] = val ? val : obj[prop];
}
}
return obj;
};
self.refreshCoinbaseToken = function() {
var network = self.coinbaseTestnet ? 'testnet' : 'livenet';
storageService.getCoinbaseRefreshToken(network, function(err, refreshToken) {
coinbaseService.refreshToken(refreshToken, function(err, data) {
if (err) {
self.coinbaseError = err;
} else if (data && data.access_token && data.refresh_token) {
storageService.setCoinbaseToken(network, data.access_token, function() {
storageService.setCoinbaseRefreshToken(network, data.refresh_token, function() {
$timeout(function() {
self.initCoinbase(data.access_token);
}, 100);
});
});
}
});
});
};
self.buyPending = function(tx) {
if (!tx) return;
var data = {
to: tx.toAddr,
amount: tx.amount.amount,
currency: tx.amount.currency,
description: 'To Copay Wallet'
};
coinbaseService.sendTo(self.coinbaseToken, self.coinbaseAccount.id, data, function(err, res) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
} else {
if (!res.data.id) {
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
coinbaseService.getTransaction(self.coinbaseToken, self.coinbaseAccount.id, res.data.id, function(err, sendTx) {
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.savePendingTransaction(sendTx.data, {}, function(err) {
$timeout(function() {
self.updateCoinbase({updateAccount: true});
}, 1000);
});
});
});
}
});
};
self.sellPending = function(tx) {
if (!tx) return;
var data = tx.amount;
data['commit'] = true;
coinbaseService.sellRequest(self.coinbaseToken, self.coinbaseAccount.id, data, function(err, res) {
if (err) {
if (err.errors[0] && err.errors[0].id == 'expired_token') {
self.refreshCoinbaseToken();
return;
}
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
} else {
if (!res.data.transaction) {
coinbaseService.savePendingTransaction(tx, {status: 'error', error: err}, function(err) {
if (err) $log.debug(err);
});
return;
}
coinbaseService.savePendingTransaction(tx, {remove: true}, function(err) {
coinbaseService.getTransaction(self.coinbaseToken, self.coinbaseAccount.id, res.data.transaction.id, function(err, updatedTx) {
coinbaseService.savePendingTransaction(updatedTx.data, {}, function(err) {
if (err) $log.debug(err);
$timeout(function() {
self.updateCoinbase({updateAccount: true});
}, 1000);
});
});
});
}
});
};
self.setAddressbook = function(ab) {
if (ab) {
self.addressbook = ab;
@ -1341,10 +1582,20 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.initGlidera(accessToken);
});
$rootScope.$on('Local/CoinbaseUpdated', function(event, accessToken) {
self.initCoinbase(accessToken);
});
$rootScope.$on('Local/GlideraTx', function(event, accessToken, permissions) {
self.updateGlidera();
});
$rootScope.$on('Local/CoinbaseTx', function(event) {
self.updateCoinbase({
updateAccount: true
});
});
$rootScope.$on('Local/GlideraError', function(event) {
self.debouncedUpdate();
});
@ -1460,6 +1711,11 @@ angular.module('copayApp.controllers').controller('indexController', function($r
self.updateGlidera();
});
}
if (self.coinbaseEnabled) {
$timeout(function() {
self.updateCoinbase();
});
}
if (self.pendingAmount) {
self.updateAll({
walletStatus: null,

View file

@ -44,7 +44,7 @@ angular.module('copayApp.controllers').controller('preferencesController',
});
} else {
if (!val && fc.hasPrivKeyEncrypted()) {
profileService.unlockFC(function(err) {
profileService.unlockFC({}, function(err) {
if (err) {
$scope.encrypt = true;
return;

View file

@ -0,0 +1,39 @@
'use strict';
angular.module('copayApp.controllers').controller('preferencesCoinbaseController',
function($scope, $modal, $timeout, applicationService, coinbaseService, storageService, animationService) {
this.revokeToken = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.ok = function() {
$modalInstance.close(true);
};
$scope.cancel = function() {
$modalInstance.dismiss();
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/coinbase-confirmation.html',
windowClass: animationService.modalAnimated.slideRight,
controller: ModalInstanceCtrl
});
modalInstance.result.then(function(ok) {
if (ok) {
storageService.removeCoinbaseToken(network, function() {
$timeout(function() {
applicationService.restart();
}, 100);
});
}
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutRight);
});
};
});

View file

@ -23,7 +23,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
}
$scope.spendUnconfirmed = config.wallet.spendUnconfirmed;
$scope.glideraEnabled = config.glidera.enabled;
$scope.glideraTestnet = config.glidera.testnet;
$scope.coinbaseEnabled = config.coinbase.enabled;
$scope.pushNotifications = config.pushNotifications.enabled;
};
@ -77,15 +77,15 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
});
});
var unwatchGlideraTestnet = $scope.$watch('glideraTestnet', function(newVal, oldVal) {
var unwatchCoinbaseEnabled = $scope.$watch('coinbaseEnabled', function(newVal, oldVal) {
if (newVal == oldVal) return;
var opts = {
glidera: {
testnet: newVal
coinbase: {
enabled: newVal
}
};
configService.set(opts, function(err) {
$rootScope.$emit('Local/GlideraUpdated');
$rootScope.$emit('Local/CoinbaseUpdated');
if (err) $log.debug(err);
});
});
@ -93,7 +93,7 @@ angular.module('copayApp.controllers').controller('preferencesGlobalController',
$scope.$on('$destroy', function() {
unwatchSpendUnconfirmed();
unwatchGlideraEnabled();
unwatchGlideraTestnet();
unwatchCoinbaseEnabled();
unwatchPushNotifications();
});
});

View file

@ -0,0 +1,301 @@
'use strict';
angular.module('copayApp.controllers').controller('sellCoinbaseController',
function($scope, $modal, $log, $timeout, lodash, profileService, coinbaseService, animationService, txService, bwsError) {
window.ignoreMobilePause = true;
var self = this;
var fc;
$scope.priceSensitivity = [
{
value : 0.5,
name: '0.5%'
},
{
value : 1,
name: '1%'
},
{
value : 2,
name: '2%'
},
{
value : 5,
name: '5%'
},
{
value : 10,
name: '10%'
}
];
$scope.selectedPriceSensitivity = $scope.priceSensitivity[1];
var otherWallets = function(testnet) {
var network = testnet ? 'testnet' : 'livenet';
return lodash.filter(profileService.getWallets(network), function(w) {
return w.network == network && w.m == 1;
});
};
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);
}
});
} catch (e) {
$log.debug(e);
};
};
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;
var ModalInstanceCtrl = function($scope, $modalInstance) {
$scope.type = 'SELL';
$scope.wallets = wallets;
$scope.noColor = true;
$scope.cancel = function() {
$modalInstance.dismiss('cancel');
};
$scope.selectWallet = function(walletId, walletName) {
if (!profileService.getClient(walletId).isComplete()) {
self.error = bwsError.msg({
'code': 'WALLET_NOT_COMPLETE'
}, 'Could not choose the wallet');
$modalInstance.dismiss('cancel');
return;
}
$modalInstance.close({
'walletId': walletId,
'walletName': walletName,
});
};
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
modalInstance.result.finally(function() {
var m = angular.element(document.getElementsByClassName('reveal-modal'));
m.addClass(animationService.modalAnimated.slideOutDown);
});
modalInstance.result.then(function(obj) {
$timeout(function() {
self.selectedWalletId = obj.walletId;
self.selectedWalletName = obj.walletName;
fc = profileService.getClient(obj.walletId);
$scope.$apply();
}, 100);
});
};
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;
this.loading = 'Sending request...';
coinbaseService.sellRequest(token, accountId, data, function(err, sell) {
self.loading = null;
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;
this.loading = 'Selling bitcoin...';
coinbaseService.sellCommit(token, accountId, sellId, function(err, data) {
self.loading = null;
if (err) {
self.error = err;
return;
}
self.success = data.data;
$scope.$emit('Local/CoinbaseTx');
});
};
this.createTx = function(token, account, amount) {
self.error = null;
var accountId = account.id;
var dataSrc = { name : 'Received from Copay: ' + self.selectedWalletName };
var outputs = [];
self.loading = 'Creating transaction...';
$timeout(function() {
coinbaseService.createAddress(token, accountId, dataSrc, function(err, data) {
if (err) {
self.loading = null;
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
});
var opts = {
selectedClient: fc,
toAddress: address,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null
};
txService.createTx(opts, function(err, txp) {
if (err) {
$log.debug(err);
self.loading = null;
self.error = {errors: [{ message: 'Could not create transaction: ' + err.message }]};
$scope.$apply();
return;
}
$scope.$emit('Local/NeedsConfirmation', txp, function(accept) {
self.loading = null;
if (accept) {
self.confirmTx(txp, function(err, tx) {
if (err) {
self.error = {errors: [{ message: 'Could not create transaction: ' + err.message }]};
return;
}
self.loading = 'Checking transaction...';
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) {
if (ctx.status == 'completed') {
self.sellRequest(token, account, ctx);
} else {
// Save to localstorage
self.loading = null;
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity;
ctx['sell_price_amount'] = self.sellPrice.amount;
ctx['sell_price_currency'] = self.sellPrice.currency;
ctx['description'] = 'Copay Wallet: ' + fc.credentials.walletName;
coinbaseService.savePendingTransaction(ctx, null, function(err) {
if (err) $log.debug(err);
self.sendInfo = ctx;
$timeout(function() {
$scope.$emit('Local/CoinbaseTx');
}, 1000);
});
}
return false;
}
});
});
});
}
});
});
});
}, 100);
};
this.confirmTx = function(txp, cb) {
txService.prepare({selectedClient: fc}, function(err) {
if (err) {
$log.debug(err);
return cb(err);
}
self.loading = 'Sending bitcoin to Coinbase...';
txService.publishTx(txp, {selectedClient: fc}, function(err, txpPublished) {
if (err) {
self.loading = null;
$log.debug(err);
return cb({errors: [{ message: 'Transaction could not be published: ' + err.message }]});
} else {
txService.signAndBroadcast(txpPublished, {selectedClient: fc}, function(err, txp) {
if (err) {
self.loading = null;
$log.debug(err);
txService.removeTx(txp, function(err) {
if (err) $log.debug(err);
});
return cb({errors: [{ message: 'The payment was created but could not be completed: ' + err.message }]});
} else {
$timeout(function() {
self.loading = null;
return cb(null, txp);
}, 5000);
}
});
}
});
});
};
});

View file

@ -70,7 +70,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
};
var modalInstance = $modal.open({
templateUrl: 'views/modals/glidera-wallets.html',
templateUrl: 'views/modals/wallets.html',
windowClass: animationService.modalAnimated.slideUp,
controller: ModalInstanceCtrl,
});
@ -129,7 +129,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
self.error = null;
txService.prepare(function(err) {
txService.prepare({selectedClient: fc}, function(err) {
if (err) {
self.error = err;
return;
@ -173,7 +173,7 @@ angular.module('copayApp.controllers').controller('sellGlideraController',
return;
}
txService.sign(txp, function(err, txp) {
txService.sign(txp, {selectedClient: fc}, function(err, txp) {
if (err) {
self.loading = null;
self.error = err;

View file

@ -943,12 +943,12 @@ angular.module('copayApp.controllers').controller('walletHomeController', functi
this.confirmTx = function(txp) {
var self = this;
txService.prepare(function(err) {
txService.prepare({}, function(err) {
if (err) {
return self.setSendError(err);
}
self.setOngoingProcess(gettextCatalog.getString('Sending transaction'));
txService.publishTx(txp, function(err, txpPublished) {
txService.publishTx(txp, {}, function(err, txpPublished) {
if (err) {
self.setOngoingProcess();
self.setSendError(err);