@switch-win/sdk 1.0.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/README.md +950 -0
- package/abi/SwitchLimitOrderABI.json +1296 -0
- package/abi/SwitchRouterABI.json +954 -0
- package/package.json +41 -0
- package/src/constants.ts +238 -0
- package/src/index.ts +110 -0
- package/src/limit-orders.ts +435 -0
- package/src/types.ts +423 -0
package/package.json
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@switch-win/sdk",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Official integration kit for the Switch DEX Aggregator on PulseChain — types, constants, ABIs, limit orders, and examples",
|
|
5
|
+
"author": "BuildTheTech",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"repository": {
|
|
8
|
+
"type": "git",
|
|
9
|
+
"url": "https://github.com/BuildTheTech/Switch-SDK.git"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"switch",
|
|
13
|
+
"dex",
|
|
14
|
+
"aggregator",
|
|
15
|
+
"pulsechain",
|
|
16
|
+
"swap",
|
|
17
|
+
"limit-order",
|
|
18
|
+
"eip712",
|
|
19
|
+
"defi",
|
|
20
|
+
"sdk"
|
|
21
|
+
],
|
|
22
|
+
"type": "module",
|
|
23
|
+
"main": "src/index.js",
|
|
24
|
+
"types": "src/index.d.ts",
|
|
25
|
+
"exports": {
|
|
26
|
+
".": "./src/index.ts",
|
|
27
|
+
"./types": "./src/types.ts",
|
|
28
|
+
"./constants": "./src/constants.ts",
|
|
29
|
+
"./limit-orders": "./src/limit-orders.ts",
|
|
30
|
+
"./abi": "./abi/SwitchRouterABI.json",
|
|
31
|
+
"./abi/limit-order": "./abi/SwitchLimitOrderABI.json"
|
|
32
|
+
},
|
|
33
|
+
"files": [
|
|
34
|
+
"src/",
|
|
35
|
+
"abi/",
|
|
36
|
+
"README.md"
|
|
37
|
+
],
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"typescript": "^5.9.3"
|
|
40
|
+
}
|
|
41
|
+
}
|
package/src/constants.ts
ADDED
|
@@ -0,0 +1,238 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Switch DEX Aggregator — Constants
|
|
3
|
+
*
|
|
4
|
+
* Addresses, ABIs, and configuration constants for PulseChain integration.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { SWITCH_ROUTER, NATIVE_PLS, ERC20_ABI } from "@switch-win/sdk/constants";
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
// ── API --
|
|
13
|
+
|
|
14
|
+
/** Base URL for the Switch quote API */
|
|
15
|
+
export const API_BASE = "https://quote.switch.win";
|
|
16
|
+
|
|
17
|
+
// ── API Endpoints --
|
|
18
|
+
|
|
19
|
+
/** Swap quote endpoint — returns optimal routing and tx calldata */
|
|
20
|
+
export const QUOTE_ENDPOINT = `${API_BASE}/swap/quote`;
|
|
21
|
+
|
|
22
|
+
/** Adapters endpoint — lists all available DEX adapters */
|
|
23
|
+
export const ADAPTERS_ENDPOINT = `${API_BASE}/swap/adapters`;
|
|
24
|
+
|
|
25
|
+
/** Check Tax endpoint — detects fee-on-transfer tokens */
|
|
26
|
+
export const CHECK_TAX_ENDPOINT = `${API_BASE}/swap/checkTax`;
|
|
27
|
+
|
|
28
|
+
// ── Chain --
|
|
29
|
+
|
|
30
|
+
/** PulseChain chain ID */
|
|
31
|
+
export const CHAIN_ID = 369;
|
|
32
|
+
|
|
33
|
+
// ── Contract addresses --
|
|
34
|
+
|
|
35
|
+
/** SwitchRouter contract — target for all swap transactions */
|
|
36
|
+
export const SWITCH_ROUTER = "0x31077B259e7fEEB7bE39bF298274BaE94Ee57B7a";
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Native PLS sentinel address.
|
|
40
|
+
* Use this as `from` or `to` when swapping native PLS (not WPLS the ERC-20).
|
|
41
|
+
*/
|
|
42
|
+
export const NATIVE_PLS = "0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE";
|
|
43
|
+
|
|
44
|
+
/** Wrapped PLS (WPLS) ERC-20 token address */
|
|
45
|
+
export const WPLS = "0xA1077a294dDE1B09bB078844df40758a5D0f9a27";
|
|
46
|
+
|
|
47
|
+
// ── Limit Order contract ────────────────────────────────────────────────
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* SwitchLimitOrder contract address on PulseChain.
|
|
51
|
+
*
|
|
52
|
+
* The EIP-712 domain `verifyingContract` MUST match this address.
|
|
53
|
+
*/
|
|
54
|
+
export const SWITCH_LIMIT_ORDER = "0x28754379e9E9867A64b77437930cBc5009939692";
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* SwitchPLSFlow contract address on PulseChain.
|
|
58
|
+
*
|
|
59
|
+
* Used for native PLS limit orders. Users send PLS to this contract,
|
|
60
|
+
* which wraps to WPLS and creates the limit order on their behalf.
|
|
61
|
+
* No EIP-712 signing required — single transaction.
|
|
62
|
+
*/
|
|
63
|
+
export const SWITCH_PLS_FLOW = "0x0fD3fD40F06159606165F21047B83136172273E3";
|
|
64
|
+
|
|
65
|
+
// ── Limit Order API endpoints ───────────────────────────────────────────────
|
|
66
|
+
|
|
67
|
+
/** Base URL for the Switch backend API (limit orders) */
|
|
68
|
+
export const LIMIT_ORDER_API_BASE = "https://api.switch.win";
|
|
69
|
+
|
|
70
|
+
/** Limit orders REST endpoint */
|
|
71
|
+
export const LIMIT_ORDERS_ENDPOINT = `${LIMIT_ORDER_API_BASE}/limit-orders`;
|
|
72
|
+
|
|
73
|
+
/** Limit order pairs endpoint */
|
|
74
|
+
export const LIMIT_ORDER_PAIRS_ENDPOINT = `${LIMIT_ORDERS_ENDPOINT}/pairs`;
|
|
75
|
+
|
|
76
|
+
/** Limit order stats endpoint */
|
|
77
|
+
export const LIMIT_ORDER_STATS_ENDPOINT = `${LIMIT_ORDERS_ENDPOINT}/stats`;
|
|
78
|
+
|
|
79
|
+
// ── Fee constants --
|
|
80
|
+
|
|
81
|
+
/** Fee denominator — all fees are in basis points (1 bps = 0.01%) */
|
|
82
|
+
export const FEE_DENOMINATOR = 10_000;
|
|
83
|
+
|
|
84
|
+
/** Maximum allowed fee (basis points). 100 bps = 1% */
|
|
85
|
+
export const MAX_FEE_BPS = 100;
|
|
86
|
+
|
|
87
|
+
/** Maximum allowed slippage (basis points). 5000 bps = 50% */
|
|
88
|
+
export const MAX_SLIPPAGE_BPS = 5_000;
|
|
89
|
+
|
|
90
|
+
/** Default slippage if not specified (basis points). 50 bps = 0.50% */
|
|
91
|
+
export const DEFAULT_SLIPPAGE_BPS = 50;
|
|
92
|
+
|
|
93
|
+
// ── Well-known tokens --
|
|
94
|
+
|
|
95
|
+
/** Well-known blue-chip tokens on PulseChain (lowercased for easy comparison) */
|
|
96
|
+
export const BLUE_CHIPS = new Set([
|
|
97
|
+
"0xefd766ccb38eaf1dfd701853bfce31359239f305", // DAI (bridged)
|
|
98
|
+
"0x15d38573d2feeb82e7ad5187ab8c1d52810b1f07", // USDC (bridged)
|
|
99
|
+
"0x0cb6f5a34ad42ec934882a05265a7d5f59b51a2f", // USDT (bridged)
|
|
100
|
+
"0x2b591e99afe9f32eaa6214f7b7629768c40eeb39", // pHEX
|
|
101
|
+
"0x57fde0a71132198bbec939b98976993d8d89d225", // eHEX
|
|
102
|
+
"0x95b303987a60c71504d99aa1b13b4da07b0790ab", // PLSX
|
|
103
|
+
"0x2fa878ab3f87cc1c9737fc071108f904c0b0c95d", // INC
|
|
104
|
+
"0x02dcdd04e3f455d838cd1249292c58f3b79e3c3c", // WETH (bridged)
|
|
105
|
+
]);
|
|
106
|
+
|
|
107
|
+
// ── ABIs --
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Minimal ERC-20 ABI for approval and allowance checks.
|
|
111
|
+
* Use with ethers.js:
|
|
112
|
+
* ```ts
|
|
113
|
+
* const token = new ethers.Contract(tokenAddress, ERC20_ABI, signer);
|
|
114
|
+
* ```
|
|
115
|
+
*/
|
|
116
|
+
export const ERC20_ABI = [
|
|
117
|
+
"function approve(address spender, uint256 amount) returns (bool)",
|
|
118
|
+
"function allowance(address owner, address spender) view returns (uint256)",
|
|
119
|
+
"function balanceOf(address account) view returns (uint256)",
|
|
120
|
+
"function decimals() view returns (uint8)",
|
|
121
|
+
"function symbol() view returns (string)",
|
|
122
|
+
"function name() view returns (string)",
|
|
123
|
+
] as const;
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Minimal ABI fragment for the SwitchRouter goSwitch function.
|
|
127
|
+
* Normally you don't need this — the API returns pre-encoded calldata in `tx.data`.
|
|
128
|
+
* Useful only if you want to decode or manually construct calldata.
|
|
129
|
+
*/
|
|
130
|
+
export const GO_SWITCH_ABI = [
|
|
131
|
+
"function goSwitch(tuple(uint256 amountIn, tuple(address tokenIn, address tokenOut, tuple(address adapter, uint256 amountIn)[] legs)[] hops)[] _routes, address _to, uint256 _minTotalAmountOut, uint256 _fee, bool _feeOnOutput, bool _unwrapOutput, address _partnerAddress) payable",
|
|
132
|
+
] as const;
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* SwitchRouter custom error signatures for decoding reverts.
|
|
136
|
+
*/
|
|
137
|
+
export const SWITCH_ROUTER_ERRORS = [
|
|
138
|
+
"error FinalAmountOutTooLow()",
|
|
139
|
+
"error ExcessiveFee()",
|
|
140
|
+
"error InsufficientFee()",
|
|
141
|
+
"error MsgValueMismatch()",
|
|
142
|
+
"error ZeroInput()",
|
|
143
|
+
] as const;
|
|
144
|
+
|
|
145
|
+
// ═══════════════════════════════════════════════════════════════════════════════
|
|
146
|
+
// Limit Order — EIP-712 Constants
|
|
147
|
+
// ═══════════════════════════════════════════════════════════════════════════════
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* EIP-712 domain for SwitchLimitOrder.
|
|
151
|
+
*
|
|
152
|
+
* Must match the domain used in the contract's constructor:
|
|
153
|
+
* `EIP712("SwitchLimitOrder", "2")`
|
|
154
|
+
*
|
|
155
|
+
* **Important:** Update `verifyingContract` when the contract is deployed.
|
|
156
|
+
*/
|
|
157
|
+
export const LIMIT_ORDER_EIP712_DOMAIN = {
|
|
158
|
+
name: "SwitchLimitOrder",
|
|
159
|
+
version: "2",
|
|
160
|
+
chainId: CHAIN_ID,
|
|
161
|
+
verifyingContract: SWITCH_LIMIT_ORDER,
|
|
162
|
+
} as const;
|
|
163
|
+
|
|
164
|
+
/**
|
|
165
|
+
* EIP-712 type definition for the LimitOrder struct.
|
|
166
|
+
*
|
|
167
|
+
* Used with `ethers.signTypedData(domain, types, value)` or equivalent.
|
|
168
|
+
* Must match the struct order and types in `SwitchLimitOrder.sol`.
|
|
169
|
+
*/
|
|
170
|
+
export const LIMIT_ORDER_EIP712_TYPES = {
|
|
171
|
+
LimitOrder: [
|
|
172
|
+
{ name: "maker", type: "address" },
|
|
173
|
+
{ name: "tokenIn", type: "address" },
|
|
174
|
+
{ name: "tokenOut", type: "address" },
|
|
175
|
+
{ name: "amountIn", type: "uint256" },
|
|
176
|
+
{ name: "minAmountOut", type: "uint256" },
|
|
177
|
+
{ name: "deadline", type: "uint256" },
|
|
178
|
+
{ name: "nonce", type: "uint256" },
|
|
179
|
+
{ name: "feeOnOutput", type: "bool" },
|
|
180
|
+
{ name: "recipient", type: "address" },
|
|
181
|
+
{ name: "unwrapOutput", type: "bool" },
|
|
182
|
+
],
|
|
183
|
+
} as const;
|
|
184
|
+
|
|
185
|
+
/**
|
|
186
|
+
* Minimal ABI for the SwitchLimitOrder contract.
|
|
187
|
+
*
|
|
188
|
+
* Includes only the functions relevant to order makers:
|
|
189
|
+
* - `invalidateNonce(uint256)` — cancel a single order on-chain
|
|
190
|
+
* - `invalidateNonces(uint256[])` — cancel multiple orders on-chain
|
|
191
|
+
* - `isNonceUsed(address,uint256)` — check if a nonce has been used
|
|
192
|
+
* - `canFillOrder(order,signature)` — check if an order is fillable
|
|
193
|
+
*
|
|
194
|
+
* The full ABI is available at `@switch-win/sdk/abi/limit-order`.
|
|
195
|
+
*/
|
|
196
|
+
export const LIMIT_ORDER_ABI = [
|
|
197
|
+
"function invalidateNonce(uint256 _nonce) external",
|
|
198
|
+
"function invalidateNonces(uint256[] calldata _nonces) external",
|
|
199
|
+
"function isNonceUsed(address maker, uint256 nonce) view returns (bool)",
|
|
200
|
+
"function canFillOrder(tuple(address maker, address tokenIn, address tokenOut, uint256 amountIn, uint256 minAmountOut, uint256 deadline, uint256 nonce, bool feeOnOutput, address recipient, bool unwrapOutput) order, bytes signature) view returns (bool)",
|
|
201
|
+
] as const;
|
|
202
|
+
|
|
203
|
+
/**
|
|
204
|
+
* SwitchLimitOrder custom error signatures for decoding reverts.
|
|
205
|
+
*/
|
|
206
|
+
export const LIMIT_ORDER_ERRORS = [
|
|
207
|
+
"error ExcessiveFee()",
|
|
208
|
+
"error InsufficientOutput()",
|
|
209
|
+
"error InvalidAmount()",
|
|
210
|
+
"error InvalidSignature()",
|
|
211
|
+
"error InvalidTokens()",
|
|
212
|
+
"error NonceAlreadyUsed()",
|
|
213
|
+
"error OrderExpired()",
|
|
214
|
+
"error RouteInputExceedsMax()",
|
|
215
|
+
"error TransferFailed()",
|
|
216
|
+
] as const;
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* Minimal ABI for the SwitchPLSFlow contract.
|
|
220
|
+
*
|
|
221
|
+
* Used for creating native PLS limit orders in a single transaction.
|
|
222
|
+
* The contract wraps PLS to WPLS and places the limit order on behalf
|
|
223
|
+
* of the sender.
|
|
224
|
+
*/
|
|
225
|
+
export const PLS_FLOW_ABI = [
|
|
226
|
+
"function createOrder(address tokenOut, uint256 minAmountOut, uint256 deadline, bool feeOnOutput, bool unwrapOutput) payable returns (uint256 nonce)",
|
|
227
|
+
"function cancelOrder(uint256 nonce) external",
|
|
228
|
+
"function orderNonces(address owner) view returns (uint256)",
|
|
229
|
+
"event PLSOrderCreated(address indexed owner, uint256 indexed nonce, address tokenOut, uint256 amountIn, uint256 minAmountOut, uint256 deadline, bool feeOnOutput, bool unwrapOutput)",
|
|
230
|
+
] as const;
|
|
231
|
+
|
|
232
|
+
/**
|
|
233
|
+
* SwitchPLSFlow custom error signatures for decoding reverts.
|
|
234
|
+
*/
|
|
235
|
+
export const PLS_FLOW_ERRORS = [
|
|
236
|
+
"error ZeroAmountIn()",
|
|
237
|
+
"error InvalidTokenOut()",
|
|
238
|
+
] as const;
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Switch SDK — Main entry point
|
|
3
|
+
*
|
|
4
|
+
* Re-exports all types and constants for convenience.
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* import { SWITCH_ROUTER, NATIVE_PLS, type BestPathResponse } from "@switch-win/sdk";
|
|
9
|
+
* ```
|
|
10
|
+
*
|
|
11
|
+
* @example
|
|
12
|
+
* ```ts
|
|
13
|
+
* import { buildLimitOrder, submitLimitOrder, type LimitOrderParams } from "@switch-win/sdk";
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
|
|
17
|
+
// Types — Swap
|
|
18
|
+
export type {
|
|
19
|
+
BestPathResponse,
|
|
20
|
+
SwapTransaction,
|
|
21
|
+
SwapPath,
|
|
22
|
+
SwapPathLeg,
|
|
23
|
+
RouteAllocationPlan,
|
|
24
|
+
SingleRouteAllocation,
|
|
25
|
+
HopAllocationPlan,
|
|
26
|
+
HopAdapterAllocationPlan,
|
|
27
|
+
TokenTaxResponse,
|
|
28
|
+
ErrorResponse,
|
|
29
|
+
QuoteResponse,
|
|
30
|
+
AdapterInfo,
|
|
31
|
+
AdaptersResponse,
|
|
32
|
+
CheckTaxResponse,
|
|
33
|
+
CheckTaxResult,
|
|
34
|
+
} from "./types.js";
|
|
35
|
+
|
|
36
|
+
// Types — Limit Orders
|
|
37
|
+
export type {
|
|
38
|
+
LimitOrderParams,
|
|
39
|
+
SignedLimitOrder,
|
|
40
|
+
CreateLimitOrderRequest,
|
|
41
|
+
CancelLimitOrderRequest,
|
|
42
|
+
LimitOrderStatus,
|
|
43
|
+
LimitOrderRecord,
|
|
44
|
+
CreateLimitOrderResponse,
|
|
45
|
+
CancelLimitOrderResponse,
|
|
46
|
+
ListLimitOrdersResponse,
|
|
47
|
+
LimitOrderPair,
|
|
48
|
+
LimitOrderStats,
|
|
49
|
+
LimitOrderMutationResponse,
|
|
50
|
+
} from "./types.js";
|
|
51
|
+
|
|
52
|
+
export { isErrorResponse } from "./types.js";
|
|
53
|
+
|
|
54
|
+
// Constants — Swap
|
|
55
|
+
export {
|
|
56
|
+
API_BASE,
|
|
57
|
+
QUOTE_ENDPOINT,
|
|
58
|
+
ADAPTERS_ENDPOINT,
|
|
59
|
+
CHECK_TAX_ENDPOINT,
|
|
60
|
+
CHAIN_ID,
|
|
61
|
+
SWITCH_ROUTER,
|
|
62
|
+
NATIVE_PLS,
|
|
63
|
+
WPLS,
|
|
64
|
+
FEE_DENOMINATOR,
|
|
65
|
+
MAX_FEE_BPS,
|
|
66
|
+
MAX_SLIPPAGE_BPS,
|
|
67
|
+
DEFAULT_SLIPPAGE_BPS,
|
|
68
|
+
BLUE_CHIPS,
|
|
69
|
+
ERC20_ABI,
|
|
70
|
+
GO_SWITCH_ABI,
|
|
71
|
+
SWITCH_ROUTER_ERRORS,
|
|
72
|
+
} from "./constants.js";
|
|
73
|
+
|
|
74
|
+
// Constants — Limit Orders
|
|
75
|
+
export {
|
|
76
|
+
SWITCH_LIMIT_ORDER,
|
|
77
|
+
SWITCH_PLS_FLOW,
|
|
78
|
+
LIMIT_ORDER_API_BASE,
|
|
79
|
+
LIMIT_ORDERS_ENDPOINT,
|
|
80
|
+
LIMIT_ORDER_PAIRS_ENDPOINT,
|
|
81
|
+
LIMIT_ORDER_STATS_ENDPOINT,
|
|
82
|
+
LIMIT_ORDER_EIP712_DOMAIN,
|
|
83
|
+
LIMIT_ORDER_EIP712_TYPES,
|
|
84
|
+
LIMIT_ORDER_ABI,
|
|
85
|
+
LIMIT_ORDER_ERRORS,
|
|
86
|
+
PLS_FLOW_ABI,
|
|
87
|
+
PLS_FLOW_ERRORS,
|
|
88
|
+
} from "./constants.js";
|
|
89
|
+
|
|
90
|
+
// Limit Order helpers
|
|
91
|
+
export {
|
|
92
|
+
buildLimitOrder,
|
|
93
|
+
getEIP712SigningParams,
|
|
94
|
+
getApprovalTarget,
|
|
95
|
+
getRouterApprovalTarget,
|
|
96
|
+
shouldUnwrapOutput,
|
|
97
|
+
getPLSFlowAddress,
|
|
98
|
+
isNativePLS,
|
|
99
|
+
submitLimitOrder,
|
|
100
|
+
cancelLimitOrder,
|
|
101
|
+
fetchLimitOrders,
|
|
102
|
+
fetchLimitOrder,
|
|
103
|
+
fetchLimitOrderPairs,
|
|
104
|
+
fetchLimitOrderStats,
|
|
105
|
+
} from "./limit-orders.js";
|
|
106
|
+
|
|
107
|
+
export type {
|
|
108
|
+
BuildLimitOrderOptions,
|
|
109
|
+
ListLimitOrdersOptions,
|
|
110
|
+
} from "./limit-orders.js";
|