From 04db3f91598152514d8eb4a2144333ceefd49e63 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Sun, 18 Jun 2017 23:36:25 -0300 Subject: [PATCH 1/4] Testing coinbase --- src/js/controllers/buyCoinbase.js | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/js/controllers/buyCoinbase.js b/src/js/controllers/buyCoinbase.js index fb6a625a5..a525fbc2b 100644 --- a/src/js/controllers/buyCoinbase.js +++ b/src/js/controllers/buyCoinbase.js @@ -1,9 +1,10 @@ 'use strict'; -angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, $ionicConfig, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, txFormatService) { +angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, $ionicConfig, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, txFormatService, feeService) { var amount; var currency; + var feeBTC; var showErrorAndBack = function(err) { $scope.sendStatus = ''; @@ -44,13 +45,33 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct $scope.$on("$ionicView.beforeEnter", function(event, data) { $scope.isFiat = data.stateParams.currency != 'bits' && data.stateParams.currency != 'BTC' ? true : false; var parsedAmount = txFormatService.parseAmount( - data.stateParams.amount, + data.stateParams.amount, data.stateParams.currency); +console.log('[buyCoinbase.js:46]',parsedAmount); //TODO/ + + // Buy always in BTC + amount = (parsedAmount.amountSat / 100000000).toFixed(8); +console.log('[buyCoinbase.js:52]',amount); //TODO/ + currency = 'BTC'; +console.log('[buyCoinbase.js:54]',currency); //TODO/ - amount = parsedAmount.amount; - currency = parsedAmount.currency; $scope.amountUnitStr = parsedAmount.amountUnitStr; +console.log('[buyCoinbase.js:57]',$scope.amountUnitStr); //TODO/ + // Fee Normal for a single transaction + var txNormalFeeKB = 450 / 1024; +console.log('[buyCoinbase.js:60]',txNormalFeeKB); //TODO/ + feeService.getCurrentFeeValue(null, 'normal', function(err, feePerKB) { + feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); +console.log('[buyCoinbase.js:60]',feePerKB, feeBTC, amount - feeBTC); //TODO/ + // Check if transaction has enough funds to transfer bitcoin from Coinbase to Copay + if (amount - feeBTC < 0) { + showErrorAndBack('Not enough funds for fee'); + return; + } + }); + + return; // TODO $scope.network = coinbaseService.getNetwork(); $scope.wallets = profileService.getWallets({ onlyComplete: true, @@ -144,7 +165,7 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct var cancelText = 'Cancel'; popupService.showConfirm(null, message, okText, cancelText, function(ok) { if (!ok) return; - + ongoingProcess.set('buyingBitcoin', true, statusChangeHandler); coinbaseService.init(function(err, res) { if (err) { From dfd3eeec540a7e322d3857472acbe3477681b72f Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Wed, 21 Jun 2017 15:59:43 -0300 Subject: [PATCH 2/4] Adds fee to transaction --- src/js/controllers/buyCoinbase.js | 108 +++++++++++++---------------- src/js/services/coinbaseService.js | 76 +++++++++++++++----- 2 files changed, 106 insertions(+), 78 deletions(-) diff --git a/src/js/controllers/buyCoinbase.js b/src/js/controllers/buyCoinbase.js index a525fbc2b..6066207ba 100644 --- a/src/js/controllers/buyCoinbase.js +++ b/src/js/controllers/buyCoinbase.js @@ -1,10 +1,9 @@ 'use strict'; -angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, $ionicConfig, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, txFormatService, feeService) { +angular.module('copayApp.controllers').controller('buyCoinbaseController', function($scope, $log, $state, $timeout, $ionicHistory, $ionicScrollDelegate, $ionicConfig, lodash, coinbaseService, popupService, profileService, ongoingProcess, walletService, txFormatService) { var amount; var currency; - var feeBTC; var showErrorAndBack = function(err) { $scope.sendStatus = ''; @@ -47,84 +46,75 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct var parsedAmount = txFormatService.parseAmount( data.stateParams.amount, data.stateParams.currency); -console.log('[buyCoinbase.js:46]',parsedAmount); //TODO/ // Buy always in BTC amount = (parsedAmount.amountSat / 100000000).toFixed(8); -console.log('[buyCoinbase.js:52]',amount); //TODO/ currency = 'BTC'; -console.log('[buyCoinbase.js:54]',currency); //TODO/ $scope.amountUnitStr = parsedAmount.amountUnitStr; -console.log('[buyCoinbase.js:57]',$scope.amountUnitStr); //TODO/ - // Fee Normal for a single transaction - var txNormalFeeKB = 450 / 1024; -console.log('[buyCoinbase.js:60]',txNormalFeeKB); //TODO/ - feeService.getCurrentFeeValue(null, 'normal', function(err, feePerKB) { - feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); -console.log('[buyCoinbase.js:60]',feePerKB, feeBTC, amount - feeBTC); //TODO/ - // Check if transaction has enough funds to transfer bitcoin from Coinbase to Copay - if (amount - feeBTC < 0) { - showErrorAndBack('Not enough funds for fee'); - return; - } - }); - - return; // TODO - $scope.network = coinbaseService.getNetwork(); - $scope.wallets = profileService.getWallets({ - onlyComplete: true, - network: $scope.network - }); - - if (lodash.isEmpty($scope.wallets)) { - showErrorAndBack('No wallets available'); - return; - } - $scope.wallet = $scope.wallets[0]; // Default first wallet - - ongoingProcess.set('connectingCoinbase', true); - coinbaseService.init(function(err, res) { + ongoingProcess.set('calculatingFee', true); + coinbaseService.checkEnoughFundsForFee(amount, function(err) { + ongoingProcess.set('calculatingFee', false); if (err) { - ongoingProcess.set('connectingCoinbase', false); showErrorAndBack(err); return; } - var accessToken = res.accessToken; - coinbaseService.buyPrice(accessToken, coinbaseService.getAvailableCurrency(), function(err, b) { - $scope.buyPrice = b.data || null; + $scope.network = coinbaseService.getNetwork(); + $scope.wallets = profileService.getWallets({ + onlyComplete: true, + network: $scope.network }); - $scope.paymentMethods = []; - $scope.selectedPaymentMethodId = { value : null }; - coinbaseService.getPaymentMethods(accessToken, function(err, p) { + if (lodash.isEmpty($scope.wallets)) { + showErrorAndBack('No wallets available'); + return; + } + $scope.wallet = $scope.wallets[0]; // Default first wallet + + ongoingProcess.set('connectingCoinbase', true); + coinbaseService.init(function(err, res) { if (err) { ongoingProcess.set('connectingCoinbase', false); showErrorAndBack(err); return; } + var accessToken = res.accessToken; - var hasPrimary; - var pm; - for(var i = 0; i < p.data.length; i++) { - pm = p.data[i]; - if (pm.allow_buy) { - $scope.paymentMethods.push(pm); + coinbaseService.buyPrice(accessToken, coinbaseService.getAvailableCurrency(), function(err, b) { + $scope.buyPrice = b.data || null; + }); + + $scope.paymentMethods = []; + $scope.selectedPaymentMethodId = { value : null }; + coinbaseService.getPaymentMethods(accessToken, function(err, p) { + if (err) { + ongoingProcess.set('connectingCoinbase', false); + showErrorAndBack(err); + return; } - if (pm.allow_buy && pm.primary_buy) { - hasPrimary = true; - $scope.selectedPaymentMethodId.value = pm.id; + + var hasPrimary; + var pm; + for(var i = 0; i < p.data.length; i++) { + pm = p.data[i]; + if (pm.allow_buy) { + $scope.paymentMethods.push(pm); + } + if (pm.allow_buy && pm.primary_buy) { + hasPrimary = true; + $scope.selectedPaymentMethodId.value = pm.id; + } } - } - if (lodash.isEmpty($scope.paymentMethods)) { - ongoingProcess.set('connectingCoinbase', false); - showErrorAndBack('No payment method available to buy'); - return; - } - if (!hasPrimary) $scope.selectedPaymentMethodId.value = $scope.paymentMethods[0].id; - $scope.buyRequest(); + if (lodash.isEmpty($scope.paymentMethods)) { + ongoingProcess.set('connectingCoinbase', false); + showErrorAndBack('No payment method available to buy'); + return; + } + if (!hasPrimary) $scope.selectedPaymentMethodId.value = $scope.paymentMethods[0].id; + $scope.buyRequest(); + }); }); }); }); @@ -160,7 +150,7 @@ console.log('[buyCoinbase.js:60]',feePerKB, feeBTC, amount - feeBTC); //TODO/ }; $scope.buyConfirm = function() { - var message = 'Buy bitcoin for ' + amount + ' ' + currency; + var message = 'Buy bitcoin for ' + $scope.amountUnitStr; var okText = 'Confirm'; var cancelText = 'Cancel'; popupService.showConfirm(null, message, okText, cancelText, function(ok) { diff --git a/src/js/services/coinbaseService.js b/src/js/services/coinbaseService.js index cce8544de..eea9f5f3a 100644 --- a/src/js/services/coinbaseService.js +++ b/src/js/services/coinbaseService.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('copayApp.services').factory('coinbaseService', function($http, $log, $window, $filter, platformInfo, lodash, storageService, configService, appConfigService, txFormatService, buyAndSellService, $rootScope) { +angular.module('copayApp.services').factory('coinbaseService', function($http, $log, $window, $filter, platformInfo, lodash, storageService, configService, appConfigService, txFormatService, buyAndSellService, $rootScope, feeService) { var root = {}; var credentials = {}; var isCordova = platformInfo.isCordova; @@ -107,6 +107,30 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ }; }; + root.getReductedAmountByFee = function(amount, cb) { + // Fee Normal for a single transaction (450 bytes) + var txNormalFeeKB = 450 / 1000; + feeService.getCurrentFeeValue(null, 'normal', function(err, feePerKB) { + if (err) return cb(err); + var feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); + + return cb(null, amount - feeBTC, feeBTC); + }); + }; + + root.checkEnoughFundsForFee = function(amount, cb) { + root.getReductedAmountByFee(amount, function(err, reductedAmount) { + if (err) return cb(err); + + // Check if transaction has enough funds to transfer bitcoin from Coinbase to Copay + if (reductedAmount < 0) { + return cb('Not enough funds for fee'); + } + + return cb(); + }); + }; + root.getSignupUrl = function() { return credentials.HOST + '/signup'; } @@ -657,13 +681,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) { if (!tx) return; var desc = appConfigService.nameCase + ' Wallet'; - var data = { - to: tx.toAddr, - amount: tx.amount.amount, - currency: tx.amount.currency, - description: desc - }; - root.sendTo(accessToken, accountId, data, function(err, res) { + root.getReductedAmountByFee(tx.amount.amount, function(err, amountBTC, feeBTC) { if (err) { _savePendingTransaction(tx, { status: 'error', @@ -672,8 +690,18 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ if (err) $log.debug(err); _updateTxs(coinbasePendingTransactions); }); - } else { - if (res.data && !res.data.id) { + return; + } + + var data = { + to: tx.toAddr, + amount: amountBTC, + currency: tx.amount.currency, + description: desc, + fee: feeBTC + }; + root.sendTo(accessToken, accountId, data, function(err, res) { + if (err) { _savePendingTransaction(tx, { status: 'error', error: err @@ -681,19 +709,29 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ if (err) $log.debug(err); _updateTxs(coinbasePendingTransactions); }); - return; - } - root.getTransaction(accessToken, accountId, res.data.id, function(err, sendTx) { - _savePendingTransaction(tx, { - remove: true - }, function(err) { - _savePendingTransaction(sendTx.data, {}, function(err) { + } else { + if (res.data && !res.data.id) { + _savePendingTransaction(tx, { + status: 'error', + error: err + }, function(err) { if (err) $log.debug(err); _updateTxs(coinbasePendingTransactions); }); + return; + } + root.getTransaction(accessToken, accountId, res.data.id, function(err, sendTx) { + _savePendingTransaction(tx, { + remove: true + }, function(err) { + _savePendingTransaction(sendTx.data, {}, function(err) { + if (err) $log.debug(err); + _updateTxs(coinbasePendingTransactions); + }); + }); }); - }); - } + } + }); }); }; From b799ed43b193dadebc134d5181ffd6d838ea48d7 Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Thu, 22 Jun 2017 16:49:24 -0300 Subject: [PATCH 3/4] Fix function name --- src/js/services/coinbaseService.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/services/coinbaseService.js b/src/js/services/coinbaseService.js index eea9f5f3a..9161f5301 100644 --- a/src/js/services/coinbaseService.js +++ b/src/js/services/coinbaseService.js @@ -110,7 +110,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ root.getReductedAmountByFee = function(amount, cb) { // Fee Normal for a single transaction (450 bytes) var txNormalFeeKB = 450 / 1000; - feeService.getCurrentFeeValue(null, 'normal', function(err, feePerKB) { + feeService.getFeeRate(null, 'normal', function(err, feePerKB) { if (err) return cb(err); var feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); From 4d23ae62c545c43f268926d90089d7564a67ceae Mon Sep 17 00:00:00 2001 From: Gustavo Maximiliano Cortez Date: Thu, 22 Jun 2017 17:20:55 -0300 Subject: [PATCH 4/4] Fix wording and function name --- src/js/services/coinbaseService.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/js/services/coinbaseService.js b/src/js/services/coinbaseService.js index 9161f5301..81566fa2b 100644 --- a/src/js/services/coinbaseService.js +++ b/src/js/services/coinbaseService.js @@ -107,23 +107,12 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ }; }; - root.getReductedAmountByFee = function(amount, cb) { - // Fee Normal for a single transaction (450 bytes) - var txNormalFeeKB = 450 / 1000; - feeService.getFeeRate(null, 'normal', function(err, feePerKB) { - if (err) return cb(err); - var feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); - - return cb(null, amount - feeBTC, feeBTC); - }); - }; - root.checkEnoughFundsForFee = function(amount, cb) { - root.getReductedAmountByFee(amount, function(err, reductedAmount) { + _getNetAmount(amount, function(err, reducedAmount) { if (err) return cb(err); // Check if transaction has enough funds to transfer bitcoin from Coinbase to Copay - if (reductedAmount < 0) { + if (reducedAmount < 0) { return cb('Not enough funds for fee'); } @@ -177,6 +166,17 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ }); }; + var _getNetAmount = function(amount, cb) { + // Fee Normal for a single transaction (450 bytes) + var txNormalFeeKB = 450 / 1000; + feeService.getFeeRate(null, 'normal', function(err, feePerKB) { + if (err) return cb(err); + var feeBTC = (feePerKB * txNormalFeeKB / 100000000).toFixed(8); + + return cb(null, amount - feeBTC, feeBTC); + }); + }; + var _refreshToken = function(refreshToken, cb) { var req = { method: 'POST', @@ -681,7 +681,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $ var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) { if (!tx) return; var desc = appConfigService.nameCase + ' Wallet'; - root.getReductedAmountByFee(tx.amount.amount, function(err, amountBTC, feeBTC) { + _getNetAmount(tx.amount.amount, function(err, amountBTC, feeBTC) { if (err) { _savePendingTransaction(tx, { status: 'error',