Migrate to our new standard for services

This commit is contained in:
Jean-Baptiste Dominguez 2018-09-05 14:43:16 +09:00
commit 9128d3ebe2

View file

@ -1,62 +1,83 @@
'use strict'; 'use strict';
angular.module('bitcoincom.services').factory('shapeshiftService', function (shapeshiftApiService) { (function(){
var root = {};
root.ShiftState = 'Shift';
root.coinIn = '';
root.coinOut = '';
root.withdrawalAddress = '';
root.returnAddress = '';
root.amount = '';
root.marketData = {};
root.getMarketData = function (coinIn, coinOut, cb) { angular
root.coinIn = coinIn; .module('bitcoincom.services')
root.coinOut = coinOut; .factory('shapeshiftService', shapeshiftService);
function shapeshiftService(shapeshiftApiService) {
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 getMarketData(coinIn, coinOut, cb) {
service.coinIn = coinIn;
service.coinOut = coinOut;
shapeshiftApiService shapeshiftApiService
.marketInfo(root.coinIn, root.coinOut) .marketInfo(service.coinIn, service.coinOut)
.then(function (marketData) { .then(function (marketData) {
root.marketData = marketData; service.marketData = marketData;
root.rateString = root.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase(); service.rateString = service.marketData.rate.toString() + ' ' + coinOut.toUpperCase() + '/' + coinIn.toUpperCase();
if (cb) { if (cb) {
cb(marketData); cb(marketData);
} }
}); });
}; }
root.coins = { function shiftIt(coinIn, coinOut, withdrawalAddress, returnAddress, amount, cb) {
'BTC': {name: 'Bitcoin', symbol: 'BTC'}, // Test if the amount is correct depending on the min and max
'BCH': {name: 'Bitcoin Cash', symbol: 'BCH'} if (typeof amount !== 'number' || amount < service.marketData.minimum || amount > service.marketData.maxLimit) {
};
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'); var err = new Error('Invalid amount');
cb(err); cb(err);
} else { } else {
root.withdrawalAddress = withdrawalAddress; // Init service data
root.returnAddress = returnAddress; service.withdrawalAddress = withdrawalAddress;
root.coinIn = coinIn; service.returnAddress = returnAddress;
root.coinOut = coinOut; service.coinIn = coinIn;
root.amount = amount; service.coinOut = coinOut;
service.amount = amount;
// Check the address
shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) { shapeshiftApiService.ValidateAddress(withdrawalAddress, coinOut).then(function onSuccess(data) {
if (data && data.isvalid) { if (data && data.isvalid) {
shapeshiftApiService.NormalTx(root).then(function onResponse(data) {
// Prepare the transaction shapeshift side
shapeshiftApiService.NormalTx(service).then(function onResponse(data) {
var txData = data; var txData = data;
// If the content is not that it was expected, get back an error
if (!txData || !txData.orderId || !txData.deposit) { if (!txData || !txData.orderId || !txData.deposit) {
cb(new Error('Invalid response')); cb(new Error('Invalid response'));
} else { } else {
root.depositInfo = txData; // Get back the data
service.depositInfo = txData;
var shapeshiftData = { var shapeshiftData = {
coinIn: coinIn, coinIn: coinIn,
coinOut: coinOut, coinOut: coinOut,
toWalletId: root.toWalletId, toWalletId: service.toWalletId,
minAmount: root.marketData.minimum, minAmount: service.marketData.minimum,
maxAmount: root.marketData.maxLimit, maxAmount: service.marketData.maxLimit,
orderId: txData.orderId, orderId: txData.orderId,
toAddress: txData.deposit toAddress: txData.deposit
}; };
cb(null, shapeshiftData); cb(null, shapeshiftData);
} }
}); });
@ -66,6 +87,6 @@ angular.module('bitcoincom.services').factory('shapeshiftService', function (sha
} }
}); });
} }
}; }
return root; }
}); })();