@ensofinance/checkout-widget 0.0.4 → 0.0.6

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ensofinance/checkout-widget",
3
- "version": "0.0.4",
3
+ "version": "0.0.6",
4
4
  "type": "module",
5
5
  "homepage": "https://www.enso.build/",
6
6
  "repository": {
@@ -22,7 +22,8 @@
22
22
  "publish": "npm publish --access public",
23
23
  "dev": "vite build --watch --mode=dev --minify=false --sourcemap=false --emptyOutDir=false",
24
24
  "build": "vite build",
25
- "preview": "vite preview"
25
+ "preview": "vite preview",
26
+ "build:pump:publish": "npm run build && npm run bump && npm run publish"
26
27
  },
27
28
  "dependencies": {
28
29
  "@chakra-ui/react": "^3.19.1",
@@ -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 { MIN_AMOUNT } from "@/util/constants";
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
- setAmount(selectedToken.balance.toString());
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
- ? MIN_AMOUNT[selectedToken.symbol as keyof typeof MIN_AMOUNT]
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>
@@ -9,7 +9,12 @@ import { Button, IconButton, Tab, Input } from "../ui";
9
9
  import CurrencySwapDisplay from "../CurrencySwapDisplay";
10
10
  import { useEnsoPrice, useEnsoToken } from "@/enso-api/api";
11
11
  import { useAppStore } from "@/store";
12
- import { normalizeValue, denormalizeValue, formatNumber } from "@/util";
12
+ import {
13
+ normalizeValue,
14
+ denormalizeValue,
15
+ formatNumber,
16
+ formatUSD,
17
+ } from "@/util";
13
18
  import { useTokenBalance } from "@/util/wallet";
14
19
 
15
20
  type InputMode = "usd" | "token";
@@ -81,10 +86,11 @@ const WalletAmountStep = ({ nextStep }: { nextStep?: string }) => {
81
86
  if (inputMode === "usd") {
82
87
  const cleanUsd = value.replace("$", "");
83
88
  // Clean the input from usd sign
89
+ console.log(cleanUsd, priceData, tokenDetails?.decimals);
84
90
  setUsdValue(cleanUsd);
85
91
  setAmountIn(
86
92
  denormalizeValue(
87
- (parseFloat(cleanUsd) * priceData).toString(),
93
+ (parseFloat(cleanUsd) / priceData).toString(),
88
94
  tokenDetails?.decimals,
89
95
  ),
90
96
  );
@@ -101,21 +107,24 @@ const WalletAmountStep = ({ nextStep }: { nextStep?: string }) => {
101
107
 
102
108
  // Get input placeholder and display value
103
109
  const getInputDisplay = () => {
110
+ const formattedValue = formatNumber(tokenValue);
111
+ const safeUsdValue = parseFloat(usdValue) > 0 ? usdValue : 0;
112
+
104
113
  if (inputMode === "usd") {
105
114
  return {
106
115
  placeholder: "$10.00",
107
- displayValue: usdValue ? `$${usdValue}` : "",
116
+ displayValue: safeUsdValue ? `$${safeUsdValue}` : "",
108
117
  equivalentValue: tokenValue
109
- ? `${formatNumber(tokenValue)} ${tokenDetails?.symbol}`
118
+ ? `${formattedValue} ${tokenDetails?.symbol}`
110
119
  : "—",
111
120
  };
112
- } else {
113
- return {
114
- placeholder: "0.00",
115
- displayValue: tokenValue,
116
- equivalentValue: usdValue ? `$${usdValue}` : "—",
117
- };
118
121
  }
122
+
123
+ return {
124
+ placeholder: "0.00",
125
+ displayValue: formattedValue,
126
+ equivalentValue: formatUSD(safeUsdValue),
127
+ };
119
128
  };
120
129
 
121
130
  const { placeholder, displayValue, equivalentValue } = getInputDisplay();
@@ -10,12 +10,15 @@ enum MESH_SYMBOLS {
10
10
  USDT = "USDT",
11
11
  WBTC = "WBTC",
12
12
  }
13
- export const MIN_AMOUNT = {
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,