@switch-win/sdk 1.0.5 → 1.0.7
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 +7 -2
- package/package.json +1 -1
- package/src/constants.ts +85 -11
- package/src/index.ts +3 -0
package/README.md
CHANGED
|
@@ -86,7 +86,8 @@ curl -H "x-api-key: YOUR_KEY" \
|
|
|
86
86
|
# 2. Approve the SwitchRouter to spend your input token (ERC-20 only, skip for native PLS)
|
|
87
87
|
|
|
88
88
|
# 3. Send the transaction using the `tx` object from the response:
|
|
89
|
-
# { to: "
|
|
89
|
+
# { to: "0x0305...", data: "0x...", value: "0" }
|
|
90
|
+
# ⚠️ Always use tx.to — do NOT hardcode the router address
|
|
90
91
|
```
|
|
91
92
|
|
|
92
93
|
### Using the SDK types (TypeScript)
|
|
@@ -106,10 +107,12 @@ const quote: BestPathResponse = await res.json();
|
|
|
106
107
|
const token = new ethers.Contract(fromToken, ["function approve(address,uint256)"], signer);
|
|
107
108
|
await (await token.approve(SWITCH_ROUTER, amount)).wait();
|
|
108
109
|
|
|
109
|
-
// 3. Send the swap
|
|
110
|
+
// 3. Send the swap — always use quote.tx which includes the correct router address
|
|
110
111
|
await signer.sendTransaction(quote.tx);
|
|
111
112
|
```
|
|
112
113
|
|
|
114
|
+
> **⚠️ Always use `tx.to` from the quote response** when sending swap transactions. Do NOT hardcode the router address — the contract may be redeployed.
|
|
115
|
+
|
|
113
116
|
For a production integration with tax token handling, adapter filtering, and fee mode selection, see [Swap Integration Flow](#swap-integration-flow) and the [full examples](examples/).
|
|
114
117
|
|
|
115
118
|
---
|
|
@@ -297,6 +300,8 @@ console.log("Swap confirmed:", receipt.hash);
|
|
|
297
300
|
|
|
298
301
|
That's it. The `tx.data` already encodes the correct `goSwitch()` call with your routes, slippage protection, fee, and partner address.
|
|
299
302
|
|
|
303
|
+
> **⚠️ Always use `tx.to` from the response** — do NOT hardcode the router address. The `SWITCH_ROUTER` constant is provided for **token approvals only**. The router contract may be redeployed; `tx.to` always points to the current production router.
|
|
304
|
+
|
|
300
305
|
The `tx` object does not include a `gas` field. Most wallet libraries (ethers.js, web3.py, MetaMask) auto-estimate gas when `gas` is omitted. For multi-hop split swaps, `500,000`–`1,000,000` gas is typically sufficient:
|
|
301
306
|
|
|
302
307
|
```js
|
package/package.json
CHANGED
package/src/constants.ts
CHANGED
|
@@ -32,8 +32,15 @@ export const CHAIN_ID = 369;
|
|
|
32
32
|
|
|
33
33
|
// ── Contract addresses --
|
|
34
34
|
|
|
35
|
-
/**
|
|
36
|
-
|
|
35
|
+
/**
|
|
36
|
+
* SwitchRouter contract — approval target for swap transactions.
|
|
37
|
+
*
|
|
38
|
+
* **Important:** Do NOT hardcode this address as the `to` field when sending
|
|
39
|
+
* swap transactions. Always use `tx.to` from the quote response — the router
|
|
40
|
+
* contract may be redeployed. This constant is safe for **approvals only**
|
|
41
|
+
* (the new router honours legacy allowances).
|
|
42
|
+
*/
|
|
43
|
+
export const SWITCH_ROUTER = "0x0305fcb5dA680EA6fd1B01A96C1949175B99d406";
|
|
37
44
|
|
|
38
45
|
/**
|
|
39
46
|
* Native PLS sentinel address.
|
|
@@ -47,11 +54,11 @@ export const WPLS = "0xA1077a294dDE1B09bB078844df40758a5D0f9a27";
|
|
|
47
54
|
// ── Limit Order contract ────────────────────────────────────────────────
|
|
48
55
|
|
|
49
56
|
/**
|
|
50
|
-
* SwitchLimitOrder contract address on PulseChain.
|
|
57
|
+
* SwitchLimitOrder contract address on PulseChain (V4).
|
|
51
58
|
*
|
|
52
59
|
* The EIP-712 domain `verifyingContract` MUST match this address.
|
|
53
60
|
*/
|
|
54
|
-
export const SWITCH_LIMIT_ORDER = "
|
|
61
|
+
export const SWITCH_LIMIT_ORDER = "0x8e3881bdF81Fc0211383B2e576076B654F7aFD86";
|
|
55
62
|
|
|
56
63
|
/**
|
|
57
64
|
* SwitchPLSFlow contract address on PulseChain.
|
|
@@ -135,13 +142,49 @@ export const GO_SWITCH_ABI = [
|
|
|
135
142
|
* SwitchRouter custom error signatures for decoding reverts.
|
|
136
143
|
*/
|
|
137
144
|
export const SWITCH_ROUTER_ERRORS = [
|
|
138
|
-
"error FinalAmountOutTooLow()",
|
|
139
|
-
"error ExcessiveFee()",
|
|
140
145
|
"error InsufficientFee()",
|
|
141
|
-
"error
|
|
146
|
+
"error ExcessiveFee()",
|
|
147
|
+
"error FinalAmountOutTooLow()",
|
|
148
|
+
"error EmptySplit()",
|
|
149
|
+
"error SplitMixedTokenIn()",
|
|
150
|
+
"error SplitMixedTokenOut()",
|
|
142
151
|
"error ZeroInput()",
|
|
152
|
+
"error MsgValueMismatch()",
|
|
153
|
+
"error PathNeedsBeginWithWPLS()",
|
|
154
|
+
"error PathNeedsEndWithWPLS()",
|
|
155
|
+
"error AdapterNotApproved(address)",
|
|
156
|
+
"error EmptyHop()",
|
|
157
|
+
"error FeeNotSupported()",
|
|
143
158
|
] as const;
|
|
144
159
|
|
|
160
|
+
/**
|
|
161
|
+
* 4-byte error selectors for SwitchRouter reverts.
|
|
162
|
+
*
|
|
163
|
+
* Useful for matching raw revert data from `eth_call` or failed transactions.
|
|
164
|
+
* Key = selector hex, value = human-readable error name.
|
|
165
|
+
*
|
|
166
|
+
* @example
|
|
167
|
+
* ```ts
|
|
168
|
+
* const selector = revertData.slice(0, 10); // "0x4a2ab023"
|
|
169
|
+
* const name = SWITCH_ROUTER_ERROR_SELECTORS[selector]; // "FinalAmountOutTooLow"
|
|
170
|
+
* ```
|
|
171
|
+
*/
|
|
172
|
+
export const SWITCH_ROUTER_ERROR_SELECTORS: Record<string, string> = {
|
|
173
|
+
"0x025dbdd4": "InsufficientFee",
|
|
174
|
+
"0x2977da44": "ExcessiveFee",
|
|
175
|
+
"0x4a2ab023": "FinalAmountOutTooLow",
|
|
176
|
+
"0x5725cad2": "EmptySplit",
|
|
177
|
+
"0xdceb8b7a": "SplitMixedTokenIn",
|
|
178
|
+
"0x45a68c8c": "SplitMixedTokenOut",
|
|
179
|
+
"0xaf458c07": "ZeroInput",
|
|
180
|
+
"0xbc6f88c5": "MsgValueMismatch",
|
|
181
|
+
"0x906478dc": "PathNeedsBeginWithWPLS",
|
|
182
|
+
"0x037ccaee": "PathNeedsEndWithWPLS",
|
|
183
|
+
"0x0c48343e": "AdapterNotApproved",
|
|
184
|
+
"0xb19ef58a": "EmptyHop",
|
|
185
|
+
"0x73620122": "FeeNotSupported",
|
|
186
|
+
};
|
|
187
|
+
|
|
145
188
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
146
189
|
// Limit Order — EIP-712 Constants
|
|
147
190
|
// ═══════════════════════════════════════════════════════════════════════════════
|
|
@@ -205,17 +248,40 @@ export const LIMIT_ORDER_ABI = [
|
|
|
205
248
|
* SwitchLimitOrder custom error signatures for decoding reverts.
|
|
206
249
|
*/
|
|
207
250
|
export const LIMIT_ORDER_ERRORS = [
|
|
208
|
-
"error ExcessiveFee()",
|
|
209
|
-
"error InsufficientOutput()",
|
|
210
|
-
"error InvalidAmount()",
|
|
211
251
|
"error InvalidSignature()",
|
|
212
|
-
"error InvalidTokens()",
|
|
213
252
|
"error NonceAlreadyUsed()",
|
|
214
253
|
"error OrderExpired()",
|
|
254
|
+
"error InsufficientOutput()",
|
|
255
|
+
"error InvalidAmount()",
|
|
256
|
+
"error ExcessiveFee()",
|
|
215
257
|
"error RouteInputExceedsMax()",
|
|
258
|
+
"error InvalidTokens()",
|
|
216
259
|
"error TransferFailed()",
|
|
260
|
+
"error OperatorOnly()",
|
|
217
261
|
] as const;
|
|
218
262
|
|
|
263
|
+
/**
|
|
264
|
+
* 4-byte error selectors for SwitchLimitOrder reverts.
|
|
265
|
+
*
|
|
266
|
+
* @example
|
|
267
|
+
* ```ts
|
|
268
|
+
* const selector = revertData.slice(0, 10); // "0x8baa579f"
|
|
269
|
+
* const name = LIMIT_ORDER_ERROR_SELECTORS[selector]; // "InvalidSignature"
|
|
270
|
+
* ```
|
|
271
|
+
*/
|
|
272
|
+
export const LIMIT_ORDER_ERROR_SELECTORS: Record<string, string> = {
|
|
273
|
+
"0x8baa579f": "InvalidSignature",
|
|
274
|
+
"0x1fb09b80": "NonceAlreadyUsed",
|
|
275
|
+
"0xc56873ba": "OrderExpired",
|
|
276
|
+
"0xbb2875c3": "InsufficientOutput",
|
|
277
|
+
"0x2c5211c6": "InvalidAmount",
|
|
278
|
+
"0x2977da44": "ExcessiveFee",
|
|
279
|
+
"0xb6972a87": "RouteInputExceedsMax",
|
|
280
|
+
"0x672215de": "InvalidTokens",
|
|
281
|
+
"0x90b8ec18": "TransferFailed",
|
|
282
|
+
"0xae5e3e00": "OperatorOnly",
|
|
283
|
+
};
|
|
284
|
+
|
|
219
285
|
/**
|
|
220
286
|
* Minimal ABI for the SwitchPLSFlow contract.
|
|
221
287
|
*
|
|
@@ -237,3 +303,11 @@ export const PLS_FLOW_ERRORS = [
|
|
|
237
303
|
"error ZeroAmountIn()",
|
|
238
304
|
"error InvalidTokenOut()",
|
|
239
305
|
] as const;
|
|
306
|
+
|
|
307
|
+
/**
|
|
308
|
+
* 4-byte error selectors for SwitchPLSFlow reverts.
|
|
309
|
+
*/
|
|
310
|
+
export const PLS_FLOW_ERROR_SELECTORS: Record<string, string> = {
|
|
311
|
+
"0x990965c1": "ZeroAmountIn",
|
|
312
|
+
"0x1b6d1fa0": "InvalidTokenOut",
|
|
313
|
+
};
|
package/src/index.ts
CHANGED
|
@@ -69,6 +69,7 @@ export {
|
|
|
69
69
|
ERC20_ABI,
|
|
70
70
|
GO_SWITCH_ABI,
|
|
71
71
|
SWITCH_ROUTER_ERRORS,
|
|
72
|
+
SWITCH_ROUTER_ERROR_SELECTORS,
|
|
72
73
|
} from "./constants.js";
|
|
73
74
|
|
|
74
75
|
// Constants — Limit Orders
|
|
@@ -83,8 +84,10 @@ export {
|
|
|
83
84
|
LIMIT_ORDER_EIP712_TYPES,
|
|
84
85
|
LIMIT_ORDER_ABI,
|
|
85
86
|
LIMIT_ORDER_ERRORS,
|
|
87
|
+
LIMIT_ORDER_ERROR_SELECTORS,
|
|
86
88
|
PLS_FLOW_ABI,
|
|
87
89
|
PLS_FLOW_ERRORS,
|
|
90
|
+
PLS_FLOW_ERROR_SELECTORS,
|
|
88
91
|
} from "./constants.js";
|
|
89
92
|
|
|
90
93
|
// Limit Order helpers
|