@b3dotfun/sdk 0.0.62 → 0.0.63-alpha.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/cjs/anyspend/react/components/AnySpend.js +61 -23
- package/dist/cjs/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/cjs/anyspend/react/components/AnySpendCustomExactIn.d.ts +34 -0
- package/dist/cjs/anyspend/react/components/AnySpendCustomExactIn.js +275 -0
- package/dist/cjs/anyspend/react/components/AnySpendStakeB3.js +5 -4
- package/dist/cjs/anyspend/react/components/AnySpendStakeB3ExactIn.d.ts +9 -0
- package/dist/cjs/anyspend/react/components/AnySpendStakeB3ExactIn.js +288 -0
- package/dist/cjs/anyspend/react/components/AnySpendStakeUpsideExactIn.d.ts +11 -0
- package/dist/cjs/anyspend/react/components/AnySpendStakeUpsideExactIn.js +33 -0
- package/dist/cjs/anyspend/react/components/AnyspendDepositHype.js +4 -4
- package/dist/cjs/anyspend/react/components/common/CryptoPaySection.js +4 -6
- package/dist/cjs/anyspend/react/components/common/CryptoPaymentMethod.js +9 -17
- package/dist/cjs/anyspend/react/components/common/CryptoReceiveSection.d.ts +6 -1
- package/dist/cjs/anyspend/react/components/common/CryptoReceiveSection.js +11 -1
- package/dist/cjs/anyspend/react/components/common/OrderDetails.js +66 -147
- package/dist/cjs/anyspend/react/components/common/OrderDetailsCollapsible.js +2 -3
- package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.d.ts +2 -1
- package/dist/cjs/anyspend/react/components/common/OrderTokenAmount.js +39 -15
- package/dist/cjs/anyspend/react/components/common/PaySection.js +1 -1
- package/dist/cjs/anyspend/react/components/common/TokenBalance.js +1 -1
- package/dist/cjs/anyspend/react/components/index.d.ts +5 -1
- package/dist/cjs/anyspend/react/components/index.js +11 -3
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.d.ts +25 -3
- package/dist/cjs/anyspend/react/hooks/useAnyspendFlow.js +42 -19
- package/dist/cjs/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +116 -0
- package/dist/cjs/anyspend/react/hooks/useAnyspendQuote.js +1 -1
- package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +26 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +56 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/cjs/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +73 -0
- package/dist/cjs/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +14 -0
- package/dist/cjs/anyspend/react/hooks/useConnectedWalletDisplay.js +57 -0
- package/dist/cjs/anyspend/react/hooks/usePhantomTransfer.d.ts +36 -0
- package/dist/cjs/anyspend/react/hooks/usePhantomTransfer.js +211 -0
- package/dist/cjs/anyspend/types/api.d.ts +665 -3
- package/dist/cjs/anyspend/utils/orderPayload.js +4 -0
- package/dist/cjs/global-account/react/components/B3DynamicModal.js +10 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +3 -1
- package/dist/cjs/global-account/react/components/SignInWithB3/steps/LoginStep.js +2 -2
- package/dist/cjs/global-account/react/hooks/index.d.ts +2 -1
- package/dist/cjs/global-account/react/hooks/index.js +5 -3
- package/dist/cjs/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/cjs/global-account/react/hooks/useAuthentication.js +7 -2
- package/dist/cjs/global-account/react/hooks/useSimBalance.d.ts +1 -1
- package/dist/cjs/global-account/react/hooks/useSimBalance.js +6 -5
- package/dist/cjs/global-account/react/hooks/useTokenBalanceDirect.d.ts +12 -0
- package/dist/cjs/global-account/react/hooks/useTokenBalanceDirect.js +62 -0
- package/dist/cjs/global-account/react/hooks/useTokenFromUrl.js +4 -3
- package/dist/cjs/global-account/react/stores/useModalStore.d.ts +31 -1
- package/dist/esm/anyspend/react/components/AnySpend.js +62 -24
- package/dist/esm/anyspend/react/components/AnySpendCustom.js +3 -0
- package/dist/esm/anyspend/react/components/AnySpendCustomExactIn.d.ts +34 -0
- package/dist/esm/anyspend/react/components/AnySpendCustomExactIn.js +269 -0
- package/dist/esm/anyspend/react/components/AnySpendStakeB3.js +7 -6
- package/dist/esm/anyspend/react/components/AnySpendStakeB3ExactIn.d.ts +9 -0
- package/dist/esm/anyspend/react/components/AnySpendStakeB3ExactIn.js +285 -0
- package/dist/esm/anyspend/react/components/AnySpendStakeUpsideExactIn.d.ts +11 -0
- package/dist/esm/anyspend/react/components/AnySpendStakeUpsideExactIn.js +30 -0
- package/dist/esm/anyspend/react/components/AnyspendDepositHype.js +4 -4
- package/dist/esm/anyspend/react/components/common/CryptoPaySection.js +5 -7
- package/dist/esm/anyspend/react/components/common/CryptoPaymentMethod.js +9 -17
- package/dist/esm/anyspend/react/components/common/CryptoReceiveSection.d.ts +6 -1
- package/dist/esm/anyspend/react/components/common/CryptoReceiveSection.js +11 -1
- package/dist/esm/anyspend/react/components/common/OrderDetails.js +67 -148
- package/dist/esm/anyspend/react/components/common/OrderDetailsCollapsible.js +2 -3
- package/dist/esm/anyspend/react/components/common/OrderTokenAmount.d.ts +2 -1
- package/dist/esm/anyspend/react/components/common/OrderTokenAmount.js +40 -16
- package/dist/esm/anyspend/react/components/common/PaySection.js +1 -1
- package/dist/esm/anyspend/react/components/common/TokenBalance.js +2 -2
- package/dist/esm/anyspend/react/components/index.d.ts +5 -1
- package/dist/esm/anyspend/react/components/index.js +5 -1
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.d.ts +25 -3
- package/dist/esm/anyspend/react/hooks/useAnyspendFlow.js +42 -19
- package/dist/esm/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +116 -0
- package/dist/esm/anyspend/react/hooks/useAnyspendQuote.js +1 -1
- package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +26 -0
- package/dist/esm/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.js +53 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/esm/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.js +70 -0
- package/dist/esm/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +14 -0
- package/dist/esm/anyspend/react/hooks/useConnectedWalletDisplay.js +54 -0
- package/dist/esm/anyspend/react/hooks/usePhantomTransfer.d.ts +36 -0
- package/dist/esm/anyspend/react/hooks/usePhantomTransfer.js +208 -0
- package/dist/esm/anyspend/types/api.d.ts +665 -3
- package/dist/esm/anyspend/utils/orderPayload.js +4 -0
- package/dist/esm/global-account/react/components/B3DynamicModal.js +11 -2
- package/dist/esm/global-account/react/components/SignInWithB3/SignInWithB3Flow.js +3 -1
- package/dist/esm/global-account/react/components/SignInWithB3/steps/LoginStep.js +2 -2
- package/dist/esm/global-account/react/hooks/index.d.ts +2 -1
- package/dist/esm/global-account/react/hooks/index.js +2 -1
- package/dist/esm/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/esm/global-account/react/hooks/useAuthentication.js +7 -2
- package/dist/esm/global-account/react/hooks/useSimBalance.d.ts +1 -1
- package/dist/esm/global-account/react/hooks/useSimBalance.js +6 -5
- package/dist/esm/global-account/react/hooks/useTokenBalanceDirect.d.ts +12 -0
- package/dist/esm/global-account/react/hooks/useTokenBalanceDirect.js +59 -0
- package/dist/esm/global-account/react/hooks/useTokenFromUrl.js +4 -3
- package/dist/esm/global-account/react/stores/useModalStore.d.ts +31 -1
- package/dist/types/anyspend/react/components/AnySpendCustomExactIn.d.ts +34 -0
- package/dist/types/anyspend/react/components/AnySpendStakeB3ExactIn.d.ts +9 -0
- package/dist/types/anyspend/react/components/AnySpendStakeUpsideExactIn.d.ts +11 -0
- package/dist/types/anyspend/react/components/common/CryptoReceiveSection.d.ts +6 -1
- package/dist/types/anyspend/react/components/common/OrderTokenAmount.d.ts +2 -1
- package/dist/types/anyspend/react/components/index.d.ts +5 -1
- package/dist/types/anyspend/react/hooks/useAnyspendFlow.d.ts +25 -3
- package/dist/types/anyspend/react/hooks/useAnyspendOrderHistory.d.ts +116 -0
- package/dist/types/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.d.ts +26 -0
- package/dist/types/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.d.ts +10 -0
- package/dist/types/anyspend/react/hooks/useConnectedWalletDisplay.d.ts +14 -0
- package/dist/types/anyspend/react/hooks/usePhantomTransfer.d.ts +36 -0
- package/dist/types/anyspend/types/api.d.ts +665 -3
- package/dist/types/global-account/react/hooks/index.d.ts +2 -1
- package/dist/types/global-account/react/hooks/useAuthentication.d.ts +2 -2
- package/dist/types/global-account/react/hooks/useSimBalance.d.ts +1 -1
- package/dist/types/global-account/react/hooks/useTokenBalanceDirect.d.ts +12 -0
- package/dist/types/global-account/react/stores/useModalStore.d.ts +31 -1
- package/package.json +3 -2
- package/src/anyspend/react/components/AnySpend.tsx +73 -22
- package/src/anyspend/react/components/AnySpendCustom.tsx +4 -0
- package/src/anyspend/react/components/AnySpendCustomExactIn.tsx +595 -0
- package/src/anyspend/react/components/AnySpendStakeB3.tsx +8 -11
- package/src/anyspend/react/components/AnySpendStakeB3ExactIn.tsx +522 -0
- package/src/anyspend/react/components/AnySpendStakeUpsideExactIn.tsx +73 -0
- package/src/anyspend/react/components/AnyspendDepositHype.tsx +7 -3
- package/src/anyspend/react/components/common/CryptoPaySection.tsx +5 -7
- package/src/anyspend/react/components/common/CryptoPaymentMethod.tsx +9 -18
- package/src/anyspend/react/components/common/CryptoReceiveSection.tsx +22 -0
- package/src/anyspend/react/components/common/OrderDetails.tsx +76 -190
- package/src/anyspend/react/components/common/OrderDetailsCollapsible.tsx +2 -3
- package/src/anyspend/react/components/common/OrderTokenAmount.tsx +48 -17
- package/src/anyspend/react/components/common/PaySection.tsx +1 -0
- package/src/anyspend/react/components/common/TokenBalance.tsx +2 -2
- package/src/anyspend/react/components/index.ts +5 -1
- package/src/anyspend/react/hooks/useAnyspendFlow.ts +51 -18
- package/src/anyspend/react/hooks/useAnyspendQuote.ts +1 -1
- package/src/anyspend/react/hooks/useAutoSelectCryptoPaymentMethod.ts +72 -0
- package/src/anyspend/react/hooks/useAutoSetActiveWalletFromWagmi.ts +80 -0
- package/src/anyspend/react/hooks/useConnectedWalletDisplay.ts +69 -0
- package/src/anyspend/react/hooks/usePhantomTransfer.ts +301 -0
- package/src/anyspend/types/api.ts +669 -1
- package/src/anyspend/utils/orderPayload.ts +5 -1
- package/src/global-account/react/components/B3DynamicModal.tsx +11 -1
- package/src/global-account/react/components/SignInWithB3/SignInWithB3Flow.tsx +3 -1
- package/src/global-account/react/components/SignInWithB3/steps/LoginStep.tsx +2 -2
- package/src/global-account/react/hooks/index.ts +2 -1
- package/src/global-account/react/hooks/useAuthentication.ts +10 -2
- package/src/global-account/react/hooks/useSimBalance.ts +6 -5
- package/src/global-account/react/hooks/useTokenBalanceDirect.tsx +84 -0
- package/src/global-account/react/hooks/useTokenFromUrl.tsx +6 -5
- package/src/global-account/react/stores/useModalStore.ts +34 -0
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.usePhantomTransfer = usePhantomTransfer;
|
|
4
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
5
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
6
|
+
const react_1 = require("react");
|
|
7
|
+
const sonner_1 = require("sonner");
|
|
8
|
+
/**
|
|
9
|
+
* Custom hook for handling Phantom wallet transfers on Solana.
|
|
10
|
+
* Supports both native SOL and SPL token transfers with automatic priority fee calculation.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* ```tsx
|
|
14
|
+
* const { initiateTransfer, isPhantomAvailable } = usePhantomTransfer();
|
|
15
|
+
*
|
|
16
|
+
* await initiateTransfer({
|
|
17
|
+
* amountLamports: "1000000000", // 1 SOL
|
|
18
|
+
* tokenAddress: "11111111111111111111111111111111",
|
|
19
|
+
* recipientAddress: "..."
|
|
20
|
+
* });
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
function usePhantomTransfer({ rpcEndpoint } = {}) {
|
|
24
|
+
// Default RPC endpoint
|
|
25
|
+
const defaultRpcEndpoint = "https://mainnet.helius-rpc.com/?api-key=efafd9b3-1807-4cf8-8aa4-3d984f56d8fb";
|
|
26
|
+
const effectiveRpcEndpoint = rpcEndpoint || defaultRpcEndpoint;
|
|
27
|
+
// Check for Phantom wallet availability
|
|
28
|
+
const isPhantomMobile = (0, react_1.useMemo)(() => navigator.userAgent.includes("Phantom"), []);
|
|
29
|
+
const isPhantomBrowser = (0, react_1.useMemo)(() => window.phantom?.solana?.isPhantom, []);
|
|
30
|
+
const isPhantomAvailable = isPhantomMobile || isPhantomBrowser;
|
|
31
|
+
/**
|
|
32
|
+
* Get the connected Phantom wallet address if available
|
|
33
|
+
*/
|
|
34
|
+
const getConnectedAddress = (0, react_1.useCallback)(() => {
|
|
35
|
+
const phantom = window.phantom?.solana;
|
|
36
|
+
if (phantom?.isConnected && phantom?.publicKey) {
|
|
37
|
+
return phantom.publicKey.toString();
|
|
38
|
+
}
|
|
39
|
+
return null;
|
|
40
|
+
}, []);
|
|
41
|
+
/**
|
|
42
|
+
* Calculate optimal priority fee based on recent network activity
|
|
43
|
+
*/
|
|
44
|
+
const calculatePriorityFee = (0, react_1.useCallback)(async (connection, fromPubkey) => {
|
|
45
|
+
let priorityFee = 10000; // Default fallback (10,000 micro-lamports)
|
|
46
|
+
try {
|
|
47
|
+
const recentFees = await connection.getRecentPrioritizationFees({
|
|
48
|
+
lockedWritableAccounts: [fromPubkey],
|
|
49
|
+
});
|
|
50
|
+
if (recentFees && recentFees.length > 0) {
|
|
51
|
+
// Use 75th percentile of recent fees for good priority
|
|
52
|
+
const sortedFees = recentFees.map(fee => fee.prioritizationFee).sort((a, b) => a - b);
|
|
53
|
+
const percentile75Index = Math.floor(sortedFees.length * 0.75);
|
|
54
|
+
priorityFee = Math.max(sortedFees[percentile75Index] || 10000, 10000);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
catch (feeError) {
|
|
58
|
+
console.warn("Failed to fetch recent priority fees, using default:", feeError);
|
|
59
|
+
}
|
|
60
|
+
return priorityFee;
|
|
61
|
+
}, []);
|
|
62
|
+
/**
|
|
63
|
+
* Create a native SOL transfer transaction with priority fees
|
|
64
|
+
*/
|
|
65
|
+
const createNativeTransferTransaction = (0, react_1.useCallback)(async (_connection, fromPubkey, toPubkey, amount, priorityFee) => {
|
|
66
|
+
const computeUnitLimit = 1000; // SOL transfer + compute budget instructions need ~600-800 CU
|
|
67
|
+
const computeUnitPrice = Math.min(priorityFee, 100000); // Cap at 100k micro-lamports for safety
|
|
68
|
+
const transaction = new web3_js_1.Transaction()
|
|
69
|
+
.add(
|
|
70
|
+
// Set compute unit limit first (must come before other instructions)
|
|
71
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
72
|
+
units: computeUnitLimit,
|
|
73
|
+
}))
|
|
74
|
+
.add(
|
|
75
|
+
// Set priority fee
|
|
76
|
+
web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
77
|
+
microLamports: computeUnitPrice,
|
|
78
|
+
}))
|
|
79
|
+
.add(
|
|
80
|
+
// Actual transfer instruction
|
|
81
|
+
web3_js_1.SystemProgram.transfer({
|
|
82
|
+
fromPubkey,
|
|
83
|
+
toPubkey,
|
|
84
|
+
lamports: Number(amount),
|
|
85
|
+
}));
|
|
86
|
+
console.log(`Using priority fee: ${computeUnitPrice} micro-lamports per CU, limit: ${computeUnitLimit} CU`);
|
|
87
|
+
return transaction;
|
|
88
|
+
}, []);
|
|
89
|
+
/**
|
|
90
|
+
* Create an SPL token transfer transaction with priority fees
|
|
91
|
+
*/
|
|
92
|
+
const createSPLTransferTransaction = (0, react_1.useCallback)(async (connection, fromPubkey, toPubkey, mintPubkey, amount, priorityFee) => {
|
|
93
|
+
// Get associated token accounts
|
|
94
|
+
const fromTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(mintPubkey, fromPubkey);
|
|
95
|
+
const toTokenAccount = (0, spl_token_1.getAssociatedTokenAddressSync)(mintPubkey, toPubkey);
|
|
96
|
+
// Check if destination token account exists
|
|
97
|
+
const toTokenAccountInfo = await connection.getAccountInfo(toTokenAccount);
|
|
98
|
+
const needsDestinationAccount = !toTokenAccountInfo;
|
|
99
|
+
// Get mint info to determine decimals
|
|
100
|
+
const mintInfo = await connection.getParsedAccountInfo(mintPubkey);
|
|
101
|
+
const decimals = mintInfo.value?.data?.parsed?.info?.decimals || 9;
|
|
102
|
+
// SPL transfers need more compute units than SOL transfers
|
|
103
|
+
// Add extra CU if we need to create destination account
|
|
104
|
+
const computeUnitLimit = needsDestinationAccount ? 40000 : 20000;
|
|
105
|
+
const computeUnitPrice = Math.min(priorityFee, 100000);
|
|
106
|
+
// Create transfer instruction
|
|
107
|
+
const transferInstruction = (0, spl_token_1.createTransferCheckedInstruction)(fromTokenAccount, mintPubkey, toTokenAccount, fromPubkey, Number(amount), decimals);
|
|
108
|
+
const transaction = new web3_js_1.Transaction()
|
|
109
|
+
.add(web3_js_1.ComputeBudgetProgram.setComputeUnitLimit({
|
|
110
|
+
units: computeUnitLimit,
|
|
111
|
+
}))
|
|
112
|
+
.add(web3_js_1.ComputeBudgetProgram.setComputeUnitPrice({
|
|
113
|
+
microLamports: computeUnitPrice,
|
|
114
|
+
}));
|
|
115
|
+
// Add create destination account instruction if needed
|
|
116
|
+
if (needsDestinationAccount) {
|
|
117
|
+
transaction.add((0, spl_token_1.createAssociatedTokenAccountInstruction)(fromPubkey, // payer
|
|
118
|
+
toTokenAccount, // ata
|
|
119
|
+
toPubkey, // owner
|
|
120
|
+
mintPubkey));
|
|
121
|
+
}
|
|
122
|
+
// Add the transfer instruction
|
|
123
|
+
transaction.add(transferInstruction);
|
|
124
|
+
console.log(`SPL Token transfer: ${computeUnitPrice} micro-lamports per CU, limit: ${computeUnitLimit} CU, creating destination: ${needsDestinationAccount}`);
|
|
125
|
+
return transaction;
|
|
126
|
+
}, []);
|
|
127
|
+
/**
|
|
128
|
+
* Initiate a Phantom wallet transfer for SOL or SPL tokens
|
|
129
|
+
*/
|
|
130
|
+
const initiateTransfer = (0, react_1.useCallback)(async ({ amountLamports, tokenAddress, recipientAddress }) => {
|
|
131
|
+
try {
|
|
132
|
+
// Step 1: Check if Phantom is installed
|
|
133
|
+
if (!isPhantomAvailable) {
|
|
134
|
+
sonner_1.toast.error("Phantom wallet not installed. Please install Phantom wallet to continue.");
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
// Step 2: Ensure Phantom is connected/unlocked
|
|
138
|
+
const phantom = window.phantom?.solana;
|
|
139
|
+
if (!phantom) {
|
|
140
|
+
sonner_1.toast.error("Phantom wallet not accessible");
|
|
141
|
+
return;
|
|
142
|
+
}
|
|
143
|
+
// Connect and unlock wallet if needed
|
|
144
|
+
let publicKey;
|
|
145
|
+
try {
|
|
146
|
+
const connection = await phantom.connect();
|
|
147
|
+
publicKey = connection.publicKey;
|
|
148
|
+
}
|
|
149
|
+
catch (connectError) {
|
|
150
|
+
sonner_1.toast.error("Failed to connect to Phantom wallet");
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
// Step 3: Setup connection and public keys
|
|
154
|
+
const connection = new web3_js_1.Connection(effectiveRpcEndpoint);
|
|
155
|
+
const fromPubkey = new web3_js_1.PublicKey(publicKey.toString());
|
|
156
|
+
const toPubkey = new web3_js_1.PublicKey(recipientAddress);
|
|
157
|
+
const amount = BigInt(amountLamports);
|
|
158
|
+
// Step 4: Calculate optimal priority fee
|
|
159
|
+
const priorityFee = await calculatePriorityFee(connection, fromPubkey);
|
|
160
|
+
// Step 5: Create transaction based on token type
|
|
161
|
+
let transaction;
|
|
162
|
+
if (tokenAddress === "11111111111111111111111111111111") {
|
|
163
|
+
// Native SOL transfer
|
|
164
|
+
transaction = await createNativeTransferTransaction(connection, fromPubkey, toPubkey, amount, priorityFee);
|
|
165
|
+
}
|
|
166
|
+
else {
|
|
167
|
+
// SPL Token transfer
|
|
168
|
+
const mintPubkey = new web3_js_1.PublicKey(tokenAddress);
|
|
169
|
+
transaction = await createSPLTransferTransaction(connection, fromPubkey, toPubkey, mintPubkey, amount, priorityFee);
|
|
170
|
+
}
|
|
171
|
+
// Step 6: Get latest blockhash and set fee payer
|
|
172
|
+
const { blockhash } = await connection.getLatestBlockhash("confirmed");
|
|
173
|
+
transaction.recentBlockhash = blockhash;
|
|
174
|
+
transaction.feePayer = fromPubkey;
|
|
175
|
+
// Step 7: Sign and send transaction
|
|
176
|
+
const signedTransaction = await phantom.signAndSendTransaction(transaction);
|
|
177
|
+
sonner_1.toast.success(`Transaction successful! Signature: ${signedTransaction.signature}`);
|
|
178
|
+
console.log("Transaction sent with priority fees. Signature:", signedTransaction.signature);
|
|
179
|
+
}
|
|
180
|
+
catch (error) {
|
|
181
|
+
console.error("Transfer error:", error);
|
|
182
|
+
const errorMessage = error instanceof Error ? error.message : String(error);
|
|
183
|
+
if (errorMessage.includes("User rejected")) {
|
|
184
|
+
sonner_1.toast.error("Transaction was cancelled by user");
|
|
185
|
+
}
|
|
186
|
+
else if (errorMessage.includes("insufficient")) {
|
|
187
|
+
sonner_1.toast.error("Insufficient balance for this transaction");
|
|
188
|
+
}
|
|
189
|
+
else if (errorMessage.includes("blockhash not found")) {
|
|
190
|
+
sonner_1.toast.error("Network congestion detected. Please try again in a moment.");
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
sonner_1.toast.error(`Transfer failed: ${errorMessage}`);
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}, [
|
|
197
|
+
isPhantomAvailable,
|
|
198
|
+
effectiveRpcEndpoint,
|
|
199
|
+
calculatePriorityFee,
|
|
200
|
+
createNativeTransferTransaction,
|
|
201
|
+
createSPLTransferTransaction,
|
|
202
|
+
]);
|
|
203
|
+
return {
|
|
204
|
+
/** Function to initiate a transfer */
|
|
205
|
+
initiateTransfer,
|
|
206
|
+
/** Whether Phantom wallet is available (installed) */
|
|
207
|
+
isPhantomAvailable,
|
|
208
|
+
/** Get the currently connected Phantom wallet address */
|
|
209
|
+
getConnectedAddress,
|
|
210
|
+
};
|
|
211
|
+
}
|