2017-12-11 17:44:53 +00:00
|
|
|
import { swap, INITIAL_STATE } from 'reducers/swap';
|
2017-08-24 08:53:59 +00:00
|
|
|
import * as swapActions from 'actions/swap';
|
2018-01-02 18:04:50 +00:00
|
|
|
import {
|
|
|
|
NormalizedBityRates,
|
|
|
|
NormalizedOptions,
|
|
|
|
NormalizedShapeshiftRates
|
|
|
|
} from 'reducers/swap/types';
|
2017-12-11 17:44:53 +00:00
|
|
|
import { normalize } from 'normalizr';
|
|
|
|
import * as schema from 'reducers/swap/schema';
|
2018-01-02 18:04:50 +00:00
|
|
|
import { TypeKeys } from 'actions/swap/constants';
|
2018-01-09 02:59:32 +00:00
|
|
|
import tokens from 'config/tokens/eth.json';
|
|
|
|
import { SHAPESHIFT_TOKEN_WHITELIST } from 'api/shapeshift';
|
|
|
|
|
|
|
|
describe('ensure whitelist', () => {
|
2018-03-07 23:36:05 +00:00
|
|
|
const findToken = (tkn: string) => tokens.find((t: any) => t.symbol === tkn);
|
2018-01-09 02:59:32 +00:00
|
|
|
SHAPESHIFT_TOKEN_WHITELIST.forEach(t => {
|
|
|
|
it(`Should find Token ${t}`, () => {
|
|
|
|
expect(findToken(t)).toBeTruthy();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
2017-08-24 08:53:59 +00:00
|
|
|
|
|
|
|
describe('swap reducer', () => {
|
2018-01-02 18:04:50 +00:00
|
|
|
const shapeshiftApiResponse = {
|
|
|
|
['1SSTANT']: {
|
|
|
|
id: '1STANT',
|
|
|
|
options: [
|
|
|
|
{
|
|
|
|
id: '1ST',
|
|
|
|
status: 'available',
|
|
|
|
image: 'https://shapeshift.io/images/coins/firstblood.png',
|
|
|
|
name: 'FirstBlood'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'ANT',
|
|
|
|
status: 'available',
|
|
|
|
image: 'https://shapeshift.io/images/coins/aragon.png',
|
|
|
|
name: 'Aragon'
|
|
|
|
}
|
|
|
|
],
|
|
|
|
rate: '0.24707537',
|
|
|
|
limit: 5908.29166225,
|
|
|
|
min: 7.86382979
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const bityApiResponse = {
|
2017-12-11 17:44:53 +00:00
|
|
|
BTCETH: {
|
|
|
|
id: 'BTCETH',
|
|
|
|
options: [{ id: 'BTC' }, { id: 'ETH' }],
|
|
|
|
rate: 23.27855114
|
|
|
|
},
|
|
|
|
ETHBTC: {
|
|
|
|
id: 'ETHBTC',
|
|
|
|
options: [{ id: 'ETH' }, { id: 'BTC' }],
|
|
|
|
rate: 0.042958
|
|
|
|
}
|
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
const normalizedBityRates: NormalizedBityRates = {
|
|
|
|
byId: normalize(bityApiResponse, [schema.providerRate]).entities.providerRates,
|
|
|
|
allIds: schema.allIds(normalize(bityApiResponse, [schema.providerRate]).entities.providerRates)
|
|
|
|
};
|
|
|
|
const normalizedShapeshiftRates: NormalizedShapeshiftRates = {
|
|
|
|
byId: normalize(shapeshiftApiResponse, [schema.providerRate]).entities.providerRates,
|
|
|
|
allIds: schema.allIds(
|
|
|
|
normalize(shapeshiftApiResponse, [schema.providerRate]).entities.providerRates
|
|
|
|
)
|
|
|
|
};
|
|
|
|
const normalizedBityOptions: NormalizedOptions = {
|
|
|
|
byId: normalize(bityApiResponse, [schema.providerRate]).entities.options,
|
|
|
|
allIds: schema.allIds(normalize(bityApiResponse, [schema.providerRate]).entities.options)
|
2017-12-11 17:44:53 +00:00
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
const normalizedShapeshiftOptions: NormalizedOptions = {
|
|
|
|
byId: normalize(shapeshiftApiResponse, [schema.providerRate]).entities.options,
|
|
|
|
allIds: schema.allIds(normalize(shapeshiftApiResponse, [schema.providerRate]).entities.options)
|
2017-12-11 17:44:53 +00:00
|
|
|
};
|
2018-01-02 18:04:50 +00:00
|
|
|
|
2017-08-24 08:53:59 +00:00
|
|
|
it('should handle SWAP_LOAD_BITY_RATES_SUCCEEDED', () => {
|
2018-01-02 18:04:50 +00:00
|
|
|
expect(swap(undefined, swapActions.loadBityRatesSucceededSwap(bityApiResponse))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityRates: normalizedBityRates,
|
|
|
|
options: normalizedBityOptions
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_LOAD_SHAPESHIFT_RATES_SUCCEEDED', () => {
|
|
|
|
expect(
|
2018-03-07 23:36:05 +00:00
|
|
|
swap(undefined, swapActions.loadShapeshiftRatesSucceededSwap(shapeshiftApiResponse as any))
|
2018-01-02 18:04:50 +00:00
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false,
|
|
|
|
shapeshiftRates: normalizedShapeshiftRates,
|
|
|
|
options: normalizedShapeshiftOptions
|
2017-08-24 08:53:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_STEP', () => {
|
|
|
|
const step = 2;
|
|
|
|
expect(swap(undefined, swapActions.changeStepSwap(step))).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
step
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_DESTINATION_ADDRESS', () => {
|
|
|
|
const destinationAddress = '341a0sdf83';
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.destinationAddressSwap(destinationAddress))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
destinationAddress
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_RESTART', () => {
|
|
|
|
expect(
|
|
|
|
swap(
|
|
|
|
{
|
|
|
|
...INITIAL_STATE,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityRates: normalizedBityRates,
|
|
|
|
shapeshiftRates: normalizedShapeshiftRates,
|
2018-03-01 17:53:29 +00:00
|
|
|
origin: { label: 'BTC', amount: 1 },
|
|
|
|
destination: { label: 'ETH', amount: 3 }
|
2017-08-24 08:53:59 +00:00
|
|
|
},
|
|
|
|
swapActions.restartSwap()
|
|
|
|
)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityRates: normalizedBityRates,
|
|
|
|
shapeshiftRates: normalizedShapeshiftRates
|
2017-08-24 08:53:59 +00:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
it('should handle SWAP_BITY_ORDER_CREATE_REQUESTED', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
2018-01-02 18:04:50 +00:00
|
|
|
type: TypeKeys.SWAP_BITY_ORDER_CREATE_REQUESTED
|
2017-10-25 02:17:26 +00:00
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
it('should handle SWAP_SHAPESHIFT_ORDER_CREATE_REQUESTED', () => {
|
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: TypeKeys.SWAP_BITY_ORDER_CREATE_REQUESTED
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_BITY_ORDER_CREATE_FAILED', () => {
|
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: TypeKeys.SWAP_BITY_ORDER_CREATE_FAILED
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_SHAPESHIFT_ORDER_CREATE_FAILED', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
2018-01-02 18:04:50 +00:00
|
|
|
type: TypeKeys.SWAP_SHAPESHIFT_ORDER_CREATE_FAILED
|
2017-10-25 02:17:26 +00:00
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isPostingOrder: false
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-17 21:12:27 +00:00
|
|
|
it('should handle SWAP_BITY_ORDER_CREATE_SUCCEEDED', () => {
|
|
|
|
const mockedBityOrder: swapActions.BityOrderPostResponse = {
|
|
|
|
payment_address: 'payment_address',
|
|
|
|
status: 'status',
|
|
|
|
input: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'input_currency',
|
|
|
|
reference: 'input_reference',
|
|
|
|
status: 'input_status'
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'output_currency',
|
|
|
|
reference: 'output_reference',
|
|
|
|
status: 'output_status'
|
|
|
|
},
|
|
|
|
timestamp_created: 'timestamp_created',
|
|
|
|
validFor: 0,
|
|
|
|
id: 'id'
|
|
|
|
};
|
|
|
|
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.bityOrderCreateSucceededSwap(mockedBityOrder))).toEqual({
|
2017-11-17 21:12:27 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
bityOrder: {
|
|
|
|
...mockedBityOrder
|
|
|
|
},
|
|
|
|
isPostingOrder: false,
|
|
|
|
originAmount: parseFloat(mockedBityOrder.input.amount),
|
|
|
|
destinationAmount: parseFloat(mockedBityOrder.output.amount),
|
|
|
|
secondsRemaining: mockedBityOrder.validFor,
|
|
|
|
validFor: mockedBityOrder.validFor,
|
|
|
|
orderTimestampCreatedISOString: mockedBityOrder.timestamp_created,
|
|
|
|
paymentAddress: mockedBityOrder.payment_address,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus: mockedBityOrder.status,
|
2017-11-17 21:12:27 +00:00
|
|
|
orderId: mockedBityOrder.id
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
it('should handle SWAP_SHAPESHIFT_ORDER_CREATE_SUCCEEDED', () => {
|
|
|
|
const mockedShapeshiftOrder: swapActions.ShapeshiftOrderResponse = {
|
|
|
|
orderId: '64d73218-0ee9-4c6c-9bbd-6da9208595f5',
|
|
|
|
pair: 'eth_ant',
|
|
|
|
withdrawal: '0x6b3a639eb96d8e0241fe4e114d99e739f906944e',
|
|
|
|
withdrawalAmount: '200.13550988',
|
|
|
|
deposit: '0x039ed77933388642fdd618d27bfc4fa3582d10c4',
|
|
|
|
depositAmount: '0.98872802',
|
|
|
|
expiration: 1514633757288,
|
|
|
|
quotedRate: '203.47912271',
|
|
|
|
maxLimit: 7.04575258,
|
|
|
|
apiPubKey:
|
|
|
|
'0ca1ccd50b708a3f8c02327f0caeeece06d3ddc1b0ac749a987b453ee0f4a29bdb5da2e53bc35e57fb4bb7ae1f43c93bb098c3c4716375fc1001c55d8c94c160',
|
|
|
|
minerFee: '1.05'
|
|
|
|
};
|
|
|
|
|
|
|
|
const swapState = swap(
|
|
|
|
undefined,
|
|
|
|
swapActions.shapeshiftOrderCreateSucceededSwap(mockedShapeshiftOrder)
|
|
|
|
);
|
|
|
|
|
|
|
|
expect(swapState).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
shapeshiftOrder: {
|
|
|
|
...mockedShapeshiftOrder
|
|
|
|
},
|
|
|
|
isPostingOrder: false,
|
|
|
|
originAmount: parseFloat(mockedShapeshiftOrder.depositAmount),
|
|
|
|
destinationAmount: parseFloat(mockedShapeshiftOrder.withdrawalAmount),
|
|
|
|
secondsRemaining: swapState.secondsRemaining,
|
|
|
|
validFor: swapState.validFor,
|
|
|
|
orderTimestampCreatedISOString: swapState.orderTimestampCreatedISOString,
|
|
|
|
paymentAddress: mockedShapeshiftOrder.deposit,
|
|
|
|
shapeshiftOrderStatus: 'no_deposits',
|
|
|
|
orderId: mockedShapeshiftOrder.orderId
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-11-17 21:12:27 +00:00
|
|
|
it('should handle SWAP_BITY_ORDER_STATUS_SUCCEEDED', () => {
|
|
|
|
const mockedBityResponse: swapActions.BityOrderResponse = {
|
|
|
|
input: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'input_currency',
|
|
|
|
reference: 'input_reference',
|
|
|
|
status: 'input_status'
|
|
|
|
},
|
|
|
|
output: {
|
|
|
|
amount: '1.111',
|
|
|
|
currency: 'output_currency',
|
|
|
|
reference: 'output_reference',
|
|
|
|
status: 'FILL'
|
|
|
|
},
|
|
|
|
status: 'status'
|
|
|
|
};
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
expect(swap(undefined, swapActions.bityOrderStatusSucceededSwap(mockedBityResponse))).toEqual({
|
2017-11-17 21:12:27 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
outputTx: mockedBityResponse.output.reference,
|
2018-01-02 18:04:50 +00:00
|
|
|
bityOrderStatus: mockedBityResponse.output.status
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_SHAPESHIFT_ORDER_STATUS_SUCCEEDED', () => {
|
|
|
|
const mockedShapeshiftResponse: swapActions.ShapeshiftStatusResponse = {
|
|
|
|
status: 'complete',
|
|
|
|
transaction: '0x039ed77933388642fdd618d27bfc4fa3582d10c4'
|
|
|
|
};
|
|
|
|
|
|
|
|
expect(
|
|
|
|
swap(undefined, swapActions.shapeshiftOrderStatusSucceededSwap(mockedShapeshiftResponse))
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
shapeshiftOrderStatus: mockedShapeshiftResponse.status,
|
|
|
|
outputTx: mockedShapeshiftResponse.transaction
|
2017-11-17 21:12:27 +00:00
|
|
|
});
|
|
|
|
});
|
2017-08-24 08:53:59 +00:00
|
|
|
|
|
|
|
it('should handle SWAP_ORDER_TIME', () => {
|
|
|
|
const secondsRemaining = 300;
|
2017-12-11 17:44:53 +00:00
|
|
|
expect(swap(undefined, swapActions.orderTimeSwap(secondsRemaining))).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
secondsRemaining
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
it('should handle SWAP_LOAD_BITY_RATES_REQUESTED', () => {
|
|
|
|
expect(
|
2017-10-25 02:17:26 +00:00
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_LOAD_BITY_RATES_REQUESTED'
|
|
|
|
} as swapActions.SwapAction)
|
2017-08-24 08:53:59 +00:00
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2018-01-02 18:04:50 +00:00
|
|
|
it('should handle SWAP_LOAD_SHAPESHIFT_RATE_REQUESTED', () => {
|
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: TypeKeys.SWAP_LOAD_SHAPESHIFT_RATES_REQUESTED
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: true
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2017-08-24 08:53:59 +00:00
|
|
|
it('should handle SWAP_STOP_LOAD_BITY_RATES', () => {
|
2017-10-25 02:17:26 +00:00
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: 'SWAP_STOP_LOAD_BITY_RATES'
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
2017-08-24 08:53:59 +00:00
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false
|
|
|
|
});
|
|
|
|
});
|
2018-01-02 18:04:50 +00:00
|
|
|
|
|
|
|
it('should handle SWAP_STOP_LOAD_SHAPESHIFT_RATES', () => {
|
|
|
|
expect(
|
|
|
|
swap(undefined, {
|
|
|
|
type: TypeKeys.SWAP_STOP_LOAD_SHAPESHIFT_RATES
|
|
|
|
} as swapActions.SwapAction)
|
|
|
|
).toEqual({
|
|
|
|
...INITIAL_STATE,
|
|
|
|
isFetchingRates: false
|
|
|
|
});
|
|
|
|
});
|
2017-08-24 08:53:59 +00:00
|
|
|
});
|