From c40eabf807fbd31f9c288df36f67fc5b31b22da4 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 10:59:09 +0900 Subject: [PATCH 01/15] Clean shapeshift service and add the correct callback --- src/js/services/shapeShiftApiService.js | 13 ++- src/js/services/shapeshift.service.js | 76 +++++++++++++ src/js/services/shapeshiftService.js | 141 ------------------------ 3 files changed, 85 insertions(+), 145 deletions(-) create mode 100644 src/js/services/shapeshift.service.js delete mode 100644 src/js/services/shapeshiftService.js diff --git a/src/js/services/shapeShiftApiService.js b/src/js/services/shapeShiftApiService.js index 210ae0fbd..411c68653 100644 --- a/src/js/services/shapeShiftApiService.js +++ b/src/js/services/shapeShiftApiService.js @@ -328,10 +328,13 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q) $scope.amount, $scope.withdrawalAddress, $scope.coinIn, $scope.coinOut ); + console.log('shapeshiftApiService.FixedAmountTx()'); console.log(fixedTx); SSA.FixedAmountTx(fixedTx, function (data) { - console.log(data) - return promise.resolve({ fixedTxData : data.success }); + console.log(data); + promise.resolve(data.success); + }, function (err) { + promise.reject(err); }); return promise.promise; }, @@ -361,8 +364,10 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q) }, ValidateAddress : function(address, coin) { var promise = $q.defer(); - SSA.ValidateAdddress(address, coin, function(data){ - promise.resolve(data); + SSA.ValidateAdddress(address, coin, function onSuccess(data){ + promise.resolve(data); + }, function onError(err) { + promise.reject(err); }); return promise.promise; } diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js new file mode 100644 index 000000000..7c1896ac7 --- /dev/null +++ b/src/js/services/shapeshift.service.js @@ -0,0 +1,76 @@ +'use strict'; + +angular.module('bitcoincom.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingDataService, platformInfo, servicesService) { + var root = {}; + root.ShiftState = 'Shift'; + root.coinIn = ''; + root.coinOut = ''; + root.withdrawalAddress = ''; + root.returnAddress = ''; + root.amount = ''; + root.marketData = {}; + + root.getMarketData = function (coinIn, coinOut, cb) { + root.coinIn = coinIn; + root.coinOut = coinOut; + if (root.coinIn !== undefined && root.coinOut !== undefined){ + 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); + } + }); + } + }; + + root.coins = { + 'BTC': {name: 'Bitcoin', symbol: 'BTC'}, + 'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} + }; + + root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, cb) { + ongoingProcess.set('connectingShapeshift', true); + root.withdrawalAddress = withdrawalAddress; + root.returnAddress = returnAddress; + root.coinIn = coinIn; + root.coinOut = coinOut; + shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { + shapeshiftApiService.FixedAmountTx(root).then(function onSuccess(txData) { + if (txData.err) { + cb(txData.err); + } else { + if (!txData.orderId || !txData.deposit) { + cb(new Error('Invalid response')); + } else { + var coinPair = txData.pair.split('_'); + txData.depositType = coinPair[0].toUpperCase(); + txData.withdrawalType = coinPair[1].toUpperCase(); + coin = root.coins[txData.depositType].name.toLowerCase(); + txData.depositQR = coin + ":" + txData.deposit + "?amount=" + txData.depositAmount; + root.txFixedPending = true; + root.depositInfo = txData; + var shapeshiftData = { + coinIn: coinIn, + coinOut: coinOut, + toWalletId: root.toWalletId, + minAmount: root.marketData.minimum, + maxAmount: root.marketData.maxLimit, + orderId: txData.orderId, + toAddress: txData.deposit + }; + + cb(null, shapeshiftData); + } + } + }, function onError(err) { + cb(err); + }); + }, function onError(err) { + cb(err); + }); + }; + return root; +}); diff --git a/src/js/services/shapeshiftService.js b/src/js/services/shapeshiftService.js deleted file mode 100644 index b1d2f6e7d..000000000 --- a/src/js/services/shapeshiftService.js +++ /dev/null @@ -1,141 +0,0 @@ -'use strict'; - -angular.module('copayApp.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingDataService, platformInfo, servicesService) { - var root = {}; - root.ShiftState = 'Shift'; - root.coinIn = ''; - root.coinOut = ''; - root.withdrawalAddress = ''; - root.returnAddress = ''; - root.amount = ''; - root.marketData = {}; - - 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.err) return true; - return false; - } - - root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, cb) { - ongoingProcess.set('connectingShapeshift', true); - root.withdrawalAddress = withdrawalAddress; - root.returnAddress = returnAddress; - root.coinIn = coinIn; - root.coinOut = coinOut; - shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function (valid) { - var tx = ShapeShift(); - var coin; - console.log("Starting"); - tx.then(function (txData) { - console.log("Got txData", txData); - if (txData['fixedTxData']) { - txData = txData.fixedTxData; - if (checkForError(txData)) return cb(txData.err); - //console.log(txData) - var coinPair = txData.pair.split('_'); - txData.depositType = coinPair[0].toUpperCase(); - txData.withdrawalType = coinPair[1].toUpperCase(); - coin = root.coins[txData.depositType].name.toLowerCase(); - - txData.depositQR = coin + ":" + txData.deposit + "?amount=" + txData.depositAmount; - - root.txFixedPending = true; - - } else if (txData['normalTxData']) { - txData = txData.normalTxData; - if (checkForError(txData)) return cb(txData.err); - coin = root.coins[txData.depositType.toUpperCase()].name.toLowerCase(); - txData.depositQR = coin + ":" + txData.deposit; - } else if (txData['cancelTxData']) { - txData = txData.cancelTxData; - if (checkForError(txData)) return cb(txData.err); - if (root.txFixedPending) { - root.txFixedPending = false; - } - root.ShiftState = 'Shift'; - } - 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'); - - ongoingProcess.set('connectingShapeshift', false); - - root.ShiftState = 'Cancel'; - //root.GetStatus(); - //root.txInterval=$interval(root.GetStatus, 8000); - - var shapeshiftData = { - coinIn: coinIn, - coinOut: coinOut, - toWalletId: root.toWalletId, - minAmount: root.marketData.minimum, - maxAmount: root.marketData.maxLimit, - orderId: root.depositInfo.orderId, - toAddress: txData.deposit - }; - // - // if (incomingDataService.redir(sendAddress, 'shapeshift', shapeshiftData)) { - ongoingProcess.set('connectingShapeshift', false); - // return; - // } - cb(null, shapeshiftData); - }); - }) - }; - - function ShapeShift() { - 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' - } - }); - }; - - return root; -}); From c553b51817f8b4c55609af6a3237f90df845aae7 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 11:02:10 +0900 Subject: [PATCH 02/15] clean up --- src/js/controllers/review.controller.js | 2 +- src/js/services/shapeshift.service.js | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/src/js/controllers/review.controller.js b/src/js/controllers/review.controller.js index dbf14937f..e34d90995 100644 --- a/src/js/controllers/review.controller.js +++ b/src/js/controllers/review.controller.js @@ -508,7 +508,7 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit } shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, function onShiftIt(err, shapeshiftData) { - if (err && err != null) { + if (err) { ongoingProcess.set('connectingShapeshift', false); popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { $ionicHistory.goBack(); diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index 7c1896ac7..1efabffd4 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -32,7 +32,6 @@ angular.module('bitcoincom.services').factory('shapeshiftService', function ($ht }; root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, cb) { - ongoingProcess.set('connectingShapeshift', true); root.withdrawalAddress = withdrawalAddress; root.returnAddress = returnAddress; root.coinIn = coinIn; From 5e034e7b8b008a04d8467232bcacbad70aa6c777 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 11:37:29 +0900 Subject: [PATCH 03/15] clean up --- src/js/services/shapeshift.service.js | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index 1efabffd4..c9c1e428a 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -13,17 +13,15 @@ angular.module('bitcoincom.services').factory('shapeshiftService', function ($ht root.getMarketData = function (coinIn, coinOut, cb) { root.coinIn = coinIn; root.coinOut = coinOut; - if (root.coinIn !== undefined && root.coinOut !== undefined){ - 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 + .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); + } + }); }; root.coins = { From f7cde7a071f73967929da42baf95f3d8f10662b1 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 12:45:20 +0900 Subject: [PATCH 04/15] clean up, migrate to no-fix amount --- src/js/controllers/amount.js | 7 ++- src/js/controllers/review.controller.js | 9 ++- src/js/services/shapeShiftApiService.js | 23 ++++---- src/js/services/shapeshift.service.js | 76 ++++++++++++------------- 4 files changed, 59 insertions(+), 56 deletions(-) diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index e861b36ff..c24c567ed 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, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window) { +function amountController(configService, $filter, gettextCatalog, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window, ongoingProcess) { var vm = this; vm.allowSend = false; @@ -74,7 +74,6 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, initCurrencies(); passthroughParams = sendFlowService.state.getClone(); - console.log('amount onBeforeEnter after back sendflow ', passthroughParams); vm.fromWalletId = passthroughParams.fromWalletId; @@ -94,9 +93,11 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, vm.fromWallet = profileService.getWallet(vm.fromWalletId); vm.toWallet = profileService.getWallet(vm.toWalletId); + ongoingProcess.set('connectingShapeshift', true); shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) { vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); + ongoingProcess.set('connectingShapeshift', false); }); } } @@ -113,7 +114,7 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory, var reOp = /^[\*\+\-\/]$/; if (!isAndroid && !isIos) { - var disableKeys = angular.element($window).on('keydown', function(e) { + angular.element($window).on('keydown', function(e) { if (!e.key) return; if (e.which === 8) { // you can add others here inside brackets. if (!altCurrencyModal) { diff --git a/src/js/controllers/review.controller.js b/src/js/controllers/review.controller.js index e34d90995..9be5f7902 100644 --- a/src/js/controllers/review.controller.js +++ b/src/js/controllers/review.controller.js @@ -507,10 +507,12 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit return; } - shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, function onShiftIt(err, shapeshiftData) { + // Need to use the correct service to do it. + var amount = parseFloat(satoshis / 100000000); + + shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, amount, function onShiftIt(err, shapeshiftData) { if (err) { - ongoingProcess.set('connectingShapeshift', false); - popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { + popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err, function () { $ionicHistory.goBack(); }); } else { @@ -520,6 +522,7 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit vm.destination.address = toAddress; vm.destination.kind = 'shapeshift'; } + ongoingProcess.set('connectingShapeshift', false); }); }); }); diff --git a/src/js/services/shapeShiftApiService.js b/src/js/services/shapeShiftApiService.js index 411c68653..cc5fb0792 100644 --- a/src/js/services/shapeShiftApiService.js +++ b/src/js/services/shapeShiftApiService.js @@ -332,17 +332,19 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q) console.log(fixedTx); SSA.FixedAmountTx(fixedTx, function (data) { console.log(data); - promise.resolve(data.success); - }, function (err) { - promise.reject(err); + promise.resolve(data); }); return promise.promise; }, NormalTx : function($scope){ var promise = $q.defer(); var normalTx = SSA.CreateNormalTx($scope.withdrawalAddress, $scope.coinIn, $scope.coinOut); + + console.log('shapeshiftApiService.NormalTx()'); + console.log(normalTx); SSA.NormalTx(normalTx, function (data) { - promise.resolve({ normalTxData : data }); + console.log(data); + promise.resolve(data); }); return promise.promise; }, @@ -363,13 +365,12 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q) return promise.promise; }, ValidateAddress : function(address, coin) { - var promise = $q.defer(); - SSA.ValidateAdddress(address, coin, function onSuccess(data){ - promise.resolve(data); - }, function onError(err) { - promise.reject(err); - }); - return promise.promise; + var promise = $q.defer(); + SSA.ValidateAdddress(address, coin, function onRequest(data){ + console.log(data); + promise.resolve(data); + }); + return promise.promise; } }; }); diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index c9c1e428a..face3e08a 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -1,6 +1,6 @@ 'use strict'; -angular.module('bitcoincom.services').factory('shapeshiftService', function ($http, $interval, $log, lodash, moment, ongoingProcess, shapeshiftApiService, storageService, configService, incomingDataService, platformInfo, servicesService) { +angular.module('bitcoincom.services').factory('shapeshiftService', function (shapeshiftApiService) { var root = {}; root.ShiftState = 'Shift'; root.coinIn = ''; @@ -29,45 +29,43 @@ angular.module('bitcoincom.services').factory('shapeshiftService', function ($ht 'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} }; - root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, cb) { - root.withdrawalAddress = withdrawalAddress; - root.returnAddress = returnAddress; - root.coinIn = coinIn; - root.coinOut = coinOut; - shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { - shapeshiftApiService.FixedAmountTx(root).then(function onSuccess(txData) { - if (txData.err) { - cb(txData.err); - } else { - if (!txData.orderId || !txData.deposit) { - cb(new Error('Invalid response')); - } else { - var coinPair = txData.pair.split('_'); - txData.depositType = coinPair[0].toUpperCase(); - txData.withdrawalType = coinPair[1].toUpperCase(); - coin = root.coins[txData.depositType].name.toLowerCase(); - txData.depositQR = coin + ":" + txData.deposit + "?amount=" + txData.depositAmount; - root.txFixedPending = true; - root.depositInfo = txData; - var shapeshiftData = { - coinIn: coinIn, - coinOut: coinOut, - toWalletId: root.toWalletId, - minAmount: root.marketData.minimum, - maxAmount: root.marketData.maxLimit, - orderId: txData.orderId, - toAddress: txData.deposit - }; - - cb(null, shapeshiftData); - } - } - }, function onError(err) { - cb(err); - }); - }, function onError(err) { + root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { + if (typeof amount !== 'number' || amount < root.marketData.minimum || amount > root.marketData.maxLimit) { + var err = new Error('Invalid amount'); cb(err); - }); + } else { + root.withdrawalAddress = withdrawalAddress; + root.returnAddress = returnAddress; + root.coinIn = coinIn; + root.coinOut = coinOut; + root.amount = amount; + shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { + if (data && data.isvalid) { + shapeshiftApiService.NormalTx(root).then(function onResponse(data) { + var txData = data; + if (!txData || !txData.orderId || !txData.deposit) { + cb(new Error('Invalid response')); + } else { + root.depositInfo = txData; + var shapeshiftData = { + coinIn: coinIn, + coinOut: coinOut, + toWalletId: root.toWalletId, + minAmount: root.marketData.minimum, + maxAmount: root.marketData.maxLimit, + orderId: txData.orderId, + toAddress: txData.deposit + }; + + cb(null, shapeshiftData); + } + }); + } else { + var err = new Error('Invalid address or coin'); + cb(err); + } + }); + } }; return root; }); From 887270bfa95cb45940e4b9a0cb3fd385272fecb4 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 14:42:27 +0900 Subject: [PATCH 05/15] clean ";" --- src/js/services/send-flow-state.service.js | 8 ++++---- src/js/services/send-flow.service.js | 3 +-- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/src/js/services/send-flow-state.service.js b/src/js/services/send-flow-state.service.js index bec2c8a3c..c19317515 100644 --- a/src/js/services/send-flow-state.service.js +++ b/src/js/services/send-flow-state.service.js @@ -101,7 +101,7 @@ angular Object.keys(params).forEach(function forNewParam(key) { service.state[key] = params[key]; }); - }; + } /** * Pop state @@ -116,7 +116,7 @@ angular } else { clear(); } - }; + } /** * Push state @@ -129,14 +129,14 @@ angular service.previousStates.push(currentParams); clearCurrent(); map(params); - }; + } /** * Is empty stack */ function isEmpty() { return service.previousStates.length == 0; - }; + } }; })(); \ No newline at end of file diff --git a/src/js/services/send-flow.service.js b/src/js/services/send-flow.service.js index 1b02c0d34..e8be2e487 100644 --- a/src/js/services/send-flow.service.js +++ b/src/js/services/send-flow.service.js @@ -144,6 +144,5 @@ angular sendFlowStateService.pop(); sendFlowRouterService.goBack(); } - }; - + } })(); \ No newline at end of file From 9128d3ebe2f01ca04d6e018948e68793446b54ca Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 14:43:16 +0900 Subject: [PATCH 06/15] Migrate to our new standard for services --- src/js/services/shapeshift.service.js | 149 +++++++++++++++----------- 1 file changed, 85 insertions(+), 64 deletions(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index face3e08a..f2d0876e7 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -1,71 +1,92 @@ 'use strict'; -angular.module('bitcoincom.services').factory('shapeshiftService', function (shapeshiftApiService) { - var root = {}; - root.ShiftState = 'Shift'; - root.coinIn = ''; - root.coinOut = ''; - root.withdrawalAddress = ''; - root.returnAddress = ''; - root.amount = ''; - root.marketData = {}; +(function(){ - root.getMarketData = function (coinIn, coinOut, cb) { - root.coinIn = coinIn; - root.coinOut = coinOut; - 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); - } - }); - }; +angular + .module('bitcoincom.services') + .factory('shapeshiftService', shapeshiftService); + + function shapeshiftService(shapeshiftApiService) { - root.coins = { - 'BTC': {name: 'Bitcoin', symbol: 'BTC'}, - 'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} - }; + var service = { + // Variables + coinIn: '', + coinOut: '', + withdrawalAddress: '', + returnAddress: '', + amount: '', + marketData: {}, + coins: { + 'BTC': {name: 'Bitcoin', symbol: 'BTC'}, + 'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} + }, - root.shiftIt = function (coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { - if (typeof amount !== 'number' || amount < root.marketData.minimum || amount > root.marketData.maxLimit) { - var err = new Error('Invalid amount'); - cb(err); - } else { - root.withdrawalAddress = withdrawalAddress; - root.returnAddress = returnAddress; - root.coinIn = coinIn; - root.coinOut = coinOut; - root.amount = amount; - shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { - if (data && data.isvalid) { - shapeshiftApiService.NormalTx(root).then(function onResponse(data) { - var txData = data; - if (!txData || !txData.orderId || !txData.deposit) { - cb(new Error('Invalid response')); - } else { - root.depositInfo = txData; - var shapeshiftData = { - coinIn: coinIn, - coinOut: coinOut, - toWalletId: root.toWalletId, - minAmount: root.marketData.minimum, - maxAmount: root.marketData.maxLimit, - orderId: txData.orderId, - toAddress: txData.deposit - }; + // Functions + getMarketData: getMarketData, + shiftIt: shiftIt + }; - cb(null, shapeshiftData); - } - }); - } else { - var err = new Error('Invalid address or coin'); - cb(err); - } - }); + return service; + + function getMarketData(coinIn, coinOut, cb) { + service.coinIn = coinIn; + service.coinOut = coinOut; + shapeshiftApiService + .marketInfo(service.coinIn, service.coinOut) + .then(function (marketData) { + service.marketData = marketData; + service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); + if (cb) { + cb(marketData); + } + }); } - }; - return root; -}); + + function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { + // Test if the amount is correct depending on the min and max + if (typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { + var err = new Error('Invalid amount'); + cb(err); + } else { + // Init service data + service.withdrawalAddress = withdrawalAddress; + service.returnAddress = returnAddress; + service.coinIn = coinIn; + service.coinOut = coinOut; + service.amount = amount; + + // Check the address + shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { + if (data && data.isvalid) { + + // Prepare the transaction shapeshift side + shapeshiftApiService.NormalTx(service).then(function onResponse(data) { + var txData = data; + + // If the content is not that it was expected, get back an error + if (!txData || !txData.orderId || !txData.deposit) { + cb(new Error('Invalid response')); + } else { + // Get back the data + service.depositInfo = txData; + var shapeshiftData = { + coinIn: coinIn, + coinOut: coinOut, + toWalletId: service.toWalletId, + minAmount: service.marketData.minimum, + maxAmount: service.marketData.maxLimit, + orderId: txData.orderId, + toAddress: txData.deposit + }; + cb(null, shapeshiftData); + } + }); + } else { + var err = new Error('Invalid address or coin'); + cb(err); + } + }); + } + } + } +})(); \ No newline at end of file From 2c398bbe03440d68835f7150f5b459a1d28c9f7a Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 14:51:06 +0900 Subject: [PATCH 07/15] Fix amount check --- src/js/services/shapeshift.service.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index f2d0876e7..c290f1fc3 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -44,7 +44,7 @@ angular function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { // Test if the amount is correct depending on the min and max - if (typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { + if (!amount || typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { var err = new Error('Invalid amount'); cb(err); } else { From fa8ce4779f5c2ec6a0076119299dc1895742bf44 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 15:32:47 +0900 Subject: [PATCH 08/15] async series with shapeshift & fee calculation --- src/js/controllers/review.controller.js | 159 +++++++++++++----------- 1 file changed, 84 insertions(+), 75 deletions(-) diff --git a/src/js/controllers/review.controller.js b/src/js/controllers/review.controller.js index 9be5f7902..f606e4898 100644 --- a/src/js/controllers/review.controller.js +++ b/src/js/controllers/review.controller.js @@ -93,26 +93,48 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit if (sendFlowData.thirdParty) { vm.thirdParty = sendFlowData.thirdParty; - handleThirdPartyInitIfBip70(); - handleThirdPartyInitIfShapeshift(); + switch (vm.thirdParty.id) { + case 'shapeshift': + initShapeshift(function (err) { + if (err) { + // Error stop here + ongoingProcess.set('connectingShapeshift', false); + popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { + $ionicHistory.goBack(); + }); + } else { + _next(data); + } + }); + break; + case 'bip70': + initBip70(); + default: + _next(data); + break; + } + } else { + _next(data); } - configService.get(function onConfig(err, configCache) { - if (err) { - $log.err('Error getting config.', err); - } else { - config = configCache; - priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat'; - vm.origin.currencyColor = (vm.originWallet.coin === 'btc' ? defaults.bitcoinWalletColor : defaults.bitcoinCashWalletColor); - console.log("coin", vm.originWallet.coin, vm.origin.currencyColor, config.bitcoinWalletColor, vm.originWallet.coin === 'btc'); - unitFromSat = 1 / config.wallet.settings.unitToSatoshi; - } - updateSendAmounts(); - getOriginWalletBalance(vm.originWallet); - handleDestinationAsAddress(toAddress, coin); - handleDestinationAsWallet(sendFlowData.toWalletId); - createVanityTransaction(data); - }); + function _next() { + configService.get(function onConfig(err, configCache) { + if (err) { + $log.err('Error getting config.', err); + } else { + config = configCache; + priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat'; + vm.origin.currencyColor = (vm.originWallet.coin === 'btc' ? defaults.bitcoinWalletColor : defaults.bitcoinCashWalletColor); + console.log("coin", vm.originWallet.coin, vm.origin.currencyColor, config.bitcoinWalletColor, vm.originWallet.coin === 'btc'); + unitFromSat = 1 / config.wallet.settings.unitToSatoshi; + } + updateSendAmounts(); + getOriginWalletBalance(vm.originWallet); + handleDestinationAsAddress(toAddress, coin); + handleDestinationAsWallet(sendFlowData.toWalletId); + createVanityTransaction(data); + }); + } } vm.approve = function() { @@ -458,75 +480,62 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit vm.destination.balanceCurrency = balanceText.currency; } - function handleThirdPartyInitIfBip70() { - if (vm.thirdParty.id === 'bip70') { - vm.sendingTitle = gettextCatalog.getString('You are paying'); - vm.memo = vm.thirdParty.memo; - vm.memoExpanded = !!vm.memo; - vm.destination.name = vm.thirdParty.name; + function initBip70() { + vm.sendingTitle = gettextCatalog.getString('You are paying'); + vm.memo = vm.thirdParty.memo; + vm.memoExpanded = !!vm.memo; + vm.destination.name = vm.thirdParty.name; - txPayproData = { - caTrusted: vm.thirdParty.caTrusted, - domain: vm.thirdParty.domain, - expires: vm.thirdParty.expires, - toAddress: toAddress, - url: vm.thirdParty.url, - verified: vm.thirdParty.verified, - }; - } + txPayproData = { + caTrusted: vm.thirdParty.caTrusted, + domain: vm.thirdParty.domain, + expires: vm.thirdParty.expires, + toAddress: toAddress, + url: vm.thirdParty.url, + verified: vm.thirdParty.verified, + }; } - function handleThirdPartyInitIfShapeshift() { - if (vm.thirdParty.id === 'shapeshift') { - vm.sendingTitle = gettextCatalog.getString('You are shifting'); - if (!vm.thirdParty.data) { - vm.thirdParty.data = {}; - } + function initShapeshift(cb) { + vm.sendingTitle = gettextCatalog.getString('You are shifting'); + if (!vm.thirdParty.data) { + vm.thirdParty.data = {}; + } - var toWallet = profileService.getWallet(destinationWalletId); - vm.destination.name = toWallet.name; - vm.destination.color = toWallet.color; - vm.destination.currency = toWallet.coin.toUpperCase(); + var toWallet = profileService.getWallet(destinationWalletId); + vm.destination.name = toWallet.name; + vm.destination.color = toWallet.color; + vm.destination.currency = toWallet.coin.toUpperCase(); - ongoingProcess.set('connectingShapeshift', true); - walletService.getAddress(vm.originWallet, false, function onReturnWalletAddress(err, returnAddr) { + ongoingProcess.set('connectingShapeshift', true); + walletService.getAddress(vm.originWallet, false, function onReturnWalletAddress(err, returnAddr) { + if (err) { + return cb(err); + } + walletService.getAddress(toWallet, false, function onWithdrawalWalletAddress(err, withdrawalAddr) { if (err) { - ongoingProcess.set('connectingShapeshift', false); - popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { - $ionicHistory.goBack(); - }); - return; + return cb(err); } - walletService.getAddress(toWallet, false, function onWithdrawalWalletAddress(err, withdrawalAddr) { + + // Need to use the correct service to do it. + var amount = parseFloat(satoshis / 100000000); + + shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, amount, function onShiftIt(err, shapeshiftData) { if (err) { + return cb(err); + } else { + vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/' + shapeshiftData.orderId; + vm.memoExpanded = !!vm.memo; + tx.toAddress = shapeshiftData.toAddress; + vm.destination.address = toAddress; + vm.destination.kind = 'shapeshift'; ongoingProcess.set('connectingShapeshift', false); - popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { - $ionicHistory.goBack(); - }); - return; - } - - // Need to use the correct service to do it. - var amount = parseFloat(satoshis / 100000000); - - shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, amount, function onShiftIt(err, shapeshiftData) { - if (err) { - popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err, function () { - $ionicHistory.goBack(); - }); - } else { - vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/' + shapeshiftData.orderId; - vm.memoExpanded = !!vm.memo; - tx.toAddress = shapeshiftData.toAddress; - vm.destination.address = toAddress; - vm.destination.kind = 'shapeshift'; - } - ongoingProcess.set('connectingShapeshift', false); - }); + cb(); + } }); }); - } + }); } function onShareTransaction() { From 2a607d245da983062b0939287d796240dd90f852 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 16:10:05 +0900 Subject: [PATCH 09/15] using ionicModal for all devices --- src/js/services/onGoingProcess.js | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-) diff --git a/src/js/services/onGoingProcess.js b/src/js/services/onGoingProcess.js index 147985dbb..a8b8a4f1d 100644 --- a/src/js/services/onGoingProcess.js +++ b/src/js/services/onGoingProcess.js @@ -82,17 +82,12 @@ angular.module('copayApp.services').factory('ongoingProcess', function($log, $ti if (customHandler) { customHandler(processName, showName, isOn); } else if (root.onGoingProcessName) { - if (isCordova && !isWindowsPhoneApp) { - window.plugins.spinnerDialog.show(null, showName, root.clear); - } else { - - var tmpl; - if (isWindowsPhoneApp) tmpl = '
' + showName + '
'; - else tmpl = '
' + showName + '
'; - $ionicLoading.show({ - template: tmpl - }); - } + var tmpl; + if (isWindowsPhoneApp) tmpl = '
' + showName + '
'; + else tmpl = '
' + showName + '
'; + $ionicLoading.show({ + template: tmpl, + }); } else { if (isCordova && !isWindowsPhoneApp) { window.plugins.spinnerDialog.hide(); From 3b6bb1fcddfe4e95c3593d98d43b843071e2d3d0 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 16:10:21 +0900 Subject: [PATCH 10/15] ionicModal using for all devices --- src/js/services/onGoingProcess.js | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/src/js/services/onGoingProcess.js b/src/js/services/onGoingProcess.js index a8b8a4f1d..9c25c3c26 100644 --- a/src/js/services/onGoingProcess.js +++ b/src/js/services/onGoingProcess.js @@ -52,11 +52,7 @@ angular.module('copayApp.services').factory('ongoingProcess', function($log, $ti root.clear = function() { ongoingProcess = {}; - if (isCordova && !isWindowsPhoneApp) { - window.plugins.spinnerDialog.hide(); - } else { - $ionicLoading.hide(); - } + $ionicLoading.hide(); }; root.get = function(processName) { @@ -89,11 +85,7 @@ angular.module('copayApp.services').factory('ongoingProcess', function($log, $ti template: tmpl, }); } else { - if (isCordova && !isWindowsPhoneApp) { - window.plugins.spinnerDialog.hide(); - } else { - $ionicLoading.hide(); - } + $ionicLoading.hide(); } }; From 2d6a1528c10fd5f02ff7170ba34731cf15b44f93 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Wed, 5 Sep 2018 17:23:09 +0900 Subject: [PATCH 11/15] handle errors properly --- src/js/controllers/amount.js | 19 ++++-- src/js/services/shapeshift.service.js | 95 +++++++++++++++++---------- 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/js/controllers/amount.js b/src/js/controllers/amount.js index c24c567ed..c1cd9a118 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, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window, ongoingProcess) { +function amountController(configService, $filter, gettextCatalog, $ionicModal, $ionicScrollDelegate, lodash, $log, nodeWebkitService, rateService, $scope, $state, $timeout, sendFlowService, shapeshiftService, txFormatService, platformInfo, profileService, walletService, $window, ongoingProcess, popupService) { var vm = this; vm.allowSend = false; @@ -94,10 +94,19 @@ function amountController(configService, $filter, gettextCatalog, $ionicModal, $ vm.toWallet = profileService.getWallet(vm.toWalletId); ongoingProcess.set('connectingShapeshift', true); - shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) { - vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); - vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); - ongoingProcess.set('connectingShapeshift', false); + shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(err, data) { + + if (err) { + // Error stop here + ongoingProcess.set('connectingShapeshift', false); + popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () { + $ionicHistory.goBack(); + }); + } else { + vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum); + vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit); + ongoingProcess.set('connectingShapeshift', false); + } }); } } diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index c290f1fc3..21a5368fe 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -28,16 +28,32 @@ angular return service; + function handleError(response, defaultMessage, cb) { + if (!response || !response.error || !response.error.message) { + if (cb) { + cb(new Error(defaultMessage)); + } + } else { + if (cb) { + cb(new Error(response.error.message)); + } + } + } + function getMarketData(coinIn, coinOut, cb) { service.coinIn = coinIn; service.coinOut = coinOut; shapeshiftApiService .marketInfo(service.coinIn, service.coinOut) - .then(function (marketData) { - service.marketData = marketData; - service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); - if (cb) { - cb(marketData); + .then(function (response) { + if (!response || response.error) { + handleError(response, 'Invalid response', cb); + } else { + service.marketData = response; + service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); + if (cb) { + cb(null, response); + } } }); } @@ -45,8 +61,7 @@ angular function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { // Test if the amount is correct depending on the min and max if (!amount || typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { - var err = new Error('Invalid amount'); - cb(err); + cb(new Error('Invalid amount')); } else { // Init service data service.withdrawalAddress = withdrawalAddress; @@ -56,36 +71,44 @@ angular service.amount = amount; // Check the address - shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { - if (data && data.isvalid) { + shapeshiftApiService + .ValidateAddress(withdrawalAddress, coinOut) + .then(function onSuccess(response) { + if (response && response.isvalid) { - // Prepare the transaction shapeshift side - shapeshiftApiService.NormalTx(service).then(function onResponse(data) { - var txData = data; - - // If the content is not that it was expected, get back an error - if (!txData || !txData.orderId || !txData.deposit) { - cb(new Error('Invalid response')); - } else { - // Get back the data - service.depositInfo = txData; - var shapeshiftData = { - coinIn: coinIn, - coinOut: coinOut, - toWalletId: service.toWalletId, - minAmount: service.marketData.minimum, - maxAmount: service.marketData.maxLimit, - orderId: txData.orderId, - toAddress: txData.deposit - }; - cb(null, shapeshiftData); - } - }); - } else { - var err = new Error('Invalid address or coin'); - cb(err); - } - }); + // Prepare the transaction shapeshift side + shapeshiftApiService.NormalTx(service).then(function onResponse(response) { + // If error, return it + if (!response || response.error) { + handleError(response, 'Invalid response', cb); + } else { + var txData = response; + + // If the content is not that it was expected, get back an error + if (!txData || !txData.orderId || !txData.deposit) { + if (cb) { + cb(new Error('Invalid response')); + } + } else { + // Get back the data + service.depositInfo = txData; + var shapeshiftData = { + coinIn: coinIn, + coinOut: coinOut, + toWalletId: service.toWalletId, + minAmount: service.marketData.minimum, + maxAmount: service.marketData.maxLimit, + orderId: txData.orderId, + toAddress: txData.deposit + }; + cb(null, shapeshiftData); + } + } + }); + } else if (cb) { + cb(new Error('Invalid address or coin')); + } + }); } } } From 98e6e8ac7574f3c5a8bcfade29b7be64cc6d7e27 Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Thu, 6 Sep 2018 15:17:37 +0900 Subject: [PATCH 12/15] Recommendation #brendon :) --- i18n/po/template.pot | 20 +++++++++++++++ src/js/controllers/review.controller.js | 6 ++--- src/js/services/shapeshift.service.js | 33 +++++++++++-------------- 3 files changed, 37 insertions(+), 22 deletions(-) diff --git a/i18n/po/template.pot b/i18n/po/template.pot index f61012914..ca6288cbe 100644 --- a/i18n/po/template.pot +++ b/i18n/po/template.pot @@ -3873,4 +3873,24 @@ msgstr "" #: www/views/includes/incomingDataMenu.html:90 msgid "Open in web browser" +msgstr "" + +#: src/js/services/shapeshift.service.js.html:90 +msgid "Invalid address" +msgstr "" + +#: src/js/services/shapeshift.service.js.html:90 +msgid "Amount is not defined" +msgstr "" + +#: src/js/services/shapeshift.service.js.html:90 +msgid "Amount is below the minimun" +msgstr "" + +#: src/js/services/shapeshift.service.js.html:90 +msgid "Amount is above the limit" +msgstr "" + +#: src/js/services/shapeshift.service.js.html:90 +msgid "Invalid response from Shapeshift" msgstr "" \ No newline at end of file diff --git a/src/js/controllers/review.controller.js b/src/js/controllers/review.controller.js index f606e4898..c82838f7c 100644 --- a/src/js/controllers/review.controller.js +++ b/src/js/controllers/review.controller.js @@ -525,11 +525,11 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit if (err) { return cb(err); } else { + vm.destination.kind = 'shapeshift'; + vm.destination.address = toAddress; + tx.toAddress = shapeshiftData.toAddress; vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/' + shapeshiftData.orderId; vm.memoExpanded = !!vm.memo; - tx.toAddress = shapeshiftData.toAddress; - vm.destination.address = toAddress; - vm.destination.kind = 'shapeshift'; ongoingProcess.set('connectingShapeshift', false); cb(); } diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index 21a5368fe..fdfe5bcca 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -6,7 +6,7 @@ angular .module('bitcoincom.services') .factory('shapeshiftService', shapeshiftService); - function shapeshiftService(shapeshiftApiService) { + function shapeshiftService(shapeshiftApiService, gettext) { var service = { // Variables @@ -30,13 +30,9 @@ angular function handleError(response, defaultMessage, cb) { if (!response || !response.error || !response.error.message) { - if (cb) { - cb(new Error(defaultMessage)); - } + cb(new Error(defaultMessage)); } else { - if (cb) { - cb(new Error(response.error.message)); - } + cb(new Error(response.error.message)); } } @@ -51,17 +47,19 @@ angular } else { service.marketData = response; service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); - if (cb) { - cb(null, response); - } + cb(null, response); } }); } function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { // Test if the amount is correct depending on the min and max - if (!amount || typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) { - cb(new Error('Invalid amount')); + if (!amount || typeof amount !== 'number') { + cb(new Error(gettext('Amount is not defined')))); + } else if (amount < service.marketData.minimum) { + cb(new Error(gettext('Amount is below the minimun'))); + } else if (amount > service.marketData.maxLimit) { + cb(new Error(gettext('Amount is above the limit'))); } else { // Init service data service.withdrawalAddress = withdrawalAddress; @@ -75,20 +73,17 @@ angular .ValidateAddress(withdrawalAddress, coinOut) .then(function onSuccess(response) { if (response && response.isvalid) { - // Prepare the transaction shapeshift side shapeshiftApiService.NormalTx(service).then(function onResponse(response) { // If error, return it if (!response || response.error) { - handleError(response, 'Invalid response', cb); + handleError(response, gettext('Invalid response from Shapeshift'), cb); } else { var txData = response; // If the content is not that it was expected, get back an error if (!txData || !txData.orderId || !txData.deposit) { - if (cb) { - cb(new Error('Invalid response')); - } + cb(new Error(gettext('Invalid response from Shapeshift'))); } else { // Get back the data service.depositInfo = txData; @@ -105,8 +100,8 @@ angular } } }); - } else if (cb) { - cb(new Error('Invalid address or coin')); + } else { + cb(new Error(gettext('Invalid address'))); } }); } From a397e83cf051d3da377d631706d0d8376200dacf Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Thu, 6 Sep 2018 15:24:16 +0900 Subject: [PATCH 13/15] error case without .message --- src/js/services/shapeshift.service.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index fdfe5bcca..7b3076b87 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -29,8 +29,10 @@ angular return service; function handleError(response, defaultMessage, cb) { - if (!response || !response.error || !response.error.message) { + if (!response) { cb(new Error(defaultMessage)); + } else if (response.error && !response.error.message) { + cb(new Error(response.error)); } else { cb(new Error(response.error.message)); } From 71e530f535c948ac30c267c506081f5dd79742df Mon Sep 17 00:00:00 2001 From: Jean-Baptiste Dominguez Date: Thu, 6 Sep 2018 15:31:22 +0900 Subject: [PATCH 14/15] Unify the translation service --- src/js/services/shapeshift.service.js | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index 7b3076b87..2bc6a20c4 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -6,7 +6,7 @@ angular .module('bitcoincom.services') .factory('shapeshiftService', shapeshiftService); - function shapeshiftService(shapeshiftApiService, gettext) { + function shapeshiftService(shapeshiftApiService, gettextCatalog) { var service = { // Variables @@ -45,7 +45,7 @@ angular .marketInfo(service.coinIn, service.coinOut) .then(function (response) { if (!response || response.error) { - handleError(response, 'Invalid response', cb); + handleError(response, 'Invalid response from Shapeshift', cb); } else { service.marketData = response; service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); @@ -57,11 +57,11 @@ angular function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) { // Test if the amount is correct depending on the min and max if (!amount || typeof amount !== 'number') { - cb(new Error(gettext('Amount is not defined')))); + cb(new Error(gettextCatalog.getString('Amount is not defined')))); } else if (amount < service.marketData.minimum) { - cb(new Error(gettext('Amount is below the minimun'))); + cb(new Error(gettextCatalog.getString('Amount is below the minimun'))); } else if (amount > service.marketData.maxLimit) { - cb(new Error(gettext('Amount is above the limit'))); + cb(new Error(gettextCatalog.getString('Amount is above the limit'))); } else { // Init service data service.withdrawalAddress = withdrawalAddress; @@ -79,13 +79,13 @@ angular shapeshiftApiService.NormalTx(service).then(function onResponse(response) { // If error, return it if (!response || response.error) { - handleError(response, gettext('Invalid response from Shapeshift'), cb); + handleError(response, gettextCatalog.getString('Invalid response from Shapeshift'), cb); } else { var txData = response; // If the content is not that it was expected, get back an error if (!txData || !txData.orderId || !txData.deposit) { - cb(new Error(gettext('Invalid response from Shapeshift'))); + cb(new Error(gettextCatalog.getString('Invalid response from Shapeshift'))); } else { // Get back the data service.depositInfo = txData; @@ -103,7 +103,7 @@ angular } }); } else { - cb(new Error(gettext('Invalid address'))); + cb(new Error(gettextCatalog.getString('Invalid address'))); } }); } From c79ca6d1afa92f56f506fb2d411cc14e16f626e8 Mon Sep 17 00:00:00 2001 From: Brendon Duncan Date: Thu, 6 Sep 2018 18:34:59 +1200 Subject: [PATCH 15/15] More specific Shapeshift error handling. --- src/js/services/shapeshift.service.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/js/services/shapeshift.service.js b/src/js/services/shapeshift.service.js index 2bc6a20c4..77f0de297 100644 --- a/src/js/services/shapeshift.service.js +++ b/src/js/services/shapeshift.service.js @@ -29,12 +29,12 @@ angular return service; function handleError(response, defaultMessage, cb) { - if (!response) { - cb(new Error(defaultMessage)); - } else if (response.error && !response.error.message) { + if (response && typeof response.error === "string") { cb(new Error(response.error)); - } else { + } else if (response && response.error && response.error.message) { cb(new Error(response.error.message)); + } else { + cb(new Error(defaultMessage)); } }