2017-06-12 01:00:28 +00:00
|
|
|
import {
|
2017-06-19 05:39:07 +00:00
|
|
|
SWAP_DESTINATION_AMOUNT,
|
|
|
|
SWAP_DESTINATION_KIND,
|
|
|
|
SWAP_ORIGIN_AMOUNT,
|
|
|
|
SWAP_ORIGIN_KIND,
|
2017-06-24 01:25:12 +00:00
|
|
|
SWAP_UPDATE_BITY_RATES,
|
|
|
|
SWAP_PART_ONE_COMPLETE
|
2017-06-19 23:35:14 +00:00
|
|
|
} from 'actions/swapConstants';
|
|
|
|
import { combineAndUpper } from 'api/bity';
|
2017-06-12 01:00:28 +00:00
|
|
|
|
2017-06-19 04:53:32 +00:00
|
|
|
export const ALL_CRYPTO_KIND_OPTIONS = ['BTC', 'ETH', 'REP'];
|
|
|
|
|
2017-06-12 01:00:28 +00:00
|
|
|
const initialState = {
|
2017-06-24 01:25:12 +00:00
|
|
|
originAmount: '',
|
|
|
|
destinationAmount: '',
|
2017-06-19 05:39:07 +00:00
|
|
|
originKind: 'BTC',
|
|
|
|
destinationKind: 'ETH',
|
|
|
|
destinationKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
|
|
|
element => element !== 'BTC'
|
|
|
|
),
|
|
|
|
originKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
|
|
|
element => element !== 'REP'
|
|
|
|
),
|
2017-06-24 01:25:12 +00:00
|
|
|
partOneComplete: false,
|
2017-06-19 05:39:07 +00:00
|
|
|
bityRates: {}
|
2017-06-12 01:00:28 +00:00
|
|
|
};
|
|
|
|
|
2017-06-19 23:35:14 +00: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-12 01:00:28 +00:00
|
|
|
export function swap(state = initialState, action) {
|
2017-06-19 05:39:07 +00:00
|
|
|
switch (action.type) {
|
|
|
|
case SWAP_ORIGIN_KIND: {
|
2017-06-19 23:35:14 +00:00
|
|
|
const newDestinationKind = buildDestinationKind(
|
2017-06-19 23:47:11 +00:00
|
|
|
action.value,
|
2017-06-19 23:35:14 +00:00
|
|
|
state.destinationKind
|
|
|
|
);
|
2017-06-19 05:39:07 +00:00
|
|
|
return {
|
|
|
|
...state,
|
2017-06-19 23:47:11 +00:00
|
|
|
originKind: action.value,
|
2017-06-19 23:35:14 +00:00
|
|
|
destinationKind: newDestinationKind,
|
2017-06-19 05:39:07 +00:00
|
|
|
destinationKindOptions: ALL_CRYPTO_KIND_OPTIONS.filter(
|
2017-06-19 23:47:11 +00:00
|
|
|
element => element !== action.value
|
2017-06-19 23:35:14 +00:00
|
|
|
),
|
|
|
|
destinationAmount: buildDestinationAmount(
|
|
|
|
state.originAmount,
|
2017-06-19 23:47:11 +00:00
|
|
|
action.value,
|
2017-06-19 23:35:14 +00:00
|
|
|
newDestinationKind,
|
2017-06-19 23:47:11 +00:00
|
|
|
state.bityRates
|
2017-06-19 05:39:07 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
}
|
|
|
|
case SWAP_DESTINATION_KIND: {
|
|
|
|
return {
|
|
|
|
...state,
|
2017-06-19 23:47:11 +00:00
|
|
|
destinationKind: action.value,
|
2017-06-19 23:35:14 +00:00
|
|
|
destinationAmount: buildDestinationAmount(
|
|
|
|
state.originAmount,
|
|
|
|
state.originKind,
|
2017-06-19 23:47:11 +00:00
|
|
|
action.value,
|
|
|
|
state.bityRates
|
2017-06-19 23:35:14 +00:00
|
|
|
)
|
2017-06-19 05:39:07 +00:00
|
|
|
};
|
2017-06-12 01:00:28 +00:00
|
|
|
}
|
2017-06-19 05:39:07 +00: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-06-24 01:25:12 +00:00
|
|
|
case SWAP_PART_ONE_COMPLETE:
|
|
|
|
return {
|
|
|
|
...state,
|
|
|
|
partOneComplete: action.value
|
|
|
|
};
|
2017-06-19 05:39:07 +00:00
|
|
|
default:
|
|
|
|
return state;
|
|
|
|
}
|
2017-06-12 01:00:28 +00:00
|
|
|
}
|