Adds fee to transaction
This commit is contained in:
parent
04db3f9159
commit
dfd3eeec54
2 changed files with 106 additions and 78 deletions
|
|
@ -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,31 +46,21 @@ 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');
|
||||
ongoingProcess.set('calculatingFee', true);
|
||||
coinbaseService.checkEnoughFundsForFee(amount, function(err) {
|
||||
ongoingProcess.set('calculatingFee', false);
|
||||
if (err) {
|
||||
showErrorAndBack(err);
|
||||
return;
|
||||
}
|
||||
});
|
||||
|
||||
return; // TODO
|
||||
$scope.network = coinbaseService.getNetwork();
|
||||
$scope.wallets = profileService.getWallets({
|
||||
onlyComplete: true,
|
||||
|
|
@ -128,6 +117,7 @@ console.log('[buyCoinbase.js:60]',feePerKB, feeBTC, amount - feeBTC); //TODO/
|
|||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
$scope.buyRequest = function() {
|
||||
ongoingProcess.set('connectingCoinbase', true);
|
||||
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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,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';
|
||||
root.getReductedAmountByFee(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 /*, …*/ ) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue