Migrate to our new standard for services
This commit is contained in:
parent
887270bfa9
commit
9128d3ebe2
1 changed files with 85 additions and 64 deletions
|
|
@ -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);
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
})();
|
||||
Loading…
Add table
Add a link
Reference in a new issue