Merge pull request #6283 from cmgustavo/bug/coinbase-02

Coinbase (purchase flow): adds bitcoin fee to create transaction
This commit is contained in:
Gabriel Edgardo Bazán 2017-06-23 10:54:57 -03:00 committed by GitHub
commit 883a7c763c
2 changed files with 111 additions and 62 deletions

View file

@ -47,10 +47,20 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct
data.stateParams.amount,
data.stateParams.currency);
amount = parsedAmount.amount;
currency = parsedAmount.currency;
// Buy always in BTC
amount = (parsedAmount.amountSat / 100000000).toFixed(8);
currency = 'BTC';
$scope.amountUnitStr = parsedAmount.amountUnitStr;
ongoingProcess.set('calculatingFee', true);
coinbaseService.checkEnoughFundsForFee(amount, function(err) {
ongoingProcess.set('calculatingFee', false);
if (err) {
showErrorAndBack(err);
return;
}
$scope.network = coinbaseService.getNetwork();
$scope.wallets = profileService.getWallets({
onlyComplete: true,
@ -107,6 +117,7 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct
});
});
});
});
$scope.buyRequest = function() {
ongoingProcess.set('connectingCoinbase', true);
@ -139,7 +150,7 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct
};
$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) {

View file

@ -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,19 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
};
};
root.checkEnoughFundsForFee = function(amount, cb) {
_getNetAmount(amount, function(err, reducedAmount) {
if (err) return cb(err);
// Check if transaction has enough funds to transfer bitcoin from Coinbase to Copay
if (reducedAmount < 0) {
return cb('Not enough funds for fee');
}
return cb();
});
};
root.getSignupUrl = function() {
return credentials.HOST + '/signup';
}
@ -153,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',
@ -657,11 +681,24 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) {
if (!tx) return;
var desc = appConfigService.nameCase + ' Wallet';
_getNetAmount(tx.amount.amount, function(err, amountBTC, feeBTC) {
if (err) {
_savePendingTransaction(tx, {
status: 'error',
error: err
}, function(err) {
if (err) $log.debug(err);
_updateTxs(coinbasePendingTransactions);
});
return;
}
var data = {
to: tx.toAddr,
amount: tx.amount.amount,
amount: amountBTC,
currency: tx.amount.currency,
description: desc
description: desc,
fee: feeBTC
};
root.sendTo(accessToken, accountId, data, function(err, res) {
if (err) {
@ -695,6 +732,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
});
}
});
});
};
var _updateCoinbasePendingTransactions = function(obj /*, …*/ ) {