@shogun-sdk/swap 0.0.2-test
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 +274 -0
- package/dist/core.cjs +619 -0
- package/dist/core.d.cts +137 -0
- package/dist/core.d.ts +137 -0
- package/dist/core.js +590 -0
- package/dist/execute-FaLLPp1i.d.cts +147 -0
- package/dist/execute-HX1fQ7wG.d.ts +147 -0
- package/dist/index.cjs +2477 -0
- package/dist/index.d.cts +10 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +2458 -0
- package/dist/react.cjs +2364 -0
- package/dist/react.d.cts +158 -0
- package/dist/react.d.ts +158 -0
- package/dist/react.js +2355 -0
- package/dist/wallet-MmUIz8GE.d.cts +29 -0
- package/dist/wallet-MmUIz8GE.d.ts +29 -0
- package/dist/wallet-adapter.cjs +175 -0
- package/dist/wallet-adapter.d.cts +34 -0
- package/dist/wallet-adapter.d.ts +34 -0
- package/dist/wallet-adapter.js +150 -0
- package/package.json +68 -0
package/README.md
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
# @shogun-sdk/swap
|
|
2
|
+
|
|
3
|
+
A comprehensive swap SDK for the Shogun Network that provides unified quote fetching across multiple chains and protocols with intelligent fallback mechanisms.
|
|
4
|
+
|
|
5
|
+
## Features
|
|
6
|
+
|
|
7
|
+
- 🔄 **Unified Quote Interface** - Single API for quotes across all supported chains
|
|
8
|
+
- 🎯 **Intelligent Fallback** - Automatically falls back from intents-sdk to one-shot API
|
|
9
|
+
- ⛓️ **Multi-Chain Support** - Ethereum, Arbitrum, Polygon, Solana, Sui, Hyperliquid, and more
|
|
10
|
+
- 🛡️ **Type Safety** - Full TypeScript support with comprehensive type definitions
|
|
11
|
+
- ⚡ **Performance Optimized** - Built for both frontend and backend applications
|
|
12
|
+
- 🔧 **Flexible Configuration** - Customizable timeouts, slippage, and routing options
|
|
13
|
+
|
|
14
|
+
## Installation
|
|
15
|
+
|
|
16
|
+
```bash
|
|
17
|
+
npm install @shogun-sdk/swap
|
|
18
|
+
pnpm install @shogun-sdk/swap
|
|
19
|
+
yarn add @shogun-sdk/swap
|
|
20
|
+
```
|
|
21
|
+
|
|
22
|
+
## Quick Start
|
|
23
|
+
|
|
24
|
+
### Basic Usage
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { SwapClient, ChainID } from "@shogun-sdk/swap";
|
|
28
|
+
|
|
29
|
+
// Initialize the client
|
|
30
|
+
const swapClient = new SwapClient({
|
|
31
|
+
apiKey: 'your-api-key',
|
|
32
|
+
apiUrl: 'https://api.shogun.network',
|
|
33
|
+
timeout: 30000
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
// Get a cross-chain quote
|
|
37
|
+
const quote = await swapClient.getQuote({
|
|
38
|
+
sourceChainId: ChainID.Ethereum,
|
|
39
|
+
destChainId: ChainID.Arbitrum,
|
|
40
|
+
tokenIn: '0xA0b86a33E6441b8c4C8C0C4C0C4C0C4C0C4C0C4C', // USDC
|
|
41
|
+
tokenOut: '0x1234567890123456789012345678901234567890', // Target token
|
|
42
|
+
amount: BigInt('1000000'), // 1 USDC (6 decimals)
|
|
43
|
+
senderAddress: '0x...',
|
|
44
|
+
destinationAddress: '0x...',
|
|
45
|
+
slippageBps: 50 // 0.5% slippage
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
console.log('Expected output:', quote.amountOut.toString());
|
|
49
|
+
console.log('Quote source:', quote.source); // 'intents-sdk' or 'one-shot'
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Single-Chain Swaps
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
// Get a quote for same-chain swap
|
|
56
|
+
const singleChainQuote = await swapClient.getSingleChainQuote(
|
|
57
|
+
ChainID.Ethereum,
|
|
58
|
+
'0xA0b86a33E6441b8c4C8C0C4C0C4C0C4C0C4C0C4C', // USDC
|
|
59
|
+
'0x1234567890123456789012345678901234567890', // Target token
|
|
60
|
+
BigInt('1000000'), // 1 USDC
|
|
61
|
+
50 // 0.5% slippage
|
|
62
|
+
);
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Solana Swaps with Jito Tips
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
const solanaQuote = await swapClient.getQuote({
|
|
69
|
+
sourceChainId: ChainID.Solana,
|
|
70
|
+
destChainId: ChainID.Solana,
|
|
71
|
+
tokenIn: 'So11111111111111111111111111111111111111112', // SOL
|
|
72
|
+
tokenOut: 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v', // USDC
|
|
73
|
+
amount: BigInt('1000000000'), // 1 SOL
|
|
74
|
+
jitoTip: 1000000, // 0.001 SOL tip
|
|
75
|
+
slippageBps: 100 // 1% slippage
|
|
76
|
+
});
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Advanced Usage
|
|
80
|
+
|
|
81
|
+
### Error Handling and Fallback
|
|
82
|
+
|
|
83
|
+
```typescript
|
|
84
|
+
try {
|
|
85
|
+
const quote = await swapClient.getQuote(params);
|
|
86
|
+
|
|
87
|
+
if (quote.error) {
|
|
88
|
+
console.warn('Quote error:', quote.error);
|
|
89
|
+
// Handle error case
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (quote.amountOut > 0n) {
|
|
93
|
+
console.log('Valid quote from:', quote.source);
|
|
94
|
+
// Proceed with swap
|
|
95
|
+
}
|
|
96
|
+
} catch (error) {
|
|
97
|
+
console.error('Failed to get quote:', error);
|
|
98
|
+
}
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Request Timeout Control
|
|
102
|
+
|
|
103
|
+
```typescript
|
|
104
|
+
const controller = new AbortController();
|
|
105
|
+
const timeoutId = setTimeout(() => controller.abort(), 10000); // 10s timeout
|
|
106
|
+
|
|
107
|
+
try {
|
|
108
|
+
const quote = await swapClient.getQuote(params, controller.signal);
|
|
109
|
+
clearTimeout(timeoutId);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
clearTimeout(timeoutId);
|
|
112
|
+
if (error.name === 'AbortError') {
|
|
113
|
+
console.error('Request timed out');
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
```
|
|
117
|
+
|
|
118
|
+
### Utility Functions
|
|
119
|
+
|
|
120
|
+
```typescript
|
|
121
|
+
import {
|
|
122
|
+
validateSwapParams,
|
|
123
|
+
calculatePriceImpact,
|
|
124
|
+
formatTokenAmount,
|
|
125
|
+
type SwapParams
|
|
126
|
+
} from "@shogun-sdk/swap";
|
|
127
|
+
|
|
128
|
+
// Validate swap parameters
|
|
129
|
+
const swapParams: SwapParams = {
|
|
130
|
+
tokenIn: '0x...',
|
|
131
|
+
tokenOut: '0x...',
|
|
132
|
+
amountIn: BigInt(1000000),
|
|
133
|
+
amountOutMin: BigInt(900000),
|
|
134
|
+
to: '0x...',
|
|
135
|
+
deadline: Math.floor(Date.now() / 1000) + 3600
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
validateSwapParams(swapParams);
|
|
139
|
+
|
|
140
|
+
// Calculate price impact
|
|
141
|
+
const priceImpact = calculatePriceImpact(
|
|
142
|
+
BigInt(1000000), // amountIn
|
|
143
|
+
BigInt(990000), // amountOut
|
|
144
|
+
1.0, // tokenInPrice
|
|
145
|
+
1.0 // tokenOutPrice
|
|
146
|
+
);
|
|
147
|
+
|
|
148
|
+
// Format token amounts
|
|
149
|
+
const formattedAmount = formatTokenAmount(BigInt(1000000), 6); // "1.0"
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## API Reference
|
|
153
|
+
|
|
154
|
+
### SwapClient
|
|
155
|
+
|
|
156
|
+
The main client class for fetching quotes.
|
|
157
|
+
|
|
158
|
+
#### Constructor
|
|
159
|
+
|
|
160
|
+
```typescript
|
|
161
|
+
new SwapClient(config: SwapClientConfig)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
**Config Options:**
|
|
165
|
+
- `apiKey: string` - API key for one-shot service
|
|
166
|
+
- `apiUrl: string` - API URL for one-shot service
|
|
167
|
+
- `timeout?: number` - Request timeout in milliseconds (optional)
|
|
168
|
+
|
|
169
|
+
#### Methods
|
|
170
|
+
|
|
171
|
+
##### `getQuote(params, signal?)`
|
|
172
|
+
|
|
173
|
+
Fetches a quote with intelligent fallback logic.
|
|
174
|
+
|
|
175
|
+
**Parameters:**
|
|
176
|
+
- `params: UnifiedQuoteParams` - Quote parameters
|
|
177
|
+
- `signal?: AbortSignal` - Optional abort signal for cancellation
|
|
178
|
+
|
|
179
|
+
**Returns:** `Promise<UnifiedQuoteResponse>`
|
|
180
|
+
|
|
181
|
+
##### `getSingleChainQuote(chainId, tokenIn, tokenOut, amount, slippageBps?, signal?)`
|
|
182
|
+
|
|
183
|
+
Gets a quote for same-chain swaps.
|
|
184
|
+
|
|
185
|
+
**Parameters:**
|
|
186
|
+
- `chainId: ChainID` - Chain identifier
|
|
187
|
+
- `tokenIn: string` - Input token address
|
|
188
|
+
- `tokenOut: string` - Output token address
|
|
189
|
+
- `amount: bigint` - Amount to swap
|
|
190
|
+
- `slippageBps?: number` - Slippage in basis points (optional)
|
|
191
|
+
- `signal?: AbortSignal` - Optional abort signal (optional)
|
|
192
|
+
|
|
193
|
+
**Returns:** `Promise<UnifiedQuoteResponse>`
|
|
194
|
+
|
|
195
|
+
### Types
|
|
196
|
+
|
|
197
|
+
#### `UnifiedQuoteParams`
|
|
198
|
+
|
|
199
|
+
```typescript
|
|
200
|
+
interface UnifiedQuoteParams {
|
|
201
|
+
sourceChainId: ChainID;
|
|
202
|
+
destChainId: ChainID;
|
|
203
|
+
tokenIn: string;
|
|
204
|
+
tokenOut: string;
|
|
205
|
+
amount: bigint;
|
|
206
|
+
senderAddress?: string;
|
|
207
|
+
destinationAddress?: string;
|
|
208
|
+
slippageBps?: number;
|
|
209
|
+
affiliateWallet?: string;
|
|
210
|
+
affiliateFee?: string;
|
|
211
|
+
jitoTip?: number;
|
|
212
|
+
gasRefuel?: number;
|
|
213
|
+
dynamicSlippage?: boolean;
|
|
214
|
+
externalCall?: string;
|
|
215
|
+
}
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
#### `UnifiedQuoteResponse`
|
|
219
|
+
|
|
220
|
+
```typescript
|
|
221
|
+
interface UnifiedQuoteResponse {
|
|
222
|
+
amountOut: bigint;
|
|
223
|
+
amountInUsd: number;
|
|
224
|
+
amountOutUsd: number;
|
|
225
|
+
amountOutReduced?: bigint;
|
|
226
|
+
amountOutUsdReduced?: number;
|
|
227
|
+
estimatedAmountInAsMinStablecoinAmount?: bigint;
|
|
228
|
+
priceImpact: number;
|
|
229
|
+
slippage: number;
|
|
230
|
+
provider: string;
|
|
231
|
+
rawQuote: any;
|
|
232
|
+
source: 'intents-sdk' | 'one-shot';
|
|
233
|
+
error?: string;
|
|
234
|
+
}
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### Utility Functions
|
|
238
|
+
|
|
239
|
+
- `validateSwapParams(params: SwapParams): void` - Validate swap parameters
|
|
240
|
+
- `calculatePriceImpact(amountIn, amountOut, tokenInPrice, tokenOutPrice): number` - Calculate price impact percentage
|
|
241
|
+
- `formatTokenAmount(amount: bigint, decimals: number): string` - Format token amounts for display
|
|
242
|
+
|
|
243
|
+
## Supported Chains
|
|
244
|
+
|
|
245
|
+
- **Ethereum**
|
|
246
|
+
- **Arbitrum**
|
|
247
|
+
- **Polygon**
|
|
248
|
+
- **Solana**
|
|
249
|
+
- **Sui**
|
|
250
|
+
- **Hyperliquid**
|
|
251
|
+
- **Base**
|
|
252
|
+
- **BSC**
|
|
253
|
+
|
|
254
|
+
## Quote Sources
|
|
255
|
+
|
|
256
|
+
The SDK uses a two-tier approach for quote fetching:
|
|
257
|
+
|
|
258
|
+
1. **Primary: Intents SDK** - Fast, local quote calculation using integrated DEX aggregators
|
|
259
|
+
2. **Fallback: One-Shot API** - Comprehensive quote service with additional routing options
|
|
260
|
+
|
|
261
|
+
This ensures maximum reliability and the best possible quotes for your swaps.
|
|
262
|
+
|
|
263
|
+
## Error Handling
|
|
264
|
+
|
|
265
|
+
The SDK provides comprehensive error handling:
|
|
266
|
+
|
|
267
|
+
- **Network Errors** - Automatic retry with fallback
|
|
268
|
+
- **Invalid Parameters** - Clear validation error messages
|
|
269
|
+
- **Timeout Handling** - Configurable request timeouts
|
|
270
|
+
- **Quote Errors** - Detailed error information in response
|
|
271
|
+
|
|
272
|
+
## License
|
|
273
|
+
|
|
274
|
+
ISC
|