astra-modal-test 1.0.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 (65) hide show
  1. package/README.md +0 -0
  2. package/dist/astra-sdk.es.js +8 -0
  3. package/dist/astra-sdk.umd.js +12576 -0
  4. package/dist/ccip-Bpb4GlU6.mjs +146 -0
  5. package/dist/hooks.module-BBZfodGH.mjs +506 -0
  6. package/dist/index-BTsa09iy.mjs +84805 -0
  7. package/dist/index-CEtKkuP2.mjs +133 -0
  8. package/dist/index-CbWGAz3l.mjs +445 -0
  9. package/dist/index-DRPxIWUd.mjs +13720 -0
  10. package/dist/index-h9_6XS0r.mjs +2929 -0
  11. package/dist/secp256k1-3OC5y4qp.mjs +1578 -0
  12. package/dist/vite.svg +1 -0
  13. package/dist/w3m-modal-R9m1z1SQ.mjs +267 -0
  14. package/eslint.config.js +39 -0
  15. package/index.html +13 -0
  16. package/package.json +50 -0
  17. package/public/vite.svg +1 -0
  18. package/src/App.css +170 -0
  19. package/src/apis/lspApi.js +59 -0
  20. package/src/apis/request.js +59 -0
  21. package/src/assets/arrow-right.svg +3 -0
  22. package/src/assets/astr.svg +13 -0
  23. package/src/assets/bridge-loading.png +0 -0
  24. package/src/assets/network/base.png +0 -0
  25. package/src/assets/network/eth.png +0 -0
  26. package/src/assets/network/lighting.png +0 -0
  27. package/src/assets/network/ligtning.svg +22 -0
  28. package/src/assets/network/solona.png +0 -0
  29. package/src/assets/powerby.svg +14 -0
  30. package/src/assets/react.svg +1 -0
  31. package/src/assets/success.svg +3 -0
  32. package/src/assets/tip.svg +5 -0
  33. package/src/assets/tokens/sol.png +0 -0
  34. package/src/assets/tokens/usdc.png +0 -0
  35. package/src/assets/tokens/usdt.png +0 -0
  36. package/src/comps/AstraImage.jsx +23 -0
  37. package/src/comps/AstraModal.jsx +207 -0
  38. package/src/comps/AstraModalLogo.jsx +53 -0
  39. package/src/comps/AstraNetwork.jsx +115 -0
  40. package/src/comps/CheckErc20Button.jsx +28 -0
  41. package/src/comps/ConnectButton.jsx +37 -0
  42. package/src/comps/EllipsisMiddle.jsx +42 -0
  43. package/src/comps/ResultModal.jsx +307 -0
  44. package/src/comps/ToLightning.jsx +502 -0
  45. package/src/comps/ToToken.jsx +467 -0
  46. package/src/constants/contracts/abi/bridge.js +1246 -0
  47. package/src/constants/contracts/abi/index.js +3 -0
  48. package/src/constants/contracts/abi/usdt.js +130 -0
  49. package/src/constants/contracts/abi/watcher.js +523 -0
  50. package/src/constants/contracts/index.js +37 -0
  51. package/src/constants/index.js +5 -0
  52. package/src/font/ClashDisplay-Variable.ttf +0 -0
  53. package/src/font/ClashDisplay-Variable.woff +0 -0
  54. package/src/font/ClashDisplay-Variable.woff2 +0 -0
  55. package/src/hooks/useContract.js +127 -0
  56. package/src/hooks/useLspApi.js +53 -0
  57. package/src/hooks/useParseInvoice.js +85 -0
  58. package/src/index.css +69 -0
  59. package/src/index.jsx +112 -0
  60. package/src/lib/bolt11.min.js +1 -0
  61. package/src/main.jsx +41 -0
  62. package/src/store/index.js +25 -0
  63. package/src/theme.js +108 -0
  64. package/src/utils/index.js +36 -0
  65. package/vite.config.js +44 -0
@@ -0,0 +1,502 @@
1
+ import { memo } from "react";
2
+ import styled from "styled-components";
3
+ import useStore from "../store";
4
+ import { Form, Button, Select, Flex, Spin } from "antd";
5
+ import { useCallback, useEffect, useState, useMemo } from "react";
6
+ import AstraImage from "./AstraImage";
7
+ import { useAccount, useBalance } from "wagmi";
8
+ import { DoubleRightOutlined } from "@ant-design/icons";
9
+ import useParseInvoice from "../hooks/useParseInvoice";
10
+ import EllipsisMiddle from "./EllipsisMiddle";
11
+ import { useBridgeContract } from "../hooks/useContract";
12
+ import { useGetReserveInfo } from "../hooks/useLspApi";
13
+ import ConnectButton from "./ConnectButton";
14
+ import Decimal from "decimal.js";
15
+ import { formatAssetByDecimal } from "../utils";
16
+ import AstraNetwork from "./AstraNetwork";
17
+ import IconTip from "../assets/tip.svg";
18
+ import IconArrowRight from "../assets/arrow-right.svg";
19
+ const WrapperToLightning = styled.div``;
20
+
21
+ const FormItem = styled(Form.Item)``;
22
+
23
+ const ClearAuthWrapper = styled.div`
24
+ display: flex;
25
+ gap: 10px;
26
+ padding: 10px 20px;
27
+ justify-content: space-between;
28
+ align-items: flex-start;
29
+ align-self: stretch;
30
+ color: #fff;
31
+ font-family: "Clash Display";
32
+ font-size: 14px;
33
+ font-style: normal;
34
+ font-weight: 400;
35
+ border-radius: 24px;
36
+ border: 1px solid #333;
37
+ background: rgba(51, 51, 51, 0.2);
38
+ backdrop-filter: blur(25px);
39
+ .clear-auth {
40
+ color: #caff33;
41
+ font-family: "Clash Display";
42
+ font-size: 14px;
43
+ font-style: normal;
44
+ font-weight: 500;
45
+ line-height: normal;
46
+ text-decoration-line: underline;
47
+ text-decoration-style: solid;
48
+ text-decoration-skip-ink: none;
49
+ text-decoration-thickness: auto;
50
+ text-underline-offset: auto;
51
+ text-underline-position: from-font;
52
+ }
53
+ `;
54
+
55
+ const AstraSelect = styled(Select)`
56
+ width: 60px !important;
57
+ .astra-select-selector {
58
+ padding: 0 0px !important;
59
+ }
60
+ .astra-select-selection-item {
61
+ display: flex !important;
62
+ align-items: center;
63
+ justify-content: center;
64
+ }
65
+ .astra-select-arrow {
66
+ color: #fff;
67
+ }
68
+ `;
69
+
70
+ const BalanceFlex = styled(Flex)`
71
+ .available_token_balance {
72
+ font-size: 12px;
73
+ color: #fff;
74
+ }
75
+ `;
76
+ const AstraSendingReceived = styled.div`
77
+ padding: 8px 20px;
78
+ border-radius: 12px;
79
+ background: rgba(166, 166, 166, 0.1);
80
+ margin-bottom: 14px;
81
+ @media (max-width: 768px) {
82
+ padding: 8px 10px;
83
+ border-radius: 12px;
84
+ background: rgba(166, 166, 166, 0.1);
85
+ margin-bottom: 14px;
86
+ }
87
+ `;
88
+ const AstraFormItemOther = styled.div`
89
+ padding: 0 20px;
90
+ @media (max-width: 768px) {
91
+ padding: 0 10px;
92
+ }
93
+ `;
94
+ const AstraButton = styled(Button)`
95
+ width: 100%;
96
+ display: flex;
97
+ justify-content: space-between;
98
+ padding: 0 30px;
99
+ font-size: 16px;
100
+ line-height: 42px;
101
+ `;
102
+ function ToLightning({
103
+ messageApi,
104
+ setResultModalShow,
105
+ setResultModalRequest,
106
+ }) {
107
+ const {
108
+ receiveArgs,
109
+ allAssetPairs,
110
+ targetNetwork,
111
+ setCurrentAssetPair,
112
+ currentAssetPair,
113
+ setAstraModalShow,
114
+ } = useStore();
115
+
116
+ const account = useAccount();
117
+ const [form] = Form.useForm();
118
+ const [submitLoading, setSubmitLoading] = useState(false);
119
+ const [approveLoading, setApproveLoading] = useState(false);
120
+ const filterdAssetPairs = useMemo(() => {
121
+ return allAssetPairs.filter((pair) => {
122
+ return pair.asset.assetId === receiveArgs.assetId;
123
+ });
124
+ }, [allAssetPairs, receiveArgs.assetId]);
125
+
126
+ const { reserveInfo, loading: reserveInfoLoading } = useGetReserveInfo(
127
+ targetNetwork,
128
+ currentAssetPair
129
+ );
130
+
131
+ const tokenBalanceRet = useBalance({
132
+ address: account?.address,
133
+ token: currentAssetPair?.token?.address,
134
+ enabled: !!currentAssetPair?.token?.address && !!account?.address,
135
+ formatUnits: currentAssetPair?.token?.decimal,
136
+ });
137
+
138
+ const mainnetBalanceRet = useBalance({
139
+ address: account?.address,
140
+ enabled: !!account?.address,
141
+ formatUnits: "ether",
142
+ });
143
+ //console.log('mainnetBalanceRet', mainnetBalanceRet)
144
+ // console.log('tokenRet', tokenBalanceRet);
145
+
146
+ const serviceFee = useMemo(() => {
147
+ const recieveBaseFee = reserveInfo?.toAssetBaseFee || 0;
148
+ return new Decimal(recieveBaseFee.toString()).div(10 ** 18).toFixed(4);
149
+ }, [reserveInfo?.toAssetBaseFee]);
150
+
151
+ const {
152
+ assetAmt,
153
+ formatAssetAmt,
154
+ tokenAmt,
155
+ formatTokenAmt,
156
+ hashlock,
157
+ parseInvoiceLoading,
158
+ } = useParseInvoice(receiveArgs?.invoice);
159
+
160
+ const {
161
+ allowanceRet,
162
+ formatAllowance,
163
+ onReloadAllowance,
164
+ onApprove,
165
+ onDeposit,
166
+ } = useBridgeContract(account);
167
+
168
+ const onAssetPairChange = useCallback(
169
+ (pairId) => {
170
+ const selectedPair = pairId
171
+ ? allAssetPairs.find((pair) => pair.pairId === pairId)
172
+ : null;
173
+ setCurrentAssetPair(selectedPair);
174
+ },
175
+ [allAssetPairs, setCurrentAssetPair]
176
+ );
177
+
178
+ const handleApprove = useCallback(
179
+ async (approveAmt) => {
180
+ try {
181
+ setApproveLoading(true);
182
+ const approveTx = await onApprove(approveAmt);
183
+ if (approveTx) {
184
+ messageApi.open({
185
+ type: "success",
186
+ content: approveAmt == 0 ? "Clear Auth success" : "Approve success",
187
+ });
188
+ }
189
+ } catch (e) {
190
+ const error = e.message;
191
+ if (error.includes("User rejected")) {
192
+ messageApi.error("User reject the request.");
193
+ } else {
194
+ messageApi.error(e.message);
195
+ }
196
+ } finally {
197
+ setApproveLoading(false);
198
+ onReloadAllowance();
199
+ }
200
+ },
201
+ [messageApi, onApprove, onReloadAllowance]
202
+ );
203
+
204
+ const btnDisabled = useMemo(() => {
205
+ if (filterdAssetPairs?.length === 0) {
206
+ return true;
207
+ }
208
+ const tokenBalance = tokenBalanceRet?.data?.value || 0;
209
+ const mainetBalance = mainnetBalanceRet?.data?.value || 0;
210
+ if (
211
+ !Number(tokenBalance) ||
212
+ Number(tokenBalance) < Number(tokenAmt) ||
213
+ Number(mainetBalance) < Number(reserveInfo?.toAssetBaseFee)
214
+ ) {
215
+ return true;
216
+ }
217
+ }, [
218
+ filterdAssetPairs?.length,
219
+ mainnetBalanceRet?.data?.value,
220
+ reserveInfo?.toAssetBaseFee,
221
+ tokenAmt,
222
+ tokenBalanceRet?.data?.value,
223
+ ]);
224
+
225
+ const memoBtn = useMemo(() => {
226
+ if (!account?.address) {
227
+ return <ConnectButton />;
228
+ }
229
+ if (Number(allowanceRet) < Number(tokenAmt)) {
230
+ return (
231
+ <AstraButton
232
+ type="primary"
233
+ disabled={btnDisabled}
234
+ onClick={() => {
235
+ handleApprove(Number.MAX_SAFE_INTEGER);
236
+ }}
237
+ loading={approveLoading}
238
+ >
239
+ <span>Approve</span>
240
+ <img src={IconArrowRight} alt="" />
241
+ </AstraButton>
242
+ );
243
+ } else {
244
+ return (
245
+ <AstraButton
246
+ type="primary"
247
+ disabled={btnDisabled}
248
+ loading={submitLoading}
249
+ htmlType="submit"
250
+ >
251
+ <span>
252
+ {submitLoading ? "Waiting for Confirmation..." : "Deposit"}
253
+ </span>
254
+ <img src={IconArrowRight} alt="" />
255
+ </AstraButton>
256
+ );
257
+ }
258
+ }, [
259
+ account?.address,
260
+ allowanceRet,
261
+ approveLoading,
262
+ btnDisabled,
263
+ handleApprove,
264
+ submitLoading,
265
+ tokenAmt,
266
+ ]);
267
+
268
+ const onFinish = useCallback(async () => {
269
+ try {
270
+ if (!account?.address) {
271
+ return;
272
+ }
273
+ setSubmitLoading(true);
274
+ if (
275
+ BigInt(assetAmt) > BigInt(reserveInfo?.maxAsset) ||
276
+ Number(formatAssetAmt) < reserveInfo.min
277
+ ) {
278
+ throw new Error(
279
+ `Amount should be between ${
280
+ reserveInfo?.min
281
+ } and ${formatAssetByDecimal(
282
+ reserveInfo?.maxAsset,
283
+ currentAssetPair.asset.decimal
284
+ )}`
285
+ );
286
+ }
287
+ await onReloadAllowance();
288
+
289
+ if (Number(allowanceRet) < Number(tokenAmt)) {
290
+ const approveTx = await onApprove(Number.MAX_SAFE_INTEGER);
291
+ if (approveTx) {
292
+ messageApi.open({
293
+ type: "success",
294
+ content: "Approve success",
295
+ });
296
+ }
297
+ }
298
+
299
+ const depositArgs = {
300
+ hashlock,
301
+ fromAddr: account.address,
302
+ toAddr: reserveInfo.lspAddress,
303
+ tokenAddr: currentAssetPair.token.address,
304
+ amount: tokenAmt,
305
+ timeLock: reserveInfo?.deltaSecond,
306
+ toLightning: true,
307
+ fee: reserveInfo?.toAssetBaseFee,
308
+ signature: "0x",
309
+ };
310
+ console.log("depositArgs", depositArgs);
311
+ const tx = await onDeposit(depositArgs);
312
+ if (tx) {
313
+ const request = {
314
+ assetId: currentAssetPair.asset.assetId,
315
+ chainId: targetNetwork.id,
316
+ lnInvoice: receiveArgs?.invoice,
317
+ amount: tokenAmt,
318
+ depositTx: tx,
319
+ };
320
+ setResultModalRequest({
321
+ txHash: tx,
322
+ timestamp: Date.now(),
323
+ formatTokenAmt,
324
+ request,
325
+ });
326
+ setAstraModalShow(false);
327
+ setResultModalShow(true);
328
+ }
329
+ } catch (e) {
330
+ const error = e.message;
331
+ if (error.includes("User rejected")) {
332
+ messageApi.error("User reject the request.");
333
+ } else {
334
+ messageApi.error(e.message);
335
+ }
336
+ } finally {
337
+ setSubmitLoading(false);
338
+ }
339
+ }, [
340
+ account.address,
341
+ assetAmt,
342
+ reserveInfo,
343
+ formatAssetAmt,
344
+ onReloadAllowance,
345
+ allowanceRet,
346
+ tokenAmt,
347
+ hashlock,
348
+ currentAssetPair,
349
+ onDeposit,
350
+ onApprove,
351
+ messageApi,
352
+ targetNetwork.id,
353
+ receiveArgs?.invoice,
354
+ setResultModalRequest,
355
+ formatTokenAmt,
356
+ setAstraModalShow,
357
+ setResultModalShow,
358
+ ]);
359
+
360
+ const onClearAuth = useCallback(async () => {
361
+ await handleApprove(0);
362
+ }, [handleApprove]);
363
+
364
+ useEffect(() => {
365
+ if (filterdAssetPairs?.length > 0) {
366
+ setCurrentAssetPair(filterdAssetPairs[0]);
367
+ form.setFieldValue("sending", filterdAssetPairs?.[0]?.pairId);
368
+ }
369
+ }, [filterdAssetPairs, form, setCurrentAssetPair]);
370
+
371
+ return (
372
+ <>
373
+ <WrapperToLightning>
374
+ <Spin spinning={reserveInfoLoading || parseInvoiceLoading}>
375
+ <Form
376
+ name="basic"
377
+ form={form}
378
+ colon={false}
379
+ // labelCol={{
380
+ // span: 8,
381
+ // }}
382
+ // wrapperCol={{
383
+ // span: 16,
384
+ // }}
385
+ style={{
386
+ width: "100%",
387
+ }}
388
+ onFinish={onFinish}
389
+ autoComplete="off"
390
+ >
391
+ <AstraNetwork
392
+ messageApi={messageApi}
393
+ balance={mainnetBalanceRet?.data}
394
+ />
395
+ <AstraSendingReceived>
396
+ <FormItem label="Sending">
397
+ <Flex justify="end" align="center" gap="5px">
398
+ <FormItem name="sending" noStyle>
399
+ <AstraSelect
400
+ variant="borderless"
401
+ options={filterdAssetPairs.map((pair) => ({
402
+ value: pair.pairId,
403
+ label: (
404
+ <AstraImage
405
+ name={pair.token.name}
406
+ width="22px"
407
+ height="22px"
408
+ />
409
+ ),
410
+ }))}
411
+ onChange={onAssetPairChange}
412
+ ></AstraSelect>
413
+ </FormItem>
414
+ <span className="fw500">{formatTokenAmt}</span>
415
+ <span className="fw500">{currentAssetPair?.token?.name}</span>
416
+ </Flex>
417
+ <BalanceFlex justify="end" align="center" gap="5px">
418
+ <span className="available_token_balance">
419
+ Available: {tokenBalanceRet?.data?.formatted || 0}{" "}
420
+ {tokenBalanceRet?.data?.symbol}
421
+ </span>
422
+ </BalanceFlex>
423
+ </FormItem>
424
+
425
+ <FormItem label="Received" style={{ marginBottom: 0 }}>
426
+ <Flex justify="end" align="center" gap="5px">
427
+ <AstraImage name={"Lightning"} />
428
+ <span className="colorcaff33">{formatAssetAmt}</span>
429
+ <span className="colorcaff33">
430
+ {currentAssetPair?.asset?.name}
431
+ </span>
432
+ </Flex>
433
+ </FormItem>
434
+ </AstraSendingReceived>
435
+ <AstraFormItemOther>
436
+ <FormItem label="Service fee">
437
+ <Flex justify="end" align="center" gap="5px">
438
+ <span>{serviceFee}</span>
439
+ <AstraImage
440
+ name={targetNetwork?.name}
441
+ width="22px"
442
+ height="22px"
443
+ />
444
+ </Flex>
445
+ </FormItem>
446
+
447
+ <FormItem label="Receiving LN invoice">
448
+ <Flex justify="end">
449
+ <EllipsisMiddle copyable suffixCount={10}>
450
+ {receiveArgs?.invoice}
451
+ </EllipsisMiddle>
452
+ </Flex>
453
+ </FormItem>
454
+
455
+ <FormItem label="Route">
456
+ <Flex justify="end" align="center" gap="5px">
457
+ <AstraImage
458
+ name={targetNetwork?.name}
459
+ width="22px"
460
+ height="22px"
461
+ />
462
+ <DoubleRightOutlined style={{ color: "#caff33" }} />
463
+ <AstraImage name="Lightning" />
464
+ </Flex>
465
+ </FormItem>
466
+ </AstraFormItemOther>
467
+ <FormItem
468
+ label={null}
469
+ labelCol={{ span: 0 }}
470
+ wrapperCol={{ span: 24 }}
471
+ >
472
+ <Flex justify="center">{memoBtn}</Flex>
473
+ </FormItem>
474
+ </Form>
475
+ </Spin>
476
+
477
+ {Number(formatAllowance) &&
478
+ Number(tokenAmt) > Number(formatAllowance) ? (
479
+ <ClearAuthWrapper>
480
+ <img src={IconTip} alt="tip" />
481
+ <span>
482
+ Your remaining authorization to Astra is {formatAllowance}{" "}
483
+ {currentAssetPair?.token?.name}. To transact more, clear the
484
+ current authorization and approve the new amount.
485
+ <Button
486
+ type="link"
487
+ loading={approveLoading}
488
+ onClick={onClearAuth}
489
+ className="clear-auth"
490
+ >
491
+ Clear Authorization
492
+ </Button>{" "}
493
+ </span>
494
+ </ClearAuthWrapper>
495
+ ) : (
496
+ <></>
497
+ )}
498
+ </WrapperToLightning>
499
+ </>
500
+ );
501
+ }
502
+ export default memo(ToLightning);