2017-06-11 18:00:28 -07:00
|
|
|
import {
|
2017-06-18 22:39:07 -07:00
|
|
|
SWAP_DESTINATION_AMOUNT,
|
|
|
|
SWAP_DESTINATION_KIND,
|
|
|
|
SWAP_ORIGIN_AMOUNT,
|
|
|
|
SWAP_ORIGIN_KIND,
|
2017-06-23 18:25:12 -07:00
|
|
|
SWAP_UPDATE_BITY_RATES,
|
2017-07-01 22:45:22 -07:00
|
|
|
SWAP_DESTINATION_ADDRESS,
|
|
|
|
SWAP_RESTART,
|
2017-07-22 14:24:03 -07:00
|
|
|
SWAP_STEP,
|
|
|
|
SWAP_REFERENCE_NUMBER
|
2017-06-19 16:35:14 -07:00
|
|
|
} from 'actions/swapConstants';
|
2017-07-22 14:24:03 -07:00
|
|
|
import { combineAndUpper } from 'utils/formatters';
|
2017-06-11 18:00:28 -07:00
|
|
|
|
2017-06-18 21:53:32 -07:00
|
|
|
export const ALL_CRYPTO_KIND_OPTIONS = ['BTC', 'ETH', 'REP'];
|
|
|
|
|
2017-06-11 18:00:28 -07:00
|
|
|
const initialState = {
|
2017-06-23 18:25:12 -07:00
|
|
|
originAmount: '',
|
|
|
|
destinationAmount: '',
|
2017-06-18 22:39:07 -07:00
|
|
|
originKind: 'BTC',
|
|
|
|
destinationKind: 'ETH',
|
|
|
|
destinationKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
|
|
|
element => element !== 'BTC'
|
|
|
|
),
|
|
|
|
originKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
|
|
|
element => element !== 'REP'
|
|
|
|
),
|
2017-07-22 14:24:03 -07:00
|
|
|
step: 1,
|
2017-06-23 23:08:33 -07:00
|
|
|
bityRates: {},
|
2017-07-22 14:24:03 -07:00
|
|
|
destinationAddress: '',
|
|
|
|
referenceNumber: '',
|
|
|
|
timeRemaining: '',
|
|
|
|
numberOfConfirmations: null,
|
|
|
|
orderStep: null
|
2017-06-11 18:00:28 -07:00
|
|
|
};
|
|
|
|
|
2017-06-19 16:35:14 -07:00
|
|
|
const buildDestinationAmount = (
|
|
|
|
originAmount,
|
|
|
|
originKind,
|
|
|
|
destinationKind,
|
|
|
|
bityRates
|
|
|
|
) => {
|
|
|
|
let pairName = combineAndUpper(originKind, destinationKind);
|
|
|
|
let bityRate = bityRates[pairName];
|
|
|
|
return originAmount * bityRate;
|
|
|
|
};
|
|
|
|
|
|
|
|
const buildDestinationKind = (originKind, destinationKind) => {
|
|
|
|
if (originKind === destinationKind) {
|
|
|
|
return ALL_CRYPTO_KIND_OPTIONS.filter(element => element !== originKind)[0];
|
|
|
|
} else {
|
|
|
|
return destinationKind;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-11 18:00:28 -07:00
|
|
|
export function swap(state = initialState, action) {
|
2017-06-18 22:39:07 -07:00
|
|
|
switch (action.type) {
|
|
|
|
case SWAP_ORIGIN_KIND: {
|
2017-06-19 16:35:14 -07:00
|
|
|
const newDestinationKind = buildDestinationKind(
|
2017-06-19 16:47:11 -07:00
|
|
|
action.value,
|
2017-06-19 16:35:14 -07:00
|
|
|
state.destinationKind
|
|
|
|
);
|
2017-06-18 22:39:07 -07:00
|
|
|
return {
|
|
|
|
...state,
|
2017-06-19 16:47:11 -07:00
|
|
|
originKind: action.value,
|
2017-06-19 16:35:14 -07:00
|
|
|
destinationKind: newDestinationKind,
|
2017-06-18 22:39:07 -07:00
|
|
|
destinationKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
2017-06-19 16:47:11 -07:00
|
|
|
element => element !== action.value
|
2017-06-19 16:35:14 -07:00
|
|
|
),
|
|
|
|
destinationAmount: buildDestinationAmount(
|
|
|
|
state.originAmount,
|
2017-06-19 16:47:11 -07:00
|
|
|
action.value,
|
2017-06-19 16:35:14 -07:00
|
|
|
newDestinationKind,
|
2017-06-19 16:47:11 -07:00
|
|
|
state.bityRates
|
2017-06-18 22:39:07 -07:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case SWAP_DESTINATION_KIND: {
|
|
|
|
return {
|
|
|
|
...state,
|
2017-06-19 16:47:11 -07:00
|
|
|
destinationKind: action.value,
|
2017-06-19 16:35:14 -07:00
|
|
|
destinationAmount: buildDestinationAmount(
|
|
|
|
state.originAmount,
|
|
|
|
state.originKind,
|
2017-06-19 16:47:11 -07:00
|
|
|
action.value,
|
|
|
|
state.bityRates
|
2017-06-19 16:35:14 -07:00
|
|
|
)
|
2017-06-18 22:39:07 -07:00
|
|
|
};
|
2017-06-11 18:00:28 -07:00
|
|
|
}
|
2017-06-18 22:39:07 -07:00
|
|
|
case SWAP_ORIGIN_AMOUNT:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
originAmount: action.value
|
|
|
|
};
|
|
|
|
case SWAP_DESTINATION_AMOUNT:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
destinationAmount: action.value
|
|
|
|
};
|
|
|
|
case SWAP_UPDATE_BITY_RATES:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
bityRates: {
|
|
|
|
...state.bityRates,
|
|
|
|
...action.value
|
|
|
|
}
|
|
|
|
};
|
2017-07-22 14:24:03 -07:00
|
|
|
case SWAP_STEP: {
|
2017-06-23 18:25:12 -07:00
|
|
|
return {
|
|
|
|
...state,
|
2017-07-22 14:24:03 -07:00
|
|
|
step: action.value
|
2017-07-01 22:45:22 -07:00
|
|
|
};
|
2017-07-22 14:24:03 -07:00
|
|
|
}
|
2017-07-01 22:45:22 -07:00
|
|
|
case SWAP_DESTINATION_ADDRESS:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
destinationAddress: action.value
|
|
|
|
};
|
|
|
|
case SWAP_RESTART:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
...initialState,
|
|
|
|
bityRates: state.bityRates
|
2017-06-23 23:08:33 -07:00
|
|
|
};
|
2017-07-22 14:24:03 -07:00
|
|
|
case SWAP_REFERENCE_NUMBER:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
referenceNumber: '2341asdfads',
|
|
|
|
timeRemaining: '2:30',
|
|
|
|
numberOfConfirmations: 3,
|
|
|
|
orderStep: 2
|
|
|
|
};
|
2017-06-18 22:39:07 -07:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-06-11 18:00:28 -07:00
|
|
|
}
|