Merge pull request #324 from Bitcoin-com/wallet/task/546
546 - revamp shapeshift service
This commit is contained in:
commit
688f2b97bc
9 changed files with 260 additions and 255 deletions
|
|
@ -3902,3 +3902,23 @@ msgstr ""
|
||||||
#: www/views/includes/incomingDataMenu.html:90
|
#: www/views/includes/incomingDataMenu.html:90
|
||||||
msgid "Open in web browser"
|
msgid "Open in web browser"
|
||||||
msgstr ""
|
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 ""
|
||||||
|
|
@ -2,7 +2,7 @@
|
||||||
|
|
||||||
angular.module('copayApp.controllers').controller('amountController', amountController);
|
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, popupService) {
|
||||||
var vm = this;
|
var vm = this;
|
||||||
|
|
||||||
vm.allowSend = false;
|
vm.allowSend = false;
|
||||||
|
|
@ -74,7 +74,6 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory,
|
||||||
initCurrencies();
|
initCurrencies();
|
||||||
|
|
||||||
passthroughParams = sendFlowService.state.getClone();
|
passthroughParams = sendFlowService.state.getClone();
|
||||||
|
|
||||||
console.log('amount onBeforeEnter after back sendflow ', passthroughParams);
|
console.log('amount onBeforeEnter after back sendflow ', passthroughParams);
|
||||||
|
|
||||||
vm.fromWalletId = passthroughParams.fromWalletId;
|
vm.fromWalletId = passthroughParams.fromWalletId;
|
||||||
|
|
@ -94,9 +93,20 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory,
|
||||||
vm.fromWallet = profileService.getWallet(vm.fromWalletId);
|
vm.fromWallet = profileService.getWallet(vm.fromWalletId);
|
||||||
vm.toWallet = profileService.getWallet(vm.toWalletId);
|
vm.toWallet = profileService.getWallet(vm.toWalletId);
|
||||||
|
|
||||||
shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(data) {
|
ongoingProcess.set('connectingShapeshift', true);
|
||||||
vm.thirdParty.data['minAmount'] = vm.minAmount = parseFloat(data.minimum);
|
shapeshiftService.getMarketData(vm.fromWallet.coin, vm.toWallet.coin, function(err, data) {
|
||||||
vm.thirdParty.data['maxAmount'] = vm.maxAmount = parseFloat(data.maxLimit);
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -113,7 +123,7 @@ function amountController(configService, $filter, gettextCatalog, $ionicHistory,
|
||||||
var reOp = /^[\*\+\-\/]$/;
|
var reOp = /^[\*\+\-\/]$/;
|
||||||
|
|
||||||
if (!isAndroid && !isIos) {
|
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.key) return;
|
||||||
if (e.which === 8) { // you can add others here inside brackets.
|
if (e.which === 8) { // you can add others here inside brackets.
|
||||||
if (!altCurrencyModal) {
|
if (!altCurrencyModal) {
|
||||||
|
|
|
||||||
|
|
@ -93,26 +93,48 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit
|
||||||
|
|
||||||
if (sendFlowData.thirdParty) {
|
if (sendFlowData.thirdParty) {
|
||||||
vm.thirdParty = sendFlowData.thirdParty;
|
vm.thirdParty = sendFlowData.thirdParty;
|
||||||
handleThirdPartyInitIfBip70();
|
switch (vm.thirdParty.id) {
|
||||||
handleThirdPartyInitIfShapeshift();
|
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) {
|
function _next() {
|
||||||
if (err) {
|
configService.get(function onConfig(err, configCache) {
|
||||||
$log.err('Error getting config.', err);
|
if (err) {
|
||||||
} else {
|
$log.err('Error getting config.', err);
|
||||||
config = configCache;
|
} else {
|
||||||
priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat';
|
config = configCache;
|
||||||
vm.origin.currencyColor = (vm.originWallet.coin === 'btc' ? defaults.bitcoinWalletColor : defaults.bitcoinCashWalletColor);
|
priceDisplayIsFiat = config.wallet.settings.priceDisplay === 'fiat';
|
||||||
console.log("coin", vm.originWallet.coin, vm.origin.currencyColor, config.bitcoinWalletColor, vm.originWallet.coin === 'btc');
|
vm.origin.currencyColor = (vm.originWallet.coin === 'btc' ? defaults.bitcoinWalletColor : defaults.bitcoinCashWalletColor);
|
||||||
unitFromSat = 1 / config.wallet.settings.unitToSatoshi;
|
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);
|
updateSendAmounts();
|
||||||
handleDestinationAsAddress(toAddress, coin);
|
getOriginWalletBalance(vm.originWallet);
|
||||||
handleDestinationAsWallet(sendFlowData.toWalletId);
|
handleDestinationAsAddress(toAddress, coin);
|
||||||
createVanityTransaction(data);
|
handleDestinationAsWallet(sendFlowData.toWalletId);
|
||||||
});
|
createVanityTransaction(data);
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
vm.approve = function() {
|
vm.approve = function() {
|
||||||
|
|
@ -458,72 +480,62 @@ function reviewController(addressbookService, bitcoinCashJsService, bitcore, bit
|
||||||
vm.destination.balanceCurrency = balanceText.currency;
|
vm.destination.balanceCurrency = balanceText.currency;
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleThirdPartyInitIfBip70() {
|
function initBip70() {
|
||||||
if (vm.thirdParty.id === 'bip70') {
|
vm.sendingTitle = gettextCatalog.getString('You are paying');
|
||||||
vm.sendingTitle = gettextCatalog.getString('You are paying');
|
vm.memo = vm.thirdParty.memo;
|
||||||
vm.memo = vm.thirdParty.memo;
|
vm.memoExpanded = !!vm.memo;
|
||||||
vm.memoExpanded = !!vm.memo;
|
vm.destination.name = vm.thirdParty.name;
|
||||||
vm.destination.name = vm.thirdParty.name;
|
|
||||||
|
|
||||||
txPayproData = {
|
txPayproData = {
|
||||||
caTrusted: vm.thirdParty.caTrusted,
|
caTrusted: vm.thirdParty.caTrusted,
|
||||||
domain: vm.thirdParty.domain,
|
domain: vm.thirdParty.domain,
|
||||||
expires: vm.thirdParty.expires,
|
expires: vm.thirdParty.expires,
|
||||||
toAddress: toAddress,
|
toAddress: toAddress,
|
||||||
url: vm.thirdParty.url,
|
url: vm.thirdParty.url,
|
||||||
verified: vm.thirdParty.verified,
|
verified: vm.thirdParty.verified,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleThirdPartyInitIfShapeshift() {
|
function initShapeshift(cb) {
|
||||||
if (vm.thirdParty.id === 'shapeshift') {
|
vm.sendingTitle = gettextCatalog.getString('You are shifting');
|
||||||
vm.sendingTitle = gettextCatalog.getString('You are shifting');
|
if (!vm.thirdParty.data) {
|
||||||
if (!vm.thirdParty.data) {
|
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();
|
||||||
|
|
||||||
|
|
||||||
|
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) {
|
||||||
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) {
|
|
||||||
if (err) {
|
if (err) {
|
||||||
ongoingProcess.set('connectingShapeshift', false);
|
return cb(err);
|
||||||
popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () {
|
|
||||||
$ionicHistory.goBack();
|
|
||||||
});
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, function onShiftIt(err, shapeshiftData) {
|
// Need to use the correct service to do it.
|
||||||
if (err && err != null) {
|
var amount = parseFloat(satoshis / 100000000);
|
||||||
ongoingProcess.set('connectingShapeshift', false);
|
|
||||||
popupService.showAlert(gettextCatalog.getString('Shapeshift Error'), err.toString(), function () {
|
shapeshiftService.shiftIt(vm.originWallet.coin, toWallet.coin, withdrawalAddr, returnAddr, amount, function onShiftIt(err, shapeshiftData) {
|
||||||
$ionicHistory.goBack();
|
if (err) {
|
||||||
});
|
return cb(err);
|
||||||
} else {
|
} else {
|
||||||
vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/' + shapeshiftData.orderId;
|
vm.destination.kind = 'shapeshift';
|
||||||
vm.memoExpanded = !!vm.memo;
|
vm.destination.address = toAddress;
|
||||||
tx.toAddress = shapeshiftData.toAddress;
|
tx.toAddress = shapeshiftData.toAddress;
|
||||||
vm.destination.address = toAddress;
|
vm.memo = 'ShapeShift Order:\nhttps://www.shapeshift.io/#/status/' + shapeshiftData.orderId;
|
||||||
vm.destination.kind = 'shapeshift';
|
vm.memoExpanded = !!vm.memo;
|
||||||
}
|
ongoingProcess.set('connectingShapeshift', false);
|
||||||
});
|
cb();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
}
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function onShareTransaction() {
|
function onShareTransaction() {
|
||||||
|
|
|
||||||
|
|
@ -52,11 +52,7 @@ angular.module('copayApp.services').factory('ongoingProcess', function($log, $ti
|
||||||
|
|
||||||
root.clear = function() {
|
root.clear = function() {
|
||||||
ongoingProcess = {};
|
ongoingProcess = {};
|
||||||
if (isCordova && !isWindowsPhoneApp) {
|
$ionicLoading.hide();
|
||||||
window.plugins.spinnerDialog.hide();
|
|
||||||
} else {
|
|
||||||
$ionicLoading.hide();
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
root.get = function(processName) {
|
root.get = function(processName) {
|
||||||
|
|
@ -82,23 +78,14 @@ angular.module('copayApp.services').factory('ongoingProcess', function($log, $ti
|
||||||
if (customHandler) {
|
if (customHandler) {
|
||||||
customHandler(processName, showName, isOn);
|
customHandler(processName, showName, isOn);
|
||||||
} else if (root.onGoingProcessName) {
|
} else if (root.onGoingProcessName) {
|
||||||
if (isCordova && !isWindowsPhoneApp) {
|
var tmpl;
|
||||||
window.plugins.spinnerDialog.show(null, showName, root.clear);
|
if (isWindowsPhoneApp) tmpl = '<div>' + showName + '</div>';
|
||||||
} else {
|
else tmpl = '<div class="item-icon-left">' + showName + '<ion-spinner class="spinner-stable" icon="lines"></ion-spinner></div>';
|
||||||
|
$ionicLoading.show({
|
||||||
var tmpl;
|
template: tmpl,
|
||||||
if (isWindowsPhoneApp) tmpl = '<div>' + showName + '</div>';
|
});
|
||||||
else tmpl = '<div class="item-icon-left">' + showName + '<ion-spinner class="spinner-stable" icon="lines"></ion-spinner></div>';
|
|
||||||
$ionicLoading.show({
|
|
||||||
template: tmpl
|
|
||||||
});
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if (isCordova && !isWindowsPhoneApp) {
|
$ionicLoading.hide();
|
||||||
window.plugins.spinnerDialog.hide();
|
|
||||||
} else {
|
|
||||||
$ionicLoading.hide();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -101,7 +101,7 @@ angular
|
||||||
Object.keys(params).forEach(function forNewParam(key) {
|
Object.keys(params).forEach(function forNewParam(key) {
|
||||||
service.state[key] = params[key];
|
service.state[key] = params[key];
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pop state
|
* Pop state
|
||||||
|
|
@ -116,7 +116,7 @@ angular
|
||||||
} else {
|
} else {
|
||||||
clear();
|
clear();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Push state
|
* Push state
|
||||||
|
|
@ -129,14 +129,14 @@ angular
|
||||||
service.previousStates.push(currentParams);
|
service.previousStates.push(currentParams);
|
||||||
clearCurrent();
|
clearCurrent();
|
||||||
map(params);
|
map(params);
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Is empty stack
|
* Is empty stack
|
||||||
*/
|
*/
|
||||||
function isEmpty() {
|
function isEmpty() {
|
||||||
return service.previousStates.length == 0;
|
return service.previousStates.length == 0;
|
||||||
};
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
@ -144,6 +144,5 @@ angular
|
||||||
sendFlowStateService.pop();
|
sendFlowStateService.pop();
|
||||||
sendFlowRouterService.goBack();
|
sendFlowRouterService.goBack();
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
})();
|
})();
|
||||||
|
|
@ -328,18 +328,23 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q)
|
||||||
$scope.amount, $scope.withdrawalAddress,
|
$scope.amount, $scope.withdrawalAddress,
|
||||||
$scope.coinIn, $scope.coinOut
|
$scope.coinIn, $scope.coinOut
|
||||||
);
|
);
|
||||||
|
console.log('shapeshiftApiService.FixedAmountTx()');
|
||||||
console.log(fixedTx);
|
console.log(fixedTx);
|
||||||
SSA.FixedAmountTx(fixedTx, function (data) {
|
SSA.FixedAmountTx(fixedTx, function (data) {
|
||||||
console.log(data)
|
console.log(data);
|
||||||
return promise.resolve({ fixedTxData : data.success });
|
promise.resolve(data);
|
||||||
});
|
});
|
||||||
return promise.promise;
|
return promise.promise;
|
||||||
},
|
},
|
||||||
NormalTx : function($scope){
|
NormalTx : function($scope){
|
||||||
var promise = $q.defer();
|
var promise = $q.defer();
|
||||||
var normalTx = SSA.CreateNormalTx($scope.withdrawalAddress, $scope.coinIn, $scope.coinOut);
|
var normalTx = SSA.CreateNormalTx($scope.withdrawalAddress, $scope.coinIn, $scope.coinOut);
|
||||||
|
|
||||||
|
console.log('shapeshiftApiService.NormalTx()');
|
||||||
|
console.log(normalTx);
|
||||||
SSA.NormalTx(normalTx, function (data) {
|
SSA.NormalTx(normalTx, function (data) {
|
||||||
promise.resolve({ normalTxData : data });
|
console.log(data);
|
||||||
|
promise.resolve(data);
|
||||||
});
|
});
|
||||||
return promise.promise;
|
return promise.promise;
|
||||||
},
|
},
|
||||||
|
|
@ -360,11 +365,12 @@ angular.module('copayApp.services').factory('shapeshiftApiService', function($q)
|
||||||
return promise.promise;
|
return promise.promise;
|
||||||
},
|
},
|
||||||
ValidateAddress : function(address, coin) {
|
ValidateAddress : function(address, coin) {
|
||||||
var promise = $q.defer();
|
var promise = $q.defer();
|
||||||
SSA.ValidateAdddress(address, coin, function(data){
|
SSA.ValidateAdddress(address, coin, function onRequest(data){
|
||||||
promise.resolve(data);
|
console.log(data);
|
||||||
});
|
promise.resolve(data);
|
||||||
return promise.promise;
|
});
|
||||||
|
return promise.promise;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
|
||||||
112
src/js/services/shapeshift.service.js
Normal file
112
src/js/services/shapeshift.service.js
Normal file
|
|
@ -0,0 +1,112 @@
|
||||||
|
'use strict';
|
||||||
|
|
||||||
|
(function(){
|
||||||
|
|
||||||
|
angular
|
||||||
|
.module('bitcoincom.services')
|
||||||
|
.factory('shapeshiftService', shapeshiftService);
|
||||||
|
|
||||||
|
function shapeshiftService(shapeshiftApiService, gettextCatalog) {
|
||||||
|
|
||||||
|
var service = {
|
||||||
|
// Variables
|
||||||
|
coinIn: '',
|
||||||
|
coinOut: '',
|
||||||
|
withdrawalAddress: '',
|
||||||
|
returnAddress: '',
|
||||||
|
amount: '',
|
||||||
|
marketData: {},
|
||||||
|
coins: {
|
||||||
|
'BTC': {name: 'Bitcoin', symbol: 'BTC'},
|
||||||
|
'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'}
|
||||||
|
},
|
||||||
|
|
||||||
|
// Functions
|
||||||
|
getMarketData: getMarketData,
|
||||||
|
shiftIt: shiftIt
|
||||||
|
};
|
||||||
|
|
||||||
|
return service;
|
||||||
|
|
||||||
|
function handleError(response, defaultMessage, cb) {
|
||||||
|
if (response && typeof response.error === "string") {
|
||||||
|
cb(new Error(response.error));
|
||||||
|
} else if (response && response.error && response.error.message) {
|
||||||
|
cb(new Error(response.error.message));
|
||||||
|
} else {
|
||||||
|
cb(new Error(defaultMessage));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
function getMarketData(coinIn, coinOut, cb) {
|
||||||
|
service.coinIn = coinIn;
|
||||||
|
service.coinOut = coinOut;
|
||||||
|
shapeshiftApiService
|
||||||
|
.marketInfo(service.coinIn, service.coinOut)
|
||||||
|
.then(function (response) {
|
||||||
|
if (!response || response.error) {
|
||||||
|
handleError(response, 'Invalid response from Shapeshift', cb);
|
||||||
|
} else {
|
||||||
|
service.marketData = response;
|
||||||
|
service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase();
|
||||||
|
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') {
|
||||||
|
cb(new Error(gettextCatalog.getString('Amount is not defined'))));
|
||||||
|
} else if (amount < service.marketData.minimum) {
|
||||||
|
cb(new Error(gettextCatalog.getString('Amount is below the minimun')));
|
||||||
|
} else if (amount > service.marketData.maxLimit) {
|
||||||
|
cb(new Error(gettextCatalog.getString('Amount is above the limit')));
|
||||||
|
} 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(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, 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(gettextCatalog.getString('Invalid response from Shapeshift')));
|
||||||
|
} 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 {
|
||||||
|
cb(new Error(gettextCatalog.getString('Invalid address')));
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})();
|
||||||
|
|
@ -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;
|
|
||||||
});
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue