@0xsquid/react-hooks 2.1.10 → 3.0.0-beta.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.
- package/dist/core/constants.js +97 -143
- package/dist/core/constants.js.map +1 -1
- package/dist/core/types/error.d.ts +1 -2
- package/dist/core/types/error.js.map +1 -1
- package/dist/core/types/wallet.d.ts +1 -1
- package/dist/core/wagmiConfig.d.ts +7 -0
- package/dist/core/wagmiConfig.js +52 -0
- package/dist/core/wagmiConfig.js.map +1 -0
- package/dist/hooks/tokens/useBalance.d.ts +4 -14
- package/dist/hooks/tokens/useBalance.js +32 -54
- package/dist/hooks/tokens/useBalance.js.map +1 -1
- package/dist/hooks/tokens/useMultiChainBalance.d.ts +1 -1
- package/dist/hooks/tokens/useMultiChainBalance.js +1 -2
- package/dist/hooks/tokens/useMultiChainBalance.js.map +1 -1
- package/dist/hooks/transaction/useApproval.js +57 -39
- package/dist/hooks/transaction/useApproval.js.map +1 -1
- package/dist/hooks/transaction/useEstimate.d.ts +13 -15
- package/dist/hooks/transaction/useEstimate.js +21 -144
- package/dist/hooks/transaction/useEstimate.js.map +1 -1
- package/dist/hooks/transaction/useExecuteTransaction.d.ts +2 -2
- package/dist/hooks/transaction/useExecuteTransaction.js +13 -9
- package/dist/hooks/transaction/useExecuteTransaction.js.map +1 -1
- package/dist/hooks/transaction/useGetRoute.js +4 -5
- package/dist/hooks/transaction/useGetRoute.js.map +1 -1
- package/dist/hooks/wallet/useAddToken.js +9 -7
- package/dist/hooks/wallet/useAddToken.js.map +1 -1
- package/dist/hooks/wallet/useGnosisContext.js +1 -2
- package/dist/hooks/wallet/useGnosisContext.js.map +1 -1
- package/dist/hooks/wallet/useMultiChainWallet.js +11 -11
- package/dist/hooks/wallet/useMultiChainWallet.js.map +1 -1
- package/dist/hooks/wallet/useSigner.d.ts +8 -0
- package/dist/hooks/wallet/useSigner.js +21 -0
- package/dist/hooks/wallet/useSigner.js.map +1 -0
- package/dist/hooks/wallet/useWallet.d.ts +1 -1
- package/dist/hooks/wallet/useWallet.js +1 -1
- package/dist/hooks/wallet/useWallet.js.map +1 -1
- package/dist/provider/index.js +18 -61
- package/dist/provider/index.js.map +1 -1
- package/dist/services/external/ens.js +2 -2
- package/dist/services/external/ens.js.map +1 -1
- package/dist/services/external/rpcService.d.ts +0 -6
- package/dist/services/external/rpcService.js +10 -61
- package/dist/services/external/rpcService.js.map +1 -1
- package/dist/services/external/secretService.js +3 -3
- package/dist/services/external/secretService.js.map +1 -1
- package/dist/services/internal/assetsService.d.ts +2 -0
- package/dist/services/internal/assetsService.js +2 -0
- package/dist/services/internal/assetsService.js.map +1 -1
- package/dist/services/internal/estimateService.d.ts +125 -0
- package/dist/services/internal/estimateService.js +201 -0
- package/dist/services/internal/estimateService.js.map +1 -0
- package/dist/services/internal/numberService.d.ts +17 -2
- package/dist/services/internal/numberService.js +52 -5
- package/dist/services/internal/numberService.js.map +1 -1
- package/dist/services/internal/walletService.d.ts +2 -2
- package/dist/services/internal/walletService.js +21 -14
- package/dist/services/internal/walletService.js.map +1 -1
- package/dist/tests/estimateService.test.d.ts +1 -0
- package/dist/tests/estimateService.test.js +92 -0
- package/dist/tests/estimateService.test.js.map +1 -0
- package/dist/tests/walletService.test.js +18 -0
- package/dist/tests/walletService.test.js.map +1 -1
- package/package.json +14 -14
- package/dist/connectors/ExodusConnector.d.ts +0 -4
- package/dist/connectors/ExodusConnector.js +0 -6
- package/dist/connectors/ExodusConnector.js.map +0 -1
- package/dist/connectors/LedgerLiveConnector.d.ts +0 -40
- package/dist/connectors/LedgerLiveConnector.js +0 -177
- package/dist/connectors/LedgerLiveConnector.js.map +0 -1
- package/dist/contracts/typechain/factories/ERC20__factory.d.ts +0 -56
- package/dist/contracts/typechain/factories/ERC20__factory.js +0 -324
- package/dist/contracts/typechain/factories/ERC20__factory.js.map +0 -1
|
@@ -1,78 +1,56 @@
|
|
|
1
1
|
import { useQuery } from "@tanstack/react-query";
|
|
2
|
-
import {
|
|
3
|
-
import { useAccount } from "wagmi";
|
|
2
|
+
import { useAccount, usePublicClient } from "wagmi";
|
|
4
3
|
import { ChainType } from "@0xsquid/squid-types";
|
|
5
|
-
import { getProvider } from "@wagmi/core";
|
|
6
4
|
import { useMemo } from "react";
|
|
7
|
-
import {
|
|
5
|
+
import { useBalance } from "wagmi";
|
|
6
|
+
import { useCosmosContext } from "../../core";
|
|
8
7
|
import { nativeEvmTokenAddress } from "../../core/constants";
|
|
9
|
-
import { useCosmosContext } from "../../core/providers/CosmosProvider";
|
|
10
8
|
import { keys } from "../../core/queries/queries-keys";
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
13
|
-
import {
|
|
9
|
+
import { formatBNToReadable } from "../../services";
|
|
10
|
+
import { useCosmosForChain } from "../cosmos/useCosmosForChain";
|
|
11
|
+
import { useSwap } from "../swap/useSwap";
|
|
12
|
+
import { useAllTokensWithBalanceForChain } from "./useTokensWithBalance";
|
|
14
13
|
const defaultRefreshIntervalMs = 15000;
|
|
15
|
-
/**
|
|
16
|
-
* Fetches the balance of a user on an EVM chain.
|
|
17
|
-
* The balance fetching only happens if the chain and token data are available, the user is connected, and the chain type is EVM.
|
|
18
|
-
*
|
|
19
|
-
* @param {boolean} enabled A flag indicating whether the balance fetching is enabled or not. Defaults to true.
|
|
20
|
-
* @param {boolean} refreshIntervalMs The interval in milliseconds at which the balance is fetched. Defaults to 15000 ms.
|
|
21
|
-
*
|
|
22
|
-
* @returns {Object} The react query object containing the balance data (defaults to "0").
|
|
23
|
-
*
|
|
24
|
-
*/
|
|
25
14
|
export const useEvmBalance = ({ chain, token, userAddress, enabled = true, refreshIntervalMs = defaultRefreshIntervalMs, }) => {
|
|
26
15
|
const { isConnected } = useAccount();
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
try {
|
|
39
|
-
const tokenBalance = await srcTokenContract.balanceOf(userAddress);
|
|
40
|
-
return utils.formatUnits(tokenBalance, token?.decimals);
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
return "0";
|
|
44
|
-
}
|
|
45
|
-
}
|
|
46
|
-
else {
|
|
47
|
-
return "0";
|
|
48
|
-
}
|
|
49
|
-
}, {
|
|
50
|
-
enabled: isConnected && enabled,
|
|
51
|
-
refetchInterval: 0 ? false : refreshIntervalMs,
|
|
52
|
-
retry: 2,
|
|
16
|
+
const { data: nativeOrTokenBalance } = useBalance({
|
|
17
|
+
address: userAddress,
|
|
18
|
+
chainId: Number(chain?.chainId),
|
|
19
|
+
token: token?.address === nativeEvmTokenAddress
|
|
20
|
+
? undefined
|
|
21
|
+
: token?.address,
|
|
22
|
+
query: {
|
|
23
|
+
enabled: isConnected && enabled && !!userAddress,
|
|
24
|
+
refetchInterval: refreshIntervalMs,
|
|
25
|
+
retry: 2,
|
|
26
|
+
},
|
|
53
27
|
});
|
|
28
|
+
const balance = formatBNToReadable(nativeOrTokenBalance?.value ?? "0", nativeOrTokenBalance?.decimals ?? 0);
|
|
54
29
|
return { balance };
|
|
55
30
|
};
|
|
56
31
|
export const useNativeTokenBalanceFromChain = () => {
|
|
57
|
-
const {
|
|
32
|
+
const { address, isConnected } = useAccount();
|
|
58
33
|
const { fromChain } = useSwap();
|
|
59
34
|
const chainId = fromChain?.chainId;
|
|
60
|
-
const
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
35
|
+
const { data: nativeBalanceWei } = useBalance({
|
|
36
|
+
address: address,
|
|
37
|
+
chainId: Number(chainId),
|
|
38
|
+
query: {
|
|
39
|
+
enabled: isConnected,
|
|
40
|
+
},
|
|
66
41
|
});
|
|
67
|
-
|
|
42
|
+
const nativeBalanceFormatted = formatBNToReadable(nativeBalanceWei?.value ?? "0", nativeBalanceWei?.decimals ?? 0);
|
|
43
|
+
return { nativeBalanceWei: nativeBalanceWei?.value, nativeBalanceFormatted };
|
|
68
44
|
};
|
|
69
45
|
export const useNativeTokenBalanceDestinationChain = () => {
|
|
70
46
|
const { isConnected, address } = useAccount();
|
|
71
47
|
const { toChain } = useSwap();
|
|
72
48
|
const chainId = toChain?.chainId;
|
|
49
|
+
const publicClient = usePublicClient({ chainId: +(chainId ?? 1) });
|
|
73
50
|
const balance = useQuery(keys().nativeBalanceBigNumber(address, chainId), async () => {
|
|
74
|
-
const
|
|
75
|
-
|
|
51
|
+
const nativeCurrencyBalance = await publicClient?.getBalance({
|
|
52
|
+
address: address,
|
|
53
|
+
});
|
|
76
54
|
return nativeCurrencyBalance;
|
|
77
55
|
}, {
|
|
78
56
|
enabled: isConnected,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useBalance.js","sourceRoot":"","sources":["../../../src/hooks/tokens/useBalance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useBalance.js","sourceRoot":"","sources":["../../../src/hooks/tokens/useBalance.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAE,MAAM,uBAAuB,CAAC;AACjD,OAAO,EAAE,UAAU,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AAGpD,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,UAAU,EAAE,MAAM,OAAO,CAAC;AACnC,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,qBAAqB,EAAE,MAAM,sBAAsB,CAAC;AAC7D,OAAO,EAAE,IAAI,EAAE,MAAM,iCAAiC,CAAC;AACvD,OAAO,EAAE,kBAAkB,EAAE,MAAM,gBAAgB,CAAC;AACpD,OAAO,EAAE,iBAAiB,EAAE,MAA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iB,CAAC,SAAS,CAAC,CAAC;IACvD,MAAM,EAAE,cAAc,EAAE,GAAG,+BAA+B,CACxD,SAAS,EACT,WAAW,CACZ,CAAC;IAEF,MAAM,oBAAoB,GAAG,WAAW,IAAI,aAAa,CAAC;IAE1D,MAAM,OAAO,GAAG,OAAO,CAAC,GAAG,EAAE;QAC3B,IACE,oBAAoB;YACpB,SAAS;YACT,SAAS,CAAC,SAAS,KAAK,SAAS,CAAC,MAAM;YACxC,WAAW,EACX;YACA,OAAO,CACL,cAAc,EAAE,IAAI,EAAE,IAAI,CACxB,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,KAAK,KAAK,EAAE,OAAO,IAAI,CAAC,CAAC,OAAO,KAAK,SAAS,CAAC,OAAO,CACvE,EAAE,OAAO,IAAI,GAAG,CAClB,CAAC;SACH;QACD,OAAO,GAAG,CAAC;IACb,CAAC,EAAE;QACD,cAAc,CAAC,IAAI;QACnB,oBAAoB;QACpB,SAAS;QACT,KAAK;QACL,WAAW;KACZ,CAAC,CAAC;IACH,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC,CAAC"}
|
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { ChainType } from "@0xsquid/squid-types";
|
|
2
|
-
import { useMemo } from "react";
|
|
3
2
|
import { useCosmosBalance, useEvmBalance } from "../../hooks/tokens/useBalance";
|
|
4
3
|
/**
|
|
5
4
|
* Exposes a hook to get balance depending on the chain type (EVM or Cosmos)
|
|
@@ -30,7 +29,7 @@ export const useMultiChainBalance = ({ chain, token, userAddress, enabled = true
|
|
|
30
29
|
/**
|
|
31
30
|
* Get either EVM or cosmos Balance
|
|
32
31
|
*/
|
|
33
|
-
const balance =
|
|
32
|
+
const balance = chain?.chainType === ChainType.EVM ? evmBalance : cosmosBalance;
|
|
34
33
|
return { balance };
|
|
35
34
|
};
|
|
36
35
|
//# sourceMappingURL=useMultiChainBalance.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useMultiChainBalance.js","sourceRoot":"","sources":["../../../src/hooks/tokens/useMultiChainBalance.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"useMultiChainBalance.js","sourceRoot":"","sources":["../../../src/hooks/tokens/useMultiChainBalance.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,SAAS,EAAE,MAAM,sBAAsB,CAAC;AACjD,OAAO,EAAE,gBAAgB,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAEhF;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,oBAAoB,GAAG,CAAC,EACnC,KAAK,EACL,KAAK,EACL,WAAW,EACX,OAAO,GAAG,IAAI,GAMf,EAAE,EAAE;IACH;;OAEG;IACH,MAAM,EAAE,OAAO,EAAE,UAAU,EAAE,GAAG,aAAa,CAAC;QAC5C,KAAK,EAAE,KAAiB;QACxB,KAAK;QACL,WAAW,EAAE,WAAW,IAAI,EAAE;QAC9B,OAAO,EAAE,KAAK,EAAE,SAAS,KAAK,SAAS,CAAC,GAAG,IAAI,OAAO;KACvD,CAAC,CAAC;IAEH;;OAEG;IACH,MAAM,EAAE,OAAO,EAAE,aAAa,EAAE,GAAG,gBAAgB,CAAC;QAClD,SAAS,EAAE,KAAoB;QAC/B,KAAK;QACL,WAAW;KACZ,CAAC,CAAC;IAEH;;OAEG;IACH,MAAM,OAAO,GACX,KAAK,EAAE,SAAS,KAAK,SAAS,CAAC,GAAG,CAAC,CAAC,CAAC,UAAU,CAAC,CAAC,CAAC,aAAa,CAAC;IAElE,OAAO,EAAE,OAAO,EAAE,CAAC;AACrB,CAAC,CAAC"}
|
|
@@ -1,15 +1,17 @@
|
|
|
1
|
-
// import type { TransactionResponse } from "@0xsquid/squid-types/ethers";
|
|
2
1
|
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
|
|
3
|
-
import {
|
|
4
|
-
import { useSigner } from "wagmi";
|
|
2
|
+
import { usePublicClient, useWalletClient } from "wagmi";
|
|
5
3
|
import { getPrefixKey, keys, QueryKeys } from "../../core/queries/queries-keys";
|
|
4
|
+
import { nativeEvmTokenAddress } from "../../core/constants";
|
|
6
5
|
import { useSquidStore } from "../store/useSquidStore";
|
|
7
6
|
import { useSwap } from "../swap/useSwap";
|
|
8
7
|
import { useMultiChainWallet } from "../wallet/useMultiChainWallet";
|
|
8
|
+
import { useSigner } from "../wallet/useSigner";
|
|
9
9
|
export const useApproval = ({ squidRoute, }) => {
|
|
10
|
-
const
|
|
10
|
+
const { data: walletClient } = useWalletClient();
|
|
11
|
+
const publicClient = usePublicClient();
|
|
11
12
|
const queryClient = useQueryClient();
|
|
12
13
|
const squid = useSquidStore((state) => state.squid);
|
|
14
|
+
const { evmSigner } = useSigner();
|
|
13
15
|
const { fromChain, fromToken } = useSwap();
|
|
14
16
|
const { connectedAddress: { address: sourceUserAddress }, } = useMultiChainWallet(fromChain);
|
|
15
17
|
/**
|
|
@@ -20,6 +22,9 @@ export const useApproval = ({ squidRoute, }) => {
|
|
|
20
22
|
*/
|
|
21
23
|
const routeApproved = useQuery(keys().routeApproved(squidRoute), async () => {
|
|
22
24
|
try {
|
|
25
|
+
if (squidRoute?.params.fromToken === nativeEvmTokenAddress) {
|
|
26
|
+
return true;
|
|
27
|
+
}
|
|
23
28
|
const { isApproved } = await squid.isRouteApproved({
|
|
24
29
|
route: squidRoute,
|
|
25
30
|
sender: sourceUserAddress,
|
|
@@ -41,44 +46,54 @@ export const useApproval = ({ squidRoute, }) => {
|
|
|
41
46
|
// ```
|
|
42
47
|
// This is why we had an unpredictable gas error for USDT approvals
|
|
43
48
|
// So it needs a custom gas limit
|
|
49
|
+
//
|
|
50
|
+
// TODO: We're only doing it for USDT on Ethereum Mainnet, need to check if it's needed for USDT on other chains
|
|
44
51
|
const approveSpecificTokenToZero = async (token) => {
|
|
45
|
-
if (
|
|
52
|
+
if (walletClient &&
|
|
46
53
|
token.symbol.toLowerCase() === "usdt" &&
|
|
47
54
|
token.chainId === "1" &&
|
|
48
55
|
fromToken) {
|
|
49
|
-
const
|
|
50
|
-
const
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
56
|
+
const fromChainRouterAddress = fromChain?.squidContracts.squidRouter;
|
|
57
|
+
const allowance = await publicClient?.readContract({
|
|
58
|
+
address: token.address,
|
|
59
|
+
abi: [
|
|
60
|
+
{
|
|
61
|
+
constant: true,
|
|
62
|
+
inputs: [
|
|
63
|
+
{ name: "_owner", type: "address" },
|
|
64
|
+
{ name: "_spender", type: "address" },
|
|
65
|
+
],
|
|
66
|
+
name: "allowance",
|
|
67
|
+
outputs: [{ name: "remaining", type: "uint256" }],
|
|
68
|
+
type: "function",
|
|
69
|
+
},
|
|
70
|
+
],
|
|
71
|
+
functionName: "allowance",
|
|
72
|
+
args: [
|
|
73
|
+
sourceUserAddress,
|
|
74
|
+
fromChainRouterAddress,
|
|
75
|
+
],
|
|
76
|
+
});
|
|
77
|
+
if (allowance && BigInt(allowance) > 0n && publicClient) {
|
|
78
|
+
const { request } = await publicClient.simulateContract({
|
|
79
|
+
address: token.address,
|
|
80
|
+
abi: [
|
|
81
|
+
{
|
|
82
|
+
constant: false,
|
|
83
|
+
inputs: [
|
|
84
|
+
{ name: "_spender", type: "address" },
|
|
85
|
+
{ name: "_value", type: "uint256" },
|
|
86
|
+
],
|
|
87
|
+
name: "approve",
|
|
88
|
+
outputs: [],
|
|
89
|
+
type: "function",
|
|
90
|
+
},
|
|
68
91
|
],
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
},
|
|
75
|
-
], signer.data);
|
|
76
|
-
// Get approval amount from usdt contract
|
|
77
|
-
const allowance = (await usdtContract.allowance(sourceUserAddress, squidRouter));
|
|
78
|
-
// If allowance is greater than 0, set it to 0
|
|
79
|
-
if (allowance.gt(constants.Zero)) {
|
|
80
|
-
const approveTx = await usdtContract.approve(squidRouter, constants.Zero);
|
|
81
|
-
await approveTx.wait();
|
|
92
|
+
functionName: "approve",
|
|
93
|
+
args: [fromChainRouterAddress, 0n],
|
|
94
|
+
account: sourceUserAddress,
|
|
95
|
+
});
|
|
96
|
+
await walletClient.writeContract(request);
|
|
82
97
|
}
|
|
83
98
|
}
|
|
84
99
|
return true;
|
|
@@ -88,11 +103,14 @@ export const useApproval = ({ squidRoute, }) => {
|
|
|
88
103
|
*/
|
|
89
104
|
const approveRoute = useMutation(async () => {
|
|
90
105
|
try {
|
|
91
|
-
if (
|
|
106
|
+
if (fromToken?.address === nativeEvmTokenAddress) {
|
|
107
|
+
return true;
|
|
108
|
+
}
|
|
109
|
+
if (!!squidRoute && walletClient && fromToken && evmSigner) {
|
|
92
110
|
await approveSpecificTokenToZero(fromToken);
|
|
93
111
|
const approved = await squid?.approveRoute({
|
|
94
112
|
route: squidRoute,
|
|
95
|
-
signer:
|
|
113
|
+
signer: evmSigner,
|
|
96
114
|
// For security reasons, we don't want to allow infinite approvals in our frontends
|
|
97
115
|
executionSettings: {
|
|
98
116
|
infiniteApproval: false,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useApproval.js","sourceRoot":"","sources":["../../../src/hooks/transaction/useApproval.ts"],"names":[],"mappings":"AAAA,
|
|
1
|
+
{"version":3,"file":"useApproval.js","sourceRoot":"","sources":["../../../src/hooks/transaction/useApproval.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,cAAc,EAAE,MAAM,uBAAuB,CAAC;AAC9E,OAAO,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,OAAO,CAAC;AACzD,OAAO,EAAE,YAAY,EAAE,IA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AS,EAAE,GAAG,EAAE;YACd,WAAW,CAAC,iBAAiB,CAAC,YAAY,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC;QACvE,CAAC;KACF,CACF,CAAC;IAEF,OAAO;QACL,aAAa;QACb,YAAY;KACb,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
import { RouteResponse } from "@0xsquid/sdk/dist/types";
|
|
2
|
-
import type { FeeCost, GasCost } from "@0xsquid/squid-types";
|
|
3
|
-
import { BigNumber } from "ethers";
|
|
4
2
|
export declare const useEstimate: (squidRoute?: RouteResponse["route"]) => {
|
|
5
3
|
balanceFormatted: string;
|
|
6
4
|
totalWithRefundEstimate: {
|
|
7
5
|
totalAmount: string;
|
|
8
6
|
totalAmountUSD: number;
|
|
9
|
-
feeToken: import("@0xsquid/squid-types").Token | undefined;
|
|
7
|
+
feeToken: import("@0xsquid/squid-types/dist/tokens").Token | undefined;
|
|
10
8
|
};
|
|
11
9
|
proposedGasDestinationAmount: {
|
|
12
10
|
value: number;
|
|
@@ -14,29 +12,29 @@ export declare const useEstimate: (squidRoute?: RouteResponse["route"]) => {
|
|
|
14
12
|
};
|
|
15
13
|
swapAmountExceedsLimit: boolean;
|
|
16
14
|
enoughBalanceToSwap: boolean;
|
|
17
|
-
fromToken:
|
|
15
|
+
fromToken: any;
|
|
18
16
|
fromAmount: string | undefined;
|
|
19
17
|
fromAmountFormatted: string;
|
|
20
|
-
sourceChainNativeToken:
|
|
21
|
-
destChainNativeToken:
|
|
18
|
+
sourceChainNativeToken: any;
|
|
19
|
+
destChainNativeToken: any;
|
|
22
20
|
toAmountUSD: string | undefined;
|
|
23
21
|
toAmountUSDFloat: number;
|
|
24
22
|
exchangeRate: string;
|
|
25
23
|
toAmountMinUSD: string;
|
|
26
24
|
toAmountMin: string;
|
|
27
25
|
toAmount: string;
|
|
28
|
-
allFeeCosts: FeeCost[];
|
|
29
|
-
allGasCosts: GasCost[];
|
|
30
|
-
firstFeeCost: FeeCost | undefined;
|
|
31
|
-
firstGasCost: GasCost | undefined;
|
|
32
|
-
expressFeeCost: FeeCost | undefined;
|
|
33
|
-
integratorFeeCost: FeeCost | undefined;
|
|
34
|
-
expectedGasRefundCost:
|
|
26
|
+
allFeeCosts: import("@0xsquid/squid-types/dist/fees").FeeCost[];
|
|
27
|
+
allGasCosts: import("@0xsquid/squid-types/dist/fees").GasCost[];
|
|
28
|
+
firstFeeCost: import("@0xsquid/squid-types/dist/fees").FeeCost | undefined;
|
|
29
|
+
firstGasCost: import("@0xsquid/squid-types/dist/fees").GasCost | undefined;
|
|
30
|
+
expressFeeCost: import("@0xsquid/squid-types/dist/fees").FeeCost | undefined;
|
|
31
|
+
integratorFeeCost: import("@0xsquid/squid-types/dist/fees").FeeCost | undefined;
|
|
32
|
+
expectedGasRefundCost: bigint;
|
|
35
33
|
expectedGasRefundCostUSD: string;
|
|
36
34
|
sameTokenBetweenFees: boolean;
|
|
37
35
|
isFromTokenNative: boolean;
|
|
38
|
-
totalNativeFees:
|
|
39
|
-
totalFeesInNativeTokenPlusRatio:
|
|
36
|
+
totalNativeFees: bigint;
|
|
37
|
+
totalFeesInNativeTokenPlusRatio: bigint;
|
|
40
38
|
fromBalanceEnoughToSwap: boolean;
|
|
41
39
|
minAmountValueWarnMsg: string | undefined;
|
|
42
40
|
estimatedRouteDuration: {
|
|
@@ -1,131 +1,34 @@
|
|
|
1
|
-
import { ChainType } from "@0xsquid/squid-types";
|
|
2
|
-
import { BigNumber, constants } from "ethers";
|
|
3
|
-
import { formatUnits } from "ethers/lib/utils";
|
|
4
1
|
import { useMemo } from "react";
|
|
5
|
-
import {
|
|
6
|
-
import { useNativeTokenBalanceFromChain } from "../../hooks/tokens/useBalance";
|
|
2
|
+
import { limitTradeSizeUsd } from "../../core/constants";
|
|
7
3
|
import { useSquidTokens } from "../../hooks/tokens/useSquidTokens";
|
|
8
|
-
import {
|
|
9
|
-
import { formatUnitsRounded } from "../../services/internal/numberService";
|
|
4
|
+
import { calculateEstimateResults, calculateTotalWithRefundEstimate, getProposedGasDestinationAmount, } from "../../services/internal/estimateService";
|
|
10
5
|
import { useConfigStore } from "../store/useSquidStore";
|
|
11
6
|
import { useSwap } from "../swap/useSwap";
|
|
7
|
+
import { useNativeTokenBalanceFromChain } from "../tokens/useBalance";
|
|
12
8
|
import { useMultiChainBalance } from "../tokens/useMultiChainBalance";
|
|
13
9
|
import { useSingleTokenPrice } from "../tokens/useSingleTokenPrice";
|
|
14
10
|
import { useEstimateExpress } from "./useExpress";
|
|
15
11
|
export const useEstimate = (squidRoute) => {
|
|
16
12
|
const { collectFees, advanced } = useConfigStore((state) => state.config);
|
|
17
|
-
const {
|
|
13
|
+
const { nativeBalanceWei } = useNativeTokenBalanceFromChain();
|
|
18
14
|
const { tokens } = useSquidTokens();
|
|
19
15
|
const { fromChain, toChain } = useSwap();
|
|
20
16
|
const { expressActivatedUI } = useEstimateExpress();
|
|
21
|
-
const estimateResults = useMemo(() => {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
const destChainNativeToken = tokens.find((t) => t.symbol === toChain?.nativeCurrency.symbol &&
|
|
31
|
-
t.chainId == toChain?.chainId);
|
|
32
|
-
const toAmountUSD = squidRoute?.estimate?.toAmountUSD;
|
|
33
|
-
const toAmountUSDFloat = toAmountUSD
|
|
34
|
-
? parseFloat(toAmountUSD.replace(/,/g, ""))
|
|
35
|
-
: 0;
|
|
36
|
-
const exchangeRate = squidRoute?.estimate.exchangeRate ?? "0";
|
|
37
|
-
const toAmountMinUSD = squidRoute?.estimate.toAmountMinUSD ?? "0";
|
|
38
|
-
const toAmountMin = formatUnits(squidRoute?.estimate.toAmountMin ?? "0", squidRoute?.estimate.toToken.decimals);
|
|
39
|
-
const toAmount = formatUnitsRounded(squidRoute?.estimate.toAmount ?? "0", squidRoute?.estimate.toToken.decimals, 14);
|
|
40
|
-
const estimate = squidRoute?.estimate;
|
|
41
|
-
const allFeeCosts = estimate?.feeCosts ?? [];
|
|
42
|
-
const allGasCosts = estimate?.gasCosts ?? [];
|
|
43
|
-
const firstFeeCost = allFeeCosts.length > 0 ? allFeeCosts[0] : undefined;
|
|
44
|
-
const firstGasCost = allGasCosts.length > 0 ? allGasCosts[0] : undefined;
|
|
45
|
-
const expressFeeCost = allFeeCosts.find((f) => f.name === "Boost fee");
|
|
46
|
-
const integratorFeeCost = allFeeCosts.length > 0 && collectFees
|
|
47
|
-
? allFeeCosts.find((f) => f.name === "Integrator Fee")
|
|
48
|
-
: undefined;
|
|
49
|
-
const expectedGasRefundCost = BigNumber.from(firstFeeCost?.amount ?? "0")
|
|
50
|
-
.mul(fromChain?.chainType === ChainType.COSMOS ? 0 : gasRefundMultiplier)
|
|
51
|
-
.div(100);
|
|
52
|
-
const expectedGasRefundCostUSD = convertTokenAmountToUSD(formatUnits(expectedGasRefundCost, firstFeeCost?.token.decimals ?? "0"), firstFeeCost?.token.usdPrice ?? "0");
|
|
53
|
-
const sameTokenBetweenFees = firstFeeCost?.token.address === firstGasCost?.token.address &&
|
|
54
|
-
firstFeeCost?.token.chainId === firstGasCost?.token.chainId;
|
|
55
|
-
const isFromTokenNative = fromToken?.symbol === fromChain?.nativeCurrency.symbol;
|
|
56
|
-
const totalNativeFees = BigNumber.from(expressFeeCost?.amount ?? constants.Zero).add(sameTokenBetweenFees
|
|
57
|
-
? BigNumber.from(firstFeeCost?.amount ?? constants.Zero).add(BigNumber.from(firstGasCost?.amount ?? constants.Zero))
|
|
58
|
-
: BigNumber.from(firstGasCost?.amount ?? constants.Zero));
|
|
59
|
-
const totalFeesInNativeTokenPlusRatio = totalNativeFees.mul(105).div(100);
|
|
60
|
-
const fromBalanceEnoughToSwap = isFromTokenNative
|
|
61
|
-
? BigNumber.from(fromAmount ?? constants.Zero)
|
|
62
|
-
.add(totalFeesInNativeTokenPlusRatio)
|
|
63
|
-
.lte(nativeTokenBalanceFromChain.data ?? constants.Zero)
|
|
64
|
-
: totalFeesInNativeTokenPlusRatio.lte(nativeTokenBalanceFromChain.data ?? constants.Zero);
|
|
65
|
-
const minAmountValueWarnMsg = isFromTokenNative
|
|
66
|
-
? (() => {
|
|
67
|
-
const fromBalance = nativeTokenBalanceFromChain.data ?? constants.Zero;
|
|
68
|
-
const minAmount = fromBalance.sub(totalFeesInNativeTokenPlusRatio);
|
|
69
|
-
return minAmount.gt(constants.Zero)
|
|
70
|
-
? formatUnits(minAmount, sourceChainNativeToken?.decimals ?? 18)
|
|
71
|
-
: "0";
|
|
72
|
-
})()
|
|
73
|
-
: undefined;
|
|
74
|
-
const isSingleChainRoute = fromChain?.chainId === toChain?.chainId;
|
|
75
|
-
const estimatedRouteDuration = (() => {
|
|
76
|
-
const fallbackDuration = 20;
|
|
77
|
-
let durationInSeconds = fallbackDuration;
|
|
78
|
-
if (squidRoute?.estimate?.estimatedRouteDuration) {
|
|
79
|
-
durationInSeconds = squidRoute.estimate.estimatedRouteDuration;
|
|
80
|
-
}
|
|
81
|
-
else if (isSingleChainRoute) {
|
|
82
|
-
durationInSeconds = 1;
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
durationInSeconds = expressActivatedUI
|
|
86
|
-
? fromChain?.estimatedBoostRouteDuration ?? fallbackDuration
|
|
87
|
-
: fromChain?.estimatedRouteDuration ?? fallbackDuration;
|
|
88
|
-
}
|
|
89
|
-
return {
|
|
90
|
-
seconds: durationInSeconds,
|
|
91
|
-
format: (sTemplate = "s", mTemplate = "m", hTemplate = "h") => formatSeconds(durationInSeconds, sTemplate, mTemplate, hTemplate),
|
|
92
|
-
};
|
|
93
|
-
})();
|
|
94
|
-
return {
|
|
95
|
-
fromToken,
|
|
96
|
-
fromAmount,
|
|
97
|
-
fromAmountFormatted,
|
|
98
|
-
sourceChainNativeToken,
|
|
99
|
-
destChainNativeToken,
|
|
100
|
-
toAmountUSD,
|
|
101
|
-
toAmountUSDFloat,
|
|
102
|
-
exchangeRate,
|
|
103
|
-
toAmountMinUSD,
|
|
104
|
-
toAmountMin,
|
|
105
|
-
toAmount,
|
|
106
|
-
allFeeCosts,
|
|
107
|
-
allGasCosts,
|
|
108
|
-
firstFeeCost,
|
|
109
|
-
firstGasCost,
|
|
110
|
-
expressFeeCost,
|
|
111
|
-
integratorFeeCost,
|
|
112
|
-
expectedGasRefundCost,
|
|
113
|
-
expectedGasRefundCostUSD,
|
|
114
|
-
sameTokenBetweenFees,
|
|
115
|
-
isFromTokenNative,
|
|
116
|
-
totalNativeFees,
|
|
117
|
-
totalFeesInNativeTokenPlusRatio,
|
|
118
|
-
fromBalanceEnoughToSwap,
|
|
119
|
-
minAmountValueWarnMsg,
|
|
120
|
-
estimatedRouteDuration,
|
|
121
|
-
};
|
|
122
|
-
}, [
|
|
17
|
+
const estimateResults = useMemo(() => calculateEstimateResults({
|
|
18
|
+
squidRoute,
|
|
19
|
+
tokens,
|
|
20
|
+
fromChain,
|
|
21
|
+
toChain,
|
|
22
|
+
collectFees: !!collectFees && collectFees.fee > 0,
|
|
23
|
+
nativeTokenBalanceFromChainWei: nativeBalanceWei ?? BigInt("0"),
|
|
24
|
+
expressActivatedUI,
|
|
25
|
+
}), [
|
|
123
26
|
squidRoute,
|
|
124
27
|
tokens,
|
|
125
28
|
fromChain,
|
|
126
29
|
toChain,
|
|
127
30
|
collectFees,
|
|
128
|
-
|
|
31
|
+
nativeBalanceWei,
|
|
129
32
|
expressActivatedUI,
|
|
130
33
|
]);
|
|
131
34
|
const balanceFormatted = useMultiChainBalance({
|
|
@@ -135,39 +38,13 @@ export const useEstimate = (squidRoute) => {
|
|
|
135
38
|
enabled: !!squidRoute,
|
|
136
39
|
}).balance ?? "0";
|
|
137
40
|
const { getUSDValue } = useSingleTokenPrice(estimateResults.firstFeeCost?.token);
|
|
138
|
-
const totalWithRefundEstimate = useMemo(() =>
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
BN: BigNumber.from(expressFeeCost?.amount ?? "0"),
|
|
146
|
-
token: expressFeeCost?.token,
|
|
147
|
-
};
|
|
148
|
-
const totalBnbAmount = firstFee.BN.add(expressFee.BN).sub(expectedGasRefundCost);
|
|
149
|
-
const totalAmount = formatUnits(totalBnbAmount, firstFee.token?.decimals ?? 18);
|
|
150
|
-
const formattedRefundCost = formatUnits(expectedGasRefundCost, firstFeeCost?.token.decimals);
|
|
151
|
-
const formattedRefundCostUSD = getUSDValue(formattedRefundCost);
|
|
152
|
-
const totalAmountUSD = +(firstFeeCost?.amountUsd ?? 0) +
|
|
153
|
-
+(expressFeeCost?.amountUsd ?? 0) -
|
|
154
|
-
+(formattedRefundCostUSD ?? 0);
|
|
155
|
-
return { totalAmount, totalAmountUSD, feeToken: firstFeeCost?.token };
|
|
156
|
-
}, [estimateResults, getUSDValue]);
|
|
157
|
-
const proposedGasDestinationAmount = useMemo(() => {
|
|
158
|
-
const nativeToken = estimateResults.destChainNativeToken?.symbol;
|
|
159
|
-
const gasAmounts = {
|
|
160
|
-
GLMR: 5.289,
|
|
161
|
-
ETH: 0.0009,
|
|
162
|
-
AVAX: 0.115,
|
|
163
|
-
BNB: 0.00425,
|
|
164
|
-
FTM: 4.45,
|
|
165
|
-
CELO: 3.052,
|
|
166
|
-
KAVA: 2.339,
|
|
167
|
-
MATIC: 1.795,
|
|
168
|
-
};
|
|
169
|
-
return { value: gasAmounts[nativeToken ?? ""] ?? 0, currency: nativeToken };
|
|
170
|
-
}, [estimateResults.destChainNativeToken]);
|
|
41
|
+
const totalWithRefundEstimate = useMemo(() => calculateTotalWithRefundEstimate(estimateResults.firstFeeCost, estimateResults.expressFeeCost, estimateResults.expectedGasRefundCost, getUSDValue), [
|
|
42
|
+
estimateResults.firstFeeCost,
|
|
43
|
+
estimateResults.expressFeeCost,
|
|
44
|
+
estimateResults.expectedGasRefundCost,
|
|
45
|
+
getUSDValue,
|
|
46
|
+
]);
|
|
47
|
+
const proposedGasDestinationAmount = useMemo(() => getProposedGasDestinationAmount(estimateResults.destChainNativeToken?.symbol), [estimateResults.destChainNativeToken]);
|
|
171
48
|
const swapAmountExceedsLimit = advanced?.disableTradeLimit
|
|
172
49
|
? false
|
|
173
50
|
: estimateResults.toAmountUSDFloat > limitTradeSizeUsd;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"useEstimate.js","sourceRoot":"","sources":["../../../src/hooks/transaction/useEstimate.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"useEstimate.js","sourceRoot":"","sources":["../../../src/hooks/transaction/useEstimate.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAChC,OAAO,EAAE,iBAAiB,EAAE,MAAM,sBAAsB,CAAC;AACzD,OAAO,EAAE,cAAc,EAAE,MAAM,mCAAmC,CAAC;AACnE,OAAO,EACL,wBAAwB,EACxB,gCAAgC,EAChC,+BAA+B,GAChC,MAAM,yCAAyC,CAAC;AACjD,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AACxD,OAAO,EAAE,OAAO,EAAE,MAAM,iBAAiB,CAAC;AAC1C,OAAO,EAAE,8BAA8B,EAAE,MAAM,sBAAsB,CAAC;AACtE,OAAO,EAAE,oBAAoB,EAAE,MAAM,gCAAgC,CAAC;AACtE,OAAO,EAAE,mBAAmB,EAAE,MAAM,+BAA+B,CAAC;AACpE,OAAO,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAElD,MAAM,CAAC,MAAM,WAAW,GAAG,CAAC,UAAmC,EAAE,EAAE;IACjE,MAAM,EAAE,WAAW,EAAE,QAAQ,EAAE,GAAG,cAAc,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,CAAC,MAAM,CAAC,CAAC;IAE1E,MAAM,EAAE,gBAAgB,EAAE,GAAG,8BAA8B,EAAE,CAAC;IAC9D,MAAM,EAAE,MAAM,EAAE,GAAG,cAAc,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,CAAC,eAAe,CAAC,oBAAoB,CAAC,CACvC,CAAC;IAEF,MAAM,sBAAsB,GAAG,QAAQ,EAAE,iBAAiB;QACxD,CAAC,CAAC,KAAK;QACP,CAAC,CAAC,eAAe,CAAC,gBAAgB,GAAG,iBAAiB,CAAC;IAEzD,MAAM,mBAAmB,GACvB,CAAC,gBAAgB,IAAI,CAAC;QACtB,CAAC,gBAAgB,GAAG,CAAC,eAAe,CAAC,mBAAmB,CAAC;IAE3D,OAAO;QACL,GAAG,eAAe;QAClB,gBAAgB;QAChB,uBAAuB;QACvB,4BAA4B;QAC5B,sBAAsB;QACtB,mBAAmB;KACpB,CAAC;AACJ,CAAC,CAAC"}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { RouteResponse } from "@0xsquid/squid-types";
|
|
2
|
-
import
|
|
2
|
+
import { TransactionReceipt } from "ethers";
|
|
3
3
|
import type { TransactionParams } from "../../core/types/transaction";
|
|
4
4
|
declare enum ExecutionState {
|
|
5
5
|
NOT_ENOUGH_BALANCE = "NOT_ENOUGH_BALANCE",
|
|
@@ -7,7 +7,7 @@ declare enum ExecutionState {
|
|
|
7
7
|
READY = "READY"
|
|
8
8
|
}
|
|
9
9
|
export declare const useExecuteTransaction: (squidRoute?: RouteResponse["route"]) => {
|
|
10
|
-
swapQuery: import("@tanstack/react-query").UseMutationResult<boolean | TransactionReceipt, any, void, unknown>;
|
|
10
|
+
swapQuery: import("@tanstack/react-query").UseMutationResult<boolean | TransactionReceipt | null, any, void, unknown>;
|
|
11
11
|
currentTransaction: TransactionParams | undefined;
|
|
12
12
|
fromToken: import("@0xsquid/squid-types").Token | undefined;
|
|
13
13
|
toToken: import("@0xsquid/squid-types").Token | undefined;
|