diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index b623aa592..88d4901e9 100644 --- a/src/js/controllers/amount.js +++ b/src/js/controllers/amount.js @@ -2,7 +2,7 @@ angular.module('copayApp.controllers').controller('amountController', amountController); -function amountController(configService, $filter, gettextCatalog, $ionicHistory, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, txFormatService, platformInfo, profileService, walletService, $window) { +function amountController(configService, $filter, gettextCatalog, $ionicHistory, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window) { var vm = this; vm.allowSend = false; @@ -21,6 +21,7 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, vm.maxAmount = 0; vm.minAmount = 0; vm.shapeshiftOrderId = ''; + vm.thirdParty = false; vm.unit = ''; vm.changeUnit = changeUnit; @@ -72,11 +73,39 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, passthroughParams = data.stateParams; console.log('stateParams:', data.stateParams); + vm.fromWalletId = data.stateParams.fromWalletId; + vm.toWalletId = data.stateParams.toWalletId; vm.minAmount = parseFloat(data.stateParams.minAmount); vm.maxAmount = parseFloat(data.stateParams.maxAmount); - vm.shapeshiftOrderId = data.stateParams.thirdPartyOrderId; + + if (passthroughParams.thirdParty) { + vm.thirdParty = JSON.parse(passthroughParams.thirdParty); // Parse stringified JSON-object + if (vm.thirdParty) { + if (vm.thirdParty.id === 'shapeshift') { + if (!vm.thirdParty.data) { + vm.thirdParty.data = {}; + } + vm.thirdParty.data['fromWalletId'] = vm.fromWalletId; + + vm.fromWallet = profileService.getWallet(vm.fromWalletId); + vm.toWallet = profileService.getWallet(vm.toWalletId); + + shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) { + console.log(data); + vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); + vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); + }); + + // if (vm.thirdParty.data['shapeshiftOrderId'] && data.stateParams.shapeshiftOrderId.length > 0) { + // vm.shapeshiftOrderId = vm.thirdParty.data['shapeshiftOrderId']; + // } + } + } + } + // vm.shapeshiftOrderId = data.stateParams.thirdPartyOrderId; vm.isRequestingSpecificAmount = !data.stateParams.fromWalletId; + var config = configService.getSync().wallet.settings; setAvailableUnits(); @@ -335,8 +364,8 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, if (a) { amountInCrypto = a; var amountInSatoshis = a * unitToSatoshi; - vm.fundsAreInsufficient = !!passthroughParams.fromWalletId - && availableSatoshis !== null + vm.fundsAreInsufficient = !!passthroughParams.fromWalletId + && availableSatoshis !== null && availableSatoshis < amountInSatoshis; vm.alternativeAmount = txFormatService.formatAmount(amountInSatoshis, true); @@ -356,8 +385,8 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, } } else { amountInCrypto = result; - vm.fundsAreInsufficient = passthroughParams.fromWalletId - && availableSatoshis !== null + vm.fundsAreInsufficient = passthroughParams.fromWalletId + && availableSatoshis !== null && availableSatoshis < result * unitToSatoshi; vm.alternativeAmount = $filter('formatFiatAmount')(toFiat(result)); @@ -441,11 +470,14 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, amount: useSendMax ? undefined : satoshis, fromWalletId: passthroughParams.fromWalletId, sendMax: useSendMax, - thirdPartyOrderId: passthroughParams.thirdPartyOrderId, toAddr: passthroughParams.toAddress, toWalletId: passthroughParams.toWalletId }; + if (vm.thirdParty) { + confirmData['thirdParty'] = JSON.stringify(this.thirdParty); + } + console.log('confirmData:', confirmData); if (!confirmData.fromWalletId) { diff --git a/src/js/routes.js b/src/js/routes.js index 286042266..d9c900e34 100644 --- a/src/js/routes.js +++ b/src/js/routes.js @@ -287,7 +287,7 @@ angular.module('copayApp').config(function(historicLogProvider, $provide, $logPr */ .state('tabs.send.amount', { - url: '/amount/:thirdPartyId/:thirdPartyOrderId/:fromWalletId/:maxAmount/:minAmount/:toWalletId/:toAddress', + url: '/amount/:thirdParty/:fromWalletId/:maxAmount/:minAmount/:toWalletId/:toAddress', views: { 'tab-send@tabs': { controller: 'amountController', diff --git a/src/js/services/incomingData.js b/src/js/services/incomingData.js index 7a9e67fe7..fb8d8868a 100644 --- a/src/js/services/incomingData.js +++ b/src/js/services/incomingData.js @@ -86,7 +86,6 @@ angular.module('copayApp.services').factory('incomingData', function($log, $stat $state.transitionTo('tabs.send.origin', { amount: amount, toAddress: addr, - displayAddress: originalAddress ? originalAddress : addr, description: message, coin: coin }); @@ -109,6 +108,7 @@ angular.module('copayApp.services').factory('incomingData', function($log, $stat // params['thirdParty']['minShapeshiftAmount'] = serviceData.minAmount; // params['thirdParty']['maxShapeshiftAmount'] = serviceData.maxAmount; // params['thirdParty']['shapeshiftOrderId'] = serviceData.orderId; + params['thirdParty'] = JSON.stringify(params['thirdParty']); $state.transitionTo('tabs.send.amount', params); } else { $state.transitionTo('tabs.send.origin', params); diff --git a/src/js/services/shapeshiftService.js b/src/js/services/shapeshiftService.js index 8a9b8fcaa..41af14002 100644 --- a/src/js/services/shapeshiftService.js +++ b/src/js/services/shapeshiftService.js @@ -1,7 +1,143 @@ 'use strict'; -angular.module('copayApp.services').factory('shapeshiftService', function($http, $log, lodash, moment, storageService, configService, platformInfo, servicesService) { +angular.module('copayApp.services').factory('shapeshiftService', function($http, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, platformInfo, servicesService) { var root = {}; - var credentials = {}; + root.ShiftState = 'Shift'; + root.withdrawalAddress = '' + root.returnAddress = '' + root.amount = ''; + root.marketData = {} + this.withdrawalAddress = function(address) { + root.withdrawalAddress = address; + }; + this.returnAddress = function(address) { + root.returnAddress = address; + }; + this.amount = function(amount) { + root.amount = amount; + }; + this.fromWalletId = function(id) { + root.fromWalletId = id; + }; + this.toWalletId = function(id) { + root.toWalletId = id; + }; + + 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); + return root.getMarketData(root.coinIn, coin); + }; + root.getMarketData = function(coinIn, coinOut, cb) { + root.coinIn = coinIn; + root.coinOut= coinOut; + if(root.coinIn === undefined || root.coinOut === undefined) return; + shapeshiftApiService + .marketInfo(root.coinIn, root.coinOut) + .then(function(marketData){ + root.marketData = marketData; + root.rateString = root.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); + if (cb) { + cb(marketData); + } + }); + }; + + /*shapeshiftApiService.coins().then(function(coins){ + root.coins = coins; + root.coinIn = coins['BTC'].symbol; + root.coinOut = coins['BCH'].symbol; + root.getMarketData(root.coinIn, root.coinOut); + });*/ + + root.coins = { + 'BTC': { name: 'Bitcoin', symbol: 'BTC' }, + 'BCH': { name: 'Bitcoin Cash', symbol: 'BCH' } + }; + + function checkForError(data){ + if(data.error) return true; + return false; + } + + root.shiftIt = function(){ + ongoingProcess.set('connectingShapeshift', true); + var validate=shapeshiftApiService.ValidateAddress(root.withdrawalAddress, root.coinOut); + validate.then(function(valid){ + //console.log(root.withdrawalAddress) + //console.log(valid) + var tx = ShapeShift(); + tx.then(function(txData){ + if(txData['fixedTxData']){ + txData = txData.fixedTxData; + if(checkForError(txData)) return; + //console.log(txData) + 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 + root.txFixedPending = true; + } else if(txData['normalTxData']){ + txData = txData.normalTxData; + if(checkForError(txData)) return; + var 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); + root.txFixedPending = false; + } + root.ShiftState = 'Shift'; + return; + } + root.depositInfo = txData; + //console.log(root.marketData); + //console.log(root.depositInfo); + var sendAddress = txData.depositQR; + if (sendAddress && sendAddress.indexOf('bitcoin cash') >= 0) + sendAddress = sendAddress.replace('bitcoin cash', 'bitcoincash'); + + var shapeshiftData = { + fromWalletId: root.fromWalletId, + minAmount: root.marketData.minimum, + maxAmount: root.marketData.maxLimit, + orderId: root.depositInfo.orderId + }; + + if (incomingData.redir(sendAddress, shapeshiftData)) { + ongoingProcess.set('connectingShapeshift', false); + return; + } + + /*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); + return shapeshiftApiService.NormalTx(root); + } + + root.GetStatus = function(){ + var address = root.depositInfo.deposit + shapeshiftApiService.GetStatusOfDepositToAddress(address).then(function(data){ + root.DepositStatus = data; + if(root.DepositStatus.status === 'complete'){ + $interval.cancel(root.txInterval); + root.depositInfo = null; + root.ShiftState = 'Shift' + } + }); + }; var servicesItem = { name: 'shapeshift', @@ -13,7 +149,6 @@ angular.module('copayApp.services').factory('shapeshiftService', function($http, var register = function() { servicesService.register(servicesItem); }; - register(); return root; }); diff --git a/src/sass/views/shapeshift.scss b/src/sass/views/shapeshift.scss index 8b4b941ab..1054fece2 100644 --- a/src/sass/views/shapeshift.scss +++ b/src/sass/views/shapeshift.scss @@ -1,4 +1,8 @@ #shapeshift { + .swap-image { + width: 70%; + max-width: 400px; + } .empty-case { @include empty-case(); } diff --git a/www/img/shapeshift_swap.png b/www/img/shapeshift_swap.png new file mode 100644 index 000000000..83905b751 Binary files /dev/null and b/www/img/shapeshift_swap.png differ diff --git a/www/views/amount.html b/www/views/amount.html index 1757bf25a..e5df7dc0f 100644 --- a/www/views/amount.html +++ b/www/views/amount.html @@ -6,15 +6,11 @@ - + - diff --git a/www/views/shapeshift.html b/www/views/shapeshift.html index 2382f9897..81e502631 100644 --- a/www/views/shapeshift.html +++ b/www/views/shapeshift.html @@ -11,8 +11,8 @@ - - Your Bitcoin wallet is empty + +