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';
|
'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 amount;
|
||||||
var currency;
|
var currency;
|
||||||
var feeBTC;
|
|
||||||
|
|
||||||
var showErrorAndBack = function(err) {
|
var showErrorAndBack = function(err) {
|
||||||
$scope.sendStatus = '';
|
$scope.sendStatus = '';
|
||||||
|
|
@ -47,84 +46,75 @@ angular.module('copayApp.controllers').controller('buyCoinbaseController', funct
|
||||||
var parsedAmount = txFormatService.parseAmount(
|
var parsedAmount = txFormatService.parseAmount(
|
||||||
data.stateParams.amount,
|
data.stateParams.amount,
|
||||||
data.stateParams.currency);
|
data.stateParams.currency);
|
||||||
console.log('[buyCoinbase.js:46]',parsedAmount); //TODO/
|
|
||||||
|
|
||||||
// Buy always in BTC
|
// Buy always in BTC
|
||||||
amount = (parsedAmount.amountSat / 100000000).toFixed(8);
|
amount = (parsedAmount.amountSat / 100000000).toFixed(8);
|
||||||
console.log('[buyCoinbase.js:52]',amount); //TODO/
|
|
||||||
currency = 'BTC';
|
currency = 'BTC';
|
||||||
console.log('[buyCoinbase.js:54]',currency); //TODO/
|
|
||||||
|
|
||||||
$scope.amountUnitStr = parsedAmount.amountUnitStr;
|
$scope.amountUnitStr = parsedAmount.amountUnitStr;
|
||||||
console.log('[buyCoinbase.js:57]',$scope.amountUnitStr); //TODO/
|
|
||||||
|
|
||||||
// Fee Normal for a single transaction
|
ongoingProcess.set('calculatingFee', true);
|
||||||
var txNormalFeeKB = 450 / 1024;
|
coinbaseService.checkEnoughFundsForFee(amount, function(err) {
|
||||||
console.log('[buyCoinbase.js:60]',txNormalFeeKB); //TODO/
|
ongoingProcess.set('calculatingFee', false);
|
||||||
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) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ongoingProcess.set('connectingCoinbase', false);
|
|
||||||
showErrorAndBack(err);
|
showErrorAndBack(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
var accessToken = res.accessToken;
|
|
||||||
|
|
||||||
coinbaseService.buyPrice(accessToken, coinbaseService.getAvailableCurrency(), function(err, b) {
|
$scope.network = coinbaseService.getNetwork();
|
||||||
$scope.buyPrice = b.data || null;
|
$scope.wallets = profileService.getWallets({
|
||||||
|
onlyComplete: true,
|
||||||
|
network: $scope.network
|
||||||
});
|
});
|
||||||
|
|
||||||
$scope.paymentMethods = [];
|
if (lodash.isEmpty($scope.wallets)) {
|
||||||
$scope.selectedPaymentMethodId = { value : null };
|
showErrorAndBack('No wallets available');
|
||||||
coinbaseService.getPaymentMethods(accessToken, function(err, p) {
|
return;
|
||||||
|
}
|
||||||
|
$scope.wallet = $scope.wallets[0]; // Default first wallet
|
||||||
|
|
||||||
|
ongoingProcess.set('connectingCoinbase', true);
|
||||||
|
coinbaseService.init(function(err, res) {
|
||||||
if (err) {
|
if (err) {
|
||||||
ongoingProcess.set('connectingCoinbase', false);
|
ongoingProcess.set('connectingCoinbase', false);
|
||||||
showErrorAndBack(err);
|
showErrorAndBack(err);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
var accessToken = res.accessToken;
|
||||||
|
|
||||||
var hasPrimary;
|
coinbaseService.buyPrice(accessToken, coinbaseService.getAvailableCurrency(), function(err, b) {
|
||||||
var pm;
|
$scope.buyPrice = b.data || null;
|
||||||
for(var i = 0; i < p.data.length; i++) {
|
});
|
||||||
pm = p.data[i];
|
|
||||||
if (pm.allow_buy) {
|
$scope.paymentMethods = [];
|
||||||
$scope.paymentMethods.push(pm);
|
$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;
|
var hasPrimary;
|
||||||
$scope.selectedPaymentMethodId.value = pm.id;
|
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)) {
|
||||||
if (lodash.isEmpty($scope.paymentMethods)) {
|
ongoingProcess.set('connectingCoinbase', false);
|
||||||
ongoingProcess.set('connectingCoinbase', false);
|
showErrorAndBack('No payment method available to buy');
|
||||||
showErrorAndBack('No payment method available to buy');
|
return;
|
||||||
return;
|
}
|
||||||
}
|
if (!hasPrimary) $scope.selectedPaymentMethodId.value = $scope.paymentMethods[0].id;
|
||||||
if (!hasPrimary) $scope.selectedPaymentMethodId.value = $scope.paymentMethods[0].id;
|
$scope.buyRequest();
|
||||||
$scope.buyRequest();
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
@ -160,7 +150,7 @@ console.log('[buyCoinbase.js:60]',feePerKB, feeBTC, amount - feeBTC); //TODO/
|
||||||
};
|
};
|
||||||
|
|
||||||
$scope.buyConfirm = function() {
|
$scope.buyConfirm = function() {
|
||||||
var message = 'Buy bitcoin for ' + amount + ' ' + currency;
|
var message = 'Buy bitcoin for ' + $scope.amountUnitStr;
|
||||||
var okText = 'Confirm';
|
var okText = 'Confirm';
|
||||||
var cancelText = 'Cancel';
|
var cancelText = 'Cancel';
|
||||||
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
|
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
'use strict';
|
'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 root = {};
|
||||||
var credentials = {};
|
var credentials = {};
|
||||||
var isCordova = platformInfo.isCordova;
|
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() {
|
root.getSignupUrl = function() {
|
||||||
return credentials.HOST + '/signup';
|
return credentials.HOST + '/signup';
|
||||||
}
|
}
|
||||||
|
|
@ -657,13 +681,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
||||||
var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) {
|
var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) {
|
||||||
if (!tx) return;
|
if (!tx) return;
|
||||||
var desc = appConfigService.nameCase + ' Wallet';
|
var desc = appConfigService.nameCase + ' Wallet';
|
||||||
var data = {
|
root.getReductedAmountByFee(tx.amount.amount, function(err, amountBTC, feeBTC) {
|
||||||
to: tx.toAddr,
|
|
||||||
amount: tx.amount.amount,
|
|
||||||
currency: tx.amount.currency,
|
|
||||||
description: desc
|
|
||||||
};
|
|
||||||
root.sendTo(accessToken, accountId, data, function(err, res) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
_savePendingTransaction(tx, {
|
_savePendingTransaction(tx, {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
|
|
@ -672,8 +690,18 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
||||||
if (err) $log.debug(err);
|
if (err) $log.debug(err);
|
||||||
_updateTxs(coinbasePendingTransactions);
|
_updateTxs(coinbasePendingTransactions);
|
||||||
});
|
});
|
||||||
} else {
|
return;
|
||||||
if (res.data && !res.data.id) {
|
}
|
||||||
|
|
||||||
|
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, {
|
_savePendingTransaction(tx, {
|
||||||
status: 'error',
|
status: 'error',
|
||||||
error: err
|
error: err
|
||||||
|
|
@ -681,19 +709,29 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
||||||
if (err) $log.debug(err);
|
if (err) $log.debug(err);
|
||||||
_updateTxs(coinbasePendingTransactions);
|
_updateTxs(coinbasePendingTransactions);
|
||||||
});
|
});
|
||||||
return;
|
} else {
|
||||||
}
|
if (res.data && !res.data.id) {
|
||||||
root.getTransaction(accessToken, accountId, res.data.id, function(err, sendTx) {
|
_savePendingTransaction(tx, {
|
||||||
_savePendingTransaction(tx, {
|
status: 'error',
|
||||||
remove: true
|
error: err
|
||||||
}, function(err) {
|
}, function(err) {
|
||||||
_savePendingTransaction(sendTx.data, {}, function(err) {
|
|
||||||
if (err) $log.debug(err);
|
if (err) $log.debug(err);
|
||||||
_updateTxs(coinbasePendingTransactions);
|
_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);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
}
|
||||||
}
|
});
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue