@ensofinance/checkout-widget 0.0.3 → 0.0.5
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/checkout-widget.es.js +3486 -3479
- package/dist/checkout-widget.es.js.map +1 -1
- package/dist/checkout-widget.umd.js +30 -30
- package/dist/checkout-widget.umd.js.map +1 -1
- package/dist/index.d.ts +4 -6
- package/package.json +1 -2
- package/src/components/Checkout.tsx +4 -4
- package/src/components/CheckoutModal.tsx +2 -0
- package/src/components/steps/ExchangeFlow.tsx +46 -18
- package/src/components/ui/index.tsx +0 -1
- package/src/index.ts +5 -3
- package/src/util/constants.tsx +4 -1
package/dist/index.d.ts
CHANGED
|
@@ -1,20 +1,18 @@
|
|
|
1
|
-
import { CheckoutConfig } from '
|
|
2
|
-
import {
|
|
3
|
-
import { CheckoutModalProps } from '../../../../../../src/types';
|
|
4
|
-
import { CheckoutModalProps as CheckoutModalProps_2 } from '../../../../../../../src/types';
|
|
1
|
+
import { CheckoutConfig } from '../../../../../../../src/types';
|
|
2
|
+
import { CheckoutModalProps } from '../../../../../../../src/types';
|
|
5
3
|
import { ComponentType } from 'react';
|
|
6
4
|
import { JSX as JSX_2 } from 'react/jsx-runtime';
|
|
7
5
|
import { SystemConfig as WidgetTheme } from '@chakra-ui/react';
|
|
8
6
|
|
|
9
7
|
export declare const Checkout: ({ config: { apiKey, tokenOut, chainIdOut, theme, enableExchange }, onClose, wrapper, }: {
|
|
10
|
-
config:
|
|
8
|
+
config: CheckoutConfig;
|
|
11
9
|
wrapper?: ComponentType;
|
|
12
10
|
onClose?: () => void;
|
|
13
11
|
}) => JSX_2.Element;
|
|
14
12
|
|
|
15
13
|
export { CheckoutConfig }
|
|
16
14
|
|
|
17
|
-
export declare const CheckoutModal: ({ config, setIsActive, isActive, onClose, }:
|
|
15
|
+
export declare const CheckoutModal: ({ config, setIsActive, isActive, onClose, }: CheckoutModalProps) => JSX_2.Element;
|
|
18
16
|
|
|
19
17
|
export { CheckoutModalProps }
|
|
20
18
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ensofinance/checkout-widget",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.5",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"homepage": "https://www.enso.build/",
|
|
6
6
|
"repository": {
|
|
@@ -28,7 +28,6 @@
|
|
|
28
28
|
"@chakra-ui/react": "^3.19.1",
|
|
29
29
|
"@emotion/cache": "^11.14.0",
|
|
30
30
|
"@emotion/react": "^11.14.0",
|
|
31
|
-
"@ensofinance/checkout-ui-library": "^0.0.2",
|
|
32
31
|
"@ensofinance/sdk": "^1.0.18",
|
|
33
32
|
"@meshconnect/web-link-sdk": "^3.2.14",
|
|
34
33
|
"@uidotdev/usehooks": "^2.4.1",
|
|
@@ -51,11 +51,9 @@ const FLOWS: Record<
|
|
|
51
51
|
},
|
|
52
52
|
};
|
|
53
53
|
|
|
54
|
-
|
|
55
|
-
{} as ICheckoutContext,
|
|
56
|
-
);
|
|
54
|
+
const CheckoutContext = createContext<ICheckoutContext>({} as ICheckoutContext);
|
|
57
55
|
|
|
58
|
-
|
|
56
|
+
const Checkout = ({
|
|
59
57
|
config: { apiKey, tokenOut, chainIdOut, theme, enableExchange },
|
|
60
58
|
onClose,
|
|
61
59
|
wrapper,
|
|
@@ -125,3 +123,5 @@ export const Checkout = ({
|
|
|
125
123
|
</CheckoutContext.Provider>
|
|
126
124
|
);
|
|
127
125
|
};
|
|
126
|
+
|
|
127
|
+
export { CheckoutConfig, Checkout, CheckoutContext };
|
|
@@ -10,7 +10,7 @@ import {
|
|
|
10
10
|
Table,
|
|
11
11
|
} from "@chakra-ui/react";
|
|
12
12
|
import { ChevronLeft, X, ArrowDownUpIcon } from "lucide-react";
|
|
13
|
-
import { useContext, useEffect, useMemo, useState } from "react";
|
|
13
|
+
import { useContext, useEffect, useMemo, useState, useCallback } from "react";
|
|
14
14
|
import { IconButton, Button, Tab, Input, Tooltip } from "../ui";
|
|
15
15
|
import { useAccount, useSignMessage, useChainId, useSwitchChain } from "wagmi";
|
|
16
16
|
import { getUserOperationHash } from "viem/account-abstraction";
|
|
@@ -28,7 +28,10 @@ import { useAppStore } from "@/store";
|
|
|
28
28
|
import { AssetCard } from "../cards";
|
|
29
29
|
import { formatNumber, formatUSD } from "@/util";
|
|
30
30
|
import { useTokenFromListBySymbols, getChainName } from "@/util/common";
|
|
31
|
-
import {
|
|
31
|
+
import {
|
|
32
|
+
EXCHANGE_MAX_LIMIT_GAP_USD,
|
|
33
|
+
EXCHANGE_MIN_LIMIT,
|
|
34
|
+
} from "@/util/constants";
|
|
32
35
|
import { useAppDetails, useRouteData } from "@/util/enso-hooks";
|
|
33
36
|
import QuoteParameters from "../QuoteParameters";
|
|
34
37
|
import { TransactionDetailRow } from "../TransactionDetailRow";
|
|
@@ -479,13 +482,47 @@ const ChooseAmountStep = ({
|
|
|
479
482
|
const { setAmountIn } = useAppStore();
|
|
480
483
|
const { tokenInData, tokenIn } = useAppDetails();
|
|
481
484
|
|
|
485
|
+
// Handle percentage selection with limits
|
|
486
|
+
const handlePercentageSelect = useCallback(
|
|
487
|
+
(percent: number) => {
|
|
488
|
+
if (!selectedToken) return;
|
|
489
|
+
|
|
490
|
+
const minAmountForToken =
|
|
491
|
+
EXCHANGE_MIN_LIMIT[
|
|
492
|
+
selectedToken.symbol as keyof typeof EXCHANGE_MIN_LIMIT
|
|
493
|
+
] || 0;
|
|
494
|
+
const maxUsdAmount =
|
|
495
|
+
selectedToken.marketValue - EXCHANGE_MAX_LIMIT_GAP_USD;
|
|
496
|
+
|
|
497
|
+
// Calculate target USD amount based on percentage
|
|
498
|
+
const targetUsdAmount = (selectedToken.marketValue * percent) / 100;
|
|
499
|
+
|
|
500
|
+
// Apply limits to the target USD amount
|
|
501
|
+
const limitedUsdAmount = Math.max(
|
|
502
|
+
minAmountForToken,
|
|
503
|
+
Math.min(targetUsdAmount, maxUsdAmount),
|
|
504
|
+
);
|
|
505
|
+
|
|
506
|
+
// Convert back to token amount
|
|
507
|
+
const tokenPrice =
|
|
508
|
+
selectedToken.marketValue / selectedToken.balance;
|
|
509
|
+
const limitedTokenAmount = Math.min(
|
|
510
|
+
limitedUsdAmount / tokenPrice,
|
|
511
|
+
selectedToken.balance,
|
|
512
|
+
);
|
|
513
|
+
|
|
514
|
+
setAmount(limitedTokenAmount.toString());
|
|
515
|
+
setUsdValue(limitedUsdAmount.toFixed(2));
|
|
516
|
+
},
|
|
517
|
+
[selectedToken],
|
|
518
|
+
);
|
|
519
|
+
|
|
482
520
|
// Set max value on load
|
|
483
521
|
useEffect(() => {
|
|
484
522
|
if (selectedToken) {
|
|
485
|
-
|
|
486
|
-
setUsdValue(selectedToken.marketValue.toFixed(2));
|
|
523
|
+
handlePercentageSelect(100);
|
|
487
524
|
}
|
|
488
|
-
}, [selectedToken]);
|
|
525
|
+
}, [selectedToken, handlePercentageSelect]);
|
|
489
526
|
|
|
490
527
|
useEffect(() => {
|
|
491
528
|
console.log("tokenIn", tokenIn);
|
|
@@ -495,17 +532,6 @@ const ChooseAmountStep = ({
|
|
|
495
532
|
);
|
|
496
533
|
}, [amount, tokenInData?.decimals]);
|
|
497
534
|
|
|
498
|
-
// Handle percentage selection
|
|
499
|
-
const handlePercentageSelect = (percent: number) => {
|
|
500
|
-
if (!selectedToken) return;
|
|
501
|
-
|
|
502
|
-
const amountToSet = (selectedToken.balance * percent) / 100;
|
|
503
|
-
const usdAmountToSet = (selectedToken.marketValue * percent) / 100;
|
|
504
|
-
|
|
505
|
-
setAmount(amountToSet.toString());
|
|
506
|
-
setUsdValue(usdAmountToSet.toFixed(2));
|
|
507
|
-
};
|
|
508
|
-
|
|
509
535
|
// Handle input change based on current mode
|
|
510
536
|
const handleInputChange = (value: string) => {
|
|
511
537
|
if (!selectedToken) return;
|
|
@@ -560,7 +586,9 @@ const ChooseAmountStep = ({
|
|
|
560
586
|
// Limits validation logic
|
|
561
587
|
const currentUsdValue = parseFloat(usdValue);
|
|
562
588
|
const minAmountForToken = selectedToken
|
|
563
|
-
?
|
|
589
|
+
? EXCHANGE_MIN_LIMIT[
|
|
590
|
+
selectedToken.symbol as keyof typeof EXCHANGE_MIN_LIMIT
|
|
591
|
+
]
|
|
564
592
|
: 0;
|
|
565
593
|
const maxUsdAmount = selectedToken ? selectedToken.marketValue - 5 : 0;
|
|
566
594
|
|
|
@@ -591,7 +619,7 @@ const ChooseAmountStep = ({
|
|
|
591
619
|
<HeaderTitle>Enter Amount</HeaderTitle>
|
|
592
620
|
<HeaderDescription>
|
|
593
621
|
Available: {formatNumber(selectedToken.balance)}{" "}
|
|
594
|
-
{selectedToken.symbol} (
|
|
622
|
+
{selectedToken.symbol} (
|
|
595
623
|
{formatUSD(selectedToken.marketValue)})
|
|
596
624
|
</HeaderDescription>
|
|
597
625
|
</Box>
|
package/src/index.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import {
|
|
2
|
+
CheckoutModal,
|
|
3
|
+
type CheckoutModalProps,
|
|
4
|
+
} from "./components/CheckoutModal";
|
|
5
|
+
import { Checkout, type CheckoutConfig } from "./components/Checkout";
|
|
4
6
|
|
|
5
7
|
// Export two versions for different integration needs
|
|
6
8
|
export {
|
package/src/util/constants.tsx
CHANGED
|
@@ -10,12 +10,15 @@ enum MESH_SYMBOLS {
|
|
|
10
10
|
USDT = "USDT",
|
|
11
11
|
WBTC = "WBTC",
|
|
12
12
|
}
|
|
13
|
-
|
|
13
|
+
|
|
14
|
+
// Exchanges approximate limits. Improve once Mesh has API
|
|
15
|
+
export const EXCHANGE_MIN_LIMIT = {
|
|
14
16
|
[MESH_SYMBOLS.ETH]: 0.02,
|
|
15
17
|
[MESH_SYMBOLS.USDC]: 20,
|
|
16
18
|
[MESH_SYMBOLS.USDT]: 20,
|
|
17
19
|
[MESH_SYMBOLS.WBTC]: 0.00000001,
|
|
18
20
|
};
|
|
21
|
+
export const EXCHANGE_MAX_LIMIT_GAP_USD = 5
|
|
19
22
|
|
|
20
23
|
export const ETH_TOKEN: Token = {
|
|
21
24
|
address: ETH_ADDRESS,
|