Wallet/src/js/services/shapeshift.service.js

71 lines
2.3 KiB
JavaScript
Raw Normal View History

'use strict';
2018-09-05 12:45:20 +09:00
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 = {};
root.getMarketData = function (coinIn, coinOut, cb) {
root.coinIn = coinIn;
root.coinOut = coinOut;
2018-09-05 11:37:29 +09:00
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'}
};
2018-09-05 12:45:20 +09:00
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
};
2018-09-05 12:45:20 +09:00
cb(null, shapeshiftData);
}
});
} else {
var err = new Error('Invalid address or coin');
cb(err);
}
});
2018-09-05 12:45:20 +09:00
}
};
return root;
});