astra-modal-test 1.0.5 → 1.0.7

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 (154) hide show
  1. package/.github/workflows/code-quality.yml +43 -0
  2. package/.github/workflows/publish.yml +64 -0
  3. package/README.md +204 -0
  4. package/dist/SwapController-CsNqEWIM.js +447 -0
  5. package/dist/add-Dm_oSEbv.js +19 -0
  6. package/dist/all-wallets-DfLlzQJ3.js +10 -0
  7. package/dist/app-store-BMdJkp6P.js +21 -0
  8. package/dist/apple-BetlVMKw.js +22 -0
  9. package/dist/arrow-bottom-BddPptxy.js +12 -0
  10. package/dist/arrow-bottom-circle-CvdJJniv.js +15 -0
  11. package/dist/arrow-left-Bbq-z1nQ.js +12 -0
  12. package/dist/arrow-right-Bu4_SVLC.js +12 -0
  13. package/dist/arrow-top-CynlXB-M.js +12 -0
  14. package/dist/astra-sdk.es.js +4 -4
  15. package/dist/bank-B9QscNnS.js +18 -0
  16. package/dist/bin-v-tshf8_.js +8 -0
  17. package/dist/bitcoin-C01gkYyN.js +17 -0
  18. package/dist/browser-9uNaCY31.js +18 -0
  19. package/dist/card-hKR9lRzU.js +18 -0
  20. package/dist/{ccip-oZOrZYR9.js → ccip-CUCziAPw.js} +52 -49
  21. package/dist/checkmark-D1M40aM5.js +15 -0
  22. package/dist/checkmark-bold-1_UGl24m.js +12 -0
  23. package/dist/chevron-bottom-BleoRNRL.js +12 -0
  24. package/dist/chevron-left-C9JMU-3B.js +12 -0
  25. package/dist/chevron-right-Daue4WBx.js +12 -0
  26. package/dist/chevron-top-yuXekbUC.js +12 -0
  27. package/dist/chrome-store-BsUFRR7F.js +65 -0
  28. package/dist/circle-BCxdhM5X.js +8 -0
  29. package/dist/clock-7a-DWZQa.js +12 -0
  30. package/dist/close-DMQYuJ0z.js +12 -0
  31. package/dist/clsx.m-DCYaa4RU.js +20 -0
  32. package/dist/coinPlaceholder-DjQ-HnSH.js +12 -0
  33. package/dist/compass-AMecXNVm.js +12 -0
  34. package/dist/copy-CgIPGYbU.js +19 -0
  35. package/dist/cursor-DpYbTNcO.js +7 -0
  36. package/dist/cursor-transparent-C8iPJkn8.js +16 -0
  37. package/dist/desktop-BWVQVGng.js +13 -0
  38. package/dist/disconnect-C6IQRrtF.js +12 -0
  39. package/dist/discord-gH3i-QsC.js +21 -0
  40. package/dist/email-BxYwJC5T.js +10 -0
  41. package/dist/embedded-wallet-DCcVNgkc.js +464 -0
  42. package/dist/ethereum-CGWsmZaC.js +14 -0
  43. package/dist/etherscan-D4EFsAJ4.js +10 -0
  44. package/dist/exclamation-triangle-DNwQ1Xq6.js +8 -0
  45. package/dist/extension-DxQPsN2z.js +12 -0
  46. package/dist/external-link-BeEzrdgR.js +12 -0
  47. package/dist/facebook-B2Ix8_d3.js +30 -0
  48. package/dist/farcaster-BB83p8dC.js +16 -0
  49. package/dist/filters-PzdIomB1.js +12 -0
  50. package/dist/github-gcp1rxX0.js +22 -0
  51. package/dist/google-Qv9lTZcz.js +22 -0
  52. package/dist/help-circle-BLXTMjzX.js +16 -0
  53. package/dist/id-BHg2CRhj.js +16 -0
  54. package/dist/image-DWi1V30x.js +8 -0
  55. package/dist/index-4lbsZ6RQ.js +33 -0
  56. package/dist/index-64U3D9_n.js +21034 -0
  57. package/dist/index-CRU49I0g.js +15911 -0
  58. package/dist/index-ChxXyuZj.js +572 -0
  59. package/dist/index-ClS956E1.js +74511 -0
  60. package/dist/index-Cs4fWw01.js +386 -0
  61. package/dist/index-DfWqgW4h.js +709 -0
  62. package/dist/index-VYNI76FM.js +315 -0
  63. package/dist/index-qfkTLN90.js +10785 -0
  64. package/dist/index-r2ztgVN2.js +1060 -0
  65. package/dist/index-wvh7Yd5J.js +70 -0
  66. package/dist/info-Bl5tmiBJ.js +7 -0
  67. package/dist/info-circle-D2uYywg-.js +16 -0
  68. package/dist/lightbulb-HvXH2AVF.js +7 -0
  69. package/dist/mail-DXOPfnog.js +12 -0
  70. package/dist/mobile-Dd0Gj4vZ.js +13 -0
  71. package/dist/more-DYcnClbB.js +15 -0
  72. package/dist/network-placeholder-CfRZGQfi.js +18 -0
  73. package/dist/nftPlaceholder-DHmFkY55.js +12 -0
  74. package/dist/off-CG2Y30qN.js +8 -0
  75. package/dist/onramp-DoAMTeNm.js +917 -0
  76. package/dist/play-store-B5EYBQto.js +36 -0
  77. package/dist/plus-DPo25_zL.js +17 -0
  78. package/dist/qr-code-BRTO19PL.js +10 -0
  79. package/dist/receive-BvclNdV_.js +179 -0
  80. package/dist/recycle-horizontal-ng_b2eJV.js +13 -0
  81. package/dist/refresh-3kiwRV0w.js +12 -0
  82. package/dist/reown-logo-_9cuIfJD.js +16 -0
  83. package/dist/search-CoJvIxpi.js +12 -0
  84. package/dist/secp256k1-wZyK2iIX.js +1257 -0
  85. package/dist/send-BVKGSR-Z.js +19 -0
  86. package/dist/send-Bkj5YtHe.js +1039 -0
  87. package/dist/socials-DL42HlL5.js +589 -0
  88. package/dist/solana-BjP-_Ioa.js +17 -0
  89. package/dist/swapHorizontal-Do3uBbum.js +12 -0
  90. package/dist/swapHorizontalBold-D7N75BE-.js +12 -0
  91. package/dist/swapHorizontalMedium-BahEVO2x.js +20 -0
  92. package/dist/swapHorizontalRoundedBold-D6Hoi132.js +12 -0
  93. package/dist/swapVertical-P-fnrADh.js +12 -0
  94. package/dist/swaps-DZgbVJZ2.js +1713 -0
  95. package/dist/telegram-DmaL2iB8.js +20 -0
  96. package/dist/three-dots-oXyAsUA6.js +9 -0
  97. package/dist/transactions-BjB5yZN7.js +35 -0
  98. package/dist/twitch-0UGrMr2K.js +22 -0
  99. package/dist/twitterIcon-B97KAbaJ.js +10 -0
  100. package/dist/ui-r6nnVhBA.js +812 -0
  101. package/dist/user-BG1yp56Q.js +7 -0
  102. package/dist/verify-CgzkMSuV.js +12 -0
  103. package/dist/verify-filled-D-TRpWtN.js +12 -0
  104. package/dist/w3m-modal-Drsibcs0.js +1041 -0
  105. package/dist/wallet-CAHsZdfj.js +12 -0
  106. package/dist/wallet-placeholder-agbbreqw.js +18 -0
  107. package/dist/walletconnect-Yd8iXqY9.js +36 -0
  108. package/dist/warning-circle-C0hqC6h_.js +16 -0
  109. package/dist/x-mark-D9_ai2sI.js +7 -0
  110. package/dist/x-z6v1Jegg.js +16 -0
  111. package/eslint.config.mjs +1 -1
  112. package/package.json +14 -13
  113. package/src/apis/lspApi.js +47 -24
  114. package/src/assets/ln.png +0 -0
  115. package/src/assets/network/Arbitrum.png +0 -0
  116. package/src/assets/network/Polygon.png +0 -0
  117. package/src/assets/network/Solana.png +0 -0
  118. package/src/assets/network/botanix.png +0 -0
  119. package/src/assets/network/bsc.svg +13 -0
  120. package/src/assets/pay.png +0 -0
  121. package/src/assets/tokens/usdc.png +0 -0
  122. package/src/assets/tokens/usdt.png +0 -0
  123. package/src/comps/AstraImage.jsx +17 -3
  124. package/src/comps/AstraModal.jsx +22 -16
  125. package/src/comps/AstraModalLogo.jsx +9 -9
  126. package/src/comps/AstraNetwork.jsx +174 -45
  127. package/src/comps/CheckErc20Button.jsx +1 -1
  128. package/src/comps/CommonStyle.jsx +4 -0
  129. package/src/comps/ConnectButton.jsx +16 -33
  130. package/src/comps/ResultModal.jsx +45 -39
  131. package/src/comps/ToLightning.jsx +207 -80
  132. package/src/comps/ToToken.jsx +278 -217
  133. package/src/constants/index.js +20 -4
  134. package/src/hooks/useContract.js +164 -125
  135. package/src/hooks/useGetMinAndMax.js +48 -0
  136. package/src/hooks/useLspApi.js +56 -39
  137. package/src/hooks/useParseInvoice.js +82 -72
  138. package/src/index.jsx +43 -10
  139. package/src/main.jsx +36 -5
  140. package/src/utils/index.js +136 -24
  141. package/vite.config.js +7 -1
  142. package/dist/astra-sdk.umd.js +0 -12578
  143. package/dist/hooks.module-BBZfodGH.js +0 -506
  144. package/dist/index-BKKAfRrw.js +0 -87520
  145. package/dist/index-C2MAIRET.js +0 -2929
  146. package/dist/index-CsRxokSv.js +0 -445
  147. package/dist/index-DQ6iiHxJ.js +0 -13720
  148. package/dist/index-SJhlkSif.js +0 -133
  149. package/dist/w3m-modal-CYJpmgnf.js +0 -267
  150. package/src/constants/contracts/abi/bridge.js +0 -1281
  151. package/src/constants/contracts/abi/index.js +0 -3
  152. package/src/constants/contracts/abi/usdt.js +0 -130
  153. package/src/constants/contracts/abi/watcher.js +0 -437
  154. package/src/constants/contracts/index.js +0 -37
@@ -1,140 +1,179 @@
1
-
2
1
  import {
3
- useChainId,
4
- useReadContract,
5
- useWriteContract,
6
- useConfig,
7
- usePublicClient
2
+ useChainId,
3
+ useReadContract,
4
+ useWriteContract,
5
+ useConfig,
6
+ usePublicClient,
8
7
  } from "wagmi";
9
8
  import { waitForTransactionReceipt, readContract } from "@wagmi/core";
10
9
  import { useMemo, useCallback } from "react";
11
- import contractConfig from "../constants/contracts";
10
+ import { useGetContractConfig } from "./useLspApi";
12
11
  import useStore from "../store";
13
- import { formatAssetByDecimal } from "../utils";
12
+ import { formatAssetByDecimal, toBytes32 } from "../utils";
14
13
  export function useBridgeContract(account) {
14
+ const chainId = useChainId();
15
+ const publicClient = usePublicClient();
16
+ const { contractConfig } = useGetContractConfig();
15
17
 
16
- const chainId = useChainId();
17
- const publicClient = usePublicClient();
18
- const config = contractConfig[chainId];
19
-
20
- const { currentAssetPair } = useStore();
21
-
22
-
23
- const bridgeContractConfig = useMemo(() => {
24
- return {
25
- address: config?.Bridge?.address,
26
- abi: config?.Bridge?.abi,
27
- enabled: !!config && !!account?.address
28
- };
29
- }, [account?.address, config]);
30
-
31
-
32
- const tokenContractConfig = useMemo(() => {
33
- return {
34
- address: currentAssetPair?.token?.address,
35
- abi: config?.USDT?.abi,
36
- enabled: !!currentAssetPair
37
- }
38
- }, [config?.USDT?.abi, currentAssetPair])
39
-
18
+ const config = useMemo(() => {
19
+ if (!contractConfig) return null;
20
+ return contractConfig[chainId];
21
+ }, [chainId, contractConfig]);
40
22
 
23
+ const { currentAssetPair } = useStore();
41
24
 
42
- const { data: allowanceRet, isLoading: allowanceLoading, refetch: onReloadAllowance } = useReadContract({
43
- ...tokenContractConfig,
44
- functionName: "allowance",
45
- args: [account?.address, config?.Bridge?.address],
46
- enabled: !!account?.address,
47
- scopeKey: "allowance"
48
- });
49
-
50
- const formatAllowance = useMemo(() => {
51
- if (!allowanceRet) return 0;
52
- return formatAssetByDecimal(allowanceRet?.toString(), currentAssetPair?.token?.decimal);
53
- }, [allowanceRet, currentAssetPair?.token?.decimal])
54
-
55
- const wagmiConfig = useConfig();
56
- const { writeContractAsync } = useWriteContract();
57
-
58
- const onApprove = useCallback(
59
- async (approveAmount, wait = true) => {
60
- //console.log("🚀 ~ onApprove ~ approveAmount:", approveAmount, config?.address)
61
- const tx = await writeContractAsync({
62
- ...tokenContractConfig,
63
- functionName: "approve",
64
- args: [config?.Bridge?.address, BigInt(approveAmount)],
65
- });
25
+ const bridgeContractConfig = useMemo(() => {
26
+ return {
27
+ address: config?.Bridge?.address,
28
+ abi: config?.Bridge?.abi,
29
+ query: {
30
+ enabled: !!config && !!account?.address,
31
+ },
32
+ };
33
+ }, [account?.address, config]);
66
34
 
67
- if (wait) {
68
- await waitForTransactionReceipt(
69
- wagmiConfig,
70
- {
71
- hash: tx
72
- },
73
- 1
74
- );
75
- if (onReloadAllowance) {
76
- await onReloadAllowance();
77
- }
78
- }
79
- return tx;
80
- },
81
- [config?.Bridge?.address, onReloadAllowance, tokenContractConfig, wagmiConfig, writeContractAsync]
35
+ const tokenContractConfig = useMemo(() => {
36
+ return {
37
+ address: currentAssetPair?.token?.address,
38
+ abi: config?.USDT?.abi,
39
+ query: {
40
+ enabled: !!currentAssetPair && !!account?.address,
41
+ },
42
+ };
43
+ }, [account?.address, config?.USDT?.abi, currentAssetPair]);
44
+
45
+ const {
46
+ data: allowanceRet,
47
+ isLoading: allowanceLoading,
48
+ refetch: onReloadAllowance,
49
+ } = useReadContract({
50
+ ...tokenContractConfig,
51
+ functionName: "allowance",
52
+ args: [account?.address, config?.Bridge?.address],
53
+ scopeKey: "allowance",
54
+ });
55
+
56
+ const formatAllowance = useMemo(() => {
57
+ if (!allowanceRet) return 0;
58
+ return formatAssetByDecimal(
59
+ allowanceRet?.toString(),
60
+ currentAssetPair?.token?.decimal
82
61
  );
62
+ }, [allowanceRet, currentAssetPair?.token?.decimal]);
83
63
 
64
+ const wagmiConfig = useConfig();
65
+ const { writeContractAsync } = useWriteContract();
84
66
 
85
-
86
- const onDeposit = useCallback(
87
- async ({ hashlock, fromAddr, toAddr, tokenAddr, amount, timeLock, fee, toLightning = true, signature = '0x' }, wait = true, reloadAllowance = true) => {
88
-
89
- const contractInfo = await readContract(wagmiConfig, {
90
- ...bridgeContractConfig,
91
- functionName: "getContractByKey",
92
- args: [hashlock],
93
- });
94
- if (contractInfo?.hashlock === hashlock) {
95
- throw new Error('Invoice already exists.');
96
- }
97
-
98
- const depositGas = await publicClient.estimateContractGas({
99
- ...bridgeContractConfig,
100
- functionName: "deposit",
101
- args: [hashlock, fromAddr, toAddr, tokenAddr, BigInt(amount), timeLock, fee, toLightning, signature],
102
- value: fee,
103
- account: account?.address,
104
- })
105
- console.log('depositGas', depositGas)
106
-
107
-
108
- const tx = await writeContractAsync({
109
- ...bridgeContractConfig,
110
- functionName: "deposit",
111
- args: [hashlock, fromAddr, toAddr, tokenAddr, BigInt(amount), timeLock, fee, toLightning, signature],
112
- value: fee
113
- });
114
-
115
- if (wait) {
116
- await waitForTransactionReceipt(
117
- wagmiConfig,
118
- {
119
- hash: tx
120
- },
121
- 1
122
- );
123
- if (onReloadAllowance && reloadAllowance) {
124
- await onReloadAllowance();
125
- }
126
- }
127
- return tx;
67
+ const onApprove = useCallback(
68
+ async (approveAmount, wait = true) => {
69
+ //console.log("🚀 ~ onApprove ~ approveAmount:", approveAmount, config?.address)
70
+ const tx = await writeContractAsync({
71
+ ...tokenContractConfig,
72
+ functionName: "approve",
73
+ args: [config?.Bridge?.address, BigInt(approveAmount)],
74
+ chainId: chainId,
75
+ });
76
+
77
+ if (wait) {
78
+ await waitForTransactionReceipt(
79
+ wagmiConfig,
80
+ {
81
+ hash: tx,
82
+ },
83
+ 1
84
+ );
85
+ if (onReloadAllowance) {
86
+ await onReloadAllowance();
87
+ }
88
+ }
89
+ return tx;
90
+ },
91
+ [
92
+ chainId,
93
+ config?.Bridge?.address,
94
+ onReloadAllowance,
95
+ tokenContractConfig,
96
+ wagmiConfig,
97
+ writeContractAsync,
98
+ ]
99
+ );
100
+
101
+ const onDeposit = useCallback(
102
+ async (
103
+ {
104
+ hashlock,
105
+ nodePubkey,
106
+ fromAddr,
107
+ toAddr,
108
+ pairId,
109
+ amount,
110
+ timeLock,
111
+ fee,
112
+ toLightning = true,
113
+ signature = "0x",
114
+ },
115
+ ) => {
116
+ const contractInfo = await readContract(wagmiConfig, {
117
+ ...bridgeContractConfig,
118
+ functionName: "getContractByKey",
119
+ args: [hashlock],
120
+ });
121
+ if (contractInfo?.hashlock === hashlock) {
122
+ throw new Error("Invoice already exists.");
123
+ }
124
+ const args = [
125
+ hashlock,
126
+ toBytes32(nodePubkey),
127
+ fromAddr,
128
+ toAddr,
129
+ pairId,
130
+ BigInt(amount),
131
+ timeLock,
132
+ fee,
133
+ toLightning,
134
+ signature,
135
+ ];
136
+ const depositGas = await publicClient.estimateContractGas({
137
+ ...bridgeContractConfig,
138
+ functionName: "deposit",
139
+ args: args,
140
+ value: fee,
141
+ account: account?.address,
142
+ });
143
+
144
+ const gasLimit = BigInt(Math.floor(Number(depositGas) * 1.2));
145
+ const tx = await writeContractAsync({
146
+ ...bridgeContractConfig,
147
+ functionName: "deposit",
148
+ args: args,
149
+ value: fee,
150
+ chainId: chainId,
151
+ gas: gasLimit,
152
+ });
153
+ console.log(`🚀 ~ onDeposit ~ tx: ${tx}, will wait for 1 confirm`);
154
+ await waitForTransactionReceipt(
155
+ wagmiConfig,
156
+ {
157
+ hash: tx,
128
158
  },
129
- [bridgeContractConfig, onReloadAllowance, wagmiConfig, writeContractAsync]
130
- );
131
-
132
- return {
133
- allowanceRet,
134
- formatAllowance,
135
- allowanceLoading,
136
- onReloadAllowance,
137
- onApprove,
138
- onDeposit
139
- };
159
+ 1
160
+ );
161
+
162
+ // if (onReloadAllowance && reloadAllowance) {
163
+ // onReloadAllowance();
164
+ // }
165
+ //}
166
+ return tx;
167
+ },
168
+ [chainId, account?.address, bridgeContractConfig, publicClient, wagmiConfig, writeContractAsync]
169
+ );
170
+
171
+ return {
172
+ allowanceRet,
173
+ formatAllowance,
174
+ allowanceLoading,
175
+ onReloadAllowance,
176
+ onApprove,
177
+ onDeposit,
178
+ };
140
179
  }
@@ -0,0 +1,48 @@
1
+ import { useMemo } from "react";
2
+ import { balanceToFixed } from "../constants";
3
+ import useStore from "../store";
4
+ export const useGetMinAndMax = (payDirection,reserveInfo) => {
5
+ const { currentAssetPair } = useStore();
6
+ // console.log("🚀 ~ useGetMinAndMax ~ currentAssetPair:",reserveInfo )
7
+ const fixedLocalChannelBalance = useMemo(() => {
8
+ if (reserveInfo) {
9
+ return balanceToFixed(reserveInfo.maxAsset, currentAssetPair?.asset?.decimal);
10
+ }
11
+ return 0;
12
+ }, [currentAssetPair?.asset?.decimal, reserveInfo]);
13
+
14
+
15
+ const fixedRemoteChannelBalance = useMemo(()=>{
16
+ if(reserveInfo){
17
+ return balanceToFixed(reserveInfo?.remoteAssetbalance, currentAssetPair?.asset?.decimal);
18
+ }
19
+ return 0;
20
+ },[currentAssetPair?.asset?.decimal, reserveInfo])
21
+
22
+
23
+ const max = useMemo(()=>{
24
+ if(payDirection==="evmToLightning"){
25
+ const value = Math.min(reserveInfo?.max, Number(fixedLocalChannelBalance));
26
+ return value
27
+ }else{
28
+ const lspBalance = balanceToFixed(reserveInfo?.maxToken, currentAssetPair?.token?.decimal);
29
+ const value = Math.min(reserveInfo?.max, Number(fixedRemoteChannelBalance), Number(lspBalance));
30
+ return value
31
+ }
32
+ },[currentAssetPair?.token?.decimal, fixedLocalChannelBalance, fixedRemoteChannelBalance, payDirection, reserveInfo?.max, reserveInfo?.maxToken])
33
+
34
+ const min = useMemo(()=>{
35
+ return Number(max)<Number(reserveInfo?.min)? Number(max): Number(reserveInfo?.min)
36
+ },[reserveInfo?.min,max])
37
+
38
+
39
+
40
+
41
+ const unitName = useMemo(()=>{
42
+ if(payDirection==="evmToLightning"){
43
+ return currentAssetPair?.token?.name;
44
+ }
45
+ return currentAssetPair?.asset?.name;
46
+ },[currentAssetPair, payDirection])
47
+ return { min, max, unitName };
48
+ };
@@ -1,53 +1,70 @@
1
1
  import { useRequest } from "ahooks";
2
2
  import { useEffect, useMemo } from "react";
3
- import { getPairs, getReserveInfo } from "../apis/lspApi";
3
+ import { getPairs, getReserveInfo, getContractConfig } from "../apis/lspApi";
4
4
  import useStore from "../store";
5
5
  export const useGetPairs = () => {
6
- const { setCurrentAssetPair, setAllAssetPairs, targetNetwork, astraApiUrl } = useStore();
7
- const { data, run, loading, refresh, cancel } = useRequest(() => getPairs({ chainId: targetNetwork?.id }), {
8
- refreshDeps: [targetNetwork?.id, astraApiUrl],
9
- ready: !!targetNetwork?.id && !!astraApiUrl
10
- });
6
+ const { setCurrentAssetPair, setAllAssetPairs, targetNetwork, astraApiUrl } =
7
+ useStore();
8
+ const { data, run, loading, refresh, cancel } = useRequest(
9
+ () => getPairs({ chainId: targetNetwork?.id }),
10
+ {
11
+ refreshDeps: [targetNetwork?.id, astraApiUrl],
12
+ ready: !!targetNetwork?.id && !!astraApiUrl,
13
+ }
14
+ );
11
15
 
12
- useEffect(() => {
13
- if (data?.data?.pairs) {
14
- setAllAssetPairs(data?.data?.pairs);
15
- //setCurrentAssetPair(data?.data?.pairs[0]);
16
- }
17
- }, [data?.data?.pairs, setAllAssetPairs, setCurrentAssetPair])
16
+ useEffect(() => {
17
+ if (data?.data?.pairs) {
18
+ setAllAssetPairs(data?.data?.pairs);
19
+ //setCurrentAssetPair(data?.data?.pairs[0]);
20
+ }
21
+ }, [data?.data?.pairs, setAllAssetPairs, setCurrentAssetPair]);
18
22
 
19
- return {
20
- summary: data?.data || null,
21
- loading,
22
- run,
23
- refresh,
24
- cancel
25
- };
23
+ return {
24
+ summary: data?.data || null,
25
+ loading,
26
+ run,
27
+ refresh,
28
+ cancel,
29
+ };
26
30
  };
27
31
 
28
32
  export const useGetReserveInfo = (targetNetwork, currentAssetPair) => {
29
-
30
- const assetId = useMemo(() => {
33
+ /* const assetId = useMemo(() => {
31
34
  return currentAssetPair?.asset?.assetId
32
- }, [currentAssetPair?.asset?.assetId])
33
- const chainId = useMemo(() => {
34
- return targetNetwork?.id
35
- }, [targetNetwork?.id])
35
+ }, [currentAssetPair?.asset?.assetId]) */
36
+ const chainId = useMemo(() => {
37
+ return targetNetwork?.id;
38
+ }, [targetNetwork?.id]);
36
39
 
37
- const pairName = useMemo(() => {
38
- return currentAssetPair?.pairName
39
- }, [currentAssetPair])
40
+ const pairId = useMemo(() => {
41
+ return currentAssetPair?.pairId;
42
+ }, [currentAssetPair]);
40
43
 
44
+ const { data, loading, refresh } = useRequest(
45
+ () => getReserveInfo({ chainId: chainId, pairId: pairId }),
46
+ {
47
+ refreshDeps: [chainId, pairId],
48
+ ready: !!chainId && !!pairId,
49
+ debounceWait: 300,
50
+ }
51
+ );
41
52
 
42
- const { data, loading, refresh } = useRequest(() => getReserveInfo({ chainId: chainId, assetId: assetId, pairName: pairName }), {
43
- refreshDeps: [assetId, chainId, pairName],
44
- ready: !!chainId && !!assetId && !!pairName,
45
- debounceWait: 300,
46
- });
53
+ return {
54
+ reserveInfo: data?.data,
55
+ loading,
56
+ refresh,
57
+ };
58
+ };
59
+ export const useGetContractConfig = () => {
60
+ const { data, loading, refresh } = useRequest(() => getContractConfig(), {
61
+ refreshDeps: [],
62
+ ready: true,
63
+ });
47
64
 
48
- return {
49
- reserveInfo: data?.data,
50
- loading,
51
- refresh
52
- }
53
- }
65
+ return {
66
+ contractConfig: data?.data,
67
+ loading,
68
+ refresh,
69
+ };
70
+ };
@@ -1,85 +1,95 @@
1
1
  import { useCallback, useMemo, useState } from "react";
2
- import "../lib/bolt11.min.js"
2
+ import "../lib/bolt11.min.js";
3
3
  import useStore from "../store";
4
- import { msatsToTprAsset } from "../utils";
4
+ import { invoiceToAssetInfo } from "../utils";
5
5
  // import { useEffect } from "react";
6
6
  import Decimal from "decimal.js";
7
7
  import { useDebounceEffect } from "ahooks";
8
8
  // window.decode = decode;
9
9
  function getPaymentHash(requestObject) {
10
- // go through the tags and find the 'payment_hash' tagName and return the 'data'
11
- const paymentHash = requestObject.tags.find(tag => tag.tagName === "payment_hash");
12
- if (!paymentHash) {
13
- return undefined;
14
- }
15
- return "0x" + paymentHash.data.toString();
10
+ // go through the tags and find the 'payment_hash' tagName and return the 'data'
11
+ const paymentHash = requestObject.tags.find(
12
+ (tag) => tag.tagName === "payment_hash"
13
+ );
14
+ if (!paymentHash) {
15
+ return undefined;
16
+ }
17
+ return "0x" + paymentHash.data.toString();
16
18
  }
17
19
  export default function useParseInvoice(invoice) {
18
- const [assetAmt, setAssetAmt] = useState(0);
19
- const [hashlock, setHashlock] = useState("");
20
- const { currentAssetPair } = useStore();
21
- const [parseInvoiceLoading, setParseInvoiceLoading] = useState(false);
22
- const formatAssetAmt = useMemo(() => {
23
- if (assetAmt && currentAssetPair) {
24
- const decimal = currentAssetPair.asset.decimal;
25
- return new Decimal(assetAmt).div(10 ** decimal).toFixed(decimal);
26
- }
27
- return 0;
28
- }, [assetAmt, currentAssetPair])
29
-
30
- const tokenAmt = useMemo(() => {
31
- if (currentAssetPair) {
32
- const decimal = currentAssetPair.token.decimal;
33
- return new Decimal(formatAssetAmt).mul(10 ** decimal).toFixed(0);
34
- }
35
- return 0;
36
- }, [currentAssetPair, formatAssetAmt])
37
-
38
- const formatTokenAmt = useMemo(() => {
39
- if (currentAssetPair) {
40
- const decimal = currentAssetPair.token.decimal;
41
- return new Decimal(tokenAmt).div(10 ** decimal).toFixed(decimal);
42
- }
43
- return 0;
44
-
45
- }, [currentAssetPair, tokenAmt])
46
-
47
-
48
- const parseInvoice = useCallback(async () => {
49
- try {
50
- setParseInvoiceLoading(true)
51
- const decoded = window.lightningPayReq.decode(invoice);
52
- const numMsat = decoded.millisatoshis;
53
- const amt = await msatsToTprAsset(numMsat, currentAssetPair.asset.assetId);
54
- setAssetAmt(amt);
55
- const hashlock = getPaymentHash(decoded);
56
- setHashlock(hashlock);
57
-
58
- } catch (e) {
59
- console.log(e.message)
60
- setAssetAmt(0);
61
- } finally {
62
- setParseInvoiceLoading(false)
63
- }
64
-
65
-
66
- }, [currentAssetPair, invoice])
67
- useDebounceEffect(() => {
68
- if (invoice && currentAssetPair) {
69
- parseInvoice();
70
- }
20
+ const [assetAmt, setAssetAmt] = useState(0);
21
+ const [hashlock, setHashlock] = useState("");
22
+ const { currentAssetPair } = useStore();
23
+ const [isExpired, setIsExpired] = useState(false);
24
+ const [parseInvoiceLoading, setParseInvoiceLoading] = useState(false);
25
+ const formatAssetAmt = useMemo(() => {
26
+ if (assetAmt && currentAssetPair) {
27
+ const decimal = currentAssetPair.asset.decimal;
28
+ return new Decimal(assetAmt).div(10 ** decimal).toFixed(decimal);
29
+ }
30
+ return 0;
31
+ }, [assetAmt, currentAssetPair]);
71
32
 
72
- }, [currentAssetPair, invoice, parseInvoice], {
73
- wait: 500
74
- })
33
+ const tokenAmt = useMemo(() => {
34
+ if (currentAssetPair) {
35
+ const decimal = currentAssetPair.token.decimal;
36
+ return new Decimal(formatAssetAmt).mul(10 ** decimal).toFixed(0);
37
+ }
38
+ return 0;
39
+ }, [currentAssetPair, formatAssetAmt]);
75
40
 
41
+ const formatTokenAmt = useMemo(() => {
42
+ if (currentAssetPair) {
43
+ const decimal = currentAssetPair.token.decimal;
44
+ const displayDecimal = currentAssetPair.asset.decimal;
45
+ return new Decimal(tokenAmt)
46
+ .div(10 ** decimal)
47
+ .toFixed(displayDecimal, Decimal.ROUND_DOWN);
48
+ }
49
+ return 0;
50
+ }, [currentAssetPair, tokenAmt]);
76
51
 
77
- return {
78
- assetAmt,
79
- formatAssetAmt,
80
- tokenAmt,
81
- formatTokenAmt,
82
- hashlock,
83
- parseInvoiceLoading
52
+ const parseInvoice = useCallback(async () => {
53
+ try {
54
+ setParseInvoiceLoading(true);
55
+ const decoded = window.lightningPayReq.decode(invoice);
56
+ if (decoded.timeExpireDate < Math.floor(Date.now() / 1000)) {
57
+ setIsExpired(true);
58
+ }
59
+ //const numMsat = decoded.millisatoshis;
60
+ const { amount: amt } = await invoiceToAssetInfo({
61
+ payReq: invoice,
62
+ assetId: currentAssetPair.asset.assetId,
63
+ });
64
+ setAssetAmt(amt);
65
+ const hashlock = getPaymentHash(decoded);
66
+ setHashlock(hashlock);
67
+ } catch (e) {
68
+ console.log(e.message);
69
+ setAssetAmt(0);
70
+ } finally {
71
+ setParseInvoiceLoading(false);
84
72
  }
85
- }
73
+ }, [currentAssetPair, invoice]);
74
+ useDebounceEffect(
75
+ () => {
76
+ if (invoice && currentAssetPair) {
77
+ parseInvoice();
78
+ }
79
+ },
80
+ [currentAssetPair, invoice, parseInvoice],
81
+ {
82
+ wait: 500,
83
+ }
84
+ );
85
+
86
+ return {
87
+ assetAmt,
88
+ formatAssetAmt,
89
+ tokenAmt,
90
+ formatTokenAmt,
91
+ hashlock,
92
+ parseInvoiceLoading,
93
+ isExpired,
94
+ };
95
+ }