2017-09-25 02:06:28 +00:00
|
|
|
import * as actionTypes from 'actions/swap';
|
2017-12-11 17:44:53 +00:00
|
|
|
import * as stateTypes from './types';
|
|
|
|
import * as schema from './schema';
|
2017-09-25 02:06:28 +00:00
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { normalize } from 'normalizr';
|
2017-09-25 02:06:28 +00:00
|
|
|
|
|
|
|
export interface State {
|
|
|
|
step: number;
|
2017-12-11 17:44:53 +00:00
|
|
|
origin: stateTypes.SwapInput;
|
|
|
|
destination: stateTypes.SwapInput;
|
|
|
|
options: stateTypes.NormalizedOptions;
|
|
|
|
bityRates: stateTypes.NormalizedBityRates;
|
2018-01-02 18:04:50 +00:00
|
|
|
// Change this
|
|
|
|
shapeshiftRates: stateTypes.NormalizedBityRates;
|
|
|
|
provider: string;
|
2017-09-25 02:06:28 +00:00
|
|
|
bityOrder: any;
|
2018-01-02 18:04:50 +00:00
|
|
|
shapeshiftOrder: any;
|
2017-09-25 02:06:28 +00:00
|
|
|
destinationAddress: string;
|
|
|
|
isFetchingRates: boolean | null;
|
|
|
|
secondsRemaining: number | null;
|
|
|
|
outputTx: string | null;
|
|
|
|
isPostingOrder: boolean;
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus: string | null;
|
|
|
|
shapeshiftOrderStatus: string | null;
|
2017-09-25 02:06:28 +00:00
|
|
|
orderTimestampCreatedISOString: string | null;
|
|
|
|
paymentAddress: string | null;
|
|
|
|
validFor: number | null;
|
|
|
|
orderId: string | null;
|
2018-01-02 18:04:50 +00:00
|
|
|
showLiteSend: boolean;
|
2017-09-25 02:06:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const INITIAL_STATE: State = {
|
|
|
|
step: 1,
|
2017-12-11 17:44:53 +00:00
|
|
|
origin: { id: 'BTC', amount: NaN },
|
|
|
|
destination: { id: 'ETH', amount: NaN },
|
|
|
|
options: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
|
|
|
bityRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
2018-01-02 18:04:50 +00:00
|
|
|
shapeshiftRates: {
|
|
|
|
byId: {},
|
|
|
|
allIds: []
|
|
|
|
},
|
2018-02-07 23:42:07 +00:00
|
|
|
provider: 'shapeshift',
|
2017-09-25 02:06:28 +00:00
|
|
|
destinationAddress: '',
|
|
|
|
bityOrder: {},
|
2018-01-02 18:04:50 +00:00
|
|
|
shapeshiftOrder: {},
|
2017-09-25 02:06:28 +00:00
|
|
|
isFetchingRates: null,
|
|
|
|
secondsRemaining: null,
|
|
|
|
outputTx: null,
|
|
|
|
isPostingOrder: false,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus: null,
|
|
|
|
shapeshiftOrderStatus: null,
|
2017-09-25 02:06:28 +00:00
|
|
|
orderTimestampCreatedISOString: null,
|
|
|
|
paymentAddress: null,
|
|
|
|
validFor: null,
|
2018-01-02 18:04:50 +00:00
|
|
|
orderId: null,
|
|
|
|
showLiteSend: false
|
2017-09-25 02:06:28 +00:00
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
export function swap(state: State = INITIAL_STATE, action: actionTypes.SwapAction) {
|
2017-09-25 02:06:28 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case TypeKeys.SWAP_LOAD_BITY_RATES_SUCCEEDED:
|
2017-12-11 17:44:53 +00:00
|
|
|
const { payload } = action;
|
2017-09-25 02:06:28 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
bityRates: {
|
2018-01-02 18:04:50 +00:00
|
|
|
byId: normalize(payload, [schema.providerRate]).entities.providerRates,
|
|
|
|
allIds: schema.allIds(normalize(payload, [schema.providerRate]).entities.providerRates)
|
2017-12-11 17:44:53 +00:00
|
|
|
},
|
|
|
|
options: {
|
2018-01-02 18:04:50 +00:00
|
|
|
byId: Object.assign(
|
|
|
|
{},
|
|
|
|
normalize(payload, [schema.providerRate]).entities.options,
|
|
|
|
state.options.byId
|
|
|
|
),
|
|
|
|
allIds: [
|
|
|
|
...schema.allIds(normalize(payload, [schema.providerRate]).entities.options),
|
|
|
|
...state.options.allIds
|
|
|
|
]
|
|
|
|
},
|
|
|
|
isFetchingRates: false
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_SUCCEEDED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
shapeshiftRates: {
|
|
|
|
byId: normalize(action.payload, [schema.providerRate]).entities.providerRates,
|
|
|
|
allIds: schema.allIds(
|
|
|
|
normalize(action.payload, [schema.providerRate]).entities.providerRates
|
|
|
|
)
|
|
|
|
},
|
|
|
|
options: {
|
|
|
|
byId: Object.assign(
|
|
|
|
{},
|
|
|
|
normalize(action.payload, [schema.providerRate]).entities.options,
|
|
|
|
state.options.byId
|
|
|
|
),
|
|
|
|
allIds: [
|
|
|
|
...schema.allIds(normalize(action.payload, [schema.providerRate]).entities.options),
|
|
|
|
...state.options.allIds
|
|
|
|
]
|
2017-09-25 02:06:28 +00:00
|
|
|
},
|
|
|
|
isFetchingRates: false
|
|
|
|
};
|
2017-12-11 17:44:53 +00:00
|
|
|
case TypeKeys.SWAP_INIT: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
origin: action.payload.origin,
|
|
|
|
destination: action.payload.destination
|
|
|
|
};
|
|
|
|
}
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.SWAP_STEP: {
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
step: action.payload
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case TypeKeys.SWAP_DESTINATION_ADDRESS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
destinationAddress: action.payload
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_RESTART:
|
|
|
|
return {
|
|
|
|
...INITIAL_STATE,
|
2018-01-02 18:04:50 +00:00
|
|
|
options: state.options,
|
|
|
|
bityRates: state.bityRates,
|
|
|
|
shapeshiftRates: state.shapeshiftRates
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_BITY_ORDER_CREATE_REQUESTED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isPostingOrder: true
|
2017-09-25 02:06:28 +00:00
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_REQUESTED:
|
2017-09-25 02:06:28 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isPostingOrder: true
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_BITY_ORDER_CREATE_FAILED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isPostingOrder: false
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_FAILED:
|
2017-09-25 02:06:28 +00:00
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isPostingOrder: false
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_BITY_ORDER_CREATE_SUCCEEDED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
bityOrder: {
|
|
|
|
...action.payload
|
|
|
|
},
|
|
|
|
isPostingOrder: false,
|
|
|
|
originAmount: parseFloat(action.payload.input.amount),
|
|
|
|
destinationAmount: parseFloat(action.payload.output.amount),
|
|
|
|
secondsRemaining: action.payload.validFor, // will get update
|
|
|
|
validFor: action.payload.validFor, // to build from local storage
|
|
|
|
orderTimestampCreatedISOString: action.payload.timestamp_created,
|
|
|
|
paymentAddress: action.payload.payment_address,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus: action.payload.status,
|
2017-09-25 02:06:28 +00:00
|
|
|
orderId: action.payload.id
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_SUCCEEDED:
|
|
|
|
const currDate = Date.now();
|
|
|
|
|
|
|
|
const secondsRemaining = Math.floor((+new Date(action.payload.expiration) - currDate) / 1000);
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
shapeshiftOrder: {
|
|
|
|
...action.payload
|
|
|
|
},
|
|
|
|
isPostingOrder: false,
|
|
|
|
originAmount: parseFloat(action.payload.depositAmount),
|
|
|
|
destinationAmount: parseFloat(action.payload.withdrawalAmount),
|
|
|
|
secondsRemaining,
|
|
|
|
validFor: secondsRemaining,
|
|
|
|
orderTimestampCreatedISOString: new Date(currDate).toISOString(),
|
|
|
|
paymentAddress: action.payload.deposit,
|
|
|
|
shapeshiftOrderStatus: 'no_deposits',
|
|
|
|
orderId: action.payload.orderId
|
|
|
|
};
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.SWAP_BITY_ORDER_STATUS_SUCCEEDED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
outputTx: action.payload.output.reference,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus:
|
2017-09-25 02:06:28 +00:00
|
|
|
action.payload.output.status === 'FILL'
|
|
|
|
? action.payload.output.status
|
|
|
|
: action.payload.input.status
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_SHAPESHIFT_ORDER_STATUS_SUCCEEDED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
outputTx: action.payload && action.payload.transaction ? action.payload.transaction : null,
|
|
|
|
shapeshiftOrderStatus: action.payload.status
|
|
|
|
};
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.SWAP_ORDER_TIME:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
secondsRemaining: action.payload
|
|
|
|
};
|
|
|
|
|
|
|
|
case TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFetchingRates: true
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFetchingRates: true
|
|
|
|
};
|
2017-09-25 02:06:28 +00:00
|
|
|
case TypeKeys.SWAP_STOP_LOAD_BITY_RATES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFetchingRates: false
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
case TypeKeys.SWAP_STOP_LOAD_SHAPESHIFT_RATES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
isFetchingRates: false
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_CHANGE_PROVIDER:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
provider: action.payload
|
|
|
|
};
|
|
|
|
case TypeKeys.SWAP_SHOW_LITE_SEND:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
showLiteSend: action.payload
|
|
|
|
};
|
2017-09-25 02:06:28 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
|
|
|
}
|