Glidera sell flow

This commit is contained in:
Gabriel Bazán 2016-12-05 10:27:53 -03:00
parent a76dfb110d
commit 0bf4ce58b4
4 changed files with 169 additions and 26 deletions

View File

@ -133,6 +133,7 @@ angular.module('copayApp.controllers').controller('confirmController', function(
$scope.alternativeAmountStr = v;
});
if ($scope.isGlidera && $scope.glideraBuy) $scope.getBuyPrice();
if ($scope.isGlidera && $scope.glideraSell) $scope.getSellPrice();
};
function resetValues() {
@ -241,7 +242,7 @@ angular.module('copayApp.controllers').controller('confirmController', function(
});
$scope.showWalletSelector = function() {
$scope.walletSelectorTitle = !$scope.isGlidera && !$scope.buyGlidera ? 'Send From' : 'Receive in';
$scope.walletSelectorTitle = $scope.glideraBuy ? 'Receive in' : $scope.glideraSell ? 'Sell From' : 'Send from';
if (!$scope.useSendMax && ($scope.insufficientFunds || $scope.noMatchingWallet)) return;
$scope.showWallets = true;
};
@ -434,6 +435,11 @@ angular.module('copayApp.controllers').controller('confirmController', function(
return;
}
var wallet = $scope.wallet;
if (!wallet) {
return setSendError(gettextCatalog.getString('No wallet selected'));
}
if ($scope.isGlidera) {
$scope.get2faCode(function(err, sent) {
if (err) {
@ -444,27 +450,36 @@ angular.module('copayApp.controllers').controller('confirmController', function(
var title = gettextCatalog.getString("Please, enter the code below");
var message = gettextCatalog.getString("A SMS containing a confirmation code was sent to your phone.");
popupService.showPrompt(title, message, null, function(twoFaCode) {
$scope.sendRequest(twoFaCode, function(err, data) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
$scope.sendStatus = 'success';
$timeout(function() {
$scope.$digest();
});
})
if ($scope.glideraBuy) {
$scope.buyRequest(wallet, twoFaCode, function(err, data) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
$scope.sendStatus = 'success';
$timeout(function() {
$scope.$digest();
});
})
}
if ($scope.glideraSell) {
$scope.sellRequest(wallet, twoFaCode, function(err, data) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err);
return;
}
$scope.sendStatus = 'success';
$timeout(function() {
$scope.$digest();
});
})
}
});
}
});
return;
}
var wallet = $scope.wallet;
if (!wallet) {
return setSendError(gettextCatalog.getString('No wallet selected'));
}
if (!wallet.canSign() && !wallet.isPrivKeyExternal()) {
$log.info('No signing proposal: No private key');
@ -593,10 +608,10 @@ angular.module('copayApp.controllers').controller('confirmController', function(
}, 100);
};
$scope.sendRequest = function(twoFaCode, cb) {
$scope.buyRequest = function(wallet, twoFaCode, cb) {
ongoingProcess.set('Buying Bitcoin...', true);
$timeout(function() {
walletService.getAddress($scope.wallet, false, function(err, walletAddr) {
walletService.getAddress(wallet, false, function(err, walletAddr) {
if (err) {
ongoingProcess.set('Buying Bitcoin...', false);
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.cb(err, 'Could not create address'));
@ -611,12 +626,107 @@ angular.module('copayApp.controllers').controller('confirmController', function(
};
glideraService.buy($scope.glideraAccessToken, twoFaCode, data, function(err, data) {
ongoingProcess.set('Buying Bitcoin...', false);
return cb(err, data)
return cb(err, data);
});
});
}, 100);
};
$scope.sellRequest = function(wallet, twoFaCode, cb) {
var outputs = [];
var config = configService.getSync();
var configWallet = config.wallet;
var walletSettings = configWallet.settings;
ongoingProcess.set('creatingTx', true);
walletService.getAddress(wallet, null, function(err, refundAddress) {
if (!refundAddress) {
ongoingProcess.clear();
popupService.showAlert(gettextCatalog.getString('Error'), bwcError.msg(err, 'Could not create address'));
return;
}
glideraService.getSellAddress($scope.glideraAccessToken, function(err, sellAddress) {
if (!sellAddress || err) {
ongoingProcess.clear();
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get the destination bitcoin address'));
return;
}
var amount = parseInt(($scope.sellPrice.qty * 100000000).toFixed(0));
var comment = 'Glidera transaction';
outputs.push({
'toAddress': sellAddress,
'amount': amount,
'message': comment
});
var txp = {
toAddress: sellAddress,
amount: amount,
outputs: outputs,
message: comment,
payProUrl: null,
excludeUnconfirmedUtxos: configWallet.spendUnconfirmed ? false : true,
feeLevel: walletSettings.feeLevel || 'normal',
customData: {
'glideraToken': $scope.glideraAccessToken
}
};
walletService.createTx(wallet, txp, function(err, createdTxp) {
ongoingProcess.clear();
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
return;
}
walletService.prepare(wallet, function(err, password) {
if (err) {
ongoingProcess.clear();
popupService.showAlert(gettextCatalog.getString('Error'), err.message || bwcError.msg(err));
return;
}
ongoingProcess.set('signingTx', true);
walletService.publishTx(wallet, createdTxp, function(err, publishedTxp) {
if (err) {
ongoingProcess.clear();
popupService.showAlert(gettextCatalog.getString('Error'), err.message ||  bwcError.msg(err));
return;
}
walletService.signTx(wallet, publishedTxp, password, function(err, signedTxp) {
if (err) {
ongoingProcess.clear();
popupService.showAlert(gettextCatalog.getString('Error'), err.message ||  bwcError.msg(err));
walletService.removeTx(wallet, signedTxp, function(err) {
if (err) $log.debug(err);
});
return;
}
var rawTx = signedTxp.raw;
var data = {
refundAddress: refundAddress,
signedTransaction: rawTx,
priceUuid: $scope.sellPrice.priceUuid,
useCurrentPrice: $scope.sellPrice.priceUuid ? false : true,
ip: null
};
ongoingProcess.set('Selling Bitcoin', true);
glideraService.sell($scope.glideraAccessToken, twoFaCode, data, function(err, data) {
ongoingProcess.clear();
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), err.message ||  bwcError.msg(err));
return;
}
return cb(err, data)
});
});
});
});
});
});
});
}
$scope.getBuyPrice = function() {
var satToBtc = 1 / 100000000;
var price = {};
@ -630,6 +740,20 @@ angular.module('copayApp.controllers').controller('confirmController', function(
});
};
$scope.getSellPrice = function() {
var satToBtc = 1 / 100000000;
var price = {};
price.qty = (toAmount * satToBtc).toFixed(8);
glideraService.sellPrice($scope.glideraAccessToken, price, function(err, sellPrice) {
if (err) {
popupService.showAlert(gettextCatalog.getString('Error'), gettextCatalog.getString('Could not get exchange information. Please, try again'));
return;
}
$scope.sellPrice = sellPrice;
});
};
function publishAndSign(wallet, txp, onSendStatusChange) {
walletService.publishAndSign(wallet, txp, function(err, txp) {
if (err) return setSendError(err);

View File

@ -18,9 +18,11 @@ angular.module('copayApp.controllers').controller('glideraController',
$scope.status = null;
$scope.limits = null;
$scope.connectingGlidera = true;
ongoingProcess.set('connectingGlidera', true);
glideraService.init($scope.token, function(err, glidera) {
ongoingProcess.set('connectingGlidera');
$scope.connectingGlidera = false;
if (err || !glidera) {
if (err) popupService.showAlert(gettextCatalog.getString('Error'), err);
return;

View File

@ -15,6 +15,7 @@
<span translate ng-if="!useSendMax && !isGlidera">Sending</span>
<span translate ng-if="useSendMax">Sending maximum amount</span>
<span ng-if="isGlidera && glideraBuy">Buying</span>
<span ng-if="isGlidera && glideraSell">Selling</span>
</div>
<div class="amount-label">
<div class="amount">{{displayAmount || '...'}} <span class="unit">{{displayUnit}}</span></div>
@ -77,7 +78,7 @@
{{fee || '...'}}
</span>
</div>
<div class="item" ng-show="isGlidera">
<div class="item" ng-show="isGlidera && glideraBuy">
<span class="label">Information</span>
<div class="glidera-explanation">
<div class="glidera-description" ng-show="buyPrice.qty">
@ -91,6 +92,20 @@
</div>
</div>
</div>
<div class="item" ng-show="isGlidera && glideraSell">
<span class="label">Information</span>
<div class="glidera-explanation">
<div class="glidera-description" ng-show="sellPrice.qty">
Sell {{sellPrice.subtotal|currency:'':2}} {{sellPrice.currency}} in Bitcoin at {{sellPrice.price|currency:'':2}} {{sellPrice.currency}}/BTC
</div>
<div class="glidera-description">
Fiat will be deposited in your bank account in 4-6 business days.
</div>
<div class="glidera-description">
Bitcoins will be immediately sent from your "{{wallet.name || '...' }}" wallet to Glidera.
</div>
</div>
</div>
<div class="text-center" ng-show="noMatchingWallet">
<span class="badge badge-energized" translate>No wallets available</span>
</div>
@ -122,8 +137,10 @@
<span ng-hide="wallet.m > 1">Payment Sent</span>
<span ng-show="wallet.m > 1">Proposal Created</span>
<div ng-show="isGlidera" class="glidera-success">
<span>A transfer has been initiated from your bank account</span>
<span>Your bitcoins should arrive to your wallet in 2-4 business day</span>
<span ng-show="glideraBuy">A transfer has been initiated from your bank account</span>
<span ng-show="glideraBuy">your bitcoins should arrive to your wallet in 2-4 business day</span>
<span ng-show="glideraSell">A transfer has been initiated to your bank account</span>
<span ng-show="glideraSell">should arrive in 4-6 business days</span>
</div>
</slide-to-accept-success>

View File

@ -7,15 +7,15 @@
<ion-content>
<div class="box-notification error m0" ng-show="!network">
<div class="box-notification error m0" ng-show="!network && !connectingGlidera">
Glidera is disabled for this application
</div>
<div class="box-notification warning m0" ng-show="network == 'testnet'">
<div class="box-notification warning m0" ng-show="network == 'testnet' && !connectingGlidera">
Testnet wallets only work with Glidera Sandbox Accounts
</div>
<div ng-if="!token">
<div ng-if="!token && !connectingGlidera">
<div ng-init="showOauthForm = false">
<div class="text-center m20v">
@ -56,7 +56,7 @@
</div>
</div>
<div ng-if="token">
<div ng-if="token && !connectingGlidera">
<div class="text-center m20v">
<img src="img/glidera-logo.png" ng-click="update({'fullUpdate': true})" width="200">
</div>