@silentswap/react 0.0.41
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/README.md +48 -0
- package/dist/contexts/AssetsContext.d.ts +24 -0
- package/dist/contexts/AssetsContext.js +83 -0
- package/dist/contexts/BalancesContext.d.ts +28 -0
- package/dist/contexts/BalancesContext.js +533 -0
- package/dist/contexts/OrdersContext.d.ts +53 -0
- package/dist/contexts/OrdersContext.js +240 -0
- package/dist/contexts/PricesContext.d.ts +12 -0
- package/dist/contexts/PricesContext.js +109 -0
- package/dist/contexts/SilentSwapContext.d.ts +58 -0
- package/dist/contexts/SilentSwapContext.js +205 -0
- package/dist/hooks/silent/orderTrackingWebSocketManager.d.ts +48 -0
- package/dist/hooks/silent/orderTrackingWebSocketManager.js +284 -0
- package/dist/hooks/silent/solana-transaction.d.ts +60 -0
- package/dist/hooks/silent/solana-transaction.js +236 -0
- package/dist/hooks/silent/useAuth.d.ts +90 -0
- package/dist/hooks/silent/useAuth.js +269 -0
- package/dist/hooks/silent/useBridgeExecution.d.ts +39 -0
- package/dist/hooks/silent/useBridgeExecution.js +877 -0
- package/dist/hooks/silent/useOrderSigning.d.ts +34 -0
- package/dist/hooks/silent/useOrderSigning.js +133 -0
- package/dist/hooks/silent/useOrderTracking.d.ts +174 -0
- package/dist/hooks/silent/useOrderTracking.js +524 -0
- package/dist/hooks/silent/useQuoteCalculation.d.ts +50 -0
- package/dist/hooks/silent/useQuoteCalculation.js +331 -0
- package/dist/hooks/silent/useQuoteFetching.d.ts +18 -0
- package/dist/hooks/silent/useQuoteFetching.js +54 -0
- package/dist/hooks/silent/useRefund.d.ts +26 -0
- package/dist/hooks/silent/useRefund.js +134 -0
- package/dist/hooks/silent/useSilentClient.d.ts +16 -0
- package/dist/hooks/silent/useSilentClient.js +32 -0
- package/dist/hooks/silent/useSilentOrders.d.ts +174 -0
- package/dist/hooks/silent/useSilentOrders.js +73 -0
- package/dist/hooks/silent/useSilentQuote.d.ts +88 -0
- package/dist/hooks/silent/useSilentQuote.js +381 -0
- package/dist/hooks/silent/useWallet.d.ts +76 -0
- package/dist/hooks/silent/useWallet.js +203 -0
- package/dist/hooks/useAssetPrice.d.ts +8 -0
- package/dist/hooks/useAssetPrice.js +47 -0
- package/dist/hooks/useContacts.d.ts +52 -0
- package/dist/hooks/useContacts.js +259 -0
- package/dist/hooks/useEgressEstimates.d.ts +32 -0
- package/dist/hooks/useEgressEstimates.js +230 -0
- package/dist/hooks/useHiddenSwapFees.d.ts +22 -0
- package/dist/hooks/useHiddenSwapFees.js +81 -0
- package/dist/hooks/useOrderEstimates.d.ts +37 -0
- package/dist/hooks/useOrderEstimates.js +393 -0
- package/dist/hooks/useOutputAssetInfo.d.ts +12 -0
- package/dist/hooks/useOutputAssetInfo.js +38 -0
- package/dist/hooks/usePrices.d.ts +60 -0
- package/dist/hooks/usePrices.js +188 -0
- package/dist/hooks/useQuote.d.ts +73 -0
- package/dist/hooks/useQuote.js +507 -0
- package/dist/hooks/useResetSwapForm.d.ts +16 -0
- package/dist/hooks/useResetSwapForm.js +68 -0
- package/dist/hooks/useSlippageUsd.d.ts +11 -0
- package/dist/hooks/useSlippageUsd.js +19 -0
- package/dist/hooks/useSolanaAdapter.d.ts +15 -0
- package/dist/hooks/useSolanaAdapter.js +55 -0
- package/dist/hooks/useStatus.d.ts +25 -0
- package/dist/hooks/useStatus.js +60 -0
- package/dist/hooks/useSwap.d.ts +67 -0
- package/dist/hooks/useSwap.js +285 -0
- package/dist/hooks/useTransaction.d.ts +119 -0
- package/dist/hooks/useTransaction.js +353 -0
- package/dist/hooks/useTransactionAddress.d.ts +11 -0
- package/dist/hooks/useTransactionAddress.js +26 -0
- package/dist/hooks/useUsdValue.d.ts +7 -0
- package/dist/hooks/useUsdValue.js +19 -0
- package/dist/index.d.ts +54 -0
- package/dist/index.js +41 -0
- package/dist/stories/SilentSwapOverview.stories.d.ts +10 -0
- package/dist/stories/SilentSwapOverview.stories.js +364 -0
- package/dist/stories/useAuth.stories.d.ts +6 -0
- package/dist/stories/useAuth.stories.js +55 -0
- package/dist/stories/useSilentClient.stories.d.ts +9 -0
- package/dist/stories/useSilentClient.stories.js +39 -0
- package/dist/stories/useSilentOrders.stories.d.ts +1 -0
- package/dist/stories/useSilentOrders.stories.js +1 -0
- package/dist/stories/useSilentQuote.stories.d.ts +6 -0
- package/dist/stories/useSilentQuote.stories.js +267 -0
- package/dist/stories/useTransaction.stories.d.ts +6 -0
- package/dist/stories/useTransaction.stories.js +121 -0
- package/dist/utils/formatters.d.ts +33 -0
- package/dist/utils/formatters.js +82 -0
- package/package.json +67 -0
|
@@ -0,0 +1,353 @@
|
|
|
1
|
+
import { useCallback, useState } from 'react';
|
|
2
|
+
import { createPublicClient, http, erc20Abi } from 'viem';
|
|
3
|
+
import { executeRelayBridge, executeDebridgeBridge, getBridgeStatus, createTransactionExecutor, createChainSwitcher, ensureChain, waitForTransactionConfirmation, parseTransactionRequestForViem, getChainById, } from '@silentswap/sdk';
|
|
4
|
+
import { createSolanaTransactionExecutor } from './silent/solana-transaction.js';
|
|
5
|
+
/**
|
|
6
|
+
* React hook for executing transactions
|
|
7
|
+
*
|
|
8
|
+
* This hook provides transaction execution for:
|
|
9
|
+
* - Bridge quotes from any provider (relay.link, deBridge)
|
|
10
|
+
* - SilentSwap deposit transactions (OrderResponse)
|
|
11
|
+
* - Token approvals for ERC-20 tokens
|
|
12
|
+
*
|
|
13
|
+
* It handles chain switching and transaction sending automatically for all transaction types.
|
|
14
|
+
*
|
|
15
|
+
* @param options - Configuration options for transaction execution
|
|
16
|
+
* @returns Object with transaction execution state and methods
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { useTransaction, useQuote } from '@silentswap/react';
|
|
21
|
+
* import { useWalletClient, useAccount } from 'wagmi';
|
|
22
|
+
*
|
|
23
|
+
* function BridgeComponent() {
|
|
24
|
+
* const { address, connector } = useAccount();
|
|
25
|
+
* const { data: walletClient } = useWalletClient();
|
|
26
|
+
*
|
|
27
|
+
* // Get the best quote from all providers
|
|
28
|
+
* const { getQuote: getBestQuote } = useQuote({ address });
|
|
29
|
+
*
|
|
30
|
+
* // Execute the transaction
|
|
31
|
+
* const {
|
|
32
|
+
* executeTransaction,
|
|
33
|
+
* getStatus,
|
|
34
|
+
* isLoading,
|
|
35
|
+
* currentStep,
|
|
36
|
+
* error
|
|
37
|
+
* } = useTransaction({
|
|
38
|
+
* address: address!,
|
|
39
|
+
* walletClient,
|
|
40
|
+
* connector,
|
|
41
|
+
* });
|
|
42
|
+
*
|
|
43
|
+
* const handleBridge = async () => {
|
|
44
|
+
* // Get the best quote from all providers
|
|
45
|
+
* const quote = await getBestQuote({
|
|
46
|
+
* srcChainId: 1, // Ethereum
|
|
47
|
+
* dstChainId: 43114, // Avalanche
|
|
48
|
+
* srcToken: '0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48', // USDC
|
|
49
|
+
* dstToken: '0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E', // USDC.e
|
|
50
|
+
* amount: '1000000', // 1 USDC
|
|
51
|
+
* recipient: address!,
|
|
52
|
+
* });
|
|
53
|
+
*
|
|
54
|
+
* if (quote) {
|
|
55
|
+
* // Execute the bridge transaction
|
|
56
|
+
* const result = await executeTransaction(quote);
|
|
57
|
+
* console.log('Bridge completed:', result);
|
|
58
|
+
*
|
|
59
|
+
* // Check status if needed
|
|
60
|
+
* if (result?.requestId) {
|
|
61
|
+
* const status = await getStatus(result.requestId, quote.provider);
|
|
62
|
+
* console.log('Bridge status:', status);
|
|
63
|
+
* }
|
|
64
|
+
* }
|
|
65
|
+
* };
|
|
66
|
+
*
|
|
67
|
+
* return (
|
|
68
|
+
* <div>
|
|
69
|
+
* {isLoading && <div>{currentStep}...</div>}
|
|
70
|
+
* {error && <div>Error: {error.message}</div>}
|
|
71
|
+
* <button onClick={handleBridge} disabled={isLoading}>
|
|
72
|
+
* Bridge Assets
|
|
73
|
+
* </button>
|
|
74
|
+
* </div>
|
|
75
|
+
* );
|
|
76
|
+
* }
|
|
77
|
+
* ```
|
|
78
|
+
*/
|
|
79
|
+
export function useTransaction({ walletClient, connector, solanaConnector, solanaConnection, solanaRpcUrl, setCurrentStep: externalSetCurrentStep, onStatus: externalOnStatus, }) {
|
|
80
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
81
|
+
const [internalCurrentStep, setInternalCurrentStep] = useState('');
|
|
82
|
+
const [error, setError] = useState(null);
|
|
83
|
+
// Use external callbacks if provided, otherwise use internal state
|
|
84
|
+
const setCurrentStep = externalSetCurrentStep || setInternalCurrentStep;
|
|
85
|
+
const currentStep = externalSetCurrentStep ? internalCurrentStep : internalCurrentStep;
|
|
86
|
+
const onStatus = externalOnStatus;
|
|
87
|
+
/**
|
|
88
|
+
* Execute a bridge transaction
|
|
89
|
+
* Supports both EVM and Solana transactions
|
|
90
|
+
*/
|
|
91
|
+
const executeTransaction = useCallback(async (quote) => {
|
|
92
|
+
// Check if quote contains Solana transactions
|
|
93
|
+
const hasSolanaTransactions = quote.txs.some((tx) => tx.instructions !== undefined);
|
|
94
|
+
if (hasSolanaTransactions) {
|
|
95
|
+
// Solana transactions require Solana connector and connection
|
|
96
|
+
if (!solanaConnector || !solanaConnection) {
|
|
97
|
+
throw new Error('Solana connector and connection are required for Solana bridge transactions. ' +
|
|
98
|
+
'Please provide solanaConnector and solanaConnection options.');
|
|
99
|
+
}
|
|
100
|
+
// Create Solana transaction executor
|
|
101
|
+
const solanaExecutor = createSolanaTransactionExecutor(solanaConnector, solanaConnection);
|
|
102
|
+
// For Solana, we still need EVM wallet client for chain switching (if needed)
|
|
103
|
+
// But the actual transaction execution will use Solana executor
|
|
104
|
+
const executeTx = createTransactionExecutor(walletClient, connector, solanaExecutor);
|
|
105
|
+
// Create a no-op chain switcher for Solana (chain switching is handled by connector)
|
|
106
|
+
const switchChain = async (_chainId) => {
|
|
107
|
+
// Solana doesn't require chain switching
|
|
108
|
+
};
|
|
109
|
+
setIsLoading(true);
|
|
110
|
+
setError(null);
|
|
111
|
+
try {
|
|
112
|
+
// Execute based on provider
|
|
113
|
+
switch (quote.provider) {
|
|
114
|
+
case 'relay':
|
|
115
|
+
return await executeRelayBridge(quote, executeTx, switchChain, (step) => {
|
|
116
|
+
setCurrentStep(step);
|
|
117
|
+
onStatus?.(step);
|
|
118
|
+
});
|
|
119
|
+
case 'debridge':
|
|
120
|
+
// DeBridge doesn't support Solana
|
|
121
|
+
throw new Error('DeBridge does not support Solana transactions');
|
|
122
|
+
default:
|
|
123
|
+
throw new Error(`Unsupported bridge provider: ${quote.provider}`);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
catch (err) {
|
|
127
|
+
const error = err instanceof Error ? err : new Error('Bridge execution failed');
|
|
128
|
+
console.error('Bridge execution failed:', error);
|
|
129
|
+
setError(error);
|
|
130
|
+
return null;
|
|
131
|
+
}
|
|
132
|
+
finally {
|
|
133
|
+
setIsLoading(false);
|
|
134
|
+
setCurrentStep('');
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
// EVM transactions
|
|
139
|
+
if (!walletClient) {
|
|
140
|
+
throw new Error('Wallet client required for EVM bridge execution');
|
|
141
|
+
}
|
|
142
|
+
if (!connector) {
|
|
143
|
+
throw new Error('Connector required for EVM bridge execution');
|
|
144
|
+
}
|
|
145
|
+
setIsLoading(true);
|
|
146
|
+
setError(null);
|
|
147
|
+
try {
|
|
148
|
+
// Create wrapper functions using shared utilities
|
|
149
|
+
const executeTx = createTransactionExecutor(walletClient, connector);
|
|
150
|
+
const switchChain = createChainSwitcher(walletClient, connector);
|
|
151
|
+
// Execute based on provider
|
|
152
|
+
switch (quote.provider) {
|
|
153
|
+
case 'relay':
|
|
154
|
+
return await executeRelayBridge(quote, executeTx, switchChain, (step) => {
|
|
155
|
+
setCurrentStep(step);
|
|
156
|
+
onStatus?.(step);
|
|
157
|
+
});
|
|
158
|
+
case 'debridge':
|
|
159
|
+
return await executeDebridgeBridge(quote, executeTx, switchChain, (step) => {
|
|
160
|
+
setCurrentStep(step);
|
|
161
|
+
onStatus?.(step);
|
|
162
|
+
});
|
|
163
|
+
default:
|
|
164
|
+
throw new Error(`Unsupported bridge provider: ${quote.provider}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
catch (err) {
|
|
168
|
+
const error = err instanceof Error ? err : new Error('Bridge execution failed');
|
|
169
|
+
console.error('Bridge execution failed:', error);
|
|
170
|
+
setError(error);
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
finally {
|
|
174
|
+
setIsLoading(false);
|
|
175
|
+
setCurrentStep('');
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
}, [walletClient, connector, solanaConnector, solanaConnection, solanaRpcUrl]);
|
|
179
|
+
/**
|
|
180
|
+
* Execute a deposit transaction from an OrderResponse
|
|
181
|
+
*
|
|
182
|
+
* Parses the transaction request from the order response, switches to the
|
|
183
|
+
* correct chain, sends the transaction, and waits for confirmation.
|
|
184
|
+
*
|
|
185
|
+
* @param orderResponse - Order response containing transaction details
|
|
186
|
+
* @returns Promise resolving to transaction result
|
|
187
|
+
*/
|
|
188
|
+
const executeSwapTransaction = useCallback(async (orderResponse) => {
|
|
189
|
+
if (!walletClient) {
|
|
190
|
+
throw new Error('Wallet client required for transaction execution');
|
|
191
|
+
}
|
|
192
|
+
if (!connector) {
|
|
193
|
+
throw new Error('Connector required for chain switching');
|
|
194
|
+
}
|
|
195
|
+
setIsLoading(true);
|
|
196
|
+
setCurrentStep('Preparing transaction');
|
|
197
|
+
onStatus?.('Preparing transaction');
|
|
198
|
+
setError(null);
|
|
199
|
+
try {
|
|
200
|
+
const transactionParams = parseTransactionRequestForViem(orderResponse.transaction);
|
|
201
|
+
if (!transactionParams) {
|
|
202
|
+
throw new Error('Invalid transaction parameters from order response');
|
|
203
|
+
}
|
|
204
|
+
const chainId = transactionParams.chainId ?? 1;
|
|
205
|
+
// Ensure correct chain
|
|
206
|
+
const wallet = await ensureChain(chainId, walletClient, connector);
|
|
207
|
+
// Create public client for the same chain
|
|
208
|
+
// Use wallet's chain if available, otherwise look up chain by ID
|
|
209
|
+
const chain = wallet.chain || getChainById(chainId);
|
|
210
|
+
if (!chain) {
|
|
211
|
+
throw new Error(`Unsupported chain ID: ${chainId}. Please ensure the chain is supported.`);
|
|
212
|
+
}
|
|
213
|
+
const publicClient = createPublicClient({
|
|
214
|
+
chain,
|
|
215
|
+
transport: http(),
|
|
216
|
+
});
|
|
217
|
+
setCurrentStep('Sending transaction');
|
|
218
|
+
onStatus?.('Sending transaction');
|
|
219
|
+
// Execute transaction
|
|
220
|
+
const hash = await wallet.sendTransaction({
|
|
221
|
+
...transactionParams,
|
|
222
|
+
chain: { id: chainId },
|
|
223
|
+
});
|
|
224
|
+
setCurrentStep('Waiting for confirmation');
|
|
225
|
+
onStatus?.('Waiting for confirmation');
|
|
226
|
+
// Wait for confirmation using core utility with public client
|
|
227
|
+
const result = await waitForTransactionConfirmation(hash, publicClient);
|
|
228
|
+
if (result.status !== 'success') {
|
|
229
|
+
throw new Error(`Transaction failed: ${result.transactionHash}`);
|
|
230
|
+
}
|
|
231
|
+
return {
|
|
232
|
+
hash,
|
|
233
|
+
chainId,
|
|
234
|
+
status: 'confirmed',
|
|
235
|
+
confirmations: 1,
|
|
236
|
+
};
|
|
237
|
+
}
|
|
238
|
+
catch (err) {
|
|
239
|
+
const error = err instanceof Error ? err : new Error('Transaction execution failed');
|
|
240
|
+
setError(error);
|
|
241
|
+
throw error;
|
|
242
|
+
}
|
|
243
|
+
finally {
|
|
244
|
+
setIsLoading(false);
|
|
245
|
+
setCurrentStep('');
|
|
246
|
+
}
|
|
247
|
+
}, [walletClient, connector, setCurrentStep, onStatus]);
|
|
248
|
+
/**
|
|
249
|
+
* Approve token spending for a given allowance target
|
|
250
|
+
*
|
|
251
|
+
* Checks current allowance and approves if needed. Used for ERC-20 token
|
|
252
|
+
* approvals before executing swaps or bridge transactions.
|
|
253
|
+
*
|
|
254
|
+
* @param chainId - Chain ID where the token is located
|
|
255
|
+
* @param tokenAddress - Token contract address
|
|
256
|
+
* @param allowanceTarget - Address to approve spending for
|
|
257
|
+
* @param amount - Amount to approve
|
|
258
|
+
* @param userAddress - User's address
|
|
259
|
+
* @returns Promise resolving to transaction hash if approval was needed, null otherwise
|
|
260
|
+
*/
|
|
261
|
+
const approveTokenSpending = useCallback(async (chainId, tokenAddress, allowanceTarget, amount, userAddress) => {
|
|
262
|
+
if (!walletClient || !connector) {
|
|
263
|
+
throw new Error('Wallet client and connector required for token approval');
|
|
264
|
+
}
|
|
265
|
+
setIsLoading(true);
|
|
266
|
+
setCurrentStep('Approving token spending');
|
|
267
|
+
onStatus?.('Approving token spending');
|
|
268
|
+
setError(null);
|
|
269
|
+
try {
|
|
270
|
+
// Switch to source chain for approval
|
|
271
|
+
const wallet = await ensureChain(chainId, walletClient, connector);
|
|
272
|
+
// Check current allowance
|
|
273
|
+
// Use wallet's chain if available, otherwise look up chain by ID
|
|
274
|
+
const chain = wallet.chain || getChainById(chainId);
|
|
275
|
+
if (!chain) {
|
|
276
|
+
throw new Error(`Unsupported chain ID: ${chainId}. Please ensure the chain is supported.`);
|
|
277
|
+
}
|
|
278
|
+
const publicClient = createPublicClient({
|
|
279
|
+
chain,
|
|
280
|
+
transport: http(),
|
|
281
|
+
});
|
|
282
|
+
const currentAllowance = (await publicClient.readContract({
|
|
283
|
+
address: tokenAddress,
|
|
284
|
+
abi: erc20Abi,
|
|
285
|
+
functionName: 'allowance',
|
|
286
|
+
args: [userAddress, allowanceTarget],
|
|
287
|
+
}));
|
|
288
|
+
// Match Svelte behavior: approve maximum uint256 value (XG_UINT256_MAX)
|
|
289
|
+
// This allows the bridge to use tokens even if the amount needs slight adjustment
|
|
290
|
+
const XG_UINT256_MAX = (1n << 256n) - 1n;
|
|
291
|
+
const amountToCheck = BigInt(amount);
|
|
292
|
+
console.log('currentAllowance', currentAllowance);
|
|
293
|
+
console.log('amountToCheck', amountToCheck);
|
|
294
|
+
// Approve if current allowance is insufficient
|
|
295
|
+
// In Svelte: checks if allowance < amount, then approves XG_UINT256_MAX (line 948, 958)
|
|
296
|
+
if (currentAllowance < amountToCheck) {
|
|
297
|
+
const hash = await walletClient.writeContract({
|
|
298
|
+
address: tokenAddress,
|
|
299
|
+
abi: erc20Abi,
|
|
300
|
+
functionName: 'approve',
|
|
301
|
+
args: [allowanceTarget, XG_UINT256_MAX],
|
|
302
|
+
account: walletClient.account,
|
|
303
|
+
chain: null,
|
|
304
|
+
});
|
|
305
|
+
// Wait for approval confirmation
|
|
306
|
+
await waitForTransactionConfirmation(hash, publicClient);
|
|
307
|
+
return hash;
|
|
308
|
+
}
|
|
309
|
+
return null;
|
|
310
|
+
}
|
|
311
|
+
catch (err) {
|
|
312
|
+
const error = err instanceof Error ? err : new Error('Token approval failed');
|
|
313
|
+
setError(error);
|
|
314
|
+
throw error;
|
|
315
|
+
}
|
|
316
|
+
finally {
|
|
317
|
+
setIsLoading(false);
|
|
318
|
+
setCurrentStep('');
|
|
319
|
+
}
|
|
320
|
+
}, [walletClient, connector]);
|
|
321
|
+
/**
|
|
322
|
+
* Get bridge status for a request
|
|
323
|
+
*/
|
|
324
|
+
const getStatus = useCallback(async (requestId, provider) => {
|
|
325
|
+
setIsLoading(true);
|
|
326
|
+
setCurrentStep('Checking bridge status');
|
|
327
|
+
setError(null);
|
|
328
|
+
try {
|
|
329
|
+
// Use shared status function
|
|
330
|
+
return await getBridgeStatus(requestId, provider);
|
|
331
|
+
}
|
|
332
|
+
catch (err) {
|
|
333
|
+
const error = err instanceof Error ? err : new Error('Failed to get bridge status');
|
|
334
|
+
setError(error);
|
|
335
|
+
return null;
|
|
336
|
+
}
|
|
337
|
+
finally {
|
|
338
|
+
setIsLoading(false);
|
|
339
|
+
setCurrentStep('');
|
|
340
|
+
}
|
|
341
|
+
}, []);
|
|
342
|
+
return {
|
|
343
|
+
// State
|
|
344
|
+
isLoading,
|
|
345
|
+
currentStep,
|
|
346
|
+
error,
|
|
347
|
+
// Methods
|
|
348
|
+
executeTransaction,
|
|
349
|
+
executeSwapTransaction,
|
|
350
|
+
approveTokenSpending,
|
|
351
|
+
getStatus,
|
|
352
|
+
};
|
|
353
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import { type AssetInfo } from '@silentswap/sdk';
|
|
2
|
+
/**
|
|
3
|
+
* Hook to select the correct transaction/quote address based on the input token.
|
|
4
|
+
* Returns EVM address for EVM tokens, Solana address for Solana tokens.
|
|
5
|
+
*
|
|
6
|
+
* @param tokenIn - The input token asset info
|
|
7
|
+
* @param evmAddress - The user's EVM address (0x...) or null
|
|
8
|
+
* @param solAddress - The user's Solana address (base58) or null
|
|
9
|
+
* @returns The appropriate address for the token's chain, or undefined if no token/address
|
|
10
|
+
*/
|
|
11
|
+
export declare function useTransactionAddress(tokenIn: AssetInfo | null | undefined, evmAddress: string | null | undefined, solAddress: string | null | undefined): `0x${string}` | string | undefined;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { isEvmAsset, isSolanaAsset } from '@silentswap/sdk';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to select the correct transaction/quote address based on the input token.
|
|
5
|
+
* Returns EVM address for EVM tokens, Solana address for Solana tokens.
|
|
6
|
+
*
|
|
7
|
+
* @param tokenIn - The input token asset info
|
|
8
|
+
* @param evmAddress - The user's EVM address (0x...) or null
|
|
9
|
+
* @param solAddress - The user's Solana address (base58) or null
|
|
10
|
+
* @returns The appropriate address for the token's chain, or undefined if no token/address
|
|
11
|
+
*/
|
|
12
|
+
export function useTransactionAddress(tokenIn, evmAddress, solAddress) {
|
|
13
|
+
return useMemo(() => {
|
|
14
|
+
if (!tokenIn) {
|
|
15
|
+
return (evmAddress ?? undefined);
|
|
16
|
+
}
|
|
17
|
+
if (isEvmAsset(tokenIn.caip19)) {
|
|
18
|
+
return (evmAddress ?? undefined);
|
|
19
|
+
}
|
|
20
|
+
if (isSolanaAsset(tokenIn.caip19)) {
|
|
21
|
+
return solAddress ?? undefined; // Return Solana address as string (not cast to 0x)
|
|
22
|
+
}
|
|
23
|
+
// Default to EVM address
|
|
24
|
+
return (evmAddress ?? undefined);
|
|
25
|
+
}, [tokenIn?.caip19, evmAddress, solAddress]);
|
|
26
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { useMemo } from 'react';
|
|
2
|
+
import { calculateUsdValue, formatUsdValue } from '../utils/formatters.js';
|
|
3
|
+
/**
|
|
4
|
+
* Hook to calculate and format USD value from amount and price
|
|
5
|
+
*/
|
|
6
|
+
export function useUsdValue(amount, price) {
|
|
7
|
+
const usdValue = useMemo(() => {
|
|
8
|
+
return calculateUsdValue(amount, price);
|
|
9
|
+
}, [amount, price]);
|
|
10
|
+
const formattedUsdValue = useMemo(() => {
|
|
11
|
+
if (!usdValue)
|
|
12
|
+
return null;
|
|
13
|
+
return formatUsdValue(usdValue);
|
|
14
|
+
}, [usdValue]);
|
|
15
|
+
return {
|
|
16
|
+
usdValue,
|
|
17
|
+
formattedUsdValue,
|
|
18
|
+
};
|
|
19
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
export { useSilentClient } from './hooks/silent/useSilentClient.js';
|
|
2
|
+
export { PricesProvider, usePricesContext } from './contexts/PricesContext.js';
|
|
3
|
+
export type { PricesContextType } from './contexts/PricesContext.js';
|
|
4
|
+
export { AssetsProvider, useAssetsContext } from './contexts/AssetsContext.js';
|
|
5
|
+
export type { AssetsContextType, Chain, ChainInfo } from './contexts/AssetsContext.js';
|
|
6
|
+
export { BalancesProvider, useBalancesContext } from './contexts/BalancesContext.js';
|
|
7
|
+
export type { BalancesContextType, UserBalance } from './contexts/BalancesContext.js';
|
|
8
|
+
export { OrdersProvider, useOrdersContext, useWalletFacilitatorGroups } from './contexts/OrdersContext.js';
|
|
9
|
+
export type { OrdersContextOrderMetadata, OrdersContextOrder, FacilitatorGroup, OrdersContextType } from './contexts/OrdersContext.js';
|
|
10
|
+
export { SilentSwapProvider, useSilentSwap } from './contexts/SilentSwapContext.js';
|
|
11
|
+
export type { SilentSwapContextType } from './contexts/SilentSwapContext.js';
|
|
12
|
+
export { useSilentOrders } from './hooks/silent/useSilentOrders.js';
|
|
13
|
+
export { useAuth } from './hooks/silent/useAuth.js';
|
|
14
|
+
export { useWallet } from './hooks/silent/useWallet.js';
|
|
15
|
+
export { useSilentQuote } from './hooks/silent/useSilentQuote.js';
|
|
16
|
+
export { useOrderTracking, OutputStage, getStatusTextFromStage, getProgressFromStage, } from './hooks/silent/useOrderTracking.js';
|
|
17
|
+
export { useRefund } from './hooks/silent/useRefund.js';
|
|
18
|
+
export { useTransaction } from './hooks/useTransaction.js';
|
|
19
|
+
export { useQuote } from './hooks/useQuote.js';
|
|
20
|
+
export { useOrderEstimates } from './hooks/useOrderEstimates.js';
|
|
21
|
+
export { usePrices } from './hooks/usePrices.js';
|
|
22
|
+
export { useSwap, getSourceAssetCaip19, X_RANGE_SLIDER_MIN_GAP, OUTPUT_LIMIT, DEFAULT_SOURCE_ASSET, DEFAULT_DEST_ASSET, } from './hooks/useSwap.js';
|
|
23
|
+
export type { Destination, SwapState } from './hooks/useSwap.js';
|
|
24
|
+
export { useEgressEstimates } from './hooks/useEgressEstimates.js';
|
|
25
|
+
export type { UseEgressEstimatesOptions } from './hooks/useEgressEstimates.js';
|
|
26
|
+
export { useStatus } from './hooks/useStatus.js';
|
|
27
|
+
export type { StatusResponse } from './hooks/useStatus.js';
|
|
28
|
+
export { useHiddenSwapFees } from './hooks/useHiddenSwapFees.js';
|
|
29
|
+
export type { HiddenSwapFees, UseHiddenSwapFeesOptions } from './hooks/useHiddenSwapFees.js';
|
|
30
|
+
export { useSlippageUsd } from './hooks/useSlippageUsd.js';
|
|
31
|
+
export type { UseSlippageUsdOptions } from './hooks/useSlippageUsd.js';
|
|
32
|
+
export { useResetSwapForm } from './hooks/useResetSwapForm.js';
|
|
33
|
+
export type { UseResetSwapFormOptions } from './hooks/useResetSwapForm.js';
|
|
34
|
+
export { useTransactionAddress } from './hooks/useTransactionAddress.js';
|
|
35
|
+
export { useSolanaAdapter } from './hooks/useSolanaAdapter.js';
|
|
36
|
+
export { useContacts, AddressBookContact, AddressBookContactSource } from './hooks/useContacts.js';
|
|
37
|
+
export type { AddressBookContactId, AddressBookContactInfo, AddressBookContactsQuery } from './hooks/useContacts.js';
|
|
38
|
+
export { useAssetPrice } from './hooks/useAssetPrice.js';
|
|
39
|
+
export { useUsdValue } from './hooks/useUsdValue.js';
|
|
40
|
+
export { useOutputAssetInfo } from './hooks/useOutputAssetInfo.js';
|
|
41
|
+
export * from './utils/formatters.js';
|
|
42
|
+
export type { SilentSwapClientConfig, BridgeProvider, BridgeQuote, BridgeTransaction, BridgeStatus, BridgeQuoteParams, RefundEligibility, GatewayOrderStatus, } from '@silentswap/sdk';
|
|
43
|
+
export { queryDepositCount, executeRelayBridge, executeDebridgeBridge, getRelayStatus, getDebridgeStatus, loadCachedAuth, saveAuth, clearAuth, getAuthStorageKey, loadWalletData, saveWalletData, clearWalletData, getWalletStorageKey, ensureChain, waitForTransactionConfirmation, DepositStatus, queryOrderStatus, checkRefundEligibility, executeRefund, checkRecoveryEligibility, orderIdToBytes32, NI_CHAIN_ID_AVALANCHE, XT_TTL_SESSION_CACHE, X_MINIMUM_INPUT_USD, X_MAX_IMPACT_PERCENT, S0X_ADDR_USDC_AVALANCHE, S_CAIP19_USDC_AVALANCHE, } from '@silentswap/sdk';
|
|
44
|
+
export type { SilentSwapWallet, FacilitatorAccount, useWalletOptions, useWalletReturn, } from './hooks/silent/useWallet.js';
|
|
45
|
+
export type { useAuthOptions, useAuthReturn } from './hooks/silent/useAuth.js';
|
|
46
|
+
export type { SwapTransaction, SwapResult, useSilentQuoteOptions, useSilentQuoteReturn, ExecuteSwapParams, } from './hooks/silent/useSilentQuote.js';
|
|
47
|
+
export type { useTransactionOptions, useTransactionReturn } from './hooks/useTransaction.js';
|
|
48
|
+
export type { useQuoteOptions, useQuoteReturn } from './hooks/useQuote.js';
|
|
49
|
+
export type { OrderEstimateResult, UseOrderEstimatesOptions } from './hooks/useOrderEstimates.js';
|
|
50
|
+
export type { usePricesOptions, usePricesReturn, CachedPrice } from './hooks/usePrices.js';
|
|
51
|
+
export type { UseOrderTrackingOptions, UseOrderTrackingReturn, OrderDeposit, OutputStatus, OrderStatus, StatusUpdate, } from './hooks/silent/useOrderTracking.js';
|
|
52
|
+
export type { UseRefundOptions, UseRefundReturn } from './hooks/silent/useRefund.js';
|
|
53
|
+
export { createSolanaTransactionExecutor, convertRelaySolanaStepToTransaction, } from './hooks/silent/solana-transaction.js';
|
|
54
|
+
export type { SolanaWalletConnector, SolanaConnection } from './hooks/silent/solana-transaction.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
export { useSilentClient } from './hooks/silent/useSilentClient.js';
|
|
2
|
+
export { PricesProvider, usePricesContext } from './contexts/PricesContext.js';
|
|
3
|
+
export { AssetsProvider, useAssetsContext } from './contexts/AssetsContext.js';
|
|
4
|
+
export { BalancesProvider, useBalancesContext } from './contexts/BalancesContext.js';
|
|
5
|
+
export { OrdersProvider, useOrdersContext, useWalletFacilitatorGroups } from './contexts/OrdersContext.js';
|
|
6
|
+
export { SilentSwapProvider, useSilentSwap } from './contexts/SilentSwapContext.js';
|
|
7
|
+
export { useSilentOrders } from './hooks/silent/useSilentOrders.js';
|
|
8
|
+
export { useAuth } from './hooks/silent/useAuth.js';
|
|
9
|
+
export { useWallet } from './hooks/silent/useWallet.js';
|
|
10
|
+
export { useSilentQuote } from './hooks/silent/useSilentQuote.js';
|
|
11
|
+
export { useOrderTracking, OutputStage, getStatusTextFromStage, getProgressFromStage, } from './hooks/silent/useOrderTracking.js';
|
|
12
|
+
export { useRefund } from './hooks/silent/useRefund.js';
|
|
13
|
+
export { useTransaction } from './hooks/useTransaction.js';
|
|
14
|
+
export { useQuote } from './hooks/useQuote.js';
|
|
15
|
+
export { useOrderEstimates } from './hooks/useOrderEstimates.js';
|
|
16
|
+
export { usePrices } from './hooks/usePrices.js';
|
|
17
|
+
export { useSwap, getSourceAssetCaip19, X_RANGE_SLIDER_MIN_GAP, OUTPUT_LIMIT, DEFAULT_SOURCE_ASSET, DEFAULT_DEST_ASSET, } from './hooks/useSwap.js';
|
|
18
|
+
export { useEgressEstimates } from './hooks/useEgressEstimates.js';
|
|
19
|
+
export { useStatus } from './hooks/useStatus.js';
|
|
20
|
+
export { useHiddenSwapFees } from './hooks/useHiddenSwapFees.js';
|
|
21
|
+
export { useSlippageUsd } from './hooks/useSlippageUsd.js';
|
|
22
|
+
export { useResetSwapForm } from './hooks/useResetSwapForm.js';
|
|
23
|
+
export { useTransactionAddress } from './hooks/useTransactionAddress.js';
|
|
24
|
+
export { useSolanaAdapter } from './hooks/useSolanaAdapter.js';
|
|
25
|
+
export { useContacts, AddressBookContact, AddressBookContactSource } from './hooks/useContacts.js';
|
|
26
|
+
export { useAssetPrice } from './hooks/useAssetPrice.js';
|
|
27
|
+
export { useUsdValue } from './hooks/useUsdValue.js';
|
|
28
|
+
export { useOutputAssetInfo } from './hooks/useOutputAssetInfo.js';
|
|
29
|
+
export * from './utils/formatters.js';
|
|
30
|
+
// Re-export functions from core SDK
|
|
31
|
+
export { queryDepositCount, executeRelayBridge, executeDebridgeBridge, getRelayStatus, getDebridgeStatus,
|
|
32
|
+
// Storage utilities
|
|
33
|
+
loadCachedAuth, saveAuth, clearAuth, getAuthStorageKey, loadWalletData, saveWalletData, clearWalletData, getWalletStorageKey,
|
|
34
|
+
// Chain utilities
|
|
35
|
+
ensureChain, waitForTransactionConfirmation,
|
|
36
|
+
// Refund utilities
|
|
37
|
+
DepositStatus, queryOrderStatus, checkRefundEligibility, executeRefund, checkRecoveryEligibility, orderIdToBytes32,
|
|
38
|
+
// Constants
|
|
39
|
+
NI_CHAIN_ID_AVALANCHE, XT_TTL_SESSION_CACHE, X_MINIMUM_INPUT_USD, X_MAX_IMPACT_PERCENT, S0X_ADDR_USDC_AVALANCHE, S_CAIP19_USDC_AVALANCHE, } from '@silentswap/sdk';
|
|
40
|
+
// Solana transaction utilities
|
|
41
|
+
export { createSolanaTransactionExecutor, convertRelaySolanaStepToTransaction, } from './hooks/silent/solana-transaction.js';
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Meta, StoryObj } from '@storybook/react';
|
|
2
|
+
import { type SilentSwapClientConfig } from '@silentswap/sdk';
|
|
3
|
+
declare function SilentSwapCompleteDemo({ config }: {
|
|
4
|
+
config: SilentSwapClientConfig;
|
|
5
|
+
}): import("react/jsx-runtime").JSX.Element;
|
|
6
|
+
declare const meta: Meta<typeof SilentSwapCompleteDemo>;
|
|
7
|
+
export default meta;
|
|
8
|
+
type Story = StoryObj<typeof meta>;
|
|
9
|
+
export declare const DefaultFlow: Story;
|
|
10
|
+
export declare const StagingEnvironment: Story;
|