diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index db1bb52fe..c8b7722f9 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -217,7 +217,7 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, }; function goBack() { - if (vm.shapeshiftOrderId) { + if (vm.thirdParty && vm.thirdParty.id === 'shapeshift') { $state.go('tabs.send').then(function() { $ionicHistory.clearHistory(); $state.go('tabs.home').then(function() { @@ -483,29 +483,6 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, if (!confirmData.fromWalletId) { $state.transitionTo('tabs.paymentRequest.confirm', confirmData); } else { - - if (vm.shapeshiftOrderId) { - var shapeshiftOrderUrl = 'https://www.shapeshift.io/#/status/'; - shapeshiftOrderUrl += vm.shapeshiftOrderId; - confirmData.description = shapeshiftOrderUrl; - - if (confirmData.sendMax) { - var wallet = lodash.find(profileService.getWallets({ coin: coin }), - function(w) { - return w.id == passthroughParams.fromWalletId; - }); - - var balance = parseFloat(wallet.cachedBalance.substring(0, wallet.cachedBalance.length-4)); - if (balance < vm.minAmount * 1.04) { - confirmData.sendMax = false; - confirmData.amount = vm.minAmount * unitToSatoshi; - } else if (balance > vm.maxAmount) { - confirmData.sendMax = false; - confirmData.amount = vm.maxAmount * unitToSatoshi * 0.99; - } - } - } - $state.transitionTo('tabs.send.review', confirmData); $scope.useSendMax = null; } diff --git a/src/js/controllers/review.controller.js b/src/js/controllers/review.controller.js index 3143fabcf..13f6e6cb1 100644 --- a/src/js/controllers/review.controller.js +++ b/src/js/controllers/review.controller.js @@ -4,7 +4,7 @@ angular .module('copayApp.controllers') .controller('reviewController', reviewController); -function reviewController(addressbookService, bitcoinCashJsService, bitcore, bitcoreCash, bwcError, configService, feeService, gettextCatalog, $ionicHistory, $ionicLoading, $ionicModal, lodash, $log, ongoingProcess, platformInfo, popupService, profileService, $scope, soundService, $state, $timeout, txConfirmNotification, txFormatService, walletService) { +function reviewController(addressbookService, bitcoinCashJsService, bitcore, bitcoreCash, bwcError, configService, feeService, gettextCatalog, $ionicHistory, $ionicLoading, $ionicModal, lodash, $log, ongoingProcess, platformInfo, popupService, profileService, $scope, shapeshiftService, soundService, $state, $timeout, txConfirmNotification, txFormatService, walletService) { var vm = this; vm.buttonText = ''; @@ -82,7 +82,20 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit if (!vm.thirdParty.data) { vm.thirdParty.data = {}; } - vm.thirdParty.data['fromWalletId'] = vm.fromWalletId; + + var toWallet = profileService.getWallet(data.stateParams.toWalletId); + $ionicLoading.show(); + walletService.getAddress(vm.originWallet, false, function onWalletAddress(err, returnAddr) { + walletService.getAddress(toWallet, false, function onWalletAddress(err, withdrawalAddr) { + $ionicLoading.hide(); + shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, function(shapeshiftData) { + vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/'+shapeshiftData.orderId; + toAddress = shapeshiftData.toAddress; + vm.destination.address = toAddress; + vm.destination.kind = 'shapeshift'; + }); + }); + }); } if (vm.thirdParty.id === 'bip70') { if (vm.thirdParty.memo) { @@ -115,8 +128,6 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit if (vm.thirdParty.id === 'shapeshift') { shapeshiftService.shiftIt(); return; - } - if (!tx || !vm.originWallet) return; if ($scope.paymentExpired) { diff --git a/src/js/controllers/walletSelectorController.js b/src/js/controllers/walletSelectorController.js index b251da987..824d0e6ca 100644 --- a/src/js/controllers/walletSelectorController.js +++ b/src/js/controllers/walletSelectorController.js @@ -103,6 +103,7 @@ angular.module('copayApp.controllers').controller('walletSelectorController', fu function handleThirdPartyIfShapeshift() { if ($scope.thirdParty.id === 'shapeshift' && $scope.type === 'destination') { // Shapeshift wants to know the + $scope.coin = profileService.getWallet(fromWalletId).coin; if ($scope.coin === 'bch') { $scope.coin = 'btc'; } else { diff --git a/src/js/services/shapeshiftService.js b/src/js/services/shapeshiftService.js index 317e72394..b3e307667 100644 --- a/src/js/services/shapeshiftService.js +++ b/src/js/services/shapeshiftService.js @@ -1,48 +1,44 @@ 'use strict'; -angular.module('copayApp.services').factory('shapeshiftService', function($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingData, platformInfo, servicesService) { +angular.module('copayApp.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingData, platformInfo, servicesService) { var root = {}; root.ShiftState = 'Shift'; - root.withdrawalAddress = '' - root.returnAddress = '' + root.coinIn = ''; + root.coinOut = ''; + root.withdrawalAddress = ''; + root.returnAddress = ''; root.amount = ''; - root.marketData = {} - root.withdrawalAddress = function(address) { + root.marketData = {}; + root.withdrawalAddress = function (address) { root.withdrawalAddress = address; }; - root.returnAddress = function(address) { + root.returnAddress = function (address) { root.returnAddress = address; }; - root.amount = function(amount) { + root.amount = function (amount) { root.amount = amount; }; - root.fromWalletId = function(id) { - root.fromWalletId = id; - }; - root.toWalletId = function(id) { - root.toWalletId = id; - }; - root.coinIn = function(coinIn) { + root.coinIn = function (coinIn) { root.coinIn = coinIn.toUpperCase(); }; - root.coinOut = function(coinOut) { + root.coinOut = function (coinOut) { root.coinOut = coinOut.toUpperCase(); }; - root.getMarketDataIn = function(coin) { - if(coin === root.coinOut) return root.getMarketData(root.coinOut, root.coinIn); + root.getMarketDataIn = function (coin) { + if (coin === root.coinOut) return root.getMarketData(root.coinOut, root.coinIn); return root.getMarketData(coin, root.coinOut); }; - root.getMarketDataOut = function(coin) { - if(coin === root.coinIn) return root.getMarketData(root.coinOut, root.coinIn); + root.getMarketDataOut = function (coin) { + if (coin === root.coinIn) return root.getMarketData(root.coinOut, root.coinIn); return root.getMarketData(root.coinIn, coin); }; - root.getMarketData = function(coinIn, coinOut, cb) { + root.getMarketData = function (coinIn, coinOut, cb) { root.coinIn = coinIn; - root.coinOut= coinOut; - if(root.coinIn === undefined || root.coinOut === undefined) return; + root.coinOut = coinOut; + if (root.coinIn === undefined || root.coinOut === undefined) return; shapeshiftApiService .marketInfo(root.coinIn, root.coinOut) - .then(function(marketData){ + .then(function (marketData) { root.marketData = marketData; root.rateString = root.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); if (cb) { @@ -59,43 +55,50 @@ angular.module('copayApp.services').factory('shapeshiftService', function($http, });*/ root.coins = { - 'BTC': { name: 'Bitcoin', symbol: 'BTC' }, - 'BCH': { name: 'Bitcoin Cash', symbol: 'BCH' } + 'BTC': {name: 'Bitcoin', symbol: 'BTC'}, + 'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} }; - function checkForError(data){ - if(data.error) return true; + function checkForError(data) { + if (data.error) return true; return false; } - root.shiftIt = function(){ + root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, cb) { ongoingProcess.set('connectingShapeshift', true); - var validate=shapeshiftApiService.ValidateAddress(root.withdrawalAddress, root.coinOut); - validate.then(function(valid){ - //console.log(root.withdrawalAddress) - //console.log(valid) + root.withdrawalAddress(withdrawalAddress); + root.returnAddress(returnAddress); + var validate = shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut); + validate.then(function (valid) { var tx = ShapeShift(); - tx.then(function(txData){ - if(txData['fixedTxData']){ + var coin; + console.log("Starting"); + tx.then(function (txData) { + console.log("Got txData", txData); + if (txData['fixedTxData']) { txData = txData.fixedTxData; - if(checkForError(txData)) return; + if (checkForError(txData)) return; //console.log(txData) - var coinPair=txData.pair.split('_'); + var coinPair = txData.pair.split('_'); txData.depositType = coinPair[0].toUpperCase(); txData.withdrawalType = coinPair[1].toUpperCase(); - var coin = root.coins[txData.depositType].name.toLowerCase(); - //console.log(coin) - txData.depositQR = coin + ":" + txData.deposit + "?amount=" + txData.depositAmount + coin = root.coins[txData.depositType].name.toLowerCase(); + + txData.depositQR = coin + ":" + txData.deposit + "?amount=" + txData.depositAmount; + root.txFixedPending = true; - } else if(txData['normalTxData']){ + + } else if (txData['normalTxData']) { + txData = txData.normalTxData; - if(checkForError(txData)) return; - var coin = root.coins[txData.depositType.toUpperCase()].name.toLowerCase(); + if (checkForError(txData)) return; + coin = root.coins[txData.depositType.toUpperCase()].name.toLowerCase(); txData.depositQR = coin + ":" + txData.deposit; - } else if(txData['cancelTxData']){ - if(checkForError(txData.cancelTxData)) return; - if(root.txFixedPending) { - $interval.cancel(root.txInterval); + + } else if (txData['cancelTxData']) { + + if (checkForError(txData.cancelTxData)) return; + if (root.txFixedPending) { root.txFixedPending = false; } root.ShiftState = 'Shift'; @@ -108,37 +111,43 @@ angular.module('copayApp.services').factory('shapeshiftService', function($http, if (sendAddress && sendAddress.indexOf('bitcoin cash') >= 0) sendAddress = sendAddress.replace('bitcoin cash', 'bitcoincash'); + ongoingProcess.set('connectingShapeshift', false); + + root.ShiftState = 'Cancel'; + root.GetStatus(); + root.txInterval=$interval(root.GetStatus, 8000); + var shapeshiftData = { - fromWalletId: root.fromWalletId, + coinIn: coinIn, + coinOut: coinOut, toWalletId: root.toWalletId, minAmount: root.marketData.minimum, maxAmount: root.marketData.maxLimit, - orderId: root.depositInfo.orderId + orderId: root.depositInfo.orderId, + toAddress: txData.deposit }; - - if (incomingData.redir(sendAddress, 'shapeshift', shapeshiftData)) { + // + // if (incomingData.redir(sendAddress, 'shapeshift', shapeshiftData)) { ongoingProcess.set('connectingShapeshift', false); - return; - } + // return; + // } + cb(shapeshiftData); - /*root.ShiftState = 'Cancel'; - root.GetStatus(); - root.txInterval=$interval(root.GetStatus, 8000);*/ }); }) }; function ShapeShift() { - if(root.ShiftState === 'Cancel') return shapeshiftApiService.CancelTx(root); - if(parseFloat(root.amount) > 0) return shapeshiftApiService.FixedAmountTx(root); + if (root.ShiftState === 'Cancel') return shapeshiftApiService.CancelTx(root); + if (parseFloat(root.amount) > 0) return shapeshiftApiService.FixedAmountTx(root); return shapeshiftApiService.NormalTx(root); } - root.GetStatus = function(){ + root.GetStatus = function () { var address = root.depositInfo.deposit - shapeshiftApiService.GetStatusOfDepositToAddress(address).then(function(data){ + shapeshiftApiService.GetStatusOfDepositToAddress(address).then(function (data) { root.DepositStatus = data; - if(root.DepositStatus.status === 'complete'){ + if (root.DepositStatus.status === 'complete') { $interval.cancel(root.txInterval); root.depositInfo = null; root.ShiftState = 'Shift' @@ -153,7 +162,7 @@ angular.module('copayApp.services').factory('shapeshiftService', function($http, sref: 'tabs.shapeshift', }; - var register = function() { + var register = function () { servicesService.register(servicesItem); }; register(); diff --git a/www/views/review.html b/www/views/review.html index 0ad5e2148..7317d7b68 100644 --- a/www/views/review.html +++ b/www/views/review.html @@ -32,13 +32,14 @@ +
To:
- +