Eddie Wang 88532cdc3c Shapeshift Integration (#564)
* progress

* Normalize bity api response

* Filter api response

* Track swap information in component state

* Update dropdown onchange

* remove dead code

* Update Min Max Validation

* Update minmax err msg && fix onChangeOriginKind

* Add origin & destination to redux state

* Update types & Update tests

* Update types

* Update swap.spec.ts test

* Remove commented out code

* Remove hardcoded coin array

* Create types.ts for swap reducer

* Update swapinput type

* Update bityRates in localStorage & Replace all instances of ...Kind / ...Amount props

* Add shapeshift banner

* initial work for sagas

* Update Types

* Update swap reducer initial state

* Update Types & Store empty obj for bityRates / options

* Update more types

* added shapeshift file and rates comments

* action reducers and prop mapping to components

* add typings and swap icon

* more actions reducers and sagas

* debugging shapeshift service

* add Headers

* Fix content type

* add order reset saga and ui fixes

* remove console log and swap b/w Bity and Shapeshift

* working state for Shapeshift and Bity - tested with mainnet

* add icon component

* UI improvements and fix select bug

* fix timer bug

* add bity fallback options and toFixed floats

* tslint errors

* add arrow to dropdown and add support footer

* Add service provider

* fix minor $ bug and stop timer on order complete

* better load UX and dropdown UX

* fixed single test

* currRate prop bugs and reduce LS bloat

* takeEvery on timer saga and don't clear state.options to restartSwap reducer

* export tx sagas and fix minor type

* Add ShapeShift Rates functionality when selecting a ShapeShift pair.

* type fixes

* BugFix: Don't change displayed ShapeShift Rate Inputs on every dropdown change
Also contains some caching / performance improvements

* BugFix: Don't remote rate inputs when falsy amount

* fix type error

* Progress commit

* Implement saga logic

* Make address field factory component

* Shorten debounce time

* Make new actions / sagas  for handling single token lookup

* Implement working version of litesend

* Change saga into selector

* Add failing spec

* fix broken test

* add debounce to error message

* fix tests

* update snapshots

* test coverage

* move setState disabled property from debounce so we instantly can go to next step on valid amounts

* much deeper test coverage, fix debounce ux, and fix bity flashing at swap page load

* fix minor failing test

* seperate shapeshift erc20 token whitelist

* fix saveState store bug

* break orderTimeRemaining saga up and rewrite tests

* add new swap icon

* remove unused allowReadOnly prop

* change offlineaware to walletdecrypt for litesend

* fix LiteSend changewallet bug

* fix error message UX

* fix button styling to match develop

* fix liteSend test

* Fix LiteSend UX on unavl tokens, dropdown null value, and don't show decrypt in litesend after successful wallet decrypt.

* add litesend network check
2018-01-02 12:04:50 -06:00

245 lines
6.9 KiB
TypeScript

import * as actionTypes from 'actions/swap';
import * as stateTypes from './types';
import * as schema from './schema';
import { TypeKeys } from 'actions/swap/constants';
import { normalize } from 'normalizr';
export interface State {
step: number;
origin: stateTypes.SwapInput;
destination: stateTypes.SwapInput;
options: stateTypes.NormalizedOptions;
bityRates: stateTypes.NormalizedBityRates;
// Change this
shapeshiftRates: stateTypes.NormalizedBityRates;
provider: string;
bityOrder: any;
shapeshiftOrder: any;
destinationAddress: string;
isFetchingRates: boolean | null;
secondsRemaining: number | null;
outputTx: string | null;
isPostingOrder: boolean;
bityOrderStatus: string | null;
shapeshiftOrderStatus: string | null;
orderTimestampCreatedISOString: string | null;
paymentAddress: string | null;
validFor: number | null;
orderId: string | null;
showLiteSend: boolean;
}
export const INITIAL_STATE: State = {
step: 1,
origin: { id: 'BTC', amount: NaN },
destination: { id: 'ETH', amount: NaN },
options: {
byId: {},
allIds: []
},
bityRates: {
byId: {},
allIds: []
},
shapeshiftRates: {
byId: {},
allIds: []
},
provider: 'bity',
destinationAddress: '',
bityOrder: {},
shapeshiftOrder: {},
isFetchingRates: null,
secondsRemaining: null,
outputTx: null,
isPostingOrder: false,
bityOrderStatus: null,
shapeshiftOrderStatus: null,
orderTimestampCreatedISOString: null,
paymentAddress: null,
validFor: null,
orderId: null,
showLiteSend: false
};
export function swap(state: State = INITIAL_STATE, action: actionTypes.SwapAction) {
switch (action.type) {
case TypeKeys.SWAP_LOAD_BITY_RATES_SUCCEEDED:
const { payload } = action;
return {
...state,
bityRates: {
byId: normalize(payload, [schema.providerRate]).entities.providerRates,
allIds: schema.allIds(normalize(payload, [schema.providerRate]).entities.providerRates)
},
options: {
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
]
},
isFetchingRates: false
};
case TypeKeys.SWAP_INIT: {
return {
...state,
origin: action.payload.origin,
destination: action.payload.destination
};
}
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,
options: state.options,
bityRates: state.bityRates,
shapeshiftRates: state.shapeshiftRates
};
case TypeKeys.SWAP_BITY_ORDER_CREATE_REQUESTED:
return {
...state,
isPostingOrder: true
};
case TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_REQUESTED:
return {
...state,
isPostingOrder: true
};
case TypeKeys.SWAP_BITY_ORDER_CREATE_FAILED:
return {
...state,
isPostingOrder: false
};
case TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_FAILED:
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,
bityOrderStatus: action.payload.status,
orderId: action.payload.id
};
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
};
case TypeKeys.SWAP_BITY_ORDER_STATUS_SUCCEEDED:
return {
...state,
outputTx: action.payload.output.reference,
bityOrderStatus:
action.payload.output.status === 'FILL'
? action.payload.output.status
: action.payload.input.status
};
case TypeKeys.SWAP_SHAPESHIFT_ORDER_STATUS_SUCCEEDED:
return {
...state,
outputTx: action.payload && action.payload.transaction ? action.payload.transaction : null,
shapeshiftOrderStatus: action.payload.status
};
case TypeKeys.SWAP_ORDER_TIME:
return {
...state,
secondsRemaining: action.payload
};
case TypeKeys.SWAP_LOAD_BITY_RATES_REQUESTED:
return {
...state,
isFetchingRates: true
};
case TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED:
return {
...state,
isFetchingRates: true
};
case TypeKeys.SWAP_STOP_LOAD_BITY_RATES:
return {
...state,
isFetchingRates: false
};
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
};
default:
return state;
}
}