@rango-dev/widget-embedded 0.30.1-next.9 → 0.31.0
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/dist/components/ConfirmWalletsModal/ConfirmWalletsModal.d.ts.map +1 -1
- package/dist/components/FilterSelector/FilterSelector.d.ts +4 -0
- package/dist/components/FilterSelector/FilterSelector.d.ts.map +1 -0
- package/dist/components/FilterSelector/FilterSelector.style.d.ts +962 -0
- package/dist/components/FilterSelector/FilterSelector.style.d.ts.map +1 -0
- package/dist/components/FilterSelector/FilterSelector.type.d.ts +14 -0
- package/dist/components/FilterSelector/FilterSelector.type.d.ts.map +1 -0
- package/dist/components/FilterSelector/FilterSelectorContent.d.ts +4 -0
- package/dist/components/FilterSelector/FilterSelectorContent.d.ts.map +1 -0
- package/dist/components/FilterSelector/index.d.ts +2 -0
- package/dist/components/FilterSelector/index.d.ts.map +1 -0
- package/dist/components/SearchInput/SearchInput.d.ts.map +1 -1
- package/dist/components/SwapDetails/SwapDetails.d.ts.map +1 -1
- package/dist/components/TokenList/TokenList.d.ts.map +1 -1
- package/dist/containers/Inputs/Inputs.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.d.ts.map +1 -1
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts +5 -1
- package/dist/containers/WidgetInfo/WidgetInfo.types.d.ts.map +1 -1
- package/dist/hooks/useObserveBalanceChanges/index.d.ts +2 -0
- package/dist/hooks/useObserveBalanceChanges/index.d.ts.map +1 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts +4 -0
- package/dist/hooks/useObserveBalanceChanges/useObserveBalanceChanges.d.ts.map +1 -0
- package/dist/hooks/useUpdateQuoteInputs/index.d.ts +2 -0
- package/dist/hooks/useUpdateQuoteInputs/index.d.ts.map +1 -0
- package/dist/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.d.ts +3 -0
- package/dist/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.d.ts.map +1 -0
- package/dist/index.js +2 -2
- package/dist/index.js.map +4 -4
- package/dist/pages/HistoryPage.d.ts.map +1 -1
- package/dist/pages/WalletsPage.d.ts.map +1 -1
- package/dist/types/quote.d.ts +9 -1
- package/dist/types/quote.d.ts.map +1 -1
- package/dist/widget-embedded.build.json +1 -1
- package/package.json +10 -10
- package/src/components/ConfirmWalletsModal/ConfirmWalletsModal.tsx +2 -1
- package/src/components/FilterSelector/FilterSelector.style.ts +95 -0
- package/src/components/FilterSelector/FilterSelector.tsx +42 -0
- package/src/components/FilterSelector/FilterSelector.type.ts +15 -0
- package/src/components/FilterSelector/FilterSelectorContent.tsx +60 -0
- package/src/components/FilterSelector/index.ts +1 -0
- package/src/components/SearchInput/SearchInput.tsx +2 -0
- package/src/components/SwapDetails/SwapDetails.tsx +1 -7
- package/src/components/TokenList/TokenList.tsx +8 -2
- package/src/containers/Inputs/Inputs.tsx +1 -0
- package/src/containers/WidgetInfo/WidgetInfo.tsx +26 -0
- package/src/containers/WidgetInfo/WidgetInfo.types.ts +5 -1
- package/src/hooks/useObserveBalanceChanges/index.ts +1 -0
- package/src/hooks/useObserveBalanceChanges/useObserveBalanceChanges.ts +68 -0
- package/src/hooks/useUpdateQuoteInputs/index.ts +1 -0
- package/src/hooks/useUpdateQuoteInputs/useUpdateQuoteInputs.ts +50 -0
- package/src/pages/HistoryPage.tsx +180 -26
- package/src/pages/WalletsPage.tsx +4 -2
- package/src/store/quote.ts +1 -1
- package/src/store/slices/data.ts +1 -1
- package/src/types/quote.ts +11 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rango-dev/widget-embedded",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.31.0",
|
|
4
4
|
"license": "MIT",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"source": "./src/index.ts",
|
|
@@ -24,15 +24,15 @@
|
|
|
24
24
|
"dependencies": {
|
|
25
25
|
"@lingui/core": "4.2.1",
|
|
26
26
|
"@lingui/react": "4.2.1",
|
|
27
|
-
"@rango-dev/logging-core": "^0.
|
|
28
|
-
"@rango-dev/provider-all": "^0.
|
|
29
|
-
"@rango-dev/queue-manager-core": "^0.
|
|
30
|
-
"@rango-dev/queue-manager-rango-preset": "^0.
|
|
31
|
-
"@rango-dev/queue-manager-react": "^0.
|
|
32
|
-
"@rango-dev/signer-solana": "^0.
|
|
33
|
-
"@rango-dev/ui": "^0.
|
|
34
|
-
"@rango-dev/wallets-react": "^0.
|
|
35
|
-
"@rango-dev/wallets-shared": "^0.
|
|
27
|
+
"@rango-dev/logging-core": "^0.6.0",
|
|
28
|
+
"@rango-dev/provider-all": "^0.36.0",
|
|
29
|
+
"@rango-dev/queue-manager-core": "^0.27.0",
|
|
30
|
+
"@rango-dev/queue-manager-rango-preset": "^0.36.0",
|
|
31
|
+
"@rango-dev/queue-manager-react": "^0.27.0",
|
|
32
|
+
"@rango-dev/signer-solana": "^0.31.0",
|
|
33
|
+
"@rango-dev/ui": "^0.37.0",
|
|
34
|
+
"@rango-dev/wallets-react": "^0.22.0",
|
|
35
|
+
"@rango-dev/wallets-shared": "^0.36.0",
|
|
36
36
|
"bignumber.js": "^9.1.1",
|
|
37
37
|
"copy-to-clipboard": "^3.3.3",
|
|
38
38
|
"dayjs": "^1.11.7",
|
|
@@ -327,6 +327,7 @@ export function ConfirmWalletsModal(props: PropTypes) {
|
|
|
327
327
|
dismissible={!showMoreWalletFor}
|
|
328
328
|
container={modalContainer}
|
|
329
329
|
{...(!showMoreWalletFor && {
|
|
330
|
+
styles: { container: { height: '100%' } },
|
|
330
331
|
footer: (
|
|
331
332
|
<ConfirmButton>
|
|
332
333
|
<Button
|
|
@@ -350,7 +351,7 @@ export function ConfirmWalletsModal(props: PropTypes) {
|
|
|
350
351
|
),
|
|
351
352
|
})}
|
|
352
353
|
{...(showMoreWalletFor && {
|
|
353
|
-
styles: { container: { padding: '$0' } },
|
|
354
|
+
styles: { container: { height: '100%', padding: '$0' } },
|
|
354
355
|
header: (
|
|
355
356
|
<ShowMoreHeader>
|
|
356
357
|
<NavigateBack
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
import { css, darkTheme, IconButton, styled } from '@rango-dev/ui';
|
|
2
|
+
|
|
3
|
+
export const IconContainer = styled('div', {
|
|
4
|
+
position: 'relative',
|
|
5
|
+
'&::before': {
|
|
6
|
+
position: 'absolute',
|
|
7
|
+
right: '1px',
|
|
8
|
+
top: '-1px',
|
|
9
|
+
width: '$8',
|
|
10
|
+
height: '$8',
|
|
11
|
+
borderRadius: '100%',
|
|
12
|
+
backgroundColor: '$neutral300',
|
|
13
|
+
[`.${darkTheme} &`]: {
|
|
14
|
+
backgroundColor: '$neutral400',
|
|
15
|
+
},
|
|
16
|
+
padding: '$2',
|
|
17
|
+
},
|
|
18
|
+
variants: {
|
|
19
|
+
isSelect: {
|
|
20
|
+
true: {
|
|
21
|
+
'&::before': {
|
|
22
|
+
content: '',
|
|
23
|
+
},
|
|
24
|
+
},
|
|
25
|
+
},
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
export const FilterButton = styled(IconButton, {
|
|
30
|
+
width: '$36',
|
|
31
|
+
height: '$36',
|
|
32
|
+
position: 'relative',
|
|
33
|
+
padding: '0',
|
|
34
|
+
overflow: 'unset',
|
|
35
|
+
backgroundColor: '$neutral300',
|
|
36
|
+
[`.${darkTheme} &`]: {
|
|
37
|
+
backgroundColor: '$neutral400',
|
|
38
|
+
},
|
|
39
|
+
'&:hover': {
|
|
40
|
+
backgroundColor: '$secondary100',
|
|
41
|
+
[`.${darkTheme} &`]: {
|
|
42
|
+
backgroundColor: '$neutral',
|
|
43
|
+
},
|
|
44
|
+
[`& ${IconContainer}::before`]: {
|
|
45
|
+
backgroundColor: '$secondary100',
|
|
46
|
+
[`.${darkTheme} &`]: {
|
|
47
|
+
backgroundColor: '$neutral',
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
},
|
|
51
|
+
variants: {
|
|
52
|
+
isSelect: {
|
|
53
|
+
true: {
|
|
54
|
+
border: '1px solid $secondary',
|
|
55
|
+
},
|
|
56
|
+
},
|
|
57
|
+
},
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
export const Badge = styled('div', {
|
|
61
|
+
position: 'absolute',
|
|
62
|
+
width: '$6',
|
|
63
|
+
height: '$6',
|
|
64
|
+
display: 'flex',
|
|
65
|
+
justifyContent: 'center',
|
|
66
|
+
alignItems: 'center',
|
|
67
|
+
borderRadius: '3px',
|
|
68
|
+
top: '$0',
|
|
69
|
+
right: '1px',
|
|
70
|
+
backgroundColor: '$secondary500',
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
export const FilterContainer = styled('div', {
|
|
74
|
+
padding: '$15',
|
|
75
|
+
borderRadius: '$sm',
|
|
76
|
+
width: '248px',
|
|
77
|
+
backgroundColor: '$background',
|
|
78
|
+
zIndex: 10,
|
|
79
|
+
});
|
|
80
|
+
|
|
81
|
+
export const centeredFlexContainer = css({
|
|
82
|
+
display: 'flex',
|
|
83
|
+
justifyContent: 'space-between',
|
|
84
|
+
alignItems: 'center',
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
export const FilterList = styled('ul', {
|
|
88
|
+
margin: 0,
|
|
89
|
+
listStyle: 'none',
|
|
90
|
+
height: '100%',
|
|
91
|
+
padding: 0,
|
|
92
|
+
'.item-start-container': {
|
|
93
|
+
paddingRight: '0 !important',
|
|
94
|
+
},
|
|
95
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { FilterSelectorPropTypes } from './FilterSelector.type';
|
|
2
|
+
|
|
3
|
+
import { FilterIcon, Popover } from '@rango-dev/ui';
|
|
4
|
+
import React from 'react';
|
|
5
|
+
|
|
6
|
+
import { getContainer } from '../../utils/common';
|
|
7
|
+
|
|
8
|
+
import { Badge, FilterButton, IconContainer } from './FilterSelector.style';
|
|
9
|
+
import { FilterSelectorContent } from './FilterSelectorContent';
|
|
10
|
+
|
|
11
|
+
export function FilterSelector(props: FilterSelectorPropTypes) {
|
|
12
|
+
const { onClickItem, onOpenChange, filterBy, list, open } = props;
|
|
13
|
+
return (
|
|
14
|
+
<div>
|
|
15
|
+
<Popover
|
|
16
|
+
open={open}
|
|
17
|
+
align="end"
|
|
18
|
+
onOpenChange={onOpenChange}
|
|
19
|
+
container={getContainer()}
|
|
20
|
+
content={
|
|
21
|
+
<FilterSelectorContent
|
|
22
|
+
list={list}
|
|
23
|
+
filterBy={filterBy}
|
|
24
|
+
onClickItem={(id) => {
|
|
25
|
+
onClickItem(id);
|
|
26
|
+
onOpenChange(false);
|
|
27
|
+
}}
|
|
28
|
+
/>
|
|
29
|
+
}>
|
|
30
|
+
<FilterButton
|
|
31
|
+
variant="default"
|
|
32
|
+
isSelect={!!filterBy}
|
|
33
|
+
onClick={() => onOpenChange(!props.open)}>
|
|
34
|
+
<IconContainer isSelect={!!filterBy}>
|
|
35
|
+
<FilterIcon size={16} color="black" />
|
|
36
|
+
{!!filterBy && <Badge />}
|
|
37
|
+
</IconContainer>
|
|
38
|
+
</FilterButton>
|
|
39
|
+
</Popover>
|
|
40
|
+
</div>
|
|
41
|
+
);
|
|
42
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type PropTypes = {
|
|
2
|
+
filterBy: string;
|
|
3
|
+
onClickItem: (id: string) => void;
|
|
4
|
+
list: Array<FilterItem>;
|
|
5
|
+
};
|
|
6
|
+
|
|
7
|
+
export type FilterItem = {
|
|
8
|
+
id: string;
|
|
9
|
+
title: string;
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
export type FilterSelectorPropTypes = PropTypes & {
|
|
13
|
+
open: boolean;
|
|
14
|
+
onOpenChange: (open: boolean) => void;
|
|
15
|
+
};
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { PropTypes } from './FilterSelector.type';
|
|
2
|
+
|
|
3
|
+
import { i18n } from '@lingui/core';
|
|
4
|
+
import {
|
|
5
|
+
Button,
|
|
6
|
+
Divider,
|
|
7
|
+
ListItemButton,
|
|
8
|
+
Radio,
|
|
9
|
+
RadioRoot,
|
|
10
|
+
Typography,
|
|
11
|
+
} from '@rango-dev/ui';
|
|
12
|
+
import React from 'react';
|
|
13
|
+
|
|
14
|
+
import {
|
|
15
|
+
centeredFlexContainer,
|
|
16
|
+
FilterContainer,
|
|
17
|
+
FilterList,
|
|
18
|
+
} from './FilterSelector.style';
|
|
19
|
+
|
|
20
|
+
export function FilterSelectorContent(props: PropTypes) {
|
|
21
|
+
const { filterBy: value, onClickItem } = props;
|
|
22
|
+
return (
|
|
23
|
+
<FilterContainer>
|
|
24
|
+
<div className={centeredFlexContainer()}>
|
|
25
|
+
<Typography size="small" variant="body">
|
|
26
|
+
{i18n.t('Status')}
|
|
27
|
+
</Typography>
|
|
28
|
+
<Button variant="ghost" size="xxsmall" onClick={() => onClickItem('')}>
|
|
29
|
+
{i18n.t('Reset')}
|
|
30
|
+
</Button>
|
|
31
|
+
</div>
|
|
32
|
+
<Divider size={10} />
|
|
33
|
+
<RadioRoot value={value}>
|
|
34
|
+
<FilterList>
|
|
35
|
+
{props.list.map((item, index) => {
|
|
36
|
+
return (
|
|
37
|
+
<ListItemButton
|
|
38
|
+
key={item.id}
|
|
39
|
+
style={{ height: '40px', width: '100%' }}
|
|
40
|
+
selected={false}
|
|
41
|
+
hasDivider={props.list.length - 1 != index}
|
|
42
|
+
id={item.id}
|
|
43
|
+
title={
|
|
44
|
+
<>
|
|
45
|
+
<Divider direction="horizontal" size={4} />
|
|
46
|
+
<Typography size="medium" variant="body">
|
|
47
|
+
{item.title}
|
|
48
|
+
</Typography>
|
|
49
|
+
</>
|
|
50
|
+
}
|
|
51
|
+
start={<Radio value={item.id} />}
|
|
52
|
+
onClick={onClickItem}
|
|
53
|
+
/>
|
|
54
|
+
);
|
|
55
|
+
})}
|
|
56
|
+
</FilterList>
|
|
57
|
+
</RadioRoot>
|
|
58
|
+
</FilterContainer>
|
|
59
|
+
);
|
|
60
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { FilterSelector } from './FilterSelector';
|
|
@@ -13,6 +13,7 @@ export function SearchInput(props: PropTypes) {
|
|
|
13
13
|
size,
|
|
14
14
|
onChange,
|
|
15
15
|
value,
|
|
16
|
+
style,
|
|
16
17
|
setValue,
|
|
17
18
|
...inputAttributes
|
|
18
19
|
} = props;
|
|
@@ -41,6 +42,7 @@ export function SearchInput(props: PropTypes) {
|
|
|
41
42
|
padding: 10,
|
|
42
43
|
borderRadius: 25,
|
|
43
44
|
alignItems: 'center',
|
|
45
|
+
...style,
|
|
44
46
|
}}
|
|
45
47
|
size={size}
|
|
46
48
|
value={value}
|
|
@@ -188,13 +188,7 @@ export function SwapDetails(props: SwapDetailsProps) {
|
|
|
188
188
|
)?.diagnosisUrl;
|
|
189
189
|
|
|
190
190
|
const outputUsdValue = numberToString(
|
|
191
|
-
parseFloat(
|
|
192
|
-
numberToString(
|
|
193
|
-
outputAmount,
|
|
194
|
-
TOKEN_AMOUNT_MIN_DECIMALS,
|
|
195
|
-
TOKEN_AMOUNT_MAX_DECIMALS
|
|
196
|
-
)
|
|
197
|
-
) * (lastStep.toUsdPrice || 0),
|
|
191
|
+
parseFloat(outputAmount || '0') * (lastStep.toUsdPrice || 0),
|
|
198
192
|
USD_VALUE_MIN_DECIMALS,
|
|
199
193
|
USD_VALUE_MAX_DECIMALS
|
|
200
194
|
);
|
|
@@ -17,6 +17,7 @@ import {
|
|
|
17
17
|
} from '@rango-dev/ui';
|
|
18
18
|
import React, { useEffect, useState } from 'react';
|
|
19
19
|
|
|
20
|
+
import { useObserveBalanceChanges } from '../../hooks/useObserveBalanceChanges';
|
|
20
21
|
import { useAppStore } from '../../store/AppStore';
|
|
21
22
|
import { useWalletsStore } from '../../store/wallets';
|
|
22
23
|
import { createTintsAndShades } from '../../utils/colors';
|
|
@@ -99,6 +100,11 @@ export function TokenList(props: PropTypes) {
|
|
|
99
100
|
const [hasNextPage, setHasNextPage] = useState<boolean>(true);
|
|
100
101
|
const { getBalanceFor, loading: loadingWallet } = useWalletsStore();
|
|
101
102
|
const { isTokenPinned } = useAppStore();
|
|
103
|
+
/**
|
|
104
|
+
* We can create the key by hashing the list of tokens,
|
|
105
|
+
* but if the list is large, the memory usage and cost of comparisons may be high.
|
|
106
|
+
*/
|
|
107
|
+
const { balanceKey } = useObserveBalanceChanges(selectedBlockchain);
|
|
102
108
|
|
|
103
109
|
const loadNextPage = () => {
|
|
104
110
|
setTokens(list.slice(0, tokens.length + PAGE_SIZE));
|
|
@@ -110,7 +116,7 @@ export function TokenList(props: PropTypes) {
|
|
|
110
116
|
|
|
111
117
|
useEffect(() => {
|
|
112
118
|
setTokens(list.slice(0, PAGE_SIZE));
|
|
113
|
-
}, [list.length, selectedBlockchain]);
|
|
119
|
+
}, [list.length, selectedBlockchain, balanceKey]);
|
|
114
120
|
|
|
115
121
|
const renderList = () => {
|
|
116
122
|
if (!tokens.length && !!searchedFor) {
|
|
@@ -244,7 +250,7 @@ export function TokenList(props: PropTypes) {
|
|
|
244
250
|
);
|
|
245
251
|
}}
|
|
246
252
|
totalCount={tokens.length}
|
|
247
|
-
key={`${selectedBlockchain}-${searchedFor}`}
|
|
253
|
+
key={`${selectedBlockchain}-${searchedFor}-${balanceKey}`}
|
|
248
254
|
/>
|
|
249
255
|
);
|
|
250
256
|
};
|
|
@@ -4,6 +4,7 @@ import { useManager } from '@rango-dev/queue-manager-react';
|
|
|
4
4
|
import React, { createContext, useContext } from 'react';
|
|
5
5
|
|
|
6
6
|
import { useLanguage } from '../../hooks/useLanguage';
|
|
7
|
+
import { useUpdateQuoteInputs } from '../../hooks/useUpdateQuoteInputs/useUpdateQuoteInputs';
|
|
7
8
|
import { useAppStore } from '../../store/AppStore';
|
|
8
9
|
import { useNotificationStore } from '../../store/notification';
|
|
9
10
|
import { useQuoteStore } from '../../store/quote';
|
|
@@ -35,6 +36,9 @@ export function WidgetInfo(props: React.PropsWithChildren) {
|
|
|
35
36
|
const resetLanguage = useLanguage().resetLanguage;
|
|
36
37
|
const notifications = useNotificationStore().getNotifications();
|
|
37
38
|
const clearNotifications = useNotificationStore().clearNotifications;
|
|
39
|
+
const updateQuoteInputs = useUpdateQuoteInputs();
|
|
40
|
+
const { fromBlockchain, toBlockchain, fromToken, toToken, inputAmount } =
|
|
41
|
+
useQuoteStore();
|
|
38
42
|
|
|
39
43
|
// eslint-disable-next-line react/jsx-no-constructed-context-values
|
|
40
44
|
const value: WidgetInfoContextInterface = {
|
|
@@ -59,6 +63,28 @@ export function WidgetInfo(props: React.PropsWithChildren) {
|
|
|
59
63
|
list: notifications,
|
|
60
64
|
clearAll: clearNotifications,
|
|
61
65
|
},
|
|
66
|
+
quote: {
|
|
67
|
+
quoteInputs: {
|
|
68
|
+
fromBlockchain: fromBlockchain?.name ?? null,
|
|
69
|
+
fromToken: fromToken
|
|
70
|
+
? {
|
|
71
|
+
symbol: fromToken.symbol,
|
|
72
|
+
blockchain: fromToken.blockchain,
|
|
73
|
+
address: fromToken.address,
|
|
74
|
+
}
|
|
75
|
+
: null,
|
|
76
|
+
toBlockchain: toBlockchain?.name ?? null,
|
|
77
|
+
toToken: toToken
|
|
78
|
+
? {
|
|
79
|
+
symbol: toToken.symbol,
|
|
80
|
+
blockchain: toToken.blockchain,
|
|
81
|
+
address: toToken.address,
|
|
82
|
+
}
|
|
83
|
+
: null,
|
|
84
|
+
requestAmount: inputAmount,
|
|
85
|
+
},
|
|
86
|
+
updateQuoteInputs,
|
|
87
|
+
},
|
|
62
88
|
};
|
|
63
89
|
|
|
64
90
|
return (
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { WidgetHistory } from './WidgetInfo.helpers';
|
|
2
2
|
import type { FetchStatus, FindToken } from '../../store/slices/data';
|
|
3
3
|
import type { ConnectedWallet } from '../../store/wallets';
|
|
4
|
-
import type { Wallet } from '../../types';
|
|
4
|
+
import type { QuoteInputs, UpdateQuoteInputs, Wallet } from '../../types';
|
|
5
5
|
import type { Notification } from '../../types/notification';
|
|
6
6
|
import type { BlockchainMeta, SwapperMeta, Token } from 'rango-sdk';
|
|
7
7
|
|
|
@@ -27,4 +27,8 @@ export interface WidgetInfoContextInterface {
|
|
|
27
27
|
list: Notification[];
|
|
28
28
|
clearAll: () => void;
|
|
29
29
|
};
|
|
30
|
+
quote: {
|
|
31
|
+
quoteInputs: QuoteInputs;
|
|
32
|
+
updateQuoteInputs: UpdateQuoteInputs;
|
|
33
|
+
};
|
|
30
34
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useObserveBalanceChanges } from './useObserveBalanceChanges';
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { WalletEventData } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { useWallets } from '@rango-dev/wallets-react';
|
|
4
|
+
import { useEffect, useRef, useState } from 'react';
|
|
5
|
+
|
|
6
|
+
import { widgetEventEmitter } from '../../services/eventEmitter';
|
|
7
|
+
import { useWalletsStore } from '../../store/wallets';
|
|
8
|
+
import { WalletEventTypes, WidgetEvents } from '../../types';
|
|
9
|
+
|
|
10
|
+
// A hook to listen for and detect changes in balances on a specific blockchain.
|
|
11
|
+
export function useObserveBalanceChanges(selectedBlockchain?: string) {
|
|
12
|
+
const { connectedWallets } = useWalletsStore();
|
|
13
|
+
const { getWalletInfo } = useWallets();
|
|
14
|
+
const prevFetchingBalanceWallets = useRef<string[]>([]);
|
|
15
|
+
// The "balanceKey" will be updated and incremented after each change in the balance for a blockchain.
|
|
16
|
+
const [balanceKey, setBalanceKey] = useState(0);
|
|
17
|
+
|
|
18
|
+
useEffect(() => {
|
|
19
|
+
const handleWalletEvent = (event: WalletEventData) => {
|
|
20
|
+
if (event.type === WalletEventTypes.DISCONNECT) {
|
|
21
|
+
const walletInfo = getWalletInfo(event.payload.walletType);
|
|
22
|
+
const blockchainSupported = walletInfo.supportedChains.some(
|
|
23
|
+
(chain) => chain.name === selectedBlockchain
|
|
24
|
+
);
|
|
25
|
+
|
|
26
|
+
if (blockchainSupported) {
|
|
27
|
+
setBalanceKey((prev) => prev + 1);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
widgetEventEmitter.on(WidgetEvents.WalletEvent, handleWalletEvent);
|
|
33
|
+
|
|
34
|
+
if (!selectedBlockchain) {
|
|
35
|
+
return setBalanceKey((prev) => prev + 1);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const supportedWallets = connectedWallets.filter(
|
|
39
|
+
(wallet) => wallet.chain === selectedBlockchain
|
|
40
|
+
);
|
|
41
|
+
|
|
42
|
+
supportedWallets.forEach((wallet) => {
|
|
43
|
+
const { walletType } = wallet;
|
|
44
|
+
const walletIsAlreadyFetching =
|
|
45
|
+
prevFetchingBalanceWallets.current.includes(walletType);
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* Watching for changes in "wallet.balances.length" is not accurate.
|
|
49
|
+
* Additionally, checking the equality of previous and current balances for a specific blockchain is resource-intensive, so we avoid these methods.
|
|
50
|
+
*/
|
|
51
|
+
if (wallet.loading && !walletIsAlreadyFetching) {
|
|
52
|
+
prevFetchingBalanceWallets.current =
|
|
53
|
+
prevFetchingBalanceWallets.current.concat(walletType);
|
|
54
|
+
} else if (!wallet.loading && walletIsAlreadyFetching) {
|
|
55
|
+
prevFetchingBalanceWallets.current =
|
|
56
|
+
prevFetchingBalanceWallets.current.filter(
|
|
57
|
+
(prevWallet) => prevWallet !== walletType
|
|
58
|
+
);
|
|
59
|
+
setBalanceKey((prev) => prev + 1);
|
|
60
|
+
}
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
return () =>
|
|
64
|
+
widgetEventEmitter.off(WidgetEvents.WalletEvent, handleWalletEvent);
|
|
65
|
+
}, [connectedWallets, selectedBlockchain, getWalletInfo]);
|
|
66
|
+
|
|
67
|
+
return { balanceKey };
|
|
68
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { useUpdateQuoteInputs } from './useUpdateQuoteInputs';
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import type { UpdateQuoteInputs } from '../../types';
|
|
2
|
+
|
|
3
|
+
import { useAppStore } from '../../store/AppStore';
|
|
4
|
+
import { useQuoteStore } from '../../store/quote';
|
|
5
|
+
|
|
6
|
+
// This hook provides a function to update quote inputs using fewer and simpler parameters.
|
|
7
|
+
export function useUpdateQuoteInputs() {
|
|
8
|
+
const { findToken } = useAppStore();
|
|
9
|
+
const blockchains = useAppStore().blockchains();
|
|
10
|
+
const tokens = useAppStore().tokens();
|
|
11
|
+
const {
|
|
12
|
+
setFromBlockchain,
|
|
13
|
+
setFromToken,
|
|
14
|
+
setToBlockchain,
|
|
15
|
+
setToToken,
|
|
16
|
+
setInputAmount,
|
|
17
|
+
} = useQuoteStore();
|
|
18
|
+
|
|
19
|
+
const updateQuoteInputs: UpdateQuoteInputs = (params) => {
|
|
20
|
+
const { fromBlockchain, fromToken, toBlockchain, toToken, requestAmount } =
|
|
21
|
+
params;
|
|
22
|
+
const meta = { blockchains, tokens };
|
|
23
|
+
|
|
24
|
+
if (fromBlockchain !== undefined) {
|
|
25
|
+
const blockchainMeta =
|
|
26
|
+
blockchains.find((blockchain) => blockchain.name === fromBlockchain) ??
|
|
27
|
+
null;
|
|
28
|
+
setFromBlockchain(blockchainMeta);
|
|
29
|
+
}
|
|
30
|
+
if (fromToken !== undefined) {
|
|
31
|
+
const token = fromToken ? findToken(fromToken) ?? null : null;
|
|
32
|
+
setFromToken({ meta, token });
|
|
33
|
+
}
|
|
34
|
+
if (toBlockchain !== undefined) {
|
|
35
|
+
const blockchainMeta =
|
|
36
|
+
blockchains.find((blockchain) => blockchain.name === toBlockchain) ??
|
|
37
|
+
null;
|
|
38
|
+
setToBlockchain(blockchainMeta);
|
|
39
|
+
}
|
|
40
|
+
if (toToken !== undefined) {
|
|
41
|
+
const token = toToken ? findToken(toToken) ?? null : null;
|
|
42
|
+
setToToken({ meta, token });
|
|
43
|
+
}
|
|
44
|
+
if (requestAmount !== undefined) {
|
|
45
|
+
setInputAmount(requestAmount);
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
return updateQuoteInputs;
|
|
50
|
+
}
|