@catalyst-team/poly-mcp 0.1.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 +317 -0
- package/dist/errors.d.ts +33 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +86 -0
- package/dist/errors.js.map +1 -0
- package/dist/index.d.ts +62 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +173 -0
- package/dist/index.js.map +1 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.d.ts.map +1 -0
- package/dist/server.js +155 -0
- package/dist/server.js.map +1 -0
- package/dist/tools/guide.d.ts +12 -0
- package/dist/tools/guide.d.ts.map +1 -0
- package/dist/tools/guide.js +801 -0
- package/dist/tools/guide.js.map +1 -0
- package/dist/tools/index.d.ts +11 -0
- package/dist/tools/index.d.ts.map +1 -0
- package/dist/tools/index.js +27 -0
- package/dist/tools/index.js.map +1 -0
- package/dist/tools/market.d.ts +11 -0
- package/dist/tools/market.d.ts.map +1 -0
- package/dist/tools/market.js +314 -0
- package/dist/tools/market.js.map +1 -0
- package/dist/tools/order.d.ts +10 -0
- package/dist/tools/order.d.ts.map +1 -0
- package/dist/tools/order.js +258 -0
- package/dist/tools/order.js.map +1 -0
- package/dist/tools/trade.d.ts +38 -0
- package/dist/tools/trade.d.ts.map +1 -0
- package/dist/tools/trade.js +313 -0
- package/dist/tools/trade.js.map +1 -0
- package/dist/tools/trader.d.ts +11 -0
- package/dist/tools/trader.d.ts.map +1 -0
- package/dist/tools/trader.js +277 -0
- package/dist/tools/trader.js.map +1 -0
- package/dist/tools/wallet.d.ts +274 -0
- package/dist/tools/wallet.d.ts.map +1 -0
- package/dist/tools/wallet.js +579 -0
- package/dist/tools/wallet.js.map +1 -0
- package/dist/types.d.ts +413 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +5 -0
- package/dist/types.js.map +1 -0
- package/docs/01-mcp.md +2075 -0
- package/package.json +55 -0
- package/src/errors.ts +124 -0
- package/src/index.ts +309 -0
- package/src/server.ts +183 -0
- package/src/tools/guide.ts +821 -0
- package/src/tools/index.ts +73 -0
- package/src/tools/market.ts +363 -0
- package/src/tools/order.ts +326 -0
- package/src/tools/trade.ts +417 -0
- package/src/tools/trader.ts +322 -0
- package/src/tools/wallet.ts +683 -0
- package/src/types.ts +472 -0
- package/tsconfig.json +20 -0
|
@@ -0,0 +1,683 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Wallet Tools - MCP tools for deposits, swaps, and authorization management
|
|
3
|
+
*
|
|
4
|
+
* These tools use SDK services:
|
|
5
|
+
* - BridgeClient for deposit addresses and supported assets
|
|
6
|
+
* - SwapService for DEX swaps on Polygon (QuickSwap V3)
|
|
7
|
+
* - AuthorizationService for allowance checks and approvals
|
|
8
|
+
* - depositUsdc() for executing USDC deposits
|
|
9
|
+
* - swapAndDeposit() for swapping any token to USDC and depositing
|
|
10
|
+
*/
|
|
11
|
+
|
|
12
|
+
import { ethers } from 'ethers';
|
|
13
|
+
import type { PolymarketSDK } from '@catalyst-team/poly-sdk';
|
|
14
|
+
import {
|
|
15
|
+
BridgeClient,
|
|
16
|
+
AuthorizationService,
|
|
17
|
+
depositUsdc,
|
|
18
|
+
swapAndDeposit,
|
|
19
|
+
SwapService,
|
|
20
|
+
getSupportedDepositTokens,
|
|
21
|
+
} from '@catalyst-team/poly-sdk';
|
|
22
|
+
import type { ToolDefinition } from '../types.js';
|
|
23
|
+
import { wrapError, McpToolError, ErrorCode } from '../errors.js';
|
|
24
|
+
|
|
25
|
+
// Tool definitions
|
|
26
|
+
export const walletToolDefinitions: ToolDefinition[] = [
|
|
27
|
+
{
|
|
28
|
+
name: 'get_supported_deposit_assets',
|
|
29
|
+
description: 'Get all supported assets and chains for deposits to Polymarket',
|
|
30
|
+
inputSchema: {
|
|
31
|
+
type: 'object',
|
|
32
|
+
properties: {
|
|
33
|
+
chainId: {
|
|
34
|
+
type: 'number',
|
|
35
|
+
description: 'Filter by chain ID (optional)',
|
|
36
|
+
},
|
|
37
|
+
},
|
|
38
|
+
},
|
|
39
|
+
},
|
|
40
|
+
{
|
|
41
|
+
name: 'get_deposit_addresses',
|
|
42
|
+
description: 'Get deposit addresses (EVM, Solana, Bitcoin) for the configured wallet',
|
|
43
|
+
inputSchema: {
|
|
44
|
+
type: 'object',
|
|
45
|
+
properties: {},
|
|
46
|
+
},
|
|
47
|
+
},
|
|
48
|
+
{
|
|
49
|
+
name: 'deposit_usdc',
|
|
50
|
+
description: 'Deposit USDC to Polymarket. Transfers USDC from your wallet to Polymarket via the Bridge.',
|
|
51
|
+
inputSchema: {
|
|
52
|
+
type: 'object',
|
|
53
|
+
properties: {
|
|
54
|
+
amount: {
|
|
55
|
+
type: 'number',
|
|
56
|
+
description: 'Amount to deposit in USDC (minimum $2)',
|
|
57
|
+
},
|
|
58
|
+
token: {
|
|
59
|
+
type: 'string',
|
|
60
|
+
enum: ['NATIVE_USDC', 'USDC_E'],
|
|
61
|
+
description: 'Which USDC token to deposit (default: NATIVE_USDC)',
|
|
62
|
+
},
|
|
63
|
+
},
|
|
64
|
+
required: ['amount'],
|
|
65
|
+
},
|
|
66
|
+
},
|
|
67
|
+
{
|
|
68
|
+
name: 'check_allowances',
|
|
69
|
+
description: 'Check all ERC20 and ERC1155 allowances required for trading',
|
|
70
|
+
inputSchema: {
|
|
71
|
+
type: 'object',
|
|
72
|
+
properties: {},
|
|
73
|
+
},
|
|
74
|
+
},
|
|
75
|
+
{
|
|
76
|
+
name: 'approve_trading',
|
|
77
|
+
description: 'Set up all required ERC20 and ERC1155 approvals for trading on Polymarket',
|
|
78
|
+
inputSchema: {
|
|
79
|
+
type: 'object',
|
|
80
|
+
properties: {},
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
{
|
|
84
|
+
name: 'swap',
|
|
85
|
+
description: 'Swap between tokens on Polygon using QuickSwap V3. Supports MATIC, WETH, USDC, USDC.e, USDT, DAI.',
|
|
86
|
+
inputSchema: {
|
|
87
|
+
type: 'object',
|
|
88
|
+
properties: {
|
|
89
|
+
tokenIn: {
|
|
90
|
+
type: 'string',
|
|
91
|
+
description: 'Token to swap from (e.g., MATIC, WETH, USDT, DAI, USDC, USDC_E)',
|
|
92
|
+
},
|
|
93
|
+
tokenOut: {
|
|
94
|
+
type: 'string',
|
|
95
|
+
description: 'Token to swap to (e.g., USDC, USDC_E, WETH)',
|
|
96
|
+
},
|
|
97
|
+
amount: {
|
|
98
|
+
type: 'string',
|
|
99
|
+
description: 'Amount to swap in token units',
|
|
100
|
+
},
|
|
101
|
+
slippage: {
|
|
102
|
+
type: 'number',
|
|
103
|
+
description: 'Slippage tolerance in percent (default: 0.5)',
|
|
104
|
+
},
|
|
105
|
+
},
|
|
106
|
+
required: ['tokenIn', 'tokenOut', 'amount'],
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
{
|
|
110
|
+
name: 'swap_and_deposit',
|
|
111
|
+
description: 'Swap any supported Polygon token to USDC and deposit to Polymarket. Supports MATIC, WETH, USDT, DAI, USDC, USDC.e.',
|
|
112
|
+
inputSchema: {
|
|
113
|
+
type: 'object',
|
|
114
|
+
properties: {
|
|
115
|
+
token: {
|
|
116
|
+
type: 'string',
|
|
117
|
+
description: 'Token to deposit (e.g., MATIC, WETH, USDT, DAI, USDC)',
|
|
118
|
+
},
|
|
119
|
+
amount: {
|
|
120
|
+
type: 'string',
|
|
121
|
+
description: 'Amount to deposit in token units',
|
|
122
|
+
},
|
|
123
|
+
slippage: {
|
|
124
|
+
type: 'number',
|
|
125
|
+
description: 'Slippage tolerance for swap in percent (default: 0.5)',
|
|
126
|
+
},
|
|
127
|
+
},
|
|
128
|
+
required: ['token', 'amount'],
|
|
129
|
+
},
|
|
130
|
+
},
|
|
131
|
+
{
|
|
132
|
+
name: 'get_token_balances',
|
|
133
|
+
description: 'Get balances for all supported tokens on Polygon for the configured wallet (requires private key)',
|
|
134
|
+
inputSchema: {
|
|
135
|
+
type: 'object',
|
|
136
|
+
properties: {},
|
|
137
|
+
},
|
|
138
|
+
},
|
|
139
|
+
{
|
|
140
|
+
name: 'get_wallet_balances',
|
|
141
|
+
description: 'Get Polygon token balances for any wallet address (MATIC, USDC, USDC.e, USDT, DAI, WETH). No private key required.',
|
|
142
|
+
inputSchema: {
|
|
143
|
+
type: 'object',
|
|
144
|
+
properties: {
|
|
145
|
+
address: {
|
|
146
|
+
type: 'string',
|
|
147
|
+
description: 'Wallet address to check (0x...)',
|
|
148
|
+
},
|
|
149
|
+
},
|
|
150
|
+
required: ['address'],
|
|
151
|
+
},
|
|
152
|
+
},
|
|
153
|
+
{
|
|
154
|
+
name: 'get_swap_quote',
|
|
155
|
+
description: 'Get a swap quote to check if a route is available and estimate output amount. No private key required. Uses QuickSwap V3 Quoter contract.',
|
|
156
|
+
inputSchema: {
|
|
157
|
+
type: 'object',
|
|
158
|
+
properties: {
|
|
159
|
+
tokenIn: {
|
|
160
|
+
type: 'string',
|
|
161
|
+
description: 'Token to swap from (e.g., MATIC, WETH, USDT, DAI, USDC, USDC_E)',
|
|
162
|
+
},
|
|
163
|
+
tokenOut: {
|
|
164
|
+
type: 'string',
|
|
165
|
+
description: 'Token to swap to (e.g., USDC, USDC_E, WETH)',
|
|
166
|
+
},
|
|
167
|
+
amount: {
|
|
168
|
+
type: 'string',
|
|
169
|
+
description: 'Amount to swap in token units',
|
|
170
|
+
},
|
|
171
|
+
},
|
|
172
|
+
required: ['tokenIn', 'tokenOut', 'amount'],
|
|
173
|
+
},
|
|
174
|
+
},
|
|
175
|
+
{
|
|
176
|
+
name: 'get_available_pools',
|
|
177
|
+
description: 'Get all available liquidity pools on QuickSwap V3. Shows which token pairs can be swapped directly.',
|
|
178
|
+
inputSchema: {
|
|
179
|
+
type: 'object',
|
|
180
|
+
properties: {},
|
|
181
|
+
},
|
|
182
|
+
},
|
|
183
|
+
];
|
|
184
|
+
|
|
185
|
+
// Input/Output types
|
|
186
|
+
interface GetSupportedAssetsInput {
|
|
187
|
+
chainId?: number;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
interface DepositUsdcInput {
|
|
191
|
+
amount: number;
|
|
192
|
+
token?: 'NATIVE_USDC' | 'USDC_E';
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// Default Polygon RPC for wallet operations
|
|
196
|
+
const POLYGON_RPC = 'https://polygon-rpc.com';
|
|
197
|
+
|
|
198
|
+
/**
|
|
199
|
+
* Helper to get signer from SDK
|
|
200
|
+
* Ensures the signer is connected to a Polygon provider
|
|
201
|
+
*/
|
|
202
|
+
function getSignerFromSdk(sdk: PolymarketSDK): ethers.Wallet {
|
|
203
|
+
const sdkAny = sdk as unknown as {
|
|
204
|
+
clobApi: {
|
|
205
|
+
signer?: ethers.Wallet;
|
|
206
|
+
};
|
|
207
|
+
};
|
|
208
|
+
|
|
209
|
+
const signer = sdkAny.clobApi?.signer;
|
|
210
|
+
if (!signer) {
|
|
211
|
+
throw new McpToolError(
|
|
212
|
+
ErrorCode.AUTH_REQUIRED,
|
|
213
|
+
'Wallet not configured. Set POLY_PRIVATE_KEY to use wallet features.'
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
// Ensure signer is connected to a provider
|
|
218
|
+
if (!signer.provider) {
|
|
219
|
+
const provider = new ethers.providers.JsonRpcProvider(POLYGON_RPC);
|
|
220
|
+
return signer.connect(provider);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
return signer;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/**
|
|
227
|
+
* Get supported deposit assets
|
|
228
|
+
*/
|
|
229
|
+
export async function handleGetSupportedAssets(
|
|
230
|
+
_sdk: PolymarketSDK,
|
|
231
|
+
input: GetSupportedAssetsInput
|
|
232
|
+
) {
|
|
233
|
+
try {
|
|
234
|
+
const bridge = new BridgeClient();
|
|
235
|
+
const assets = await bridge.getSupportedAssets();
|
|
236
|
+
|
|
237
|
+
// Filter by chainId if provided
|
|
238
|
+
const filtered = input.chainId
|
|
239
|
+
? assets.filter((a) => a.chainId === input.chainId)
|
|
240
|
+
: assets;
|
|
241
|
+
|
|
242
|
+
// Get unique chains
|
|
243
|
+
const chainSet = new Set<string>();
|
|
244
|
+
for (const asset of filtered) {
|
|
245
|
+
chainSet.add(asset.chainName);
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
return {
|
|
249
|
+
assets: filtered.map((a) => ({
|
|
250
|
+
chainId: a.chainId,
|
|
251
|
+
chainName: a.chainName,
|
|
252
|
+
tokenSymbol: a.tokenSymbol,
|
|
253
|
+
tokenName: a.tokenName,
|
|
254
|
+
tokenAddress: a.tokenAddress,
|
|
255
|
+
decimals: a.decimals,
|
|
256
|
+
minDeposit: a.minDeposit,
|
|
257
|
+
minDepositUsd: a.minDepositUsd,
|
|
258
|
+
})),
|
|
259
|
+
summary: {
|
|
260
|
+
totalChains: chainSet.size,
|
|
261
|
+
totalAssets: filtered.length,
|
|
262
|
+
chains: Array.from(chainSet),
|
|
263
|
+
},
|
|
264
|
+
};
|
|
265
|
+
} catch (err) {
|
|
266
|
+
throw wrapError(err);
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/**
|
|
271
|
+
* Get deposit addresses for the wallet
|
|
272
|
+
*/
|
|
273
|
+
export async function handleGetDepositAddresses(sdk: PolymarketSDK) {
|
|
274
|
+
const signer = getSignerFromSdk(sdk);
|
|
275
|
+
const walletAddress = signer.address;
|
|
276
|
+
|
|
277
|
+
try {
|
|
278
|
+
const bridge = new BridgeClient();
|
|
279
|
+
const result = await bridge.createDepositAddresses(walletAddress);
|
|
280
|
+
|
|
281
|
+
const evmChains = ['Ethereum', 'Polygon', 'Arbitrum', 'Base', 'Optimism'];
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
wallet: walletAddress,
|
|
285
|
+
addresses: {
|
|
286
|
+
evm: result.address.evm,
|
|
287
|
+
solana: result.address.svm,
|
|
288
|
+
bitcoin: result.address.btc,
|
|
289
|
+
},
|
|
290
|
+
evmChains,
|
|
291
|
+
instructions: `Send assets to these addresses to deposit to your Polymarket account.
|
|
292
|
+
|
|
293
|
+
EVM Chains (Ethereum, Polygon, Arbitrum, Base, Optimism):
|
|
294
|
+
${result.address.evm}
|
|
295
|
+
|
|
296
|
+
Solana:
|
|
297
|
+
${result.address.svm}
|
|
298
|
+
|
|
299
|
+
Bitcoin:
|
|
300
|
+
${result.address.btc}
|
|
301
|
+
|
|
302
|
+
Assets will be automatically converted to USDC.e on Polygon.`,
|
|
303
|
+
};
|
|
304
|
+
} catch (err) {
|
|
305
|
+
throw wrapError(err);
|
|
306
|
+
}
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Deposit USDC to Polymarket
|
|
311
|
+
*/
|
|
312
|
+
export async function handleDepositUsdc(
|
|
313
|
+
sdk: PolymarketSDK,
|
|
314
|
+
input: DepositUsdcInput
|
|
315
|
+
) {
|
|
316
|
+
const signer = getSignerFromSdk(sdk);
|
|
317
|
+
|
|
318
|
+
try {
|
|
319
|
+
const result = await depositUsdc(signer, input.amount, {
|
|
320
|
+
token: input.token || 'NATIVE_USDC',
|
|
321
|
+
});
|
|
322
|
+
|
|
323
|
+
if (!result.success) {
|
|
324
|
+
return {
|
|
325
|
+
success: false,
|
|
326
|
+
error: result.error,
|
|
327
|
+
wallet: signer.address,
|
|
328
|
+
amount: input.amount,
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
return {
|
|
333
|
+
success: true,
|
|
334
|
+
wallet: signer.address,
|
|
335
|
+
amount: result.amount,
|
|
336
|
+
txHash: result.txHash,
|
|
337
|
+
depositAddress: result.depositAddress,
|
|
338
|
+
message: `Successfully deposited $${result.amount} USDC. The bridge will automatically convert it to USDC.e on Polygon (typically 1-5 minutes).`,
|
|
339
|
+
explorerUrl: `https://polygonscan.com/tx/${result.txHash}`,
|
|
340
|
+
};
|
|
341
|
+
} catch (err) {
|
|
342
|
+
throw wrapError(err);
|
|
343
|
+
}
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
/**
|
|
347
|
+
* Check all allowances required for trading
|
|
348
|
+
*/
|
|
349
|
+
export async function handleCheckAllowances(sdk: PolymarketSDK) {
|
|
350
|
+
const signer = getSignerFromSdk(sdk);
|
|
351
|
+
|
|
352
|
+
try {
|
|
353
|
+
const authService = new AuthorizationService(signer);
|
|
354
|
+
const result = await authService.checkAllowances();
|
|
355
|
+
|
|
356
|
+
return {
|
|
357
|
+
wallet: result.wallet,
|
|
358
|
+
erc20: {
|
|
359
|
+
usdc: {
|
|
360
|
+
balance: result.usdcBalance,
|
|
361
|
+
allowances: result.erc20Allowances,
|
|
362
|
+
},
|
|
363
|
+
},
|
|
364
|
+
erc1155: {
|
|
365
|
+
conditionalTokens: {
|
|
366
|
+
approvals: result.erc1155Approvals,
|
|
367
|
+
},
|
|
368
|
+
},
|
|
369
|
+
tradingReady: result.tradingReady,
|
|
370
|
+
issues: result.issues,
|
|
371
|
+
};
|
|
372
|
+
} catch (err) {
|
|
373
|
+
throw wrapError(err);
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Set up all required approvals for trading
|
|
379
|
+
*/
|
|
380
|
+
export async function handleApproveTrading(sdk: PolymarketSDK) {
|
|
381
|
+
const signer = getSignerFromSdk(sdk);
|
|
382
|
+
|
|
383
|
+
try {
|
|
384
|
+
const authService = new AuthorizationService(signer);
|
|
385
|
+
const result = await authService.approveAll();
|
|
386
|
+
|
|
387
|
+
return {
|
|
388
|
+
wallet: result.wallet,
|
|
389
|
+
erc20Approvals: result.erc20Approvals,
|
|
390
|
+
erc1155Approvals: result.erc1155Approvals,
|
|
391
|
+
allApproved: result.allApproved,
|
|
392
|
+
summary: result.summary,
|
|
393
|
+
};
|
|
394
|
+
} catch (err) {
|
|
395
|
+
throw wrapError(err);
|
|
396
|
+
}
|
|
397
|
+
}
|
|
398
|
+
|
|
399
|
+
// Input types for new tools
|
|
400
|
+
interface SwapInput {
|
|
401
|
+
tokenIn: string;
|
|
402
|
+
tokenOut: string;
|
|
403
|
+
amount: string;
|
|
404
|
+
slippage?: number;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
interface SwapAndDepositInput {
|
|
408
|
+
token: string;
|
|
409
|
+
amount: string;
|
|
410
|
+
slippage?: number;
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
/**
|
|
414
|
+
* Swap between tokens on Polygon using QuickSwap V3
|
|
415
|
+
*/
|
|
416
|
+
export async function handleSwap(sdk: PolymarketSDK, input: SwapInput) {
|
|
417
|
+
const signer = getSignerFromSdk(sdk);
|
|
418
|
+
|
|
419
|
+
try {
|
|
420
|
+
const swapService = new SwapService(signer);
|
|
421
|
+
|
|
422
|
+
// Check balance first
|
|
423
|
+
const balance = await swapService.getBalance(input.tokenIn);
|
|
424
|
+
if (parseFloat(balance) < parseFloat(input.amount)) {
|
|
425
|
+
return {
|
|
426
|
+
success: false,
|
|
427
|
+
error: `Insufficient ${input.tokenIn} balance. Have: ${balance}, Need: ${input.amount}`,
|
|
428
|
+
wallet: signer.address,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// Execute swap
|
|
433
|
+
const result = await swapService.swap(input.tokenIn, input.tokenOut, input.amount, {
|
|
434
|
+
slippage: input.slippage || 0.5,
|
|
435
|
+
});
|
|
436
|
+
|
|
437
|
+
if (!result.success) {
|
|
438
|
+
return {
|
|
439
|
+
success: false,
|
|
440
|
+
error: 'Swap failed',
|
|
441
|
+
wallet: signer.address,
|
|
442
|
+
txHash: result.transactionHash,
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
return {
|
|
447
|
+
success: true,
|
|
448
|
+
wallet: signer.address,
|
|
449
|
+
tokenIn: result.tokenIn,
|
|
450
|
+
tokenOut: result.tokenOut,
|
|
451
|
+
amountIn: result.amountIn,
|
|
452
|
+
amountOut: result.amountOut,
|
|
453
|
+
txHash: result.transactionHash,
|
|
454
|
+
gasUsed: result.gasUsed,
|
|
455
|
+
explorerUrl: `https://polygonscan.com/tx/${result.transactionHash}`,
|
|
456
|
+
};
|
|
457
|
+
} catch (err) {
|
|
458
|
+
throw wrapError(err);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/**
|
|
463
|
+
* Swap any token to USDC and deposit to Polymarket
|
|
464
|
+
*/
|
|
465
|
+
export async function handleSwapAndDeposit(sdk: PolymarketSDK, input: SwapAndDepositInput) {
|
|
466
|
+
const signer = getSignerFromSdk(sdk);
|
|
467
|
+
|
|
468
|
+
try {
|
|
469
|
+
const result = await swapAndDeposit(signer, input.token, input.amount, {
|
|
470
|
+
slippage: input.slippage || 0.5,
|
|
471
|
+
});
|
|
472
|
+
|
|
473
|
+
if (!result.success) {
|
|
474
|
+
return {
|
|
475
|
+
success: false,
|
|
476
|
+
error: result.error,
|
|
477
|
+
wallet: signer.address,
|
|
478
|
+
token: input.token,
|
|
479
|
+
amount: input.amount,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
|
|
483
|
+
return {
|
|
484
|
+
success: true,
|
|
485
|
+
wallet: signer.address,
|
|
486
|
+
tokenIn: result.tokenIn,
|
|
487
|
+
amountIn: result.amountIn,
|
|
488
|
+
usdcAmount: result.usdcAmount,
|
|
489
|
+
swapTxHash: result.swapTxHash || null,
|
|
490
|
+
depositTxHash: result.depositTxHash,
|
|
491
|
+
depositAddress: result.depositAddress,
|
|
492
|
+
message: result.swapTxHash
|
|
493
|
+
? `Swapped ${result.amountIn} ${result.tokenIn} to ${result.usdcAmount} USDC, then deposited to Polymarket.`
|
|
494
|
+
: `Deposited ${result.usdcAmount} USDC to Polymarket.`,
|
|
495
|
+
explorerUrl: result.depositTxHash
|
|
496
|
+
? `https://polygonscan.com/tx/${result.depositTxHash}`
|
|
497
|
+
: undefined,
|
|
498
|
+
};
|
|
499
|
+
} catch (err) {
|
|
500
|
+
throw wrapError(err);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
|
|
504
|
+
/**
|
|
505
|
+
* Get balances for all supported tokens (requires configured wallet)
|
|
506
|
+
*/
|
|
507
|
+
export async function handleGetTokenBalances(sdk: PolymarketSDK) {
|
|
508
|
+
const signer = getSignerFromSdk(sdk);
|
|
509
|
+
|
|
510
|
+
try {
|
|
511
|
+
const swapService = new SwapService(signer);
|
|
512
|
+
const balances = await swapService.getBalances();
|
|
513
|
+
|
|
514
|
+
// Filter out zero balances
|
|
515
|
+
const nonZeroBalances = balances.filter((b) => parseFloat(b.balance) > 0);
|
|
516
|
+
|
|
517
|
+
return {
|
|
518
|
+
wallet: signer.address,
|
|
519
|
+
balances: balances.map((b) => ({
|
|
520
|
+
token: b.token,
|
|
521
|
+
symbol: b.symbol,
|
|
522
|
+
balance: b.balance,
|
|
523
|
+
decimals: b.decimals,
|
|
524
|
+
})),
|
|
525
|
+
nonZeroBalances: nonZeroBalances.map((b) => ({
|
|
526
|
+
token: b.token,
|
|
527
|
+
balance: b.balance,
|
|
528
|
+
})),
|
|
529
|
+
supportedTokens: getSupportedDepositTokens(),
|
|
530
|
+
};
|
|
531
|
+
} catch (err) {
|
|
532
|
+
throw wrapError(err);
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
|
|
536
|
+
/**
|
|
537
|
+
* Get balances for any wallet address (no private key required)
|
|
538
|
+
*/
|
|
539
|
+
export async function handleGetWalletBalances(
|
|
540
|
+
_sdk: PolymarketSDK,
|
|
541
|
+
input: { address: string }
|
|
542
|
+
) {
|
|
543
|
+
try {
|
|
544
|
+
// Validate address
|
|
545
|
+
if (!input.address || !input.address.startsWith('0x') || input.address.length !== 42) {
|
|
546
|
+
throw new McpToolError(
|
|
547
|
+
ErrorCode.INVALID_INPUT,
|
|
548
|
+
'Invalid wallet address. Must be a valid Ethereum address (0x...)'
|
|
549
|
+
);
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
const balances = await SwapService.getWalletBalances(input.address);
|
|
553
|
+
|
|
554
|
+
// Filter out zero balances
|
|
555
|
+
const nonZeroBalances = balances.filter((b) => parseFloat(b.balance) > 0);
|
|
556
|
+
|
|
557
|
+
// Calculate total USD value (simplified, assumes stablecoins = $1)
|
|
558
|
+
let totalUsdValue = 0;
|
|
559
|
+
for (const b of nonZeroBalances) {
|
|
560
|
+
const amount = parseFloat(b.balance);
|
|
561
|
+
if (['USDC', 'USDC_E', 'USDT', 'DAI'].includes(b.token)) {
|
|
562
|
+
totalUsdValue += amount;
|
|
563
|
+
}
|
|
564
|
+
// For MATIC/WETH, would need price oracle for accurate value
|
|
565
|
+
}
|
|
566
|
+
|
|
567
|
+
return {
|
|
568
|
+
address: input.address,
|
|
569
|
+
balances: balances.map((b) => ({
|
|
570
|
+
token: b.token,
|
|
571
|
+
symbol: b.symbol,
|
|
572
|
+
balance: b.balance,
|
|
573
|
+
decimals: b.decimals,
|
|
574
|
+
})),
|
|
575
|
+
nonZeroBalances: nonZeroBalances.map((b) => ({
|
|
576
|
+
token: b.token,
|
|
577
|
+
balance: b.balance,
|
|
578
|
+
})),
|
|
579
|
+
summary: {
|
|
580
|
+
totalTokens: nonZeroBalances.length,
|
|
581
|
+
stablecoinValue: totalUsdValue.toFixed(2),
|
|
582
|
+
},
|
|
583
|
+
supportedTokens: getSupportedDepositTokens(),
|
|
584
|
+
};
|
|
585
|
+
} catch (err) {
|
|
586
|
+
throw wrapError(err);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
|
|
590
|
+
// Input types for quote tools
|
|
591
|
+
interface GetSwapQuoteInput {
|
|
592
|
+
tokenIn: string;
|
|
593
|
+
tokenOut: string;
|
|
594
|
+
amount: string;
|
|
595
|
+
}
|
|
596
|
+
|
|
597
|
+
/**
|
|
598
|
+
* Get a swap quote (check route availability and estimate output)
|
|
599
|
+
* No private key required - uses read-only provider
|
|
600
|
+
*/
|
|
601
|
+
export async function handleGetSwapQuote(
|
|
602
|
+
_sdk: PolymarketSDK,
|
|
603
|
+
input: GetSwapQuoteInput
|
|
604
|
+
) {
|
|
605
|
+
try {
|
|
606
|
+
// Validate inputs
|
|
607
|
+
if (!input.tokenIn || !input.tokenOut || !input.amount) {
|
|
608
|
+
throw new McpToolError(
|
|
609
|
+
ErrorCode.INVALID_INPUT,
|
|
610
|
+
'tokenIn, tokenOut, and amount are all required'
|
|
611
|
+
);
|
|
612
|
+
}
|
|
613
|
+
|
|
614
|
+
// Create a read-only SwapService with a dummy wallet
|
|
615
|
+
// The getQuote method only uses the provider, not the signer
|
|
616
|
+
const provider = new ethers.providers.JsonRpcProvider(POLYGON_RPC);
|
|
617
|
+
const dummyWallet = ethers.Wallet.createRandom().connect(provider);
|
|
618
|
+
const swapService = new SwapService(dummyWallet);
|
|
619
|
+
|
|
620
|
+
const quote = await swapService.getQuote(input.tokenIn, input.tokenOut, input.amount);
|
|
621
|
+
|
|
622
|
+
if (quote.possible) {
|
|
623
|
+
return {
|
|
624
|
+
possible: true,
|
|
625
|
+
tokenIn: quote.tokenIn,
|
|
626
|
+
tokenOut: quote.tokenOut,
|
|
627
|
+
amountIn: quote.amountIn,
|
|
628
|
+
amountOut: quote.amountOut,
|
|
629
|
+
route: quote.route,
|
|
630
|
+
routeDescription: quote.route.length === 2
|
|
631
|
+
? `Direct swap: ${quote.route.join(' → ')}`
|
|
632
|
+
: `Multi-hop: ${quote.route.join(' → ')}`,
|
|
633
|
+
message: `Can swap ${quote.amountIn} ${quote.tokenIn} for ~${quote.amountOut} ${quote.tokenOut}`,
|
|
634
|
+
};
|
|
635
|
+
} else {
|
|
636
|
+
return {
|
|
637
|
+
possible: false,
|
|
638
|
+
tokenIn: quote.tokenIn,
|
|
639
|
+
tokenOut: quote.tokenOut,
|
|
640
|
+
amountIn: quote.amountIn,
|
|
641
|
+
amountOut: null,
|
|
642
|
+
route: quote.route,
|
|
643
|
+
poolExists: quote.poolExists,
|
|
644
|
+
reason: quote.reason,
|
|
645
|
+
suggestion: quote.tokenOut === 'USDC_E'
|
|
646
|
+
? 'For USDC.e, use: MATIC → USDC via swap, then USDC → USDC.e via deposit_usdc (Bridge)'
|
|
647
|
+
: 'Try swapping through USDC as an intermediate token',
|
|
648
|
+
};
|
|
649
|
+
}
|
|
650
|
+
} catch (err) {
|
|
651
|
+
throw wrapError(err);
|
|
652
|
+
}
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
/**
|
|
656
|
+
* Get all available liquidity pools on QuickSwap V3
|
|
657
|
+
*/
|
|
658
|
+
export async function handleGetAvailablePools(_sdk: PolymarketSDK) {
|
|
659
|
+
try {
|
|
660
|
+
// Create a read-only SwapService
|
|
661
|
+
const provider = new ethers.providers.JsonRpcProvider(POLYGON_RPC);
|
|
662
|
+
const dummyWallet = ethers.Wallet.createRandom().connect(provider);
|
|
663
|
+
const swapService = new SwapService(dummyWallet);
|
|
664
|
+
|
|
665
|
+
const pools = await swapService.getAvailablePools();
|
|
666
|
+
|
|
667
|
+
return {
|
|
668
|
+
pools: pools.map((p) => ({
|
|
669
|
+
pair: `${p.tokenA}/${p.tokenB}`,
|
|
670
|
+
tokenA: p.tokenA,
|
|
671
|
+
tokenB: p.tokenB,
|
|
672
|
+
poolAddress: p.poolAddress,
|
|
673
|
+
})),
|
|
674
|
+
totalPools: pools.length,
|
|
675
|
+
supportedTokens: swapService.getSupportedTokens().filter(
|
|
676
|
+
(t) => t !== 'NATIVE_USDC' && t !== 'WMATIC' // Exclude aliases
|
|
677
|
+
),
|
|
678
|
+
note: 'These are direct swap pairs. For pairs not listed, multi-hop routing through USDC or WMATIC may be available. Use get_swap_quote to check.',
|
|
679
|
+
};
|
|
680
|
+
} catch (err) {
|
|
681
|
+
throw wrapError(err);
|
|
682
|
+
}
|
|
683
|
+
}
|