2017-12-04 04:27:55 +01:00
|
|
|
// @flow
|
2017-12-21 18:32:51 +01:00
|
|
|
import Promise from 'bluebird';
|
2018-07-09 17:04:29 +02:00
|
|
|
import * as SHAPESHIFT_STATUSES from 'constants/shape_shift';
|
2017-12-21 18:32:51 +01:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
|
|
|
import { coinRegexPatterns } from 'util/shape_shift';
|
2017-12-04 04:27:55 +01:00
|
|
|
import type {
|
|
|
|
GetSupportedCoinsSuccess,
|
|
|
|
GetCoinStatsStart,
|
|
|
|
GetCoinStatsSuccess,
|
|
|
|
GetCoinStatsFail,
|
|
|
|
PrepareShapeShiftSuccess,
|
|
|
|
PrepareShapeShiftFail,
|
|
|
|
GetActiveShiftSuccess,
|
|
|
|
GetActiveShiftFail,
|
2017-12-21 18:32:51 +01:00
|
|
|
} from 'redux/reducers/shape_shift';
|
|
|
|
import type { FormikActions } from 'types/common';
|
2017-12-01 04:51:55 +01:00
|
|
|
|
2017-12-04 04:27:55 +01:00
|
|
|
// use promise chains instead of callbacks for shapeshift api
|
2017-12-21 18:32:51 +01:00
|
|
|
const shapeShift = Promise.promisifyAll(require('shapeshift.io'));
|
2017-12-01 04:51:55 +01:00
|
|
|
|
2017-12-04 04:27:55 +01:00
|
|
|
// All ShapeShift actions
|
|
|
|
// Action types defined in the reducer will contain some payload
|
|
|
|
export type Action =
|
2017-12-21 18:32:51 +01:00
|
|
|
| { type: ACTIONS.GET_SUPPORTED_COINS_START }
|
|
|
|
| { type: ACTIONS.GET_SUPPORTED_COINS_FAIL }
|
2017-12-04 04:27:55 +01:00
|
|
|
| GetSupportedCoinsSuccess
|
|
|
|
| GetCoinStatsStart
|
2017-12-21 18:32:51 +01:00
|
|
|
| { type: ACTIONS.GET_COIN_STATS_START }
|
2017-12-04 04:27:55 +01:00
|
|
|
| GetCoinStatsFail
|
|
|
|
| GetCoinStatsSuccess
|
2017-12-21 18:32:51 +01:00
|
|
|
| { type: ACTIONS.PREPARE_SHAPE_SHIFT_START }
|
2017-12-04 04:27:55 +01:00
|
|
|
| PrepareShapeShiftFail
|
|
|
|
| PrepareShapeShiftSuccess
|
2017-12-21 18:32:51 +01:00
|
|
|
| { type: ACTIONS.GET_ACTIVE_SHIFT_START }
|
2017-12-04 04:27:55 +01:00
|
|
|
| GetActiveShiftFail
|
|
|
|
| GetActiveShiftSuccess;
|
|
|
|
|
|
|
|
// Basic thunk types
|
|
|
|
// It would be nice to import these from types/common
|
|
|
|
// Not sure how that would work since they rely on the Action type
|
|
|
|
type PromiseAction = Promise<Action>;
|
2017-12-21 18:32:51 +01:00
|
|
|
export type Dispatch = (action: Action | PromiseAction | Array<Action>) => any;
|
2017-12-04 04:27:55 +01:00
|
|
|
type ThunkAction = (dispatch: Dispatch) => any;
|
|
|
|
|
|
|
|
// ShapeShift form values
|
|
|
|
export type ShapeShiftFormValues = {
|
|
|
|
originCoin: string,
|
|
|
|
returnAddress: ?string,
|
|
|
|
receiveAddress: string,
|
|
|
|
};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const getCoinStats = (coin: string) => (dispatch: Dispatch): ThunkAction => {
|
|
|
|
const pair = `${coin.toLowerCase()}_lbc`;
|
|
|
|
|
|
|
|
dispatch({ type: ACTIONS.GET_COIN_STATS_START, data: coin });
|
|
|
|
|
|
|
|
return shapeShift
|
|
|
|
.marketInfoAsync(pair)
|
|
|
|
.then(marketInfo => dispatch({ type: ACTIONS.GET_COIN_STATS_SUCCESS, data: marketInfo }))
|
|
|
|
.catch(err => dispatch({ type: ACTIONS.GET_COIN_STATS_FAIL, data: err }));
|
|
|
|
};
|
|
|
|
|
2017-12-04 04:27:55 +01:00
|
|
|
export const shapeShiftInit = () => (dispatch: Dispatch): ThunkAction => {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({ type: ACTIONS.GET_SUPPORTED_COINS_START });
|
2017-12-01 04:51:55 +01:00
|
|
|
|
|
|
|
return shapeShift
|
|
|
|
.coinsAsync()
|
|
|
|
.then(coinData => {
|
2018-07-09 17:04:29 +02:00
|
|
|
if (coinData.LBC.status === SHAPESHIFT_STATUSES.UNAVAILABLE) {
|
|
|
|
return dispatch({
|
|
|
|
type: ACTIONS.GET_SUPPORTED_COINS_FAIL,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-12-01 04:51:55 +01:00
|
|
|
let supportedCoins = [];
|
|
|
|
Object.keys(coinData).forEach(symbol => {
|
2018-07-31 00:23:25 +02:00
|
|
|
if (coinData[symbol].status === SHAPESHIFT_STATUSES.AVAILABLE) {
|
2017-12-01 04:51:55 +01:00
|
|
|
supportedCoins.push(coinData[symbol]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
// only use larger coins with client side validation
|
|
|
|
supportedCoins = supportedCoins
|
|
|
|
.filter(coin => coinRegexPatterns[coin.symbol])
|
|
|
|
.map(coin => coin.symbol);
|
|
|
|
|
|
|
|
dispatch({
|
2017-12-21 18:32:51 +01:00
|
|
|
type: ACTIONS.GET_SUPPORTED_COINS_SUCCESS,
|
2017-12-01 04:51:55 +01:00
|
|
|
data: supportedCoins,
|
|
|
|
});
|
2018-07-09 17:04:29 +02:00
|
|
|
return dispatch(getCoinStats(supportedCoins[0]));
|
2017-12-01 04:51:55 +01:00
|
|
|
})
|
2017-12-21 18:32:51 +01:00
|
|
|
.catch(err => dispatch({ type: ACTIONS.GET_SUPPORTED_COINS_FAIL, data: err }));
|
2017-12-01 04:51:55 +01:00
|
|
|
};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const createShapeShift = (values: ShapeShiftFormValues, actions: FormikActions) => (
|
2017-12-04 04:27:55 +01:00
|
|
|
dispatch: Dispatch
|
|
|
|
): ThunkAction => {
|
2017-12-21 18:32:51 +01:00
|
|
|
const { originCoin, returnAddress, receiveAddress: withdrawalAddress } = values;
|
2017-12-01 04:51:55 +01:00
|
|
|
|
|
|
|
const pair = `${originCoin.toLowerCase()}_lbc`;
|
|
|
|
const options = {
|
2017-12-13 22:36:30 +01:00
|
|
|
returnAddress,
|
2017-12-01 04:51:55 +01:00
|
|
|
};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({ type: ACTIONS.PREPARE_SHAPE_SHIFT_START });
|
2017-12-01 04:51:55 +01:00
|
|
|
return shapeShift
|
|
|
|
.shiftAsync(withdrawalAddress, pair, options)
|
2017-12-21 18:32:51 +01:00
|
|
|
.then(res => dispatch({ type: ACTIONS.PREPARE_SHAPE_SHIFT_SUCCESS, data: res }))
|
2017-12-01 04:51:55 +01:00
|
|
|
.catch(err => {
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({ type: ACTIONS.PREPARE_SHAPE_SHIFT_FAIL, data: err });
|
2017-12-04 04:27:55 +01:00
|
|
|
// for formik to stop the submit
|
2017-12-01 04:51:55 +01:00
|
|
|
actions.setSubmitting(false);
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2017-12-21 18:32:51 +01:00
|
|
|
export const getActiveShift = (depositAddress: string) => (dispatch: Dispatch): ThunkAction => {
|
|
|
|
dispatch({ type: ACTIONS.GET_ACTIVE_SHIFT_START });
|
2017-12-01 04:51:55 +01:00
|
|
|
|
|
|
|
return shapeShift
|
|
|
|
.statusAsync(depositAddress)
|
2017-12-21 18:32:51 +01:00
|
|
|
.then(res => dispatch({ type: ACTIONS.GET_ACTIVE_SHIFT_SUCCESS, data: res }))
|
|
|
|
.catch(err => dispatch({ type: ACTIONS.GET_ACTIVE_SHIFT_FAIL, data: err }));
|
2017-12-01 04:51:55 +01:00
|
|
|
};
|
|
|
|
|
2017-12-04 04:27:55 +01:00
|
|
|
export const clearShapeShift = () => (dispatch: Dispatch): Action =>
|
2017-12-21 18:32:51 +01:00
|
|
|
dispatch({ type: ACTIONS.CLEAR_SHAPE_SHIFT });
|