From b65827038fdac31e5131045c6e2a2a44b9727147 Mon Sep 17 00:00:00 2001 From: WrBug Date: Mon, 19 Jan 2026 12:50:20 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E8=AE=A2=E5=8D=95=E8=AF=A6?= =?UTF-8?q?=E6=83=85=E6=9F=A5=E8=AF=A2=E8=84=9A=E6=9C=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加 get-order-detail.js 脚本,用于获取 Polymarket 订单详情 - 添加 package.json 配置文件 - 忽略 package-lock.json (已在 .gitignore 中通过 node_modules/ 规则处理) --- scripts/get-order-detail.js | 177 ++++++++++++++++++++++++++++++++++++ scripts/package.json | 13 +++ 2 files changed, 190 insertions(+) create mode 100644 scripts/get-order-detail.js create mode 100644 scripts/package.json diff --git a/scripts/get-order-detail.js b/scripts/get-order-detail.js new file mode 100644 index 0000000..2a6c42c --- /dev/null +++ b/scripts/get-order-detail.js @@ -0,0 +1,177 @@ +#!/usr/bin/env node + +/** + * 获取订单详情脚本 + * + * 使用方法: + * node scripts/get-order-detail.js + * + * 参数说明: + * private_key: 钱包私钥(用于签名) + * order_id: 订单 ID + * + * 示例: + * node scripts/get-order-detail.js "0x..." "0x123..." + */ + +import { Wallet } from '@ethersproject/wallet'; +import { ClobClient } from '@polymarket/clob-client'; + +// Polymarket CLOB 主机地址 +const HOST = 'https://clob.polymarket.com'; +const CHAIN_ID = 137; // Polygon 主网 + +async function getOrderDetail(privateKey, orderId) { + try { + console.log('正在初始化钱包...'); + const wallet = new Wallet(privateKey); + console.log(`钱包地址: ${wallet.address}`); + + console.log('\n正在初始化 ClobClient...'); + const clobClient = new ClobClient( + HOST, + CHAIN_ID, + wallet + ); + + console.log('\n正在获取或创建 API Key...'); + try { + // 尝试 derive API key(如果已存在) + const creds = await clobClient.deriveApiKey(); + console.log(`✅ API Key 已获取`); + console.log(` API Key: ${creds.key.substring(0, 10)}...`); + console.log(` Passphrase: ${creds.passphrase.substring(0, 10)}...`); + + // 使用 creds 初始化一个新的 ClobClient 实例用于 L2 认证 + const authenticatedClient = new ClobClient( + HOST, + CHAIN_ID, + wallet, + creds + ); + + console.log(`\n正在获取订单详情...`); + console.log(` 订单 ID: ${orderId}`); + + const orderDetail = await authenticatedClient.getOrder(orderId); + + console.log('\n================ 订单详情 ================'); + console.log(`订单 ID: ${orderDetail.id}`); + console.log(`状态: ${orderDetail.status}`); + console.log(`所有者: ${orderDetail.owner}`); + console.log(`Maker 地址: ${orderDetail.maker_address}`); + console.log(`市场 ID: ${orderDetail.market}`); + console.log(`资产 ID: ${orderDetail.asset_id}`); + console.log(`方向: ${orderDetail.side}`); + console.log(`原始数量: ${orderDetail.original_size}`); + console.log(`已匹配数量: ${orderDetail.size_matched}`); + console.log(`价格: ${orderDetail.price}`); + console.log(`结果: ${orderDetail.outcome}`); + console.log(`创建时间: ${new Date(orderDetail.created_at * 1000).toISOString()}`); + console.log(`过期时间: ${orderDetail.expiration}`); + console.log(`订单类型: ${orderDetail.order_type}`); + + if (orderDetail.associate_trades && orderDetail.associate_trades.length > 0) { + console.log(`关联交易数量: ${orderDetail.associate_trades.length}`); + console.log(`关联交易 IDs: ${orderDetail.associate_trades.join(', ')}`); + } + console.log('=========================================\n'); + + } catch (apiKeyError) { + if (apiKeyError.message && apiKeyError.message.includes('API key does not exist')) { + console.log('⚠️ API Key 不存在,正在创建新的 API Key...'); + + // 创建新的 API key + const creds = await clobClient.createApiKey(); + console.log(`✅ API Key 已创建`); + console.log(` API Key: ${creds.key.substring(0, 10)}...`); + console.log(` Passphrase: ${creds.passphrase.substring(0, 10)}...`); + + // 使用 creds 初始化一个新的 ClobClient 实例用于 L2 认证 + const authenticatedClient = new ClobClient( + HOST, + CHAIN_ID, + wallet, + creds + ); + + console.log(`\n正在获取订单详情...`); + console.log(` 订单 ID: ${orderId}`); + + const orderDetail = await authenticatedClient.getOrder(orderId); + + console.log('\n================ 订单详情 ================'); + console.log(`订单 ID: ${orderDetail.id}`); + console.log(`状态: ${orderDetail.status}`); + console.log(`所有者: ${orderDetail.owner}`); + console.log(`Maker 地址: ${orderDetail.maker_address}`); + console.log(`市场 ID: ${orderDetail.market}`); + console.log(`资产 ID: ${orderDetail.asset_id}`); + console.log(`方向: ${orderDetail.side}`); + console.log(`原始数量: ${orderDetail.original_size}`); + console.log(`已匹配数量: ${orderDetail.size_matched}`); + console.log(`价格: ${orderDetail.price}`); + console.log(`结果: ${orderDetail.outcome}`); + console.log(`创建时间: ${new Date(orderDetail.created_at * 1000).toISOString()}`); + console.log(`过期时间: ${orderDetail.expiration}`); + console.log(`订单类型: ${orderDetail.order_type}`); + + if (orderDetail.associate_trades && orderDetail.associate_trades.length > 0) { + console.log(`关联交易数量: ${orderDetail.associate_trades.length}`); + console.log(`关联交易 IDs: ${orderDetail.associate_trades.join(', ')}`); + } + console.log('=========================================\n'); + } else { + throw apiKeyError; + } + } + + } catch (error) { + console.error('\n❌ 获取订单详情失败:'); + console.error(error.message); + + if (error.response) { + console.error(`\nHTTP 状态码: ${error.response.status}`); + console.error(`响应数据:`, error.response.data); + } + + process.exit(1); + } +} + +// 检查命令行参数 +const args = process.argv.slice(2); + +if (args.length < 2) { + console.error('错误: 缺少必要参数'); + console.error('\n使用方法:'); + console.error(' node scripts/get-order-detail.js '); + console.error('\n参数说明:'); + console.error(' private_key: 钱包私钥(用于签名)'); + console.error(' order_id: 订单 ID'); + console.error('\n示例:'); + console.error(' node scripts/get-order-detail.js "0x123..." "0x456..."'); + process.exit(1); +} + +const [privateKey, orderId] = args; + +// 验证私钥格式 +if (!privateKey.startsWith('0x')) { + console.error('错误: 私钥格式不正确,必须以 0x 开头'); + process.exit(1); +} + +if (privateKey.length !== 66) { + console.error('错误: 私钥长度不正确,应为 66 个字符(包括 0x 前缀)'); + process.exit(1); +} + +// 验证订单 ID +if (!orderId.startsWith('0x')) { + console.error('错误: 订单 ID 格式不正确,必须以 0x 开头'); + process.exit(1); +} + +// 执行主函数 +getOrderDetail(privateKey, orderId); diff --git a/scripts/package.json b/scripts/package.json new file mode 100644 index 0000000..a990256 --- /dev/null +++ b/scripts/package.json @@ -0,0 +1,13 @@ +{ + "name": "polyhermes-scripts", + "version": "1.0.0", + "description": "Utility scripts for Polyhermes", + "type": "module", + "scripts": { + "get-order-detail": "node get-order-detail.js" + }, + "dependencies": { + "@ethersproject/wallet": "^5.7.0", + "@polymarket/clob-client": "^5.2.1" + } +}