Fix buy and sell
This commit is contained in:
parent
4febe719b0
commit
1a586f6fcc
8 changed files with 268 additions and 228 deletions
|
|
@ -72,6 +72,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
|
|||
}
|
||||
|
||||
$scope.coinbasePaymentMethods = [];
|
||||
$scope.coinbaseSelectedPaymentMethodId = { value : null };
|
||||
coinbaseService.getPaymentMethods(data.accessToken, function(err, p) {
|
||||
lodash.each(p.data, function(pm) {
|
||||
if ($scope.isCoinbase == 'sell') {
|
||||
|
|
@ -79,14 +80,14 @@ angular.module('copayApp.controllers').controller('amountController', function($
|
|||
$scope.coinbasePaymentMethods.push(pm);
|
||||
}
|
||||
if (pm.allow_sell && pm.primary_sell) {
|
||||
$scope.coinbaseSelectedPaymentMethod = pm;
|
||||
$scope.coinbaseSelectedPaymentMethodId.value = pm.id;
|
||||
}
|
||||
} else {
|
||||
if (pm.allow_buy) {
|
||||
$scope.coinbasePaymentMethods.push(pm);
|
||||
}
|
||||
if (pm.allow_buy && pm.primary_buy) {
|
||||
$scope.coinbaseSelectedPaymentMethod = pm;
|
||||
$scope.coinbaseSelectedPaymentMethodId.value = pm.id;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
|
@ -398,7 +399,7 @@ angular.module('copayApp.controllers').controller('amountController', function($
|
|||
glideraAccessToken: $scope.glideraAccessToken
|
||||
});
|
||||
} else if ($scope.isCoinbase) {
|
||||
if (lodash.isEmpty($scope.coinbaseSelectedPaymentMethod)) {
|
||||
if (lodash.isEmpty($scope.coinbaseSelectedPaymentMethodId.value)) {
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'No Payment Method Selected');
|
||||
return;
|
||||
}
|
||||
|
|
@ -407,12 +408,17 @@ angular.module('copayApp.controllers').controller('amountController', function($
|
|||
return;
|
||||
}
|
||||
var amountUSD = $scope.showAlternativeAmount ? _amount : $filter('formatFiatAmount')(toFiat(_amount));
|
||||
if (amountUSD < 1) {
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Amount must be at least 1.00 USD');
|
||||
return;
|
||||
}
|
||||
|
||||
var amount = $scope.showAlternativeAmount ? fromFiat(_amount) : _amount;
|
||||
$state.transitionTo('tabs.buyandsell.coinbase.confirm', {
|
||||
toAmount: (amount * unitToSatoshi).toFixed(0),
|
||||
toAddress: $scope.toAddress,
|
||||
isCoinbase: $scope.isCoinbase,
|
||||
coinbasePaymentMethodId: $scope.coinbaseSelectedPaymentMethod.id,
|
||||
coinbasePaymentMethodId: $scope.coinbaseSelectedPaymentMethodId.value,
|
||||
coinbaseAmount: amountUSD,
|
||||
coinbaseAmountCurrency: 'USD'
|
||||
});
|
||||
|
|
|
|||
|
|
@ -26,10 +26,8 @@ angular.module('copayApp.controllers').controller('coinbaseController', function
|
|||
|
||||
$scope.updateTransactions = function() {
|
||||
$log.debug('Getting transactions...');
|
||||
coinbaseService.getPendingTransactions($scope.accessToken, $scope.accountId, function(err, txs) {
|
||||
$scope.pendingTransactions = txs;
|
||||
});
|
||||
|
||||
$scope.pendingTransactions = { data: {} };
|
||||
coinbaseService.getPendingTransactions($scope.pendingTransactions);
|
||||
};
|
||||
|
||||
this.openAuthenticateWindow = function() {
|
||||
|
|
|
|||
|
|
@ -159,7 +159,7 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
if ($scope.paypro) _paymentTimeControl($scope.paypro.expires);
|
||||
|
||||
displayValues();
|
||||
if ($scope.wallets.length > 1) $scope.showWalletSelector();
|
||||
if ($scope.wallets.length > 1 && $scope.isCoinbase != 'buy') $scope.showWalletSelector();
|
||||
else setWallet($scope.wallets[0]);
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
|
|
@ -175,6 +175,8 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
$scope.alternativeAmountStr = $filter('formatFiatAmount')($scope.cardAmountUSD) + ' USD';
|
||||
} else if ($scope.giftCardAmountUSD) {
|
||||
$scope.alternativeAmountStr = $filter('formatFiatAmount')($scope.giftCardAmountUSD) + ' USD';
|
||||
} else if ($scope.coinbaseAmount) {
|
||||
$scope.alternativeAmountStr = $filter('formatFiatAmount')($scope.coinbaseAmount) + ' ' + $scope.coinbaseAmountCurrency;
|
||||
} else {
|
||||
txFormatService.formatAlternativeStr(toAmount, function(v) {
|
||||
$scope.alternativeAmountStr = v;
|
||||
|
|
@ -197,6 +199,10 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
coinbaseService.sellPrice(data.accessToken, $scope.coinbaseAmountCurrency, function(err, sell) {
|
||||
$scope.coinbaseSellPrice = sell.data;
|
||||
});
|
||||
coinbaseService.getPaymentMethod(data.accessToken, $scope.coinbasePaymentMethodId, function(err, data) {
|
||||
if (err) $log.error(err);
|
||||
$scope.coinbasePaymentMethodInfo = data.data;
|
||||
});
|
||||
});
|
||||
}
|
||||
};
|
||||
|
|
@ -567,30 +573,45 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
var accountId = res.accountId;
|
||||
coinbaseService.buyCommit(token, accountId, $scope.coinbaseBuyRequest.id, function(err, b) {
|
||||
if (err) {
|
||||
$log.error(err);
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Could not complete purchase');
|
||||
return;
|
||||
}
|
||||
var tx = b.data.transaction;
|
||||
if (!tx) return;
|
||||
if (!tx) {
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Transaction not found');
|
||||
return;
|
||||
}
|
||||
|
||||
coinbaseService.getTransaction(token, accountId, tx.id, function(err, updatedTx) {
|
||||
if (err) $log.debug(err);
|
||||
walletService.getAddress($scope.wallet, false, function(err, walletAddr) {
|
||||
$timeout(function() {
|
||||
coinbaseService.getTransaction(token, accountId, tx.id, function(err, updatedTx) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Transaction error');
|
||||
return;
|
||||
}
|
||||
updatedTx.data['toAddr'] = walletAddr;
|
||||
coinbaseService.savePendingTransaction(updatedTx.data, {}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
if (updatedTx.data.status == 'completed') {
|
||||
coinbaseSendToCopay(token, accountId, updatedTx.data, onSendStatusChange);
|
||||
} else {
|
||||
walletService.getAddress($scope.wallet, false, function(err, walletAddr) {
|
||||
if (err) {
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), err);
|
||||
return;
|
||||
}
|
||||
updatedTx.data['toAddr'] = walletAddr;
|
||||
updatedTx.data['status'] = 'pending'; // Forcing "pending" status to process later
|
||||
|
||||
$log.debug('Saving transaction to process later...');
|
||||
coinbaseService.savePendingTransaction(updatedTx.data, {}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
$scope.coinbaseBuySuccess = updatedTx.data;
|
||||
}
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
}, 8000);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -610,7 +631,6 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
|
||||
popupService.showConfirm(null, message, okText, cancelText, function(ok) {
|
||||
if (!ok) {
|
||||
$scope.sendStatus = '';
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
});
|
||||
|
|
@ -618,56 +638,13 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
}
|
||||
// SIMULATE publishAndSign
|
||||
//$log.warn('Simulating publishAndSign...');
|
||||
publishAndSign(wallet, txp, onSendStatusChange);
|
||||
|
||||
$timeout(function() {
|
||||
|
||||
coinbaseService.init(function(err, res) {
|
||||
if (err) {
|
||||
$log.error(err);
|
||||
return;
|
||||
}
|
||||
var token = res.accessToken;
|
||||
var accountId = res.accountId;
|
||||
|
||||
coinbaseService.getAddressTransactions(token, accountId, $scope.toAddress, function(err, ctxs) {
|
||||
if (err) {
|
||||
ongoingProcess.clear();
|
||||
$log.debug(err);
|
||||
return;
|
||||
}
|
||||
var coinbaseTransactions = ctxs.data;
|
||||
var txFound = false;
|
||||
var ctx;
|
||||
for(var i = 0; i < coinbaseTransactions.length; i++) {
|
||||
ctx = coinbaseTransactions[i];
|
||||
if (ctx.type == 'send' && ctx.from) {
|
||||
txFound = true;
|
||||
if (ctx.status == 'completed') {
|
||||
coinbaseSellRequest(token, accountId, ctx, onSendStatusChange);
|
||||
} else {
|
||||
$log.debug('Saving transaction to process later');
|
||||
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity.data;
|
||||
ctx['sell_price_amount'] = $scope.coinbaseSellPrice ? $scope.coinbaseSellPrice.amount : '';
|
||||
ctx['sell_price_currency'] = $scope.coinbaseSellPrice ? $scope.coinbaseSellPrice.currency : 'USD';
|
||||
ctx['description'] = $window.appConfig.nameCase + ' Wallet: ' + $scope.wallet.name;
|
||||
$scope.coinbaseSendInfo = ctx;
|
||||
coinbaseService.savePendingTransaction(ctx, null, function(err) {
|
||||
ongoingProcess.set('sellingBitcoin', false, onSendStatusChange);
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!txFound) {
|
||||
// Transaction sent, but can not verify it on Coinbase.com
|
||||
// TODO: improve error message
|
||||
ongoingProcess.clear();
|
||||
return setSendError('Transaction not found. Please, verify if transaction exists on Coinbase.com');
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 5000);
|
||||
$log.debug('Plublish and Sign...');
|
||||
publishAndSign(wallet, txp, function() {}, function(err) {
|
||||
if (err) return setSendError(err);
|
||||
$log.debug('Finished publish and sign. Trying to sell...');
|
||||
var count = 0;
|
||||
checkTransaction(count, txp, onSendStatusChange);
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
|
@ -727,7 +704,7 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
((processName === 'signingTx') && $scope.wallet.m > 1) ||
|
||||
(processName == 'sendingTx' && !$scope.wallet.canSign() && !$scope.wallet.isPrivKeyExternal()) ||
|
||||
(processName == 'buyingBitcoin') ||
|
||||
(processName == 'sellingBitcoin' && ($scope.coinbaseSellRequest || $scope.coinbaseSendInfo))
|
||||
(processName == 'sellingBitcoin')
|
||||
) && !isOn) {
|
||||
$scope.sendStatus = 'success';
|
||||
$timeout(function() {
|
||||
|
|
@ -960,18 +937,26 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
});
|
||||
};
|
||||
|
||||
function publishAndSign(wallet, txp, onSendStatusChange) {
|
||||
function publishAndSign(wallet, txp, onSendStatusChange, cb) {
|
||||
|
||||
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
|
||||
$log.info('No signing proposal: No private key');
|
||||
|
||||
return walletService.onlyPublish(wallet, txp, function(err) {
|
||||
if (err) setSendError(err);
|
||||
walletService.onlyPublish(wallet, txp, function(err) {
|
||||
if (err) {
|
||||
if (cb) return cb(err);
|
||||
else return setSendError(err);
|
||||
}
|
||||
if (cb) return cb();
|
||||
else return;
|
||||
}, onSendStatusChange);
|
||||
}
|
||||
|
||||
walletService.publishAndSign(wallet, txp, function(err, txp) {
|
||||
if (err) return setSendError(err);
|
||||
if (err) {
|
||||
if (cb) return cb(err);
|
||||
else return setSendError(err);
|
||||
}
|
||||
|
||||
var previousView = $ionicHistory.viewHistory().backView && $ionicHistory.viewHistory().backView.stateName;
|
||||
var fromAmazon = previousView.match(/tabs.giftcards.amazon/) ? true : false;
|
||||
|
|
@ -990,8 +975,72 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
ongoingProcess.set('creatingGiftCard', true);
|
||||
debounceCreate(count, dataSrc, onSendStatusChange);
|
||||
}
|
||||
if (cb) return cb();
|
||||
}, onSendStatusChange);
|
||||
}
|
||||
};
|
||||
|
||||
var checkTransaction = lodash.throttle(function(count, txp, onSendStatusChange) {
|
||||
$log.warn('Check if transaction has been received by Coinbase. Try ' + count + '/5');
|
||||
coinbaseService.init(function(err, res) {
|
||||
if (err) {
|
||||
ongoingProcess.set('sellingBitcoin', false, onSendStatusChange);
|
||||
$log.error(err);
|
||||
checkTransaction(count, txp, onSendStatusChange);
|
||||
return;
|
||||
}
|
||||
var token = res.accessToken;
|
||||
var accountId = res.accountId;
|
||||
|
||||
coinbaseService.getTransactions(token, accountId, function(err, ctxs) {
|
||||
if (err) {
|
||||
ongoingProcess.set('sellingBitcoin', false, onSendStatusChange);
|
||||
$log.debug(err);
|
||||
checkTransaction(count, txp, onSendStatusChange);
|
||||
return;
|
||||
}
|
||||
|
||||
// TX amount in BTC
|
||||
var satToBtc = 1 / 100000000;
|
||||
var amountBTC = (txp.amount * satToBtc).toFixed(8);
|
||||
|
||||
var coinbaseTransactions = ctxs.data;
|
||||
var txFound = false;
|
||||
var ctx;
|
||||
for(var i = 0; i < coinbaseTransactions.length; i++) {
|
||||
ctx = coinbaseTransactions[i];
|
||||
if (ctx.type == 'send' && ctx.from && ctx.amount.amount == amountBTC ) {
|
||||
$log.warn('Transaction found!', ctx);
|
||||
txFound = true;
|
||||
$log.debug('Saving transaction to process later...');
|
||||
ctx['payment_method'] = $scope.coinbasePaymentMethodId;
|
||||
ctx['status'] = 'pending'; // Forcing "pending" status to process later
|
||||
ctx['price_sensitivity'] = $scope.selectedPriceSensitivity.data;
|
||||
ctx['sell_price_amount'] = $scope.coinbaseSellPrice ? $scope.coinbaseSellPrice.amount : '';
|
||||
ctx['sell_price_currency'] = $scope.coinbaseSellPrice ? $scope.coinbaseSellPrice.currency : 'USD';
|
||||
ctx['description'] = $window.appConfig.nameCase + ' Wallet: ' + $scope.wallet.name;
|
||||
coinbaseService.savePendingTransaction(ctx, null, function(err) {
|
||||
ongoingProcess.set('sellingBitcoin', false, onSendStatusChange);
|
||||
if (err) $log.debug(err);
|
||||
$scope.coinbaseSendInfo = ctx;
|
||||
$timeout(function() {
|
||||
$scope.$apply();
|
||||
});
|
||||
});
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!txFound) {
|
||||
// Transaction sent, but could not be verified by Coinbase.com
|
||||
$log.warn('Transaction not found in Coinbase.');
|
||||
if (count < 5) {
|
||||
checkTransaction(count + 1, txp, onSendStatusChange);
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
}, 8000, {
|
||||
'leading': true
|
||||
});
|
||||
|
||||
var debounceCreate = lodash.throttle(function(count, dataSrc) {
|
||||
debounceCreateGiftCard(count, dataSrc);
|
||||
|
|
@ -1067,12 +1116,25 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
};
|
||||
coinbaseService.init(function(err, res) {
|
||||
if (err) {
|
||||
$scope.showWallets = null;
|
||||
$log.error(err);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Could not connect to Coinbase', function() {
|
||||
$ionicHistory.goBack();
|
||||
});
|
||||
return;
|
||||
}
|
||||
coinbaseService.getPaymentMethod(res.accessToken, paymentMethodId, function(err, data) {
|
||||
if (err) $log.error(err);
|
||||
$scope.coinbasePaymentMethodInfo = data.data;
|
||||
});
|
||||
|
||||
coinbaseService.buyRequest(res.accessToken, res.accountId, dataSrc, function(err, data) {
|
||||
if (err) {
|
||||
$scope.showWallets = null;
|
||||
$log.error(err);
|
||||
popupService.showAlert(gettextCatalog.getString('Error'), 'Could not create a buy request', function() {
|
||||
$ionicHistory.goBack();
|
||||
});
|
||||
return;
|
||||
}
|
||||
$scope.coinbaseBuyRequest = data.data;
|
||||
|
|
@ -1080,48 +1142,4 @@ angular.module('copayApp.controllers').controller('confirmController', function(
|
|||
});
|
||||
};
|
||||
|
||||
var coinbaseSellRequest = function(accessToken, accountId, ctx, onSendStatusChange) {
|
||||
var dataSrc = ctx.amount;
|
||||
dataSrc['payment_method'] = $scope.coinbasePaymentMethodId || null;
|
||||
dataSrc['commit'] = true;
|
||||
|
||||
coinbaseService.sellRequest(accessToken, accountId, dataSrc, function(err, data) {
|
||||
if (err) {
|
||||
ongoingProcess.clear();
|
||||
$log.error(err);
|
||||
return;
|
||||
}
|
||||
$scope.coinbaseSellRequest = data.data;
|
||||
coinbaseService.savePendingTransaction($scope.coinbaseSellRequest, null, function(err) {
|
||||
ongoingProcess.set('sellingBitcoin', false, onSendStatusChange);
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var coinbaseSendToCopay = function(token, accountId, tx, onSendStatusChange) {
|
||||
var data = {
|
||||
to: tx.toAddr,
|
||||
amount: tx.amount.amount,
|
||||
currency: tx.amount.currency,
|
||||
description: 'Copay Wallet: ' + $scope.wallet.name
|
||||
};
|
||||
coinbaseService.sendTo(token, accountId, data, function(err, res) {
|
||||
if (err) {
|
||||
return;
|
||||
}
|
||||
$scope.coinbaseReceiveInfo = 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) {
|
||||
ongoingProcess.set('buyingBitcoin', false, onSendStatusChange);
|
||||
// TODO
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
});
|
||||
|
|
|
|||
|
|
@ -476,74 +476,90 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
|||
});
|
||||
};
|
||||
|
||||
root.getPendingTransactions = function(accessToken, accountId, cb) {
|
||||
var coinbasePendingTransactions;
|
||||
storageService.getCoinbaseTxs(credentials.NETWORK, function(err, txs) {
|
||||
txs = txs ? JSON.parse(txs) : {};
|
||||
coinbasePendingTransactions = lodash.isEmpty(txs) ? null : txs;
|
||||
lodash.forEach(coinbasePendingTransactions, 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 cb(null, coinbasePendingTransactions);
|
||||
root.getTransaction(accessToken, accountId, txId, function(err, tx) {
|
||||
if (err || lodash.isEmpty(tx)) {
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
return cb(err);
|
||||
}
|
||||
_updateCoinbasePendingTransactions(dataFromStorage, tx.data);
|
||||
coinbasePendingTransactions[txId] = dataFromStorage;
|
||||
if (tx.data.type == 'send' && tx.data.status == 'completed' && tx.data.from) {
|
||||
root.sellPrice(accessToken, dataFromStorage.sell_price_currency, function(err, s) {
|
||||
if (err) {
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
return cb(err);
|
||||
}
|
||||
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) {
|
||||
_sellPending(tx.data, accessToken, accountId);
|
||||
} else {
|
||||
var error = {
|
||||
errors: [{
|
||||
message: 'Price falls over the selected percentage'
|
||||
}]
|
||||
};
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: error
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if (tx.data.type == 'buy' && tx.data.status == 'completed' && tx.data.buy) {
|
||||
_sendToCopay(dataFromStorage, accessToken, accountId);
|
||||
} else {
|
||||
_savePendingTransaction(dataFromStorage, {}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
});
|
||||
}
|
||||
return cb(null, coinbasePendingTransactions);
|
||||
root.getPendingTransactions = function(coinbasePendingTransactions) {
|
||||
root.init(function(err, data) {
|
||||
var accessToken = data.accessToken;
|
||||
var accountId = data.accountId;
|
||||
storageService.getCoinbaseTxs(credentials.NETWORK, function(err, txs) {
|
||||
txs = txs ? JSON.parse(txs) : {};
|
||||
console.log('[coinbaseService.js:484] getCoinbaseTxs',err, txs); //TODO
|
||||
coinbasePendingTransactions.data = lodash.isEmpty(txs) ? null : txs;
|
||||
|
||||
lodash.forEach(coinbasePendingTransactions.data, 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;
|
||||
root.getTransaction(accessToken, accountId, txId, function(err, tx) {
|
||||
console.log('[coinbaseService.js:494] getTransaction to UPDATE from Coinbase',err, tx); //TODO
|
||||
if (err || lodash.isEmpty(tx) || (tx.data && tx.data.error)) {
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: (tx.data && tx.data.error) ? tx.data.error : err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
return;
|
||||
}
|
||||
_updateCoinbasePendingTransactions(dataFromStorage, tx.data);
|
||||
coinbasePendingTransactions.data[txId] = dataFromStorage;
|
||||
if (tx.data.type == 'send' && tx.data.status == 'completed' && tx.data.from) {
|
||||
root.sellPrice(accessToken, dataFromStorage.sell_price_currency, function(err, s) {
|
||||
if (err) {
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
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) {
|
||||
_sellPending(dataFromStorage, accessToken, accountId, coinbasePendingTransactions);
|
||||
} else {
|
||||
var error = {
|
||||
errors: [{
|
||||
message: 'Price falls over the selected percentage'
|
||||
}]
|
||||
};
|
||||
_savePendingTransaction(dataFromStorage, {
|
||||
status: 'error',
|
||||
error: error
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
}
|
||||
});
|
||||
} else if (tx.data.type == 'buy' && tx.data.status == 'completed' && tx.data.buy) {
|
||||
_sendToWallet(dataFromStorage, accessToken, accountId, coinbasePendingTransactions);
|
||||
} else {
|
||||
_savePendingTransaction(dataFromStorage, {}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
}
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
var _sellPending = function(tx, accessToken, accountId) {
|
||||
if (!tx) return;
|
||||
var _updateTxs = function(coinbasePendingTransactions) {
|
||||
storageService.getCoinbaseTxs(credentials.NETWORK, function(err, txs) {
|
||||
txs = txs ? JSON.parse(txs) : {};
|
||||
coinbasePendingTransactions.data = lodash.isEmpty(txs) ? null : txs;
|
||||
});
|
||||
};
|
||||
|
||||
var _sellPending = function(tx, accessToken, accountId, coinbasePendingTransactions) {
|
||||
var data = tx.amount;
|
||||
data['payment_method'] = tx.payment_method || null;
|
||||
data['commit'] = true;
|
||||
root.sellRequest(accessToken, accountId, data, function(err, res) {
|
||||
if (err) {
|
||||
|
|
@ -552,14 +568,16 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
|||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
} else {
|
||||
if (!res.data.transaction) {
|
||||
if (res.data && !res.data.transaction) {
|
||||
_savePendingTransaction(tx, {
|
||||
status: 'error',
|
||||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
|
@ -569,6 +587,7 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
|||
root.getTransaction(accessToken, accountId, res.data.transaction.id, function(err, updatedTx) {
|
||||
_savePendingTransaction(updatedTx.data, {}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
@ -576,38 +595,43 @@ angular.module('copayApp.services').factory('coinbaseService', function($http, $
|
|||
});
|
||||
};
|
||||
|
||||
var _sendToCopay = function(tx, accessToken, accountId) {
|
||||
var _sendToWallet = function(tx, accessToken, accountId, coinbasePendingTransactions) {
|
||||
if (!tx) return;
|
||||
var data = {
|
||||
to: tx.toAddr,
|
||||
amount: tx.amount.amount,
|
||||
currency: tx.amount.currency,
|
||||
description: 'To Copay Wallet'
|
||||
description: 'To Wallet'
|
||||
};
|
||||
root.sendTo(accessToken, accountId, data, function(err, res) {
|
||||
console.log('[coinbaseService.js:591] SEND TO COPAY',err, res); //TODO
|
||||
if (err) {
|
||||
_savePendingTransaction(tx, {
|
||||
status: 'error',
|
||||
error: err
|
||||
}, function(err) {
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
} else {
|
||||
if (!res.data.id) {
|
||||
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) {
|
||||
console.log('[coinbaseService.js:633] GET UPDATED TX', err, sendTx); //TODO
|
||||
_savePendingTransaction(tx, {
|
||||
remove: true
|
||||
}, function(err) {
|
||||
_savePendingTransaction(sendTx.data, {}, function(err) {
|
||||
// TODO
|
||||
if (err) $log.debug(err);
|
||||
_updateTxs(coinbasePendingTransactions);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
|||
|
|
@ -74,7 +74,7 @@
|
|||
<span ng-show="isCoinbase == 'buy'">Payment Method</span>
|
||||
<span ng-show="isCoinbase == 'sell'">Deposit into</span>
|
||||
</div>
|
||||
<select ng-model="coinbaseSelectedPaymentMethod.id"
|
||||
<select ng-model="coinbaseSelectedPaymentMethodId.value"
|
||||
ng-options="item.id as item.name for item in coinbasePaymentMethods">
|
||||
</select>
|
||||
</label>
|
||||
|
|
|
|||
|
|
@ -86,8 +86,8 @@
|
|||
Activity
|
||||
</div>
|
||||
<a class="item"
|
||||
ng-if="pendingTransactions && !error"
|
||||
ng-repeat="(id, tx) in pendingTransactions | orderObjectBy:'updated_at':true track by $index"
|
||||
ng-if="pendingTransactions.data && !error"
|
||||
ng-repeat="(id, tx) in pendingTransactions.data | orderObjectBy:'updated_at':true track by $index"
|
||||
ng-click="coinbase.openTxModal(tx)">
|
||||
|
||||
<span class="item-note">
|
||||
|
|
|
|||
|
|
@ -46,19 +46,18 @@
|
|||
will be sent to your Coinbase account, and sold when Coinbase accepts the transaction (usually one
|
||||
hour).
|
||||
</div>
|
||||
<div>
|
||||
<div>
|
||||
Estimated sale value:
|
||||
<span ng-if="coinbaseSellPrice">
|
||||
{{coinbaseSellPrice.amount * coinbaseAmountBTC | currency : 'USD ' : 2}}
|
||||
</span>
|
||||
</div>
|
||||
Still sell if price fall until:
|
||||
<span ng-if="coinbaseSellPrice">
|
||||
{{(coinbaseSellPrice.amount - (selectedPriceSensitivity.data.value / 100) *
|
||||
coinbaseSellPrice.amount) * coinbaseAmountBTC | currency : 'USD ' : 2}}
|
||||
</span>
|
||||
</div>
|
||||
<span class="label" ng-if="coinbaseSellPrice">Estimated sale value:
|
||||
{{coinbaseSellPrice.amount * coinbaseAmountBTC | currency : 'USD ' : 2}}
|
||||
</span>
|
||||
<span class="label" ng-if="coinbaseSellPrice">Still sell if price fall until:
|
||||
{{(coinbaseSellPrice.amount - (selectedPriceSensitivity.data.value / 100) *
|
||||
coinbaseSellPrice.amount) * coinbaseAmountBTC | currency : 'USD ' : 2}}
|
||||
</span>
|
||||
</div>
|
||||
<div class="item single-line" ng-if="isCoinbase && coinbasePaymentMethodInfo">
|
||||
<span class="label" ng-show="isCoinbase == 'buy'">Payment Method</span>
|
||||
<span class="label" ng-show="isCoinbase == 'sell'">Deposit into</span>
|
||||
<span class="item-note">{{coinbasePaymentMethodInfo.name}}</span>
|
||||
</div>
|
||||
<div class="item">
|
||||
<span class="label" ng-if="!(isGlidera || isCoinbase)" translate>To</span>
|
||||
|
|
@ -147,15 +146,19 @@
|
|||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="size-12 m10" ng-show="isCoinbase == 'buy'">
|
||||
<div>
|
||||
Subtotal: {{coinbaseBuyRequest.subtotal.amount}} {{coinbaseBuyRequest.subtotal.currency}}
|
||||
<div ng-show="isCoinbase == 'buy'">
|
||||
<div class="item single-line">
|
||||
<span class="label">Subtotal</span>
|
||||
<span class="item-note">{{coinbaseBuyRequest.subtotal.amount}}
|
||||
{{coinbaseBuyRequest.subtotal.currency}}</span>
|
||||
</div>
|
||||
<div>
|
||||
Total: {{coinbaseBuyRequest.total.amount}} {{coinbaseBuyRequest.total.currency}}
|
||||
<div class="item single-line">
|
||||
<span class="label">Total</span>
|
||||
<span class="item-note">{{coinbaseBuyRequest.total.amount}} {{coinbaseBuyRequest.total.currency}}</span>
|
||||
</div>
|
||||
<div>
|
||||
Payout at: {{coinbaseBuyRequest.payout_at | amCalendar}}
|
||||
<div class="item single-line">
|
||||
<span class="label">Payout at</span>
|
||||
<span class="item-note">{{coinbaseBuyRequest.payout_at | amCalendar}}</span>
|
||||
</div>
|
||||
</div>
|
||||
<div class="text-center" ng-show="noMatchingWallet">
|
||||
|
|
@ -191,7 +194,6 @@
|
|||
slide-success-hide-on-confirm="true">
|
||||
<span ng-show="isCoinbase == 'buy'">Bought</span>
|
||||
<span ng-show="isCoinbase == 'sell' && coinbaseSendInfo">Funds sent to Coinbase Account</span>
|
||||
<span ng-show="isCoinbase == 'sell' && coinbaseSellRequest">Sale initiated</span>
|
||||
<div ng-show="!isCoinbase">
|
||||
<span ng-show="wallet.m == 1 && (wallet.canSign() || wallet.isPrivKeyExternal())" translate>Payment Sent</span>
|
||||
<span ng-show="wallet.m > 1 && (wallet.canSign() || wallet.isPrivKeyExternal())" translate>Proposal Created</span>
|
||||
|
|
@ -204,18 +206,12 @@
|
|||
<div ng-show="isCoinbase" class="glidera-success">
|
||||
<span ng-show="isCoinbase == 'buy'">
|
||||
<span ng-show="coinbaseBuySuccess">
|
||||
Bitcoin purchase completed. Coinbase has queued the transfer to your selected Copay wallet
|
||||
</span>
|
||||
<span ng-show="coinbaseReceiveInfo">
|
||||
Buy confirmed. Funds will be send soon to your selected wallet
|
||||
Bitcoin purchase completed. Coinbase has queued the transfer to your selected wallet
|
||||
</span>
|
||||
</span>
|
||||
<span ng-show="isCoinbase == 'sell'">
|
||||
<span ng-show="coinbaseSendInfo">
|
||||
The transaction is not yet confirmed, and will show as "Processing" in your Activity. The bitcoin sale will be completed automatically once it is confirmed by Coinbase.
|
||||
</span>
|
||||
<span ng-show="coinbaseSellRequest">
|
||||
A transfer has been initiated to your bank account and should arrive at {{coinbaseSellRequest.payout_at | amCalendar}}.
|
||||
The transaction is not yet confirmed, and will show as "Pending" in your Activity. The bitcoin sale will be completed automatically once it is confirmed by Coinbase.
|
||||
</span>
|
||||
</span>
|
||||
</div>
|
||||
|
|
|
|||
|
|
@ -40,11 +40,9 @@
|
|||
</div>
|
||||
</div>
|
||||
|
||||
<div class="box-notification error" ng-show="tx.error">
|
||||
<ul class="card list">
|
||||
<li class="item" ng-repeat="err in tx.error.errors" ng-bind-html="err.message"></li>
|
||||
</ul>
|
||||
</div>
|
||||
<ul class="card list" ng-show="tx.error">
|
||||
<li class="item" ng-repeat="err in tx.error.errors" ng-bind-html="err.message"></li>
|
||||
</ul>
|
||||
|
||||
<ul class="list">
|
||||
<li ng-show="tx.details && tx.status != 'pending'" class="item">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue