Skip to content

Commit

Permalink
Merge pull request #11 from niloo-fs/niloofar/75911/Convert-constants…
Browse files Browse the repository at this point in the history
…-contract-currency-to-TS

Niloofar/ Convert constants, contract, currency to TS [shared-utiles]
  • Loading branch information
jim-deriv authored Oct 3, 2022
2 parents 104c4d2 + 13187de commit f12a8ad
Show file tree
Hide file tree
Showing 10 changed files with 254 additions and 148 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,18 @@ export const getLocalizedBasis = () => ({
* components can be undef or an array containing any of: 'start_date', 'barrier', 'last_digit'
* ['duration', 'amount'] are omitted, as they're available in all contract types
*/
export const getContractTypesConfig = symbol => ({
type TContractTypesConfig = {
title: string;
trade_types: string[];
basis: string[];
components: string[];
barrier_count?: number;
config?: { hide_duration: boolean };
};

type TGetContractTypesConfig = (symbol: string) => Record<string, TContractTypesConfig>;

export const getContractTypesConfig: TGetContractTypesConfig = symbol => ({
rise_fall: {
title: localize('Rise/Fall'),
trade_types: ['CALL', 'PUT'],
Expand Down Expand Up @@ -340,7 +351,8 @@ export const getUnsupportedContracts = () => ({
},
});

export const getSupportedContracts = is_high_low => ({
type TGetSupportedContracts = keyof ReturnType<typeof getSupportedContracts>;
export const getSupportedContracts = (is_high_low?: boolean) => ({
CALL: {
name: is_high_low ? <Localize i18n_default_text='Higher' /> : <Localize i18n_default_text='Rise' />,
position: 'top',
Expand Down Expand Up @@ -399,17 +411,20 @@ export const getSupportedContracts = is_high_low => ({
},
});

export const getContractConfig = is_high_low => ({
export const getContractConfig = (is_high_low: boolean) => ({
...getSupportedContracts(is_high_low),
...getUnsupportedContracts(),
});

export const getContractTypeDisplay = (type, is_high_low = false) => {
return getContractConfig(is_high_low)[type] ? getContractConfig(is_high_low)[type.toUpperCase()].name : '';
};
/*
// TODO we can combine getContractTypeDisplay and getContractTypePosition functions.
the difference between these two functions is just the property they return. (name/position)
*/
export const getContractTypeDisplay = (type: TGetSupportedContracts, is_high_low = false) =>
getContractConfig(is_high_low)[type].name;

export const getContractTypePosition = (type, is_high_low = false) =>
getContractConfig(is_high_low)[type] ? getContractConfig(is_high_low)[type.toUpperCase()].position : 'top';
export const getContractTypePosition = (type: TGetSupportedContracts, is_high_low = false) =>
getContractConfig(is_high_low)[type].position || 'top';

export const isCallPut = trade_type =>
export const isCallPut = (trade_type: 'rise_fall' | 'rise_fall_equal' | 'high_low'): boolean =>
trade_type === 'rise_fall' || trade_type === 'rise_fall_equal' || trade_type === 'high_low';
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
import { expect } from 'chai';
import * as ContractUtils from '../contract';
import {
TContractInfo,
TDigitsInfo,
TTickItem,
TGetFinalPrice,
TIsEnded,
TGetDisplayStatus,
TIsValidToSell,
} from '../contract-types';

describe('getFinalPrice', () => {
it("should return sell_price as final price when it's available", () => {
const contract_info = {
sell_price: 12345,
bid_price: 0,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(12345);
});
Expand All @@ -17,92 +27,97 @@ describe('getFinalPrice', () => {
});
it('should return bid_price as final price when sell_price is not available and bid_price is available', () => {
const contract_info = {
sell_price: 0,
bid_price: 789,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(789);
});
it('should return 0 as final price when sell_price and bid_price are empty', () => {
const contract_info = {
sell_price: false,
bid_price: false,
sell_price: 0,
bid_price: 0,
};
expect(ContractUtils.getFinalPrice(contract_info)).to.eql(0);
});
});

describe('getIndicativePrice', () => {
it('should return getFinalPrice if it has final price and contract is ended', () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
sell_price: 12345,
bid_price: 0,
status: 'sold',
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(12345);
});
it("should return null if it doesn't have final price, bid_price and contract is not ended", () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
status: 'open',
sell_price: 0,
bid_price: 0,
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(null);
});
it("should return bid_price if it doesn't have final price, has bid_price and contract is not ended", () => {
const contract_info = {
const contract_info: TGetFinalPrice & TIsEnded = {
status: 'open',
bid_price: 12345,
sell_price: 0,
};
expect(ContractUtils.getIndicativePrice(contract_info)).to.eql(12345);
});
});

describe('isEnded', () => {
it("should return false when there is status and it's equal to open in contract info", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
it("should return true when there is status and it's not equal to open in contract info", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'sold',
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is expired', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: true,
is_expired: 1,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is settleable', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: false,
is_settleable: true,
is_expired: 0,
is_settleable: 1,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(true);
});
it('should return true when contract is not expired', () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
is_expired: false,
is_expired: 0,
};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
it('should return true when contract does not have is_settleable, is_expired and status', () => {
const contract_info = {};
const contract_info: TContractInfo = {};
expect(ContractUtils.isEnded(contract_info)).to.eql(false);
});
});

describe('isUserSold', () => {
it("should return true if contract's status is sold", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'sold',
};
expect(ContractUtils.isUserSold(contract_info)).to.eql(true);
});
it("should return false if contract's status is not sold", () => {
const contract_info = {
const contract_info: TContractInfo = {
status: 'open',
};
expect(ContractUtils.isUserSold(contract_info)).to.eql(false);
Expand All @@ -111,42 +126,42 @@ describe('isUserSold', () => {

describe('isValidToSell', () => {
it('should return true if contract is not ended and is not sold and contract is valid to_sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'open',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(true);
});
it('should return false if contract is ended and is sold and contract is valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'sold',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is not sold and contract is valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'won',
is_valid_to_sell: 1,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'sold',
is_valid_to_sell: 0,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is ended and is not sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'won',
is_valid_to_sell: 0,
};
expect(ContractUtils.isValidToSell(contract_info)).to.eql(false);
});
it('should return false if contract is not ended and is not sold and contract is not valid to sell', () => {
const contract_info = {
const contract_info: TIsValidToSell = {
status: 'open',
is_valid_to_sell: 0,
};
Expand All @@ -156,7 +171,7 @@ describe('isValidToSell', () => {

describe('getLastTickFromTickStream', () => {
it('should return the last tick in the tick_stream array', () => {
const tick_stream = [
const tick_stream: TTickItem[] = [
{
tick: 766.53,
epoch: 1000001,
Expand All @@ -172,7 +187,7 @@ describe('getLastTickFromTickStream', () => {
});
});
it('should return an empty object if the tick_stream array is empty', () => {
const tick_stream = [];
const tick_stream: TTickItem[] = [];
expect(ContractUtils.getLastTickFromTickStream(tick_stream)).to.eql({});
});
});
Expand All @@ -189,18 +204,18 @@ describe('isDigitContract', () => {

describe('getDigitInfo', () => {
it('should return an empty object when tick_stream is not in contract_info', () => {
const contract_info = {};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eql({});
const contract_info: TContractInfo = {};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eq({});
});
it('should return an empty object if tick_stream data is already in digits_info', () => {
const contract_info = {
const contract_info: TContractInfo = {
entry_tick_time: 1544707342,
entry_tick: 123.99,
current_spot_time: 10000000,
current_spot: 456.99,
exit_tick_time: 10000001,
contract_type: 'DIGITMATCH',
barrier: 9,
barrier: '9',
tick_stream: [
{
tick: 123.456,
Expand All @@ -209,16 +224,16 @@ describe('getDigitInfo', () => {
},
],
};
const digits_info = {
const digits_info: TDigitsInfo = {
1544707344: {
digit: 6,
spot: '123.456',
},
};
expect(ContractUtils.getDigitInfo(digits_info, contract_info)).to.deep.eql({});
expect(ContractUtils.getDigitInfo(digits_info, contract_info)).to.deep.eq({});
});
it('should return a digits_info object with the latest tick_stream array data', () => {
const contract_info = {
const contract_info: TContractInfo = {
tick_stream: [
{
tick: 123.456,
Expand All @@ -232,41 +247,46 @@ describe('getDigitInfo', () => {
},
],
};
const digits_info = {
const digits_info: TDigitsInfo = {
1544707346: {
digit: 3,
spot: '456.993',
},
};
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eql(digits_info);
expect(ContractUtils.getDigitInfo({}, contract_info)).to.deep.eq(digits_info);
});
});

expect('getDisplayStatus', () => {
describe('getDisplayStatus', () => {
it('should return won if contract is ended and profit is more than zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: 100,
buy_price: 0,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('won');
});
it('should return lost if contract is ended and profit is less than zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: -100,
buy_price: 100,
bid_price: 0,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('loss');
});
it('should return won if contract is ended and profit is zero', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'sold',
profit: 0,
buy_price: 100,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('won');
});
it('should return purchased if contract is not ended', () => {
const contract_info = {
const contract_info: TGetDisplayStatus = {
status: 'open',
buy_price: 0,
bid_price: 100,
};
expect(ContractUtils.getDisplayStatus(contract_info)).to.eql('purchased');
});
Expand Down
Loading

0 comments on commit f12a8ad

Please sign in to comment.