@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.
Files changed (86) hide show
  1. package/README.md +48 -0
  2. package/dist/contexts/AssetsContext.d.ts +24 -0
  3. package/dist/contexts/AssetsContext.js +83 -0
  4. package/dist/contexts/BalancesContext.d.ts +28 -0
  5. package/dist/contexts/BalancesContext.js +533 -0
  6. package/dist/contexts/OrdersContext.d.ts +53 -0
  7. package/dist/contexts/OrdersContext.js +240 -0
  8. package/dist/contexts/PricesContext.d.ts +12 -0
  9. package/dist/contexts/PricesContext.js +109 -0
  10. package/dist/contexts/SilentSwapContext.d.ts +58 -0
  11. package/dist/contexts/SilentSwapContext.js +205 -0
  12. package/dist/hooks/silent/orderTrackingWebSocketManager.d.ts +48 -0
  13. package/dist/hooks/silent/orderTrackingWebSocketManager.js +284 -0
  14. package/dist/hooks/silent/solana-transaction.d.ts +60 -0
  15. package/dist/hooks/silent/solana-transaction.js +236 -0
  16. package/dist/hooks/silent/useAuth.d.ts +90 -0
  17. package/dist/hooks/silent/useAuth.js +269 -0
  18. package/dist/hooks/silent/useBridgeExecution.d.ts +39 -0
  19. package/dist/hooks/silent/useBridgeExecution.js +877 -0
  20. package/dist/hooks/silent/useOrderSigning.d.ts +34 -0
  21. package/dist/hooks/silent/useOrderSigning.js +133 -0
  22. package/dist/hooks/silent/useOrderTracking.d.ts +174 -0
  23. package/dist/hooks/silent/useOrderTracking.js +524 -0
  24. package/dist/hooks/silent/useQuoteCalculation.d.ts +50 -0
  25. package/dist/hooks/silent/useQuoteCalculation.js +331 -0
  26. package/dist/hooks/silent/useQuoteFetching.d.ts +18 -0
  27. package/dist/hooks/silent/useQuoteFetching.js +54 -0
  28. package/dist/hooks/silent/useRefund.d.ts +26 -0
  29. package/dist/hooks/silent/useRefund.js +134 -0
  30. package/dist/hooks/silent/useSilentClient.d.ts +16 -0
  31. package/dist/hooks/silent/useSilentClient.js +32 -0
  32. package/dist/hooks/silent/useSilentOrders.d.ts +174 -0
  33. package/dist/hooks/silent/useSilentOrders.js +73 -0
  34. package/dist/hooks/silent/useSilentQuote.d.ts +88 -0
  35. package/dist/hooks/silent/useSilentQuote.js +381 -0
  36. package/dist/hooks/silent/useWallet.d.ts +76 -0
  37. package/dist/hooks/silent/useWallet.js +203 -0
  38. package/dist/hooks/useAssetPrice.d.ts +8 -0
  39. package/dist/hooks/useAssetPrice.js +47 -0
  40. package/dist/hooks/useContacts.d.ts +52 -0
  41. package/dist/hooks/useContacts.js +259 -0
  42. package/dist/hooks/useEgressEstimates.d.ts +32 -0
  43. package/dist/hooks/useEgressEstimates.js +230 -0
  44. package/dist/hooks/useHiddenSwapFees.d.ts +22 -0
  45. package/dist/hooks/useHiddenSwapFees.js +81 -0
  46. package/dist/hooks/useOrderEstimates.d.ts +37 -0
  47. package/dist/hooks/useOrderEstimates.js +393 -0
  48. package/dist/hooks/useOutputAssetInfo.d.ts +12 -0
  49. package/dist/hooks/useOutputAssetInfo.js +38 -0
  50. package/dist/hooks/usePrices.d.ts +60 -0
  51. package/dist/hooks/usePrices.js +188 -0
  52. package/dist/hooks/useQuote.d.ts +73 -0
  53. package/dist/hooks/useQuote.js +507 -0
  54. package/dist/hooks/useResetSwapForm.d.ts +16 -0
  55. package/dist/hooks/useResetSwapForm.js +68 -0
  56. package/dist/hooks/useSlippageUsd.d.ts +11 -0
  57. package/dist/hooks/useSlippageUsd.js +19 -0
  58. package/dist/hooks/useSolanaAdapter.d.ts +15 -0
  59. package/dist/hooks/useSolanaAdapter.js +55 -0
  60. package/dist/hooks/useStatus.d.ts +25 -0
  61. package/dist/hooks/useStatus.js +60 -0
  62. package/dist/hooks/useSwap.d.ts +67 -0
  63. package/dist/hooks/useSwap.js +285 -0
  64. package/dist/hooks/useTransaction.d.ts +119 -0
  65. package/dist/hooks/useTransaction.js +353 -0
  66. package/dist/hooks/useTransactionAddress.d.ts +11 -0
  67. package/dist/hooks/useTransactionAddress.js +26 -0
  68. package/dist/hooks/useUsdValue.d.ts +7 -0
  69. package/dist/hooks/useUsdValue.js +19 -0
  70. package/dist/index.d.ts +54 -0
  71. package/dist/index.js +41 -0
  72. package/dist/stories/SilentSwapOverview.stories.d.ts +10 -0
  73. package/dist/stories/SilentSwapOverview.stories.js +364 -0
  74. package/dist/stories/useAuth.stories.d.ts +6 -0
  75. package/dist/stories/useAuth.stories.js +55 -0
  76. package/dist/stories/useSilentClient.stories.d.ts +9 -0
  77. package/dist/stories/useSilentClient.stories.js +39 -0
  78. package/dist/stories/useSilentOrders.stories.d.ts +1 -0
  79. package/dist/stories/useSilentOrders.stories.js +1 -0
  80. package/dist/stories/useSilentQuote.stories.d.ts +6 -0
  81. package/dist/stories/useSilentQuote.stories.js +267 -0
  82. package/dist/stories/useTransaction.stories.d.ts +6 -0
  83. package/dist/stories/useTransaction.stories.js +121 -0
  84. package/dist/utils/formatters.d.ts +33 -0
  85. package/dist/utils/formatters.js +82 -0
  86. package/package.json +67 -0
@@ -0,0 +1,34 @@
1
+ import type { Hex, WalletClient } from 'viem';
2
+ import type { Connector } from 'wagmi';
3
+ import { type QuoteResponse, type OrderResponse, type HdFacilitatorGroup, type AuthorizationInstruction, type SilentSwapClient } from '@silentswap/sdk';
4
+ /**
5
+ * Hook for signing SilentSwap orders and authorizations
6
+ *
7
+ * This hook handles the signing process for SilentSwap orders, including:
8
+ * - EIP-3009 deposit authorizations (for direct USDC deposits)
9
+ * - EIP-712 order signatures (for order creation)
10
+ * - Proxy authorization approvals (for facilitator operations)
11
+ *
12
+ * @param walletClient - Wallet client for signing operations
13
+ * @param connector - Wagmi connector for chain switching
14
+ * @param client - SilentSwap client instance
15
+ * @param setCurrentStep - Callback to set current step message
16
+ * @param onStatus - Optional status update callback
17
+ * @returns Functions for signing authorizations and orders
18
+ */
19
+ export declare function useOrderSigning(walletClient: WalletClient | undefined, connector: Connector | undefined, client: SilentSwapClient, setCurrentStep: (step: string) => void, onStatus?: (status: string) => void): {
20
+ signAuthorizations: (authorizations: AuthorizationInstruction[], isDepositingDirectly: boolean) => Promise<Array<AuthorizationInstruction & {
21
+ signature: Hex;
22
+ }>>;
23
+ createOrder: (quoteResponse: QuoteResponse, signedAuths: Array<AuthorizationInstruction & {
24
+ signature: Hex;
25
+ }>, metadata: {
26
+ sourceAsset: {
27
+ caip19: string;
28
+ amount: `${bigint}`;
29
+ };
30
+ sourceSender: {
31
+ contactId: string;
32
+ };
33
+ }, overrideFacilitatorGroup?: HdFacilitatorGroup) => Promise<OrderResponse>;
34
+ };
@@ -0,0 +1,133 @@
1
+ import { useCallback } from 'react';
2
+ import { quoteResponseToEip712Document, ensureChain, NI_CHAIN_ID_AVALANCHE, } from '@silentswap/sdk';
3
+ /**
4
+ * Hook for signing SilentSwap orders and authorizations
5
+ *
6
+ * This hook handles the signing process for SilentSwap orders, including:
7
+ * - EIP-3009 deposit authorizations (for direct USDC deposits)
8
+ * - EIP-712 order signatures (for order creation)
9
+ * - Proxy authorization approvals (for facilitator operations)
10
+ *
11
+ * @param walletClient - Wallet client for signing operations
12
+ * @param connector - Wagmi connector for chain switching
13
+ * @param client - SilentSwap client instance
14
+ * @param setCurrentStep - Callback to set current step message
15
+ * @param onStatus - Optional status update callback
16
+ * @returns Functions for signing authorizations and orders
17
+ */
18
+ export function useOrderSigning(walletClient, connector, client, setCurrentStep, onStatus) {
19
+ /**
20
+ * Sign authorizations for a quote
21
+ *
22
+ * Signs EIP-3009 deposit authorizations if depositing directly to USDC on Avalanche.
23
+ * For proxy deposits, returns empty signatures as the proxy will handle signing.
24
+ *
25
+ * @param authorizations - Array of authorization instructions from the quote
26
+ * @param isDepositingDirectly - Whether depositing directly to USDC on Avalanche
27
+ * @returns Promise resolving to signed authorizations
28
+ */
29
+ const signAuthorizations = useCallback(async (authorizations, isDepositingDirectly) => {
30
+ if (!walletClient) {
31
+ throw new Error('Wallet client required for signing');
32
+ }
33
+ setCurrentStep('Signing authorizations');
34
+ onStatus?.('Signing authorizations');
35
+ if (!connector) {
36
+ throw new Error('Connector required for chain switching');
37
+ }
38
+ await ensureChain(NI_CHAIN_ID_AVALANCHE, walletClient, connector);
39
+ return Promise.all(authorizations.map(async (auth) => ({
40
+ ...auth,
41
+ signature: await (async () => {
42
+ // EIP-3009 deposit authorization
43
+ if (auth.type === 'eip3009_deposit') {
44
+ if (isDepositingDirectly) {
45
+ // Sign EIP-712 typed data for direct deposit
46
+ return await walletClient.signTypedData(auth.eip712);
47
+ }
48
+ else {
49
+ // Will be deposited through proxy, no signature needed
50
+ return '0x';
51
+ }
52
+ }
53
+ throw new Error(`Authorization type not implemented: ${auth.type}`);
54
+ })(),
55
+ })));
56
+ }, [walletClient, connector, setCurrentStep, onStatus]);
57
+ /**
58
+ * Create and sign a SilentSwap order
59
+ *
60
+ * Creates an order by:
61
+ * 1. Signing the quote's EIP-712 document
62
+ * 2. Approving proxy authorizations from facilitators
63
+ * 3. Submitting the order to the SilentSwap API
64
+ *
65
+ * @param quoteResponse - Quote response from getQuote
66
+ * @param signedAuths - Signed authorization instructions
67
+ * @param metadata - Order metadata (source asset, sender contact ID)
68
+ * @param overrideFacilitatorGroup - Optional facilitator group to override the hook's group
69
+ * @returns Promise resolving to order response
70
+ */
71
+ const createOrder = useCallback(async (quoteResponse, signedAuths, metadata, overrideFacilitatorGroup) => {
72
+ if (!walletClient) {
73
+ throw new Error('Wallet client required for order creation');
74
+ }
75
+ if (!connector) {
76
+ throw new Error('Connector required for chain switching');
77
+ }
78
+ // Use override group if provided, otherwise use hook's group
79
+ const effectiveGroup = overrideFacilitatorGroup;
80
+ if (!effectiveGroup) {
81
+ throw new Error('Facilitator group required for order creation');
82
+ }
83
+ // Switch to Ethereum for order signing (orders are signed on Ethereum mainnet)
84
+ setCurrentStep('Preparing order signature');
85
+ onStatus?.('Preparing order signature');
86
+ await ensureChain(1, walletClient, connector); // Ethereum mainnet
87
+ // Create EIP-712 document for order
88
+ const orderDoc = quoteResponseToEip712Document(quoteResponse);
89
+ // Sign the order's EIP-712 document
90
+ // Note: viem's signTypedData may normalize the domain.chainId to match the wallet client's
91
+ // chain configuration, which can result in chainId being a BigInt instead of a number.
92
+ // This happens because viem uses the wallet's chain configuration internally.
93
+ const orderSignature = await walletClient.signTypedData({
94
+ ...orderDoc,
95
+ account: walletClient.account,
96
+ });
97
+ // Approve proxy authorizations from facilitators
98
+ const facilitatorReplies = await effectiveGroup.approveProxyAuthorizations(quoteResponse.facilitators, {
99
+ proxyPublicKey: client.proxyPublicKey,
100
+ });
101
+ setCurrentStep('Placing order');
102
+ onStatus?.('Placing order');
103
+ // Convert eip712Domain to serializable format (chainId BigInt -> number)
104
+ // Why: viem's signTypedData may normalize chainId to BigInt based on wallet client's chain config.
105
+ // JSON.stringify doesn't handle BigInt, so we need to convert it to a number.
106
+ // Note: chainId should be safe to convert to number (Ethereum chain IDs are small integers).
107
+ const domainChainId = orderDoc.domain.chainId;
108
+ const serializableDomain = {
109
+ ...orderDoc.domain,
110
+ chainId: typeof domainChainId === 'bigint'
111
+ ? Number(domainChainId)
112
+ : (domainChainId ?? 1), // Default to 1 (Ethereum mainnet) if undefined
113
+ };
114
+ // Create the order
115
+ const [orderError, orderResponse] = await client.order({
116
+ quote: quoteResponse.quote,
117
+ quoteId: quoteResponse.quoteId,
118
+ authorizations: signedAuths,
119
+ eip712Domain: serializableDomain,
120
+ signature: orderSignature,
121
+ facilitators: facilitatorReplies,
122
+ metadata,
123
+ });
124
+ if (orderError || !orderResponse) {
125
+ throw new Error(`Failed to create order: ${orderError?.error}`);
126
+ }
127
+ return orderResponse;
128
+ }, [walletClient, connector, client, setCurrentStep, onStatus]);
129
+ return {
130
+ signAuthorizations,
131
+ createOrder,
132
+ };
133
+ }
@@ -0,0 +1,174 @@
1
+ import type { SilentSwapClient } from '@silentswap/sdk';
2
+ /**
3
+ * Output stage enum matching Svelte's OutputStage (string values)
4
+ */
5
+ export declare enum OutputStage {
6
+ NONE = "NONE",
7
+ INIT = "INIT",
8
+ FUNDED = "FUNDED",
9
+ REDEEMED = "REDEEMED",
10
+ IBC_SENT = "IBC_SENT",
11
+ IBC_RCVD = "IBC_RCVD",
12
+ BRIDGE_SENT = "BRIDGE_SENT",
13
+ BRIDGE_CFRM = "BRIDGE_CFRM",
14
+ BRIDGE_RCVD = "BRIDGE_RCVD",
15
+ SWAP_USDC_GAS = "SWAP_USDC_GAS",
16
+ SWAP_USDC_TRG = "SWAP_USDC_TRG",
17
+ LTRL_TRG_SENT = "LTRL_TRG_SENT",
18
+ LTRL_TRG_RCVD = "LTRL_TRG_RCVD",
19
+ SWAP_TRG_DST = "SWAP_TRG_DST",
20
+ XFER_TRG_DST = "XFER_TRG_DST",
21
+ REFUND_NATIVE = "REFUND_NATIVE",
22
+ FINALIZED = "FINALIZED"
23
+ }
24
+ /**
25
+ * Deposit information
26
+ */
27
+ export type OrderDeposit = {
28
+ amount: string;
29
+ timestamp: number;
30
+ duration: number;
31
+ orderId?: string;
32
+ tx?: string;
33
+ };
34
+ /**
35
+ * Output status information
36
+ */
37
+ export type OutputStatus = {
38
+ index: number;
39
+ stage: OutputStage;
40
+ timestamp: number;
41
+ recipient?: string;
42
+ asset?: {
43
+ caip19: string;
44
+ amount: string;
45
+ decimals: number;
46
+ priceUsd?: number;
47
+ };
48
+ txs?: OrderTransactions;
49
+ };
50
+ /**
51
+ * Order transactions
52
+ */
53
+ export type OrderTransactions = {
54
+ RECEIPT?: {
55
+ chain: string;
56
+ txId: string;
57
+ };
58
+ REFUND?: {
59
+ chain: string;
60
+ txId: string;
61
+ };
62
+ };
63
+ /**
64
+ * Order tracking status (matches Svelte's OrderStatus)
65
+ */
66
+ export type OrderStatus = {
67
+ priority?: string;
68
+ signer: string;
69
+ deposit: OrderDeposit;
70
+ outputs: Array<{
71
+ stage: OutputStage;
72
+ timestamp: number;
73
+ asset: string;
74
+ value: string;
75
+ recipient: string;
76
+ txs?: OrderTransactions;
77
+ output?: {
78
+ caip19: string;
79
+ amount: string;
80
+ decimals: number;
81
+ priceUsd?: number;
82
+ };
83
+ }>;
84
+ metadata?: {
85
+ sourceAsset?: {
86
+ caip19: string;
87
+ amount: string;
88
+ };
89
+ sourceSender?: {
90
+ contactId: string;
91
+ };
92
+ };
93
+ };
94
+ /**
95
+ * Status update types (matches Svelte's StatusUpdate)
96
+ */
97
+ export type StatusUpdate = {
98
+ type: 'deposit';
99
+ data: OrderDeposit;
100
+ } | {
101
+ type: 'stage';
102
+ data: {
103
+ orderId: string;
104
+ index: number;
105
+ stage: OutputStage;
106
+ timestamp: number;
107
+ asset?: {
108
+ caip19: string;
109
+ amount: string;
110
+ decimals: number;
111
+ priceUsd?: number;
112
+ };
113
+ };
114
+ } | {
115
+ type: 'transaction';
116
+ data: {
117
+ orderId: string;
118
+ index: number;
119
+ chain: string;
120
+ txId: string;
121
+ kind: keyof OrderTransactions;
122
+ };
123
+ } | {
124
+ type: 'error';
125
+ data: {
126
+ orderId: string;
127
+ message: string;
128
+ index?: number;
129
+ };
130
+ };
131
+ /**
132
+ * JSON-RPC error type
133
+ */
134
+ export type JsonRpcError = {
135
+ code: number;
136
+ message: string;
137
+ data?: unknown;
138
+ };
139
+ export interface UseOrderTrackingOptions {
140
+ client?: SilentSwapClient;
141
+ orderId?: string;
142
+ viewingAuth?: string;
143
+ onStatusUpdate?: (status: OrderStatus) => void;
144
+ onError?: (error: Error) => void;
145
+ onComplete?: () => void;
146
+ }
147
+ export interface UseOrderTrackingReturn {
148
+ isConnected: boolean;
149
+ isLoading: boolean;
150
+ error: Error | null;
151
+ orderStatus: OrderStatus | null;
152
+ deposit: OrderDeposit | null;
153
+ outputs: OutputStatus[];
154
+ progresses: (number | undefined)[];
155
+ statusTexts: string[];
156
+ completedTimestamp: number | null;
157
+ isComplete: boolean;
158
+ connect: (orderId: string, auth: string) => void;
159
+ disconnect: () => void;
160
+ getStatusText: (stage: OutputStage) => string;
161
+ getProgress: (stage: OutputStage) => number;
162
+ }
163
+ /**
164
+ * Get human-readable status text from output stage
165
+ */
166
+ export declare function getStatusTextFromStage(stage: OutputStage): string;
167
+ /**
168
+ * Get progress value (0-1) from output stage
169
+ */
170
+ export declare function getProgressFromStage(stage: OutputStage): number;
171
+ /**
172
+ * React hook for tracking SilentSwap orders via WebSocket
173
+ */
174
+ export declare function useOrderTracking({ client, orderId: initialOrderId, viewingAuth: initialAuth, onStatusUpdate, onError, onComplete, }?: UseOrderTrackingOptions): UseOrderTrackingReturn;