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.
- package/.github/workflows/code-quality.yml +43 -0
- package/.github/workflows/publish.yml +64 -0
- package/README.md +204 -0
- package/dist/SwapController-CsNqEWIM.js +447 -0
- package/dist/add-Dm_oSEbv.js +19 -0
- package/dist/all-wallets-DfLlzQJ3.js +10 -0
- package/dist/app-store-BMdJkp6P.js +21 -0
- package/dist/apple-BetlVMKw.js +22 -0
- package/dist/arrow-bottom-BddPptxy.js +12 -0
- package/dist/arrow-bottom-circle-CvdJJniv.js +15 -0
- package/dist/arrow-left-Bbq-z1nQ.js +12 -0
- package/dist/arrow-right-Bu4_SVLC.js +12 -0
- package/dist/arrow-top-CynlXB-M.js +12 -0
- package/dist/astra-sdk.es.js +4 -4
- package/dist/bank-B9QscNnS.js +18 -0
- package/dist/bin-v-tshf8_.js +8 -0
- package/dist/bitcoin-C01gkYyN.js +17 -0
- package/dist/browser-9uNaCY31.js +18 -0
- package/dist/card-hKR9lRzU.js +18 -0
- package/dist/{ccip-oZOrZYR9.js → ccip-CUCziAPw.js} +52 -49
- package/dist/checkmark-D1M40aM5.js +15 -0
- package/dist/checkmark-bold-1_UGl24m.js +12 -0
- package/dist/chevron-bottom-BleoRNRL.js +12 -0
- package/dist/chevron-left-C9JMU-3B.js +12 -0
- package/dist/chevron-right-Daue4WBx.js +12 -0
- package/dist/chevron-top-yuXekbUC.js +12 -0
- package/dist/chrome-store-BsUFRR7F.js +65 -0
- package/dist/circle-BCxdhM5X.js +8 -0
- package/dist/clock-7a-DWZQa.js +12 -0
- package/dist/close-DMQYuJ0z.js +12 -0
- package/dist/clsx.m-DCYaa4RU.js +20 -0
- package/dist/coinPlaceholder-DjQ-HnSH.js +12 -0
- package/dist/compass-AMecXNVm.js +12 -0
- package/dist/copy-CgIPGYbU.js +19 -0
- package/dist/cursor-DpYbTNcO.js +7 -0
- package/dist/cursor-transparent-C8iPJkn8.js +16 -0
- package/dist/desktop-BWVQVGng.js +13 -0
- package/dist/disconnect-C6IQRrtF.js +12 -0
- package/dist/discord-gH3i-QsC.js +21 -0
- package/dist/email-BxYwJC5T.js +10 -0
- package/dist/embedded-wallet-DCcVNgkc.js +464 -0
- package/dist/ethereum-CGWsmZaC.js +14 -0
- package/dist/etherscan-D4EFsAJ4.js +10 -0
- package/dist/exclamation-triangle-DNwQ1Xq6.js +8 -0
- package/dist/extension-DxQPsN2z.js +12 -0
- package/dist/external-link-BeEzrdgR.js +12 -0
- package/dist/facebook-B2Ix8_d3.js +30 -0
- package/dist/farcaster-BB83p8dC.js +16 -0
- package/dist/filters-PzdIomB1.js +12 -0
- package/dist/github-gcp1rxX0.js +22 -0
- package/dist/google-Qv9lTZcz.js +22 -0
- package/dist/help-circle-BLXTMjzX.js +16 -0
- package/dist/id-BHg2CRhj.js +16 -0
- package/dist/image-DWi1V30x.js +8 -0
- package/dist/index-4lbsZ6RQ.js +33 -0
- package/dist/index-64U3D9_n.js +21034 -0
- package/dist/index-CRU49I0g.js +15911 -0
- package/dist/index-ChxXyuZj.js +572 -0
- package/dist/index-ClS956E1.js +74511 -0
- package/dist/index-Cs4fWw01.js +386 -0
- package/dist/index-DfWqgW4h.js +709 -0
- package/dist/index-VYNI76FM.js +315 -0
- package/dist/index-qfkTLN90.js +10785 -0
- package/dist/index-r2ztgVN2.js +1060 -0
- package/dist/index-wvh7Yd5J.js +70 -0
- package/dist/info-Bl5tmiBJ.js +7 -0
- package/dist/info-circle-D2uYywg-.js +16 -0
- package/dist/lightbulb-HvXH2AVF.js +7 -0
- package/dist/mail-DXOPfnog.js +12 -0
- package/dist/mobile-Dd0Gj4vZ.js +13 -0
- package/dist/more-DYcnClbB.js +15 -0
- package/dist/network-placeholder-CfRZGQfi.js +18 -0
- package/dist/nftPlaceholder-DHmFkY55.js +12 -0
- package/dist/off-CG2Y30qN.js +8 -0
- package/dist/onramp-DoAMTeNm.js +917 -0
- package/dist/play-store-B5EYBQto.js +36 -0
- package/dist/plus-DPo25_zL.js +17 -0
- package/dist/qr-code-BRTO19PL.js +10 -0
- package/dist/receive-BvclNdV_.js +179 -0
- package/dist/recycle-horizontal-ng_b2eJV.js +13 -0
- package/dist/refresh-3kiwRV0w.js +12 -0
- package/dist/reown-logo-_9cuIfJD.js +16 -0
- package/dist/search-CoJvIxpi.js +12 -0
- package/dist/secp256k1-wZyK2iIX.js +1257 -0
- package/dist/send-BVKGSR-Z.js +19 -0
- package/dist/send-Bkj5YtHe.js +1039 -0
- package/dist/socials-DL42HlL5.js +589 -0
- package/dist/solana-BjP-_Ioa.js +17 -0
- package/dist/swapHorizontal-Do3uBbum.js +12 -0
- package/dist/swapHorizontalBold-D7N75BE-.js +12 -0
- package/dist/swapHorizontalMedium-BahEVO2x.js +20 -0
- package/dist/swapHorizontalRoundedBold-D6Hoi132.js +12 -0
- package/dist/swapVertical-P-fnrADh.js +12 -0
- package/dist/swaps-DZgbVJZ2.js +1713 -0
- package/dist/telegram-DmaL2iB8.js +20 -0
- package/dist/three-dots-oXyAsUA6.js +9 -0
- package/dist/transactions-BjB5yZN7.js +35 -0
- package/dist/twitch-0UGrMr2K.js +22 -0
- package/dist/twitterIcon-B97KAbaJ.js +10 -0
- package/dist/ui-r6nnVhBA.js +812 -0
- package/dist/user-BG1yp56Q.js +7 -0
- package/dist/verify-CgzkMSuV.js +12 -0
- package/dist/verify-filled-D-TRpWtN.js +12 -0
- package/dist/w3m-modal-Drsibcs0.js +1041 -0
- package/dist/wallet-CAHsZdfj.js +12 -0
- package/dist/wallet-placeholder-agbbreqw.js +18 -0
- package/dist/walletconnect-Yd8iXqY9.js +36 -0
- package/dist/warning-circle-C0hqC6h_.js +16 -0
- package/dist/x-mark-D9_ai2sI.js +7 -0
- package/dist/x-z6v1Jegg.js +16 -0
- package/eslint.config.mjs +1 -1
- package/package.json +14 -13
- package/src/apis/lspApi.js +47 -24
- package/src/assets/ln.png +0 -0
- package/src/assets/network/Arbitrum.png +0 -0
- package/src/assets/network/Polygon.png +0 -0
- package/src/assets/network/Solana.png +0 -0
- package/src/assets/network/botanix.png +0 -0
- package/src/assets/network/bsc.svg +13 -0
- package/src/assets/pay.png +0 -0
- package/src/assets/tokens/usdc.png +0 -0
- package/src/assets/tokens/usdt.png +0 -0
- package/src/comps/AstraImage.jsx +17 -3
- package/src/comps/AstraModal.jsx +22 -16
- package/src/comps/AstraModalLogo.jsx +9 -9
- package/src/comps/AstraNetwork.jsx +174 -45
- package/src/comps/CheckErc20Button.jsx +1 -1
- package/src/comps/CommonStyle.jsx +4 -0
- package/src/comps/ConnectButton.jsx +16 -33
- package/src/comps/ResultModal.jsx +45 -39
- package/src/comps/ToLightning.jsx +207 -80
- package/src/comps/ToToken.jsx +278 -217
- package/src/constants/index.js +20 -4
- package/src/hooks/useContract.js +164 -125
- package/src/hooks/useGetMinAndMax.js +48 -0
- package/src/hooks/useLspApi.js +56 -39
- package/src/hooks/useParseInvoice.js +82 -72
- package/src/index.jsx +43 -10
- package/src/main.jsx +36 -5
- package/src/utils/index.js +136 -24
- package/vite.config.js +7 -1
- package/dist/astra-sdk.umd.js +0 -12578
- package/dist/hooks.module-BBZfodGH.js +0 -506
- package/dist/index-BKKAfRrw.js +0 -87520
- package/dist/index-C2MAIRET.js +0 -2929
- package/dist/index-CsRxokSv.js +0 -445
- package/dist/index-DQ6iiHxJ.js +0 -13720
- package/dist/index-SJhlkSif.js +0 -133
- package/dist/w3m-modal-CYJpmgnf.js +0 -267
- package/src/constants/contracts/abi/bridge.js +0 -1281
- package/src/constants/contracts/abi/index.js +0 -3
- package/src/constants/contracts/abi/usdt.js +0 -130
- package/src/constants/contracts/abi/watcher.js +0 -437
- package/src/constants/contracts/index.js +0 -37
package/src/hooks/useContract.js
CHANGED
|
@@ -1,140 +1,179 @@
|
|
|
1
|
-
|
|
2
1
|
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
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
|
|
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
|
-
|
|
17
|
-
|
|
18
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
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
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
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
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
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
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
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
|
+
};
|
package/src/hooks/useLspApi.js
CHANGED
|
@@ -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
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
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
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
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
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
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
|
-
|
|
34
|
-
|
|
35
|
-
|
|
35
|
+
}, [currentAssetPair?.asset?.assetId]) */
|
|
36
|
+
const chainId = useMemo(() => {
|
|
37
|
+
return targetNetwork?.id;
|
|
38
|
+
}, [targetNetwork?.id]);
|
|
36
39
|
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
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 {
|
|
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
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
return
|
|
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
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
73
|
-
|
|
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
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
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
|
+
}
|