@lifi/widget 4.8.0 → 4.9.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.
Files changed (115) hide show
  1. package/dist/esm/components/AmountInputCard/AmountInputCardPair.js +5 -2
  2. package/dist/esm/components/AmountInputCard/AmountInputCardPair.js.map +1 -1
  3. package/dist/esm/components/SelectChainAndToken.js +5 -2
  4. package/dist/esm/components/SelectChainAndToken.js.map +1 -1
  5. package/dist/esm/components/SendToWallet/SendToWalletButton.js +5 -4
  6. package/dist/esm/components/SendToWallet/SendToWalletButton.js.map +1 -1
  7. package/dist/esm/components/TokenList/useTokenSelect.d.ts.map +1 -1
  8. package/dist/esm/components/TokenList/useTokenSelect.js +10 -0
  9. package/dist/esm/components/TokenList/useTokenSelect.js.map +1 -1
  10. package/dist/esm/config/constants.js.map +1 -1
  11. package/dist/esm/config/version.d.ts +1 -1
  12. package/dist/esm/config/version.js +1 -1
  13. package/dist/esm/config/version.js.map +1 -1
  14. package/dist/esm/hooks/useAccountsBalancesData.js +5 -2
  15. package/dist/esm/hooks/useAccountsBalancesData.js.map +1 -1
  16. package/dist/esm/hooks/useAddressValidation.d.ts +3 -2
  17. package/dist/esm/hooks/useAddressValidation.d.ts.map +1 -1
  18. package/dist/esm/hooks/useAddressValidation.js +48 -21
  19. package/dist/esm/hooks/useAddressValidation.js.map +1 -1
  20. package/dist/esm/hooks/useGasRefuel.js +4 -1
  21. package/dist/esm/hooks/useGasRefuel.js.map +1 -1
  22. package/dist/esm/hooks/useRoutes.d.ts.map +1 -1
  23. package/dist/esm/hooks/useRoutes.js +11 -6
  24. package/dist/esm/hooks/useRoutes.js.map +1 -1
  25. package/dist/esm/hooks/useToAddressAutoPopulate.js +6 -8
  26. package/dist/esm/hooks/useToAddressAutoPopulate.js.map +1 -1
  27. package/dist/esm/hooks/useToAddressRequirements.d.ts +3 -2
  28. package/dist/esm/hooks/useToAddressRequirements.d.ts.map +1 -1
  29. package/dist/esm/hooks/useToAddressRequirements.js +8 -7
  30. package/dist/esm/hooks/useToAddressRequirements.js.map +1 -1
  31. package/dist/esm/hooks/useToAddressReset.js +6 -3
  32. package/dist/esm/hooks/useToAddressReset.js.map +1 -1
  33. package/dist/esm/hooks/useTokenBalance.js +2 -1
  34. package/dist/esm/hooks/useTokenBalance.js.map +1 -1
  35. package/dist/esm/hooks/useTokens.js +1 -1
  36. package/dist/esm/i18n/bn.json +2 -1
  37. package/dist/esm/i18n/de.json +2 -1
  38. package/dist/esm/i18n/en.json +2 -1
  39. package/dist/esm/i18n/es.json +2 -1
  40. package/dist/esm/i18n/fr.json +2 -1
  41. package/dist/esm/i18n/hi.json +2 -1
  42. package/dist/esm/i18n/id.json +2 -1
  43. package/dist/esm/i18n/it.json +2 -1
  44. package/dist/esm/i18n/ja.json +2 -1
  45. package/dist/esm/i18n/ko.json +2 -1
  46. package/dist/esm/i18n/pl.json +2 -1
  47. package/dist/esm/i18n/pt.json +2 -1
  48. package/dist/esm/i18n/th.json +2 -1
  49. package/dist/esm/i18n/tr.json +2 -1
  50. package/dist/esm/i18n/uk.json +2 -1
  51. package/dist/esm/i18n/vi.json +2 -1
  52. package/dist/esm/i18n/zh.json +2 -1
  53. package/dist/esm/pages/SendToWallet/BookmarkAddressSheet.js +7 -4
  54. package/dist/esm/pages/SendToWallet/BookmarkAddressSheet.js.map +1 -1
  55. package/dist/esm/pages/SendToWallet/BookmarksPage.js +35 -31
  56. package/dist/esm/pages/SendToWallet/BookmarksPage.js.map +1 -1
  57. package/dist/esm/pages/SendToWallet/ConnectedWalletsPage.js +4 -3
  58. package/dist/esm/pages/SendToWallet/ConnectedWalletsPage.js.map +1 -1
  59. package/dist/esm/pages/SendToWallet/RecentWalletsPage.js +43 -39
  60. package/dist/esm/pages/SendToWallet/RecentWalletsPage.js.map +1 -1
  61. package/dist/esm/pages/SendToWallet/SendToConfiguredWalletPage.js +32 -26
  62. package/dist/esm/pages/SendToWallet/SendToConfiguredWalletPage.js.map +1 -1
  63. package/dist/esm/pages/SendToWallet/SendToWalletPage.js +5 -10
  64. package/dist/esm/pages/SendToWallet/SendToWalletPage.js.map +1 -1
  65. package/dist/esm/providers/WalletProvider/WalletProvider.d.ts.map +1 -1
  66. package/dist/esm/providers/WalletProvider/WalletProvider.js +14 -2
  67. package/dist/esm/providers/WalletProvider/WalletProvider.js.map +1 -1
  68. package/dist/esm/providers/WidgetProvider/WidgetProvider.d.ts.map +1 -1
  69. package/dist/esm/providers/WidgetProvider/WidgetProvider.js +10 -14
  70. package/dist/esm/providers/WidgetProvider/WidgetProvider.js.map +1 -1
  71. package/dist/esm/stores/bookmarks/types.d.ts +3 -0
  72. package/dist/esm/stores/bookmarks/types.d.ts.map +1 -1
  73. package/dist/esm/stores/chains/ChainOrderStore.d.ts.map +1 -1
  74. package/dist/esm/stores/chains/ChainOrderStore.js +4 -1
  75. package/dist/esm/stores/chains/ChainOrderStore.js.map +1 -1
  76. package/dist/esm/stores/form/FormStore.js +3 -2
  77. package/dist/esm/stores/form/FormStore.js.map +1 -1
  78. package/dist/esm/stores/form/URLSearchParamsBuilder.js +7 -3
  79. package/dist/esm/stores/form/URLSearchParamsBuilder.js.map +1 -1
  80. package/dist/esm/stores/form/getDefaultValuesFromQueryString.js +4 -1
  81. package/dist/esm/stores/form/getDefaultValuesFromQueryString.js.map +1 -1
  82. package/dist/esm/utils/chainType.js +32 -1
  83. package/dist/esm/utils/chainType.js.map +1 -1
  84. package/dist/esm/utils/customReceiver.js +6 -1
  85. package/dist/esm/utils/customReceiver.js.map +1 -1
  86. package/package.json +7 -7
  87. package/src/components/AmountInputCard/AmountInputCardPair.tsx +6 -2
  88. package/src/components/SelectChainAndToken.tsx +5 -1
  89. package/src/components/SendToWallet/SendToWalletButton.tsx +13 -8
  90. package/src/components/TokenList/useTokenSelect.ts +15 -0
  91. package/src/config/constants.ts +3 -0
  92. package/src/config/version.ts +1 -1
  93. package/src/hooks/useAccountsBalancesData.ts +6 -2
  94. package/src/hooks/useAddressValidation.ts +87 -32
  95. package/src/hooks/useGasRefuel.ts +11 -1
  96. package/src/hooks/useRoutes.ts +23 -18
  97. package/src/hooks/useToAddressAutoPopulate.ts +13 -17
  98. package/src/hooks/useToAddressRequirements.ts +19 -4
  99. package/src/hooks/useToAddressReset.ts +13 -4
  100. package/src/hooks/useTokenBalance.ts +6 -1
  101. package/src/pages/SendToWallet/BookmarkAddressSheet.tsx +5 -1
  102. package/src/pages/SendToWallet/BookmarksPage.tsx +42 -52
  103. package/src/pages/SendToWallet/ConnectedWalletsPage.tsx +4 -14
  104. package/src/pages/SendToWallet/RecentWalletsPage.tsx +59 -66
  105. package/src/pages/SendToWallet/SendToConfiguredWalletPage.tsx +45 -50
  106. package/src/pages/SendToWallet/SendToWalletPage.tsx +4 -16
  107. package/src/providers/WalletProvider/WalletProvider.tsx +28 -3
  108. package/src/providers/WidgetProvider/WidgetProvider.tsx +20 -19
  109. package/src/stores/bookmarks/types.ts +3 -0
  110. package/src/stores/chains/ChainOrderStore.tsx +4 -1
  111. package/src/stores/form/FormStore.tsx +2 -0
  112. package/src/stores/form/URLSearchParamsBuilder.tsx +4 -2
  113. package/src/stores/form/getDefaultValuesFromQueryString.ts +19 -0
  114. package/src/utils/chainType.ts +73 -1
  115. package/src/utils/customReceiver.ts +36 -0
@@ -45,6 +45,7 @@ export const FormStoreProvider: React.FC<FormStoreProviderProps> = ({
45
45
  toAddress,
46
46
  formUpdateKey,
47
47
  buildUrl,
48
+ chains,
48
49
  } = widgetConfig
49
50
 
50
51
  const storeRef = useRef<FormStoreStore>(null)
@@ -113,6 +114,7 @@ export const FormStoreProvider: React.FC<FormStoreProviderProps> = ({
113
114
  const queryDefaults = getDefaultValuesFromQueryString({
114
115
  buildUrl,
115
116
  includeToAddress: false,
117
+ chains,
116
118
  })
117
119
  storeRef.current = createFormStore(
118
120
  initialiseDefaultValues({
@@ -34,16 +34,17 @@ export const URLSearchParamsBuilder = () => {
34
34
  const values = useFieldValues(...formValueKeys, ...limitFormValueKeys)
35
35
  const { setSelectedBookmark, addRecentWallet } = useBookmarkActions()
36
36
  const { validateAddress } = useAddressValidation()
37
- const { buildUrl, mode } = useWidgetConfig()
37
+ const { buildUrl, mode, chains } = useWidgetConfig()
38
38
  // Using these methods as trying to use the touchedFields and values above
39
39
  // often has a lag that can effect the widgets initialisation sequence
40
40
  // and accidentally cause values to be wiped from the query string
41
41
  const { getFieldValues, isTouched, setUserAndDefaultValues } =
42
42
  useFieldActions()
43
43
 
44
+ // biome-ignore lint/correctness/useExhaustiveDependencies: the URL is read once, not again on each config render
44
45
  useEffect(() => {
45
46
  // get the initial values from the querystring
46
- const formValues = getDefaultValuesFromQueryString({ buildUrl })
47
+ const formValues = getDefaultValuesFromQueryString({ buildUrl, chains })
47
48
  const { toAddress, ...initialFormValues } = formValues
48
49
 
49
50
  /**
@@ -62,6 +63,7 @@ export const URLSearchParamsBuilder = () => {
62
63
  const bookmark = {
63
64
  address: validationResult.address,
64
65
  chainType: validationResult.chainType,
66
+ chainId: validationResult.chainId,
65
67
  }
66
68
  setUserAndDefaultValues({ toAddress })
67
69
  setSelectedBookmark(bookmark)
@@ -1,14 +1,21 @@
1
+ import type { WidgetChains } from '../../types/widget.js'
2
+ import {
3
+ isDestinationOnlyChain,
4
+ isUnservedDestinationChain,
5
+ } from '../../utils/chainType.js'
1
6
  import { formatInputAmount } from '../../utils/format.js'
2
7
  import type { DefaultValues } from './types.js'
3
8
 
4
9
  interface GetDefaultValuesFromQueryStringOptions {
5
10
  includeToAddress?: boolean
6
11
  buildUrl?: boolean
12
+ chains?: WidgetChains
7
13
  }
8
14
 
9
15
  export const getDefaultValuesFromQueryString = ({
10
16
  buildUrl = false,
11
17
  includeToAddress = true,
18
+ chains,
12
19
  }: GetDefaultValuesFromQueryStringOptions): Partial<DefaultValues> => {
13
20
  if (typeof window === 'undefined') {
14
21
  return {}
@@ -22,6 +29,18 @@ export const getDefaultValuesFromQueryString = ({
22
29
  new URLSearchParams(window.location.search)
23
30
  )
24
31
 
32
+ // The loop below then drops their tokens too.
33
+ if (isDestinationOnlyChain(Number.parseInt(searchParams.fromChain, 10))) {
34
+ delete searchParams.fromChain
35
+ }
36
+ if (
37
+ isUnservedDestinationChain(
38
+ Number.parseInt(searchParams.toChain, 10),
39
+ chains
40
+ )
41
+ ) {
42
+ delete searchParams.toChain
43
+ }
25
44
  // Prevent using fromToken/toToken params if chain is not selected.
26
45
  ;(['from', 'to'] as const).forEach((key) => {
27
46
  if (searchParams[`${key}Token`] && !searchParams[`${key}Chain`]) {
@@ -1,4 +1,6 @@
1
- import { ChainId, ChainType } from '@lifi/sdk'
1
+ import { ChainId, ChainType, type SDKProvider } from '@lifi/sdk'
2
+ import type { WidgetChains, WidgetConfig } from '../types/widget.js'
3
+ import { isItemAllowed } from './item.js'
2
4
 
3
5
  export const defaultChainIdsByType: Record<ChainType, ChainId> = {
4
6
  [ChainType.EVM]: ChainId.ETH,
@@ -8,3 +10,73 @@ export const defaultChainIdsByType: Record<ChainType, ChainId> = {
8
10
  [ChainType.TVM]: ChainId.TRN,
9
11
  [ChainType.STL]: ChainId.XLM,
10
12
  }
13
+
14
+ // No wallet can sign on these chains yet, so a route may only deliver to them.
15
+ const destinationOnlyChainIds: ReadonlySet<number> = new Set<number>([
16
+ ChainId.ZEC,
17
+ ])
18
+
19
+ export const isDestinationOnlyChain = (chainId?: number): boolean =>
20
+ chainId !== undefined && destinationOnlyChainIds.has(chainId)
21
+
22
+ export const withDestinationOnlyChains = (
23
+ chains: WidgetChains | undefined
24
+ ): WidgetChains => {
25
+ const allow = chains?.from?.allow?.filter(
26
+ (chainId) => !isDestinationOnlyChain(chainId)
27
+ )
28
+ return {
29
+ ...chains,
30
+ from: {
31
+ ...chains?.from,
32
+ ...(allow && { allow }),
33
+ deny: [...(chains?.from?.deny ?? []), ...destinationOnlyChainIds],
34
+ },
35
+ }
36
+ }
37
+
38
+ // A destination-only chain takes a receiver only through a provider that lists it.
39
+ export const withServedDestinationChains = (
40
+ chains: WidgetChains | undefined,
41
+ providers: readonly Pick<SDKProvider, 'chainIds'>[]
42
+ ): WidgetChains | undefined => {
43
+ const unserved = [...destinationOnlyChainIds].filter(
44
+ (chainId) =>
45
+ !providers.some((provider) =>
46
+ provider.chainIds?.some((id) => id === chainId)
47
+ )
48
+ )
49
+ if (!unserved.length) {
50
+ return chains
51
+ }
52
+ const allow = chains?.allow?.filter((chainId) => !unserved.includes(chainId))
53
+ return {
54
+ ...chains,
55
+ ...(allow && { allow }),
56
+ deny: [...(chains?.deny ?? []), ...unserved],
57
+ }
58
+ }
59
+
60
+ // The form keeps a preset chain that the chain list does not offer.
61
+ export const isUnservedDestinationChain = (
62
+ chainId: number | undefined,
63
+ chains: WidgetChains | undefined
64
+ ): boolean => isDestinationOnlyChain(chainId) && !isItemAllowed(chainId, chains)
65
+
66
+ // Omitted, not undefined: an own undefined key resets the form field.
67
+ export const withoutUnservedToChain = <
68
+ T extends Pick<WidgetConfig, 'chains' | 'toChain' | 'toToken'>,
69
+ >(
70
+ config: T
71
+ ): T => {
72
+ if (!isUnservedDestinationChain(config.toChain, config.chains)) {
73
+ return config
74
+ }
75
+ const { toChain: _toChain, toToken: _toToken, ...rest } = config
76
+ return rest as T
77
+ }
78
+
79
+ export const isDestinationAllowedAsSource = (
80
+ toChainId: number | undefined,
81
+ chains: WidgetChains | undefined
82
+ ): boolean => !toChainId || isItemAllowed(toChainId, chains?.from)
@@ -1,4 +1,5 @@
1
1
  import { ChainType } from '@lifi/sdk'
2
+ import type { ChainRef, IsAddressForChain } from '@lifi/widget-provider'
2
3
 
3
4
  // Stellar routes always settle to the account that signs them.
4
5
  export const isCustomReceiverUnsupported = (
@@ -29,3 +30,38 @@ export const isCustomReceiverBlocked = ({
29
30
  }
30
31
  return toAddress.toLowerCase() !== signerAddress?.toLowerCase()
31
32
  }
33
+
34
+ // Without a receiver the API delivers to the signer when both chains share a
35
+ // chain type, and a Bitcoin signer cannot receive on ZEC.
36
+ export const isSignerInvalidReceiver = ({
37
+ signerAddress,
38
+ toAddress,
39
+ fromChain,
40
+ toChain,
41
+ isAddressForChain,
42
+ }: {
43
+ signerAddress: string | undefined
44
+ toAddress: string | undefined
45
+ fromChain: ChainRef | undefined
46
+ toChain: ChainRef | undefined
47
+ isAddressForChain: IsAddressForChain
48
+ }): boolean =>
49
+ Boolean(
50
+ signerAddress &&
51
+ !toAddress &&
52
+ fromChain &&
53
+ toChain &&
54
+ fromChain.chainType === toChain.chainType &&
55
+ !isAddressForChain(signerAddress, toChain)
56
+ )
57
+
58
+ export const canQuoteWithToAddress = (
59
+ toAddress: string | undefined,
60
+ toChain: ChainRef | undefined,
61
+ isAddressForChain: IsAddressForChain
62
+ ): boolean => {
63
+ if (!toAddress) {
64
+ return true
65
+ }
66
+ return toChain ? isAddressForChain(toAddress, toChain) : false
67
+ }