@oanda/labs-spread-cost-calculator-widget 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/codegen.ts +15 -0
- package/dist/main/SpreadCostCalculatorWidget/Main.js +115 -0
- package/dist/main/SpreadCostCalculatorWidget/Main.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.js +54 -0
- package/dist/main/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/ValidationWrapper.js +46 -0
- package/dist/main/SpreadCostCalculatorWidget/ValidationWrapper.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/constant.js +8 -0
- package/dist/main/SpreadCostCalculatorWidget/constant.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/index.js +28 -0
- package/dist/main/SpreadCostCalculatorWidget/index.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/render.js +51 -0
- package/dist/main/SpreadCostCalculatorWidget/render.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/types.js +6 -0
- package/dist/main/SpreadCostCalculatorWidget/types.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/useCalculateSpread.js +24 -0
- package/dist/main/SpreadCostCalculatorWidget/useCalculateSpread.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/useInstrumentChange.js +51 -0
- package/dist/main/SpreadCostCalculatorWidget/useInstrumentChange.js.map +1 -0
- package/dist/main/SpreadCostCalculatorWidget/utils.js +17 -0
- package/dist/main/SpreadCostCalculatorWidget/utils.js.map +1 -0
- package/dist/main/gql/types/fragment-masking.js +23 -0
- package/dist/main/gql/types/fragment-masking.js.map +1 -0
- package/dist/main/gql/types/gql.js +16 -0
- package/dist/main/gql/types/gql.js.map +1 -0
- package/dist/main/gql/types/graphql.js +223 -0
- package/dist/main/gql/types/graphql.js.map +1 -0
- package/dist/main/gql/types/index.js +28 -0
- package/dist/main/gql/types/index.js.map +1 -0
- package/dist/main/gql/validateInstruments.js +16 -0
- package/dist/main/gql/validateInstruments.js.map +1 -0
- package/dist/main/index.js +28 -0
- package/dist/main/index.js.map +1 -0
- package/dist/main/translations/index.js +27 -0
- package/dist/main/translations/index.js.map +1 -0
- package/dist/main/translations/sources/en.json +11 -0
- package/dist/main/translations/sources/es.json +11 -0
- package/dist/main/translations/sources/th.json +11 -0
- package/dist/main/translations/sources/zh_TW.json +11 -0
- package/dist/main/translations/translations.js +8 -0
- package/dist/main/translations/translations.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/Main.js +107 -0
- package/dist/module/SpreadCostCalculatorWidget/Main.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.js +46 -0
- package/dist/module/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/ValidationWrapper.js +38 -0
- package/dist/module/SpreadCostCalculatorWidget/ValidationWrapper.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/constant.js +2 -0
- package/dist/module/SpreadCostCalculatorWidget/constant.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/index.js +3 -0
- package/dist/module/SpreadCostCalculatorWidget/index.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/render.js +48 -0
- package/dist/module/SpreadCostCalculatorWidget/render.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/types.js +2 -0
- package/dist/module/SpreadCostCalculatorWidget/types.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/useCalculateSpread.js +17 -0
- package/dist/module/SpreadCostCalculatorWidget/useCalculateSpread.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/useInstrumentChange.js +44 -0
- package/dist/module/SpreadCostCalculatorWidget/useInstrumentChange.js.map +1 -0
- package/dist/module/SpreadCostCalculatorWidget/utils.js +10 -0
- package/dist/module/SpreadCostCalculatorWidget/utils.js.map +1 -0
- package/dist/module/gql/types/fragment-masking.js +15 -0
- package/dist/module/gql/types/fragment-masking.js.map +1 -0
- package/dist/module/gql/types/gql.js +8 -0
- package/dist/module/gql/types/gql.js.map +1 -0
- package/dist/module/gql/types/graphql.js +217 -0
- package/dist/module/gql/types/graphql.js.map +1 -0
- package/dist/module/gql/types/index.js +3 -0
- package/dist/module/gql/types/index.js.map +1 -0
- package/dist/module/gql/validateInstruments.js +10 -0
- package/dist/module/gql/validateInstruments.js.map +1 -0
- package/dist/module/index.js +3 -0
- package/dist/module/index.js.map +1 -0
- package/dist/module/translations/index.js +20 -0
- package/dist/module/translations/index.js.map +1 -0
- package/dist/module/translations/sources/en.json +11 -0
- package/dist/module/translations/sources/es.json +11 -0
- package/dist/module/translations/sources/th.json +11 -0
- package/dist/module/translations/sources/zh_TW.json +11 -0
- package/dist/module/translations/translations.js +2 -0
- package/dist/module/translations/translations.js.map +1 -0
- package/dist/types/SpreadCostCalculatorWidget/Main.d.ts +4 -0
- package/dist/types/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.d.ts +4 -0
- package/dist/types/SpreadCostCalculatorWidget/ValidationWrapper.d.ts +4 -0
- package/dist/types/SpreadCostCalculatorWidget/constant.d.ts +1 -0
- package/dist/types/SpreadCostCalculatorWidget/index.d.ts +2 -0
- package/dist/types/SpreadCostCalculatorWidget/render.d.ts +1 -0
- package/dist/types/SpreadCostCalculatorWidget/types.d.ts +20 -0
- package/dist/types/SpreadCostCalculatorWidget/useCalculateSpread.d.ts +8 -0
- package/dist/types/SpreadCostCalculatorWidget/useInstrumentChange.d.ts +12 -0
- package/dist/types/SpreadCostCalculatorWidget/utils.d.ts +2 -0
- package/dist/types/gql/types/fragment-masking.d.ts +15 -0
- package/dist/types/gql/types/gql.d.ts +37 -0
- package/dist/types/gql/types/graphql.d.ts +389 -0
- package/dist/types/gql/types/index.d.ts +2 -0
- package/dist/types/gql/validateInstruments.d.ts +1 -0
- package/dist/types/index.d.ts +2 -0
- package/dist/types/translations/index.d.ts +4 -0
- package/dist/types/translations/translations.d.ts +1 -0
- package/index.ts +1 -0
- package/lokalise.config.json +18 -0
- package/package.json +26 -0
- package/src/SpreadCostCalculatorWidget/Main.tsx +138 -0
- package/src/SpreadCostCalculatorWidget/SpreadCostCalculatorWidget.tsx +52 -0
- package/src/SpreadCostCalculatorWidget/ValidationWrapper.tsx +51 -0
- package/src/SpreadCostCalculatorWidget/constant.ts +1 -0
- package/src/SpreadCostCalculatorWidget/index.ts +2 -0
- package/src/SpreadCostCalculatorWidget/render.tsx +60 -0
- package/src/SpreadCostCalculatorWidget/types.ts +27 -0
- package/src/SpreadCostCalculatorWidget/useCalculateSpread.ts +38 -0
- package/src/SpreadCostCalculatorWidget/useInstrumentChange.ts +47 -0
- package/src/SpreadCostCalculatorWidget/utils.ts +12 -0
- package/src/gql/types/fragment-masking.ts +66 -0
- package/src/gql/types/gql.ts +42 -0
- package/src/gql/types/graphql.ts +430 -0
- package/src/gql/types/index.ts +2 -0
- package/src/gql/validateInstruments.ts +10 -0
- package/src/index.ts +2 -0
- package/src/translations/index.ts +17 -0
- package/src/translations/sources/en.json +11 -0
- package/src/translations/sources/es.json +11 -0
- package/src/translations/sources/th.json +11 -0
- package/src/translations/sources/zh_TW.json +11 -0
- package/src/translations/translations.ts +1 -0
- package/test/SpreadCostCalculator.test.tsx +31 -0
- package/test/useCalculateSpread.test.tsx +131 -0
- package/test/useInstrumentChange.test.tsx +54 -0
- package/tsconfig.types.json +12 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import React, {
|
|
2
|
+
FC, useContext, useEffect,
|
|
3
|
+
} from 'react';
|
|
4
|
+
import { useLocale } from '@oanda/mono-i18n';
|
|
5
|
+
import {
|
|
6
|
+
ChartError,
|
|
7
|
+
cn,
|
|
8
|
+
Label,
|
|
9
|
+
NumberInput,
|
|
10
|
+
Select,
|
|
11
|
+
Size,
|
|
12
|
+
ThemeContext,
|
|
13
|
+
useNumberFormat,
|
|
14
|
+
} from '@oanda/labs-widget-common';
|
|
15
|
+
import { useCalculateSpread } from './useCalculateSpread';
|
|
16
|
+
import { getInstrumentCurrency } from './utils';
|
|
17
|
+
import { useInstrumentChange } from './useInstrumentChange';
|
|
18
|
+
import { MainProps } from './types';
|
|
19
|
+
import { CELL_EMPTY_VALUE } from './constant';
|
|
20
|
+
|
|
21
|
+
const Main: FC<MainProps> = ({
|
|
22
|
+
instruments,
|
|
23
|
+
}) => {
|
|
24
|
+
const { size } = useContext(ThemeContext);
|
|
25
|
+
const isDesktop = size === Size.DESKTOP;
|
|
26
|
+
|
|
27
|
+
const selectOptions = instruments.map((instrument) => ({
|
|
28
|
+
id: instrument.name,
|
|
29
|
+
label: instrument.displayName,
|
|
30
|
+
}));
|
|
31
|
+
|
|
32
|
+
const {
|
|
33
|
+
value: unitsTraded,
|
|
34
|
+
handleValueChange: onUnitChange,
|
|
35
|
+
increment: incrementTradedUnits,
|
|
36
|
+
decrement: decrementTradedUnits,
|
|
37
|
+
} = useNumberFormat({});
|
|
38
|
+
|
|
39
|
+
const {
|
|
40
|
+
value: spread,
|
|
41
|
+
handleValueChange: onSpreadChange,
|
|
42
|
+
increment: incrementSpread,
|
|
43
|
+
decrement: decrementSpread,
|
|
44
|
+
} = useNumberFormat({
|
|
45
|
+
step: 0.1,
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
const {
|
|
49
|
+
selectedInstrument,
|
|
50
|
+
setSelectedInstrument,
|
|
51
|
+
pipLocation,
|
|
52
|
+
initialSpread,
|
|
53
|
+
loading,
|
|
54
|
+
error,
|
|
55
|
+
} = useInstrumentChange({
|
|
56
|
+
initialInstrument: {
|
|
57
|
+
id: instruments[0].name,
|
|
58
|
+
label: instruments[0].displayName,
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
useEffect(() => {
|
|
63
|
+
onSpreadChange({
|
|
64
|
+
floatValue: initialSpread,
|
|
65
|
+
formattedValue: initialSpread?.toString() || '',
|
|
66
|
+
value: initialSpread?.toString() || '',
|
|
67
|
+
});
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
+
}, [initialSpread]);
|
|
70
|
+
|
|
71
|
+
const { lang } = useLocale();
|
|
72
|
+
|
|
73
|
+
const spreadCost = useCalculateSpread({
|
|
74
|
+
spread,
|
|
75
|
+
unitsTraded,
|
|
76
|
+
pipLocation,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<>
|
|
81
|
+
{error ? (
|
|
82
|
+
<div className="lw-flex lw-h-[300px] lw-w-full lw-items-center lw-border lw-border-solid lw-border-border-primary">
|
|
83
|
+
<ChartError />
|
|
84
|
+
</div>
|
|
85
|
+
) : (
|
|
86
|
+
<div
|
|
87
|
+
data-testid="spread-cost-calculator"
|
|
88
|
+
className="lw-mx-auto lw-flex lw-w-full lw-max-w-full lw-flex-col lw-content-center lw-justify-center lw-gap-8 lw-p-4"
|
|
89
|
+
>
|
|
90
|
+
<div className="lw-flex lw-flex-col lw-gap-2">
|
|
91
|
+
<Label htmlFor={lang('instrument')} className="lw-text-text-primary">
|
|
92
|
+
{lang('instrument')}
|
|
93
|
+
</Label>
|
|
94
|
+
<Select
|
|
95
|
+
searchPlaceholder={lang('search')}
|
|
96
|
+
options={selectOptions}
|
|
97
|
+
selectedOption={selectedInstrument}
|
|
98
|
+
className="lw-grow-0"
|
|
99
|
+
setSelectedOption={setSelectedInstrument}
|
|
100
|
+
maxHeight={isDesktop ? 190 : 220}
|
|
101
|
+
/>
|
|
102
|
+
</div>
|
|
103
|
+
<div className="lw-flex lw-max-w-full lw-flex-col lw-content-center lw-justify-center lw-gap-3">
|
|
104
|
+
<div className={cn('lw-flex lw-max-w-full lw-flex-row lw-gap-3', !isDesktop && 'lw-flex-col lw-gap-8')}>
|
|
105
|
+
<NumberInput
|
|
106
|
+
onValueChange={onUnitChange}
|
|
107
|
+
value={unitsTraded}
|
|
108
|
+
label={lang('units_traded')}
|
|
109
|
+
onIncrement={incrementTradedUnits}
|
|
110
|
+
onDecrement={decrementTradedUnits}
|
|
111
|
+
placeholder={lang('volume')}
|
|
112
|
+
disabled={loading}
|
|
113
|
+
withoutArrows
|
|
114
|
+
/>
|
|
115
|
+
<NumberInput
|
|
116
|
+
onValueChange={onSpreadChange}
|
|
117
|
+
value={spread}
|
|
118
|
+
label={lang('spread')}
|
|
119
|
+
onIncrement={incrementSpread}
|
|
120
|
+
onDecrement={decrementSpread}
|
|
121
|
+
placeholder={lang('pips')}
|
|
122
|
+
disabled={loading}
|
|
123
|
+
/>
|
|
124
|
+
</div>
|
|
125
|
+
<div className="lw-pt-4 lw-text-text-primary">
|
|
126
|
+
<div>{lang('absolute_spread_cost')}</div>
|
|
127
|
+
<div className="lw-pt-2 lw-text-3xl lw-font-bold">
|
|
128
|
+
{spreadCost ? `${spreadCost} ${getInstrumentCurrency(selectedInstrument.id)}` : CELL_EMPTY_VALUE }
|
|
129
|
+
</div>
|
|
130
|
+
</div>
|
|
131
|
+
</div>
|
|
132
|
+
</div>
|
|
133
|
+
)}
|
|
134
|
+
</>
|
|
135
|
+
);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export { Main };
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import React, { FC, Suspense } from 'react';
|
|
2
|
+
import { ApolloClient, ApolloProvider, InMemoryCache } from '@apollo/client';
|
|
3
|
+
import {
|
|
4
|
+
getLocale, LiveRatesProvider, ThemeProvider,
|
|
5
|
+
} from '@oanda/labs-widget-common';
|
|
6
|
+
import { Division } from '@oanda/labs-live-rates-table-widget';
|
|
7
|
+
import { LocaleProvider } from '@oanda/mono-i18n';
|
|
8
|
+
import { SpreadCostCalculatorWidgetConfig } from './types';
|
|
9
|
+
import { translations } from '../translations';
|
|
10
|
+
import { ValidationWrapper } from './ValidationWrapper';
|
|
11
|
+
|
|
12
|
+
const SpreadCostCalculatorWidget: FC<SpreadCostCalculatorWidgetConfig> = ({
|
|
13
|
+
graphqlUrl,
|
|
14
|
+
liveRatesUrl,
|
|
15
|
+
theme,
|
|
16
|
+
locale,
|
|
17
|
+
isParamError,
|
|
18
|
+
removePadding,
|
|
19
|
+
division,
|
|
20
|
+
instruments,
|
|
21
|
+
}) => {
|
|
22
|
+
const client = new ApolloClient({
|
|
23
|
+
uri: graphqlUrl,
|
|
24
|
+
cache: new InMemoryCache(),
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const divisionCode = division === Division.Opt ? Division.Ogm : division;
|
|
28
|
+
const dataSource = divisionCode === Division.Ogm ? 'MT5' : 'V20';
|
|
29
|
+
|
|
30
|
+
return (
|
|
31
|
+
<LocaleProvider locale={getLocale(locale)} translations={translations}>
|
|
32
|
+
<LiveRatesProvider
|
|
33
|
+
url={liveRatesUrl}
|
|
34
|
+
options={{ divisionCode, dataSource }}
|
|
35
|
+
>
|
|
36
|
+
<ApolloProvider client={client}>
|
|
37
|
+
<Suspense>
|
|
38
|
+
<ThemeProvider theme={theme} removePadding={removePadding}>
|
|
39
|
+
<ValidationWrapper
|
|
40
|
+
instruments={instruments}
|
|
41
|
+
division={division}
|
|
42
|
+
isParamError={isParamError}
|
|
43
|
+
/>
|
|
44
|
+
</ThemeProvider>
|
|
45
|
+
</Suspense>
|
|
46
|
+
</ApolloProvider>
|
|
47
|
+
</LiveRatesProvider>
|
|
48
|
+
</LocaleProvider>
|
|
49
|
+
);
|
|
50
|
+
};
|
|
51
|
+
|
|
52
|
+
export { SpreadCostCalculatorWidget };
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import React, { useContext } from 'react';
|
|
2
|
+
import { useSuspenseQuery } from '@apollo/client';
|
|
3
|
+
import { ChartError, ThemeContext } from '@oanda/labs-widget-common';
|
|
4
|
+
import { Main } from './Main';
|
|
5
|
+
import { validateInstruments } from '../gql/validateInstruments';
|
|
6
|
+
import { ValidateInstrumentsQuery, ValidateInstrumentsQueryVariables } from '../gql/types/graphql';
|
|
7
|
+
import { ValidationWrapperProps } from './types';
|
|
8
|
+
|
|
9
|
+
const ValidationWrapper = ({
|
|
10
|
+
division,
|
|
11
|
+
instruments,
|
|
12
|
+
isParamError,
|
|
13
|
+
}: ValidationWrapperProps) => {
|
|
14
|
+
const { size } = useContext(ThemeContext);
|
|
15
|
+
const { data, error } = useSuspenseQuery<
|
|
16
|
+
ValidateInstrumentsQuery,
|
|
17
|
+
ValidateInstrumentsQueryVariables
|
|
18
|
+
>(validateInstruments, {
|
|
19
|
+
variables: {
|
|
20
|
+
instruments,
|
|
21
|
+
division,
|
|
22
|
+
},
|
|
23
|
+
fetchPolicy: 'network-only',
|
|
24
|
+
errorPolicy: 'all',
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const instrumentsData = data?.mapInstrumentNames?.map((instrument) => ({
|
|
28
|
+
name: instrument!.name,
|
|
29
|
+
displayName: instrument!.displayName,
|
|
30
|
+
})) || [];
|
|
31
|
+
|
|
32
|
+
const showError = !!error || isParamError;
|
|
33
|
+
|
|
34
|
+
return (
|
|
35
|
+
<>
|
|
36
|
+
{size && (
|
|
37
|
+
<>
|
|
38
|
+
{showError ? (
|
|
39
|
+
<div className="lw-flex lw-h-[300px] lw-w-full lw-items-center lw-border lw-border-solid lw-border-border-primary">
|
|
40
|
+
<ChartError />
|
|
41
|
+
</div>
|
|
42
|
+
) : (
|
|
43
|
+
<Main instruments={instrumentsData} />
|
|
44
|
+
)}
|
|
45
|
+
</>
|
|
46
|
+
)}
|
|
47
|
+
</>
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
export { ValidationWrapper };
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export const CELL_EMPTY_VALUE = '\u2014';
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import React from 'react';
|
|
2
|
+
import { createRoot } from 'react-dom/client';
|
|
3
|
+
import { Theme, validateLocale, validateToolParams } from '@oanda/labs-widget-common';
|
|
4
|
+
import { SpreadCostCalculatorWidget } from './SpreadCostCalculatorWidget';
|
|
5
|
+
|
|
6
|
+
const {
|
|
7
|
+
graphqlUrl,
|
|
8
|
+
liveRatesUrl,
|
|
9
|
+
} = window.widgetsConfig || {};
|
|
10
|
+
const spreadCostCalculatorParamsElements = document.querySelectorAll(
|
|
11
|
+
'div[data-spread-cost-calculator-params]',
|
|
12
|
+
);
|
|
13
|
+
|
|
14
|
+
spreadCostCalculatorParamsElements.forEach((element) => {
|
|
15
|
+
const root = createRoot(element);
|
|
16
|
+
const params = element.getAttribute('data-spread-cost-calculator-params');
|
|
17
|
+
const mode = element.getAttribute('data-mode');
|
|
18
|
+
const { locale, instruments, division } = JSON.parse(params as string);
|
|
19
|
+
|
|
20
|
+
const isParamError = validateToolParams(
|
|
21
|
+
{
|
|
22
|
+
locale,
|
|
23
|
+
graphqlUrl,
|
|
24
|
+
instruments,
|
|
25
|
+
division,
|
|
26
|
+
liveRatesUrl,
|
|
27
|
+
},
|
|
28
|
+
[
|
|
29
|
+
{
|
|
30
|
+
name: 'locale',
|
|
31
|
+
valueCheck: (value) => validateLocale(value),
|
|
32
|
+
},
|
|
33
|
+
{
|
|
34
|
+
name: 'graphqlUrl',
|
|
35
|
+
},
|
|
36
|
+
{
|
|
37
|
+
name: 'liveRatesUrl',
|
|
38
|
+
},
|
|
39
|
+
{
|
|
40
|
+
name: 'division',
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
name: 'instruments',
|
|
44
|
+
valueCheck: (value) => (value as string[]).length > 0 && (value as string[]).length <= 10,
|
|
45
|
+
},
|
|
46
|
+
],
|
|
47
|
+
);
|
|
48
|
+
|
|
49
|
+
root.render(
|
|
50
|
+
<SpreadCostCalculatorWidget
|
|
51
|
+
division={division}
|
|
52
|
+
instruments={instruments}
|
|
53
|
+
liveRatesUrl={liveRatesUrl}
|
|
54
|
+
graphqlUrl={graphqlUrl}
|
|
55
|
+
locale={locale}
|
|
56
|
+
theme={mode as Theme}
|
|
57
|
+
isParamError={isParamError}
|
|
58
|
+
/>,
|
|
59
|
+
);
|
|
60
|
+
});
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { WidgetWrapperProps } from '@oanda/labs-widget-common';
|
|
2
|
+
import { Division } from '../gql/types/graphql';
|
|
3
|
+
|
|
4
|
+
export type SpreadCostCalculatorWidgetConfig = Pick<
|
|
5
|
+
WidgetWrapperProps,
|
|
6
|
+
'locale' | 'theme' | 'isParamError' | 'removePadding'
|
|
7
|
+
> & {
|
|
8
|
+
graphqlUrl: string;
|
|
9
|
+
liveRatesUrl: string;
|
|
10
|
+
division: Division;
|
|
11
|
+
instruments: string[];
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export type InstrumentResponse = {
|
|
15
|
+
name: string;
|
|
16
|
+
displayName: string;
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
export type MainProps = {
|
|
20
|
+
instruments: InstrumentResponse[];
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export interface ValidationWrapperProps {
|
|
24
|
+
division: Division;
|
|
25
|
+
instruments: string[];
|
|
26
|
+
isParamError?: boolean;
|
|
27
|
+
}
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import { useDebounceValue } from 'usehooks-ts';
|
|
2
|
+
import { useEffect } from 'react';
|
|
3
|
+
import { Voidable } from '@oanda/labs-widget-common';
|
|
4
|
+
|
|
5
|
+
export type UseCalculateSpreadInput = {
|
|
6
|
+
spread: Voidable<number>;
|
|
7
|
+
unitsTraded: Voidable<number>;
|
|
8
|
+
pipLocation: Voidable<number>;
|
|
9
|
+
delay?: number;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
const calculateAbsoluteSpreadCost = (
|
|
13
|
+
unitsTraded?: number,
|
|
14
|
+
spread?: number,
|
|
15
|
+
pipLocation?: number,
|
|
16
|
+
): number | undefined => (unitsTraded !== undefined
|
|
17
|
+
&& spread !== undefined
|
|
18
|
+
&& pipLocation !== undefined
|
|
19
|
+
? Math.ceil(
|
|
20
|
+
((unitsTraded * spread * Number((10 ** pipLocation).toFixed(Math.abs(pipLocation)))) / 2)
|
|
21
|
+
* 10000,
|
|
22
|
+
) / 10000
|
|
23
|
+
: undefined);
|
|
24
|
+
|
|
25
|
+
export const useCalculateSpread = ({
|
|
26
|
+
spread,
|
|
27
|
+
unitsTraded,
|
|
28
|
+
pipLocation,
|
|
29
|
+
delay = 350,
|
|
30
|
+
}: UseCalculateSpreadInput) => {
|
|
31
|
+
const [debouncedValue, setValue] = useDebounceValue<undefined | number>(undefined, delay);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setValue(calculateAbsoluteSpreadCost(unitsTraded, spread, pipLocation));
|
|
35
|
+
}, [spread, unitsTraded, pipLocation, setValue]);
|
|
36
|
+
|
|
37
|
+
return debouncedValue;
|
|
38
|
+
};
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import { SelectOption, useLiveRatesMessage, useLiveRatesQuery } from '@oanda/labs-widget-common';
|
|
2
|
+
import { useEffect, useState } from 'react';
|
|
3
|
+
|
|
4
|
+
export type UseInstrumentChangeInput = {
|
|
5
|
+
initialInstrument: SelectOption;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export const useInstrumentChange = ({
|
|
9
|
+
initialInstrument,
|
|
10
|
+
}: UseInstrumentChangeInput) => {
|
|
11
|
+
const { target, setQuery, closeQuery } = useLiveRatesQuery();
|
|
12
|
+
const [loading, setLoading] = useState(true);
|
|
13
|
+
const [selectedInstrument, setSelectedInstrument] = useState(initialInstrument);
|
|
14
|
+
const [pipLocation, setPipLocation] = useState<undefined | number>(undefined);
|
|
15
|
+
const [initialSpread, setSpread] = useState<undefined | number>(undefined);
|
|
16
|
+
|
|
17
|
+
const { update, error, connectionError } = useLiveRatesMessage(
|
|
18
|
+
selectedInstrument.id,
|
|
19
|
+
target,
|
|
20
|
+
);
|
|
21
|
+
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (selectedInstrument.id === update?.symbol) {
|
|
24
|
+
setPipLocation(update.pipLocation);
|
|
25
|
+
setSpread(update.spread);
|
|
26
|
+
setLoading(false);
|
|
27
|
+
closeQuery();
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
31
|
+
}, [update]);
|
|
32
|
+
|
|
33
|
+
useEffect(() => {
|
|
34
|
+
setLoading(true);
|
|
35
|
+
setQuery({ instruments: [selectedInstrument.id] });
|
|
36
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
37
|
+
}, [selectedInstrument.id]);
|
|
38
|
+
|
|
39
|
+
return {
|
|
40
|
+
selectedInstrument,
|
|
41
|
+
setSelectedInstrument,
|
|
42
|
+
pipLocation,
|
|
43
|
+
initialSpread,
|
|
44
|
+
loading,
|
|
45
|
+
error: !!error || !!connectionError,
|
|
46
|
+
};
|
|
47
|
+
};
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { InstrumentResponse } from './types';
|
|
2
|
+
|
|
3
|
+
export const getInstrumentCurrency = (instrument: string | InstrumentResponse | null): string => {
|
|
4
|
+
if (typeof instrument === 'string') {
|
|
5
|
+
return instrument.split('_')[1] || ' ';
|
|
6
|
+
}
|
|
7
|
+
if (instrument?.name) {
|
|
8
|
+
return instrument.name.split('_')[1] || ' ';
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
return ' ';
|
|
12
|
+
};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from '@graphql-typed-document-node/core';
|
|
2
|
+
import { FragmentDefinitionNode } from 'graphql';
|
|
3
|
+
import { Incremental } from './graphql';
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> = TDocumentType extends DocumentTypeDecoration<
|
|
7
|
+
infer TType,
|
|
8
|
+
any
|
|
9
|
+
>
|
|
10
|
+
? [TType] extends [{ ' $fragmentName'?: infer TKey }]
|
|
11
|
+
? TKey extends string
|
|
12
|
+
? { ' $fragmentRefs'?: { [key in TKey]: TType } }
|
|
13
|
+
: never
|
|
14
|
+
: never
|
|
15
|
+
: never;
|
|
16
|
+
|
|
17
|
+
// return non-nullable if `fragmentType` is non-nullable
|
|
18
|
+
export function useFragment<TType>(
|
|
19
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
20
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
|
|
21
|
+
): TType;
|
|
22
|
+
// return nullable if `fragmentType` is nullable
|
|
23
|
+
export function useFragment<TType>(
|
|
24
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
25
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
|
|
26
|
+
): TType | null | undefined;
|
|
27
|
+
// return array of non-nullable if `fragmentType` is array of non-nullable
|
|
28
|
+
export function useFragment<TType>(
|
|
29
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
30
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
|
|
31
|
+
): ReadonlyArray<TType>;
|
|
32
|
+
// return array of nullable if `fragmentType` is array of nullable
|
|
33
|
+
export function useFragment<TType>(
|
|
34
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
35
|
+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
36
|
+
): ReadonlyArray<TType> | null | undefined;
|
|
37
|
+
export function useFragment<TType>(
|
|
38
|
+
_documentNode: DocumentTypeDecoration<TType, any>,
|
|
39
|
+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
|
|
40
|
+
): TType | ReadonlyArray<TType> | null | undefined {
|
|
41
|
+
return fragmentType as any;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
export function makeFragmentData<
|
|
46
|
+
F extends DocumentTypeDecoration<any, any>,
|
|
47
|
+
FT extends ResultOf<F>
|
|
48
|
+
>(data: FT, _fragment: F): FragmentType<F> {
|
|
49
|
+
return data as FragmentType<F>;
|
|
50
|
+
}
|
|
51
|
+
export function isFragmentReady<TQuery, TFrag>(
|
|
52
|
+
queryNode: DocumentTypeDecoration<TQuery, any>,
|
|
53
|
+
fragmentNode: TypedDocumentNode<TFrag>,
|
|
54
|
+
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
|
|
55
|
+
): data is FragmentType<typeof fragmentNode> {
|
|
56
|
+
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
|
|
57
|
+
?.deferredFields;
|
|
58
|
+
|
|
59
|
+
if (!deferredFields) return true;
|
|
60
|
+
|
|
61
|
+
const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
|
|
62
|
+
const fragName = fragDef?.name?.value;
|
|
63
|
+
|
|
64
|
+
const fields = (fragName && deferredFields[fragName]) || [];
|
|
65
|
+
return fields.length > 0 && fields.every(field => data && field in data);
|
|
66
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
/* eslint-disable */
|
|
2
|
+
import * as types from './graphql';
|
|
3
|
+
import { TypedDocumentNode as DocumentNode } from '@graphql-typed-document-node/core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Map of all GraphQL operations in the project.
|
|
7
|
+
*
|
|
8
|
+
* This map has several performance disadvantages:
|
|
9
|
+
* 1. It is not tree-shakeable, so it will include all operations in the project.
|
|
10
|
+
* 2. It is not minifiable, so the string of a GraphQL query will be multiple times inside the bundle.
|
|
11
|
+
* 3. It does not support dead code elimination, so it will add unused operations.
|
|
12
|
+
*
|
|
13
|
+
* Therefore it is highly recommended to use the babel or swc plugin for production.
|
|
14
|
+
*/
|
|
15
|
+
const documents = {
|
|
16
|
+
"\n query validateInstruments($instruments: [String]!, $division: Division) {\n mapInstrumentNames(instruments: $instruments, division: $division) {\n name\n displayName\n }\n }\n": types.ValidateInstrumentsDocument,
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
|
21
|
+
*
|
|
22
|
+
*
|
|
23
|
+
* @example
|
|
24
|
+
* ```ts
|
|
25
|
+
* const query = graphql(`query GetUser($id: ID!) { user(id: $id) { name } }`);
|
|
26
|
+
* ```
|
|
27
|
+
*
|
|
28
|
+
* The query argument is unknown!
|
|
29
|
+
* Please regenerate the types.
|
|
30
|
+
*/
|
|
31
|
+
export function graphql(source: string): unknown;
|
|
32
|
+
|
|
33
|
+
/**
|
|
34
|
+
* The graphql function is used to parse GraphQL queries into a document that can be used by GraphQL clients.
|
|
35
|
+
*/
|
|
36
|
+
export function graphql(source: "\n query validateInstruments($instruments: [String]!, $division: Division) {\n mapInstrumentNames(instruments: $instruments, division: $division) {\n name\n displayName\n }\n }\n"): (typeof documents)["\n query validateInstruments($instruments: [String]!, $division: Division) {\n mapInstrumentNames(instruments: $instruments, division: $division) {\n name\n displayName\n }\n }\n"];
|
|
37
|
+
|
|
38
|
+
export function graphql(source: string) {
|
|
39
|
+
return (documents as any)[source] ?? {};
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
export type DocumentType<TDocumentNode extends DocumentNode<any, any>> = TDocumentNode extends DocumentNode< infer TType, any> ? TType : never;
|