@b3dotfun/sdk 0.0.65-alpha.0 → 0.0.65-alpha.2
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/cjs/anyspend/react/components/AnySpend.js +38 -24
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +22 -9
- package/dist/cjs/anyspend/react/components/AnySpendCustomExactIn.js +7 -7
- package/dist/cjs/anyspend/react/components/AnyspendDepositHype.js +5 -5
- package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.js +2 -10
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +16 -10
- package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
- package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +13 -9
- package/dist/cjs/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
- package/dist/cjs/anyspend/react/hooks/useCryptoPaymentMethodState.js +51 -0
- package/dist/cjs/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +6 -14
- package/dist/esm/anyspend/react/components/AnySpend.js +38 -24
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +22 -9
- package/dist/esm/anyspend/react/components/AnySpendCustomExactIn.js +7 -7
- package/dist/esm/anyspend/react/components/AnyspendDepositHype.js +5 -5
- package/dist/esm/anyspend/react/components/common/OrderTokenAmount.js +2 -10
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +16 -10
- package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
- package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +13 -9
- package/dist/esm/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
- package/dist/esm/anyspend/react/hooks/useCryptoPaymentMethodState.js +48 -0
- package/dist/esm/global-account/react/hooks/useUnifiedChainSwitchAndExecute.js +6 -14
- package/dist/types/anyspend/react/hooks/useAnyspendFlow.d.ts +5 -1
- package/dist/types/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +7 -5
- package/dist/types/anyspend/react/hooks/useCryptoPaymentMethodState.d.ts +42 -0
- package/package.json +1 -1
- package/src/anyspend/react/components/AnySpend.tsx +50 -29
- package/src/anyspend/react/components/AnySpendCustom.tsx +28 -15
- package/src/anyspend/react/components/AnySpendCustomExactIn.tsx +8 -7
- package/src/anyspend/react/components/AnyspendDepositHype.tsx +5 -4
- package/src/anyspend/react/components/common/OrderTokenAmount.tsx +2 -13
- package/src/anyspend/react/hooks/useAnyspendFlow.ts +24 -12
- package/src/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.ts +20 -12
- package/src/anyspend/react/hooks/useCryptoPaymentMethodState.ts +71 -0
- package/src/global-account/react/hooks/useUnifiedChainSwitchAndExecute.ts +6 -12
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
2
|
+
import { CryptoPaymentMethodType } from "../components/common/CryptoPaymentMethod";
|
|
3
|
+
|
|
4
|
+
interface UseCryptoPaymentMethodStateResult {
|
|
5
|
+
/** Auto-selected payment method based on balance */
|
|
6
|
+
cryptoPaymentMethod: CryptoPaymentMethodType;
|
|
7
|
+
/** Function to update the auto-selected payment method */
|
|
8
|
+
setCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
|
|
9
|
+
/** User explicitly selected payment method (NONE means no explicit selection) */
|
|
10
|
+
selectedCryptoPaymentMethod: CryptoPaymentMethodType;
|
|
11
|
+
/** Function to update the user-selected payment method */
|
|
12
|
+
setSelectedCryptoPaymentMethod: (method: CryptoPaymentMethodType) => void;
|
|
13
|
+
/** Effective payment method (user selection takes priority over auto-selection) */
|
|
14
|
+
effectiveCryptoPaymentMethod: CryptoPaymentMethodType;
|
|
15
|
+
/** Reset both payment method states to NONE */
|
|
16
|
+
resetPaymentMethods: () => void;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* Custom hook to manage crypto payment method state with dual-state system:
|
|
21
|
+
*
|
|
22
|
+
* - `cryptoPaymentMethod`: Auto-selected based on wallet availability and balance
|
|
23
|
+
* - `selectedCryptoPaymentMethod`: Explicitly selected by user
|
|
24
|
+
* - `effectiveCryptoPaymentMethod`: User selection takes priority over auto-selection
|
|
25
|
+
*
|
|
26
|
+
* This allows automatic payment method suggestions while respecting explicit user choices.
|
|
27
|
+
*
|
|
28
|
+
* @example
|
|
29
|
+
* ```tsx
|
|
30
|
+
* const {
|
|
31
|
+
* cryptoPaymentMethod,
|
|
32
|
+
* setCryptoPaymentMethod,
|
|
33
|
+
* selectedCryptoPaymentMethod,
|
|
34
|
+
* setSelectedCryptoPaymentMethod,
|
|
35
|
+
* effectiveCryptoPaymentMethod,
|
|
36
|
+
* resetPaymentMethods
|
|
37
|
+
* } = useCryptoPaymentMethodState();
|
|
38
|
+
*
|
|
39
|
+
* // Use effectiveCryptoPaymentMethod for display
|
|
40
|
+
* // Use setSelectedCryptoPaymentMethod when user explicitly selects
|
|
41
|
+
* // Call resetPaymentMethods when switching tabs or going back
|
|
42
|
+
* ```
|
|
43
|
+
*/
|
|
44
|
+
export function useCryptoPaymentMethodState(): UseCryptoPaymentMethodStateResult {
|
|
45
|
+
// cryptoPaymentMethod: auto-selected based on balance
|
|
46
|
+
const [cryptoPaymentMethod, setCryptoPaymentMethod] = useState<CryptoPaymentMethodType>(CryptoPaymentMethodType.NONE);
|
|
47
|
+
|
|
48
|
+
// selectedCryptoPaymentMethod: explicitly selected by user (NONE means no explicit selection)
|
|
49
|
+
const [selectedCryptoPaymentMethod, setSelectedCryptoPaymentMethod] = useState<CryptoPaymentMethodType>(
|
|
50
|
+
CryptoPaymentMethodType.NONE,
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
// The effective payment method (user selection takes priority over auto-selection)
|
|
54
|
+
const effectiveCryptoPaymentMethod =
|
|
55
|
+
selectedCryptoPaymentMethod !== CryptoPaymentMethodType.NONE ? selectedCryptoPaymentMethod : cryptoPaymentMethod;
|
|
56
|
+
|
|
57
|
+
// Helper function to reset both states
|
|
58
|
+
const resetPaymentMethods = () => {
|
|
59
|
+
setCryptoPaymentMethod(CryptoPaymentMethodType.NONE);
|
|
60
|
+
setSelectedCryptoPaymentMethod(CryptoPaymentMethodType.NONE);
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
return {
|
|
64
|
+
cryptoPaymentMethod,
|
|
65
|
+
setCryptoPaymentMethod,
|
|
66
|
+
selectedCryptoPaymentMethod,
|
|
67
|
+
setSelectedCryptoPaymentMethod,
|
|
68
|
+
effectiveCryptoPaymentMethod,
|
|
69
|
+
resetPaymentMethods,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
@@ -6,7 +6,6 @@ import invariant from "invariant";
|
|
|
6
6
|
import { useCallback, useState } from "react";
|
|
7
7
|
import { toast } from "sonner";
|
|
8
8
|
import { prepareTransaction, sendTransaction as twSendTransaction } from "thirdweb";
|
|
9
|
-
import { useActiveWallet } from "thirdweb/react";
|
|
10
9
|
import { isAddress } from "viem";
|
|
11
10
|
import { useSwitchChain } from "wagmi";
|
|
12
11
|
import { useB3 } from "../components";
|
|
@@ -29,7 +28,6 @@ invariant(partnerId, "Partner ID is required");
|
|
|
29
28
|
export function useUnifiedChainSwitchAndExecute() {
|
|
30
29
|
const { switchChainAsync } = useSwitchChain();
|
|
31
30
|
const [isSwitchingOrExecuting, setIsSwitchingOrExecuting] = useState(false);
|
|
32
|
-
const activeWallet = useActiveWallet();
|
|
33
31
|
|
|
34
32
|
const { isActiveSmartWallet, isActiveEOAWallet, connectedEOAWallet } = useAccountWallet();
|
|
35
33
|
const { account: aaAccount } = useB3();
|
|
@@ -49,12 +47,12 @@ export function useUnifiedChainSwitchAndExecute() {
|
|
|
49
47
|
return;
|
|
50
48
|
}
|
|
51
49
|
|
|
52
|
-
const currentChainId =
|
|
50
|
+
const currentChainId = connectedEOAWallet?.getChain()?.id;
|
|
53
51
|
const onCorrectChain = currentChainId === targetChainId;
|
|
54
52
|
|
|
55
53
|
// Helper function to execute the transaction
|
|
56
54
|
const executeTransaction = async (): Promise<string> => {
|
|
57
|
-
const signer =
|
|
55
|
+
const signer = connectedEOAWallet?.getAccount();
|
|
58
56
|
if (!signer) {
|
|
59
57
|
throw new Error("No account connected");
|
|
60
58
|
}
|
|
@@ -62,7 +60,7 @@ export function useUnifiedChainSwitchAndExecute() {
|
|
|
62
60
|
// Coinbase Smart Wallet specific chain switching (different behavior from other wallets)
|
|
63
61
|
const walletChain = connectedEOAWallet.getChain();
|
|
64
62
|
if (walletChain?.id !== targetChainId) {
|
|
65
|
-
|
|
63
|
+
connectedEOAWallet?.switchChain(getThirdwebChain(targetChainId));
|
|
66
64
|
}
|
|
67
65
|
|
|
68
66
|
invariant(isAddress(params.to), "params.to is not a valid address");
|
|
@@ -121,7 +119,7 @@ export function useUnifiedChainSwitchAndExecute() {
|
|
|
121
119
|
setIsSwitchingOrExecuting(false);
|
|
122
120
|
}
|
|
123
121
|
},
|
|
124
|
-
[connectedEOAWallet,
|
|
122
|
+
[connectedEOAWallet, switchChainAsync],
|
|
125
123
|
);
|
|
126
124
|
|
|
127
125
|
// Handle AA wallet transaction (no chain switch needed for AA)
|
|
@@ -186,14 +184,10 @@ export function useUnifiedChainSwitchAndExecute() {
|
|
|
186
184
|
// Check which wallet type is active
|
|
187
185
|
if (isActiveSmartWallet) {
|
|
188
186
|
return handleAASendTransaction(targetChainId, params);
|
|
189
|
-
} else if (isActiveEOAWallet) {
|
|
190
|
-
return handleEOASwitchChainAndSendTransaction(targetChainId, params);
|
|
191
|
-
} else {
|
|
192
|
-
toast.error("No wallet connected");
|
|
193
|
-
return undefined;
|
|
194
187
|
}
|
|
188
|
+
return handleEOASwitchChainAndSendTransaction(targetChainId, params);
|
|
195
189
|
},
|
|
196
|
-
[isActiveSmartWallet,
|
|
190
|
+
[isActiveSmartWallet, handleAASendTransaction, handleEOASwitchChainAndSendTransaction],
|
|
197
191
|
);
|
|
198
192
|
|
|
199
193
|
return {
|