polymarket-helper 1.0.3

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 ADDED
@@ -0,0 +1,125 @@
1
+ # Prediction SDK
2
+
3
+ SDK for prediction market trading including Polymarket, Kalshi, Limitless, and more.
4
+
5
+ ## 安装
6
+
7
+ ```bash
8
+ npm install prediction-sdk
9
+ ```
10
+
11
+ ## 使用教程
12
+
13
+ ### Polymarket
14
+
15
+ #### 1. 初始化客户端
16
+
17
+ ```typescript
18
+ import Polymarket from 'prediction-sdk'
19
+
20
+ const polymarket = new Polymarket({
21
+ rpcUrl: 'https://polygon-rpc.com', // Polygon RPC 端点
22
+ privateKey: '0x...', // 你的私钥
23
+ })
24
+ ```
25
+
26
+ #### 2. 创建客户端连接
27
+
28
+ ```typescript
29
+ await polymarket.createClient()
30
+ ```
31
+
32
+ #### 3. 授权交易(首次使用需要)
33
+
34
+ 在交易前,需要授权 Polymarket 交易所管理你的代币:
35
+
36
+ ```typescript
37
+ // 检查是否已授权
38
+ const isApproved = await polymarket.isApproved()
39
+
40
+ if (!isApproved) {
41
+ // 授权交易所管理所有代币
42
+ await polymarket.approvalAll()
43
+ }
44
+ ```
45
+
46
+ #### 4. 提交订单
47
+
48
+ ```typescript
49
+ import { UserMarketOrder } from '@polymarket/clob-client'
50
+
51
+ const order: UserMarketOrder = {
52
+ market: '0x...', // 市场合约地址
53
+ side: 'YES', // 'YES' 或 'NO'
54
+ size: '1000000000', // 订单数量(原始单位)
55
+ price: '0.5', // 价格(0-1之间)
56
+ }
57
+
58
+ await polymarket.postOrder(order)
59
+ ```
60
+
61
+ #### 完整示例
62
+
63
+ ```typescript
64
+ import Polymarket from 'prediction-sdk'
65
+
66
+ async function trade() {
67
+ // 1. 初始化
68
+ const polymarket = new Polymarket({
69
+ rpcUrl: 'https://polygon-rpc.com',
70
+ privateKey: process.env.PRIVATE_KEY!,
71
+ })
72
+
73
+ // 2. 创建客户端
74
+ await polymarket.createClient()
75
+
76
+ // 3. 检查并授权
77
+ if (!(await polymarket.isApproved())) {
78
+ await polymarket.approvalAll()
79
+ }
80
+
81
+ // 4. 提交订单
82
+ await polymarket.postOrder({
83
+ market: '0x...',
84
+ side: 'YES',
85
+ size: '1000000000',
86
+ price: '0.5',
87
+ })
88
+ }
89
+ ```
90
+
91
+ ### 工具函数
92
+
93
+ #### 格式化金额
94
+
95
+ ```typescript
96
+ import { formatClobAmount } from 'prediction-sdk'
97
+
98
+ // 将原始金额转换为可读格式
99
+ const amount = formatClobAmount('1000000', 6) // "1.0" (USDC)
100
+ ```
101
+
102
+ #### 检查是否为 Gnosis Safe
103
+
104
+ ```typescript
105
+ import { isGnosisSafe } from 'prediction-sdk'
106
+
107
+ const isSafe = await isGnosisSafe('0x...', 'https://polygon-rpc.com')
108
+ ```
109
+
110
+ ## 开发
111
+
112
+ ```bash
113
+ # 安装依赖
114
+ bun install
115
+
116
+ # 构建
117
+ bun run build
118
+
119
+ # 开发模式(监听文件变化)
120
+ bun run dev
121
+ ```
122
+
123
+ ## License
124
+
125
+ ISC
@@ -0,0 +1,146 @@
1
+ import { ClobClient, UserMarketOrder, CreateOrderOptions } from '@polymarket/clob-client';
2
+
3
+ /**
4
+ * Type definitions for prediction market platforms
5
+ *
6
+ * This module contains TypeScript interfaces and types used across
7
+ * the prediction SDK for various market platforms.
8
+ */
9
+ /**
10
+ * Configuration options for Polymarket client initialization
11
+ *
12
+ * @interface PolymarketOptions
13
+ * @property {string} rpcUrl - Polygon RPC endpoint URL for blockchain interactions
14
+ * @property {string} privateKey - Private key of the wallet to use for signing transactions
15
+ * @property {string} [cLobHttpUrl] - Optional CLOB API endpoint URL (defaults to https://clob.polymarket.com)
16
+ */
17
+ interface PolymarketOptions {
18
+ /** Polygon RPC endpoint URL for blockchain interactions */
19
+ rpcUrl: string;
20
+ /** Private key of the wallet to use for signing transactions */
21
+ privateKey: string;
22
+ /** Optional CLOB API endpoint URL (defaults to https://clob.polymarket.com) */
23
+ cLobHttpUrl?: string;
24
+ }
25
+
26
+ /**
27
+ * Polymarket integration module
28
+ *
29
+ * This module provides a client for interacting with Polymarket,
30
+ * a decentralized prediction market platform built on Polygon.
31
+ *
32
+ * Features:
33
+ * - Create and post market orders
34
+ * - Manage token approvals for trading
35
+ * - Support for both EOA and Gnosis Safe wallets
36
+ */
37
+
38
+ /**
39
+ * Conditional Token Framework contract address for Polygon
40
+ * This contract is used to manage conditional tokens (market shares)
41
+ */
42
+ declare const CTFContract: string;
43
+ /**
44
+ * Polymarket client class
45
+ *
46
+ * Provides methods to interact with Polymarket prediction markets,
47
+ * including creating orders, managing approvals, and handling transactions.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const polymarket = new Polymarket({
52
+ * rpcUrl: "https://polygon-rpc.com",
53
+ * privateKey: "0x...",
54
+ * });
55
+ *
56
+ * await polymarket.createClient();
57
+ * await polymarket.approvalAll();
58
+ * await polymarket.postOrder(order);
59
+ * ```
60
+ */
61
+ declare class Polymarket {
62
+ /** Configuration options for the Polymarket client */
63
+ private options;
64
+ /** Ethers.js provider for blockchain interactions */
65
+ private provider;
66
+ /** Wallet signer for transaction signing */
67
+ private signer;
68
+ /** CLOB signer for Polymarket SDK (ethers v5) */
69
+ private clobSigner;
70
+ /** Conditional Token Framework contract instance */
71
+ private ctfContract;
72
+ /** Polymarket CLOB client instance */
73
+ private client;
74
+ /** ClobClient signer type (ethers v5 compatibility) */
75
+ private getClobSigner;
76
+ /**
77
+ * Creates a new Polymarket client instance
78
+ *
79
+ * @param options - Configuration options including RPC URL and private key
80
+ * @throws {Error} If the private key or RPC URL is invalid
81
+ */
82
+ constructor(options: PolymarketOptions);
83
+ /**
84
+ * Creates and initializes the CLOB client
85
+ *
86
+ * Automatically detects if the wallet is a Gnosis Safe proxy or EOA,
87
+ * and configures the appropriate signature type.
88
+ *
89
+ * @returns {Promise<ClobClient>} The initialized CLOB client
90
+ * @throws {Error} If client creation fails
91
+ */
92
+ createClient(): Promise<ClobClient>;
93
+ /**
94
+ * Posts a market order to Polymarket
95
+ *
96
+ * Creates a signed market order and submits it to the order book
97
+ * using Fill-or-Kill (FOK) order type.
98
+ *
99
+ * @param {UserMarketOrder} userMarketOrder - The market order to post
100
+ * @param {Partial<CreateOrderOptions>} [options] - Optional order creation options
101
+ * @returns {Promise<void>} Promise that resolves when the order is posted
102
+ * @throws {Error} If order creation or posting fails
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * await polymarket.postOrder({
107
+ * market: "0x...",
108
+ * side: "YES",
109
+ * size: "1000000000",
110
+ * price: "0.5"
111
+ * });
112
+ * ```
113
+ */
114
+ postOrder(userMarketOrder: UserMarketOrder, options?: Partial<CreateOrderOptions>): Promise<void>;
115
+ /**
116
+ * Checks if the exchange contract is approved to manage tokens
117
+ *
118
+ * This checks if setApprovalForAll has been called for the Polymarket
119
+ * exchange contract, which is required before trading.
120
+ *
121
+ * @returns {Promise<boolean>} True if approved, false otherwise
122
+ */
123
+ isApproved(): Promise<boolean>;
124
+ /**
125
+ * Approves the Polymarket exchange to manage all tokens
126
+ *
127
+ * This is a one-time operation that allows the exchange contract
128
+ * to transfer conditional tokens on your behalf. Required before trading.
129
+ *
130
+ * Uses a 50% gas price buffer to ensure transaction confirmation.
131
+ *
132
+ * @returns {Promise<boolean>} True if approval succeeds
133
+ * @throws {Error} If the approval transaction fails or is not confirmed
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const isApproved = await polymarket.isApproved();
138
+ * if (!isApproved) {
139
+ * await polymarket.approvalAll();
140
+ * }
141
+ * ```
142
+ */
143
+ approvalAll(): Promise<boolean>;
144
+ }
145
+
146
+ export { CTFContract, Polymarket as default };
@@ -0,0 +1,146 @@
1
+ import { ClobClient, UserMarketOrder, CreateOrderOptions } from '@polymarket/clob-client';
2
+
3
+ /**
4
+ * Type definitions for prediction market platforms
5
+ *
6
+ * This module contains TypeScript interfaces and types used across
7
+ * the prediction SDK for various market platforms.
8
+ */
9
+ /**
10
+ * Configuration options for Polymarket client initialization
11
+ *
12
+ * @interface PolymarketOptions
13
+ * @property {string} rpcUrl - Polygon RPC endpoint URL for blockchain interactions
14
+ * @property {string} privateKey - Private key of the wallet to use for signing transactions
15
+ * @property {string} [cLobHttpUrl] - Optional CLOB API endpoint URL (defaults to https://clob.polymarket.com)
16
+ */
17
+ interface PolymarketOptions {
18
+ /** Polygon RPC endpoint URL for blockchain interactions */
19
+ rpcUrl: string;
20
+ /** Private key of the wallet to use for signing transactions */
21
+ privateKey: string;
22
+ /** Optional CLOB API endpoint URL (defaults to https://clob.polymarket.com) */
23
+ cLobHttpUrl?: string;
24
+ }
25
+
26
+ /**
27
+ * Polymarket integration module
28
+ *
29
+ * This module provides a client for interacting with Polymarket,
30
+ * a decentralized prediction market platform built on Polygon.
31
+ *
32
+ * Features:
33
+ * - Create and post market orders
34
+ * - Manage token approvals for trading
35
+ * - Support for both EOA and Gnosis Safe wallets
36
+ */
37
+
38
+ /**
39
+ * Conditional Token Framework contract address for Polygon
40
+ * This contract is used to manage conditional tokens (market shares)
41
+ */
42
+ declare const CTFContract: string;
43
+ /**
44
+ * Polymarket client class
45
+ *
46
+ * Provides methods to interact with Polymarket prediction markets,
47
+ * including creating orders, managing approvals, and handling transactions.
48
+ *
49
+ * @example
50
+ * ```typescript
51
+ * const polymarket = new Polymarket({
52
+ * rpcUrl: "https://polygon-rpc.com",
53
+ * privateKey: "0x...",
54
+ * });
55
+ *
56
+ * await polymarket.createClient();
57
+ * await polymarket.approvalAll();
58
+ * await polymarket.postOrder(order);
59
+ * ```
60
+ */
61
+ declare class Polymarket {
62
+ /** Configuration options for the Polymarket client */
63
+ private options;
64
+ /** Ethers.js provider for blockchain interactions */
65
+ private provider;
66
+ /** Wallet signer for transaction signing */
67
+ private signer;
68
+ /** CLOB signer for Polymarket SDK (ethers v5) */
69
+ private clobSigner;
70
+ /** Conditional Token Framework contract instance */
71
+ private ctfContract;
72
+ /** Polymarket CLOB client instance */
73
+ private client;
74
+ /** ClobClient signer type (ethers v5 compatibility) */
75
+ private getClobSigner;
76
+ /**
77
+ * Creates a new Polymarket client instance
78
+ *
79
+ * @param options - Configuration options including RPC URL and private key
80
+ * @throws {Error} If the private key or RPC URL is invalid
81
+ */
82
+ constructor(options: PolymarketOptions);
83
+ /**
84
+ * Creates and initializes the CLOB client
85
+ *
86
+ * Automatically detects if the wallet is a Gnosis Safe proxy or EOA,
87
+ * and configures the appropriate signature type.
88
+ *
89
+ * @returns {Promise<ClobClient>} The initialized CLOB client
90
+ * @throws {Error} If client creation fails
91
+ */
92
+ createClient(): Promise<ClobClient>;
93
+ /**
94
+ * Posts a market order to Polymarket
95
+ *
96
+ * Creates a signed market order and submits it to the order book
97
+ * using Fill-or-Kill (FOK) order type.
98
+ *
99
+ * @param {UserMarketOrder} userMarketOrder - The market order to post
100
+ * @param {Partial<CreateOrderOptions>} [options] - Optional order creation options
101
+ * @returns {Promise<void>} Promise that resolves when the order is posted
102
+ * @throws {Error} If order creation or posting fails
103
+ *
104
+ * @example
105
+ * ```typescript
106
+ * await polymarket.postOrder({
107
+ * market: "0x...",
108
+ * side: "YES",
109
+ * size: "1000000000",
110
+ * price: "0.5"
111
+ * });
112
+ * ```
113
+ */
114
+ postOrder(userMarketOrder: UserMarketOrder, options?: Partial<CreateOrderOptions>): Promise<void>;
115
+ /**
116
+ * Checks if the exchange contract is approved to manage tokens
117
+ *
118
+ * This checks if setApprovalForAll has been called for the Polymarket
119
+ * exchange contract, which is required before trading.
120
+ *
121
+ * @returns {Promise<boolean>} True if approved, false otherwise
122
+ */
123
+ isApproved(): Promise<boolean>;
124
+ /**
125
+ * Approves the Polymarket exchange to manage all tokens
126
+ *
127
+ * This is a one-time operation that allows the exchange contract
128
+ * to transfer conditional tokens on your behalf. Required before trading.
129
+ *
130
+ * Uses a 50% gas price buffer to ensure transaction confirmation.
131
+ *
132
+ * @returns {Promise<boolean>} True if approval succeeds
133
+ * @throws {Error} If the approval transaction fails or is not confirmed
134
+ *
135
+ * @example
136
+ * ```typescript
137
+ * const isApproved = await polymarket.isApproved();
138
+ * if (!isApproved) {
139
+ * await polymarket.approvalAll();
140
+ * }
141
+ * ```
142
+ */
143
+ approvalAll(): Promise<boolean>;
144
+ }
145
+
146
+ export { CTFContract, Polymarket as default };