@rainprotocolsdk/sdk 2.1.1 → 2.2.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/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
1
  export { Rain } from './Rain.js';
2
2
  export { RainAA } from './RainAA.js';
3
+ export { RainSocket } from './socket/RainSocket.js';
3
4
  export * from './types.js';
4
5
  export type { LoginParams, LoginResult } from './auth/types.js';
6
+ export type { EnterOptionEventData, OrderEventData, OrderFilledEventData, DisputeOpenedEventData, AppealOpenedEventData, DisputeTimeExtendedEventData, DisputeWinnerEventData, AppealWinnerEventData, ClaimRewardEventData } from './socket/types.js';
package/dist/index.js CHANGED
@@ -1,3 +1,4 @@
1
1
  export { Rain } from './Rain.js';
2
2
  export { RainAA } from './RainAA.js';
3
+ export { RainSocket } from './socket/RainSocket.js';
3
4
  export * from './types.js';
@@ -0,0 +1,59 @@
1
+ import { RainCoreConfig } from '../types.js';
2
+ import { EnterOptionEventData, OrderEventData, OrderFilledEventData, DisputeOpenedEventData, AppealOpenedEventData, DisputeTimeExtendedEventData, DisputeWinnerEventData, AppealWinnerEventData, ClaimRewardEventData } from './types.js';
3
+ export declare class RainSocket {
4
+ private socket;
5
+ constructor(config?: RainCoreConfig);
6
+ get connected(): boolean;
7
+ onConnect(callback: () => void): void;
8
+ onDisconnect(callback: () => void): void;
9
+ /**
10
+ * Subscribe to the enter-option event for a specific market.
11
+ * Fires when a user buys into a market option.
12
+ * Returns an unsubscribe function — call it to stop listening.
13
+ */
14
+ onEnterOption(marketId: string, callback: (data: EnterOptionEventData) => void): () => void;
15
+ /**
16
+ * Subscribe to the order-created event for a specific market.
17
+ * Fires when a new limit order is placed.
18
+ */
19
+ onOrderCreated(marketId: string, callback: (data: OrderEventData) => void): () => void;
20
+ /**
21
+ * Subscribe to the order-cancelled event for a specific market.
22
+ * Fires when an open order is cancelled.
23
+ */
24
+ onOrderCancelled(marketId: string, callback: (data: OrderEventData) => void): () => void;
25
+ /**
26
+ * Subscribe to the order-filled event for a specific market.
27
+ * Fires when an order is partially or fully filled.
28
+ */
29
+ onOrderFilled(marketId: string, callback: (data: OrderFilledEventData) => void): () => void;
30
+ /**
31
+ * Subscribe to the dispute-opened event for a specific market.
32
+ */
33
+ onDisputeOpened(marketId: string, callback: (data: DisputeOpenedEventData) => void): () => void;
34
+ /**
35
+ * Subscribe to the appeal-opened event for a specific market.
36
+ */
37
+ onAppealOpened(marketId: string, callback: (data: AppealOpenedEventData) => void): () => void;
38
+ /**
39
+ * Subscribe to the dispute-time-extented event for a specific market.
40
+ * Note: event name matches the backend spelling ("extented").
41
+ */
42
+ onDisputeTimeExtended(marketId: string, callback: (data: DisputeTimeExtendedEventData) => void): () => void;
43
+ /**
44
+ * Subscribe to the dispute-winner event for a specific market.
45
+ * Fires when a dispute winner is decided.
46
+ */
47
+ onDisputeWinner(marketId: string, callback: (data: DisputeWinnerEventData) => void): () => void;
48
+ /**
49
+ * Subscribe to the appeal-winner event for a specific market.
50
+ * Fires when an appeal winner is finalized.
51
+ */
52
+ onAppealWinner(marketId: string, callback: (data: AppealWinnerEventData) => void): () => void;
53
+ /**
54
+ * Subscribe to the claim-reward event for a specific market and user.
55
+ * Fires when a user's reward claim is confirmed.
56
+ */
57
+ onClaimReward(marketId: string, userId: string, callback: (data: ClaimRewardEventData) => void): () => void;
58
+ disconnect(): void;
59
+ }
@@ -0,0 +1,193 @@
1
+ import { io } from 'socket.io-client';
2
+ import { ALLOWED_ENVIRONMENTS, ENV_CONFIG } from '../config/environments.js';
3
+ export class RainSocket {
4
+ socket;
5
+ constructor(config = {}) {
6
+ const { environment = 'development' } = config;
7
+ function isValidEnvironment(env) {
8
+ return ALLOWED_ENVIRONMENTS.includes(env);
9
+ }
10
+ if (!isValidEnvironment(environment)) {
11
+ throw new Error(`Invalid environment "${environment}". Allowed values: ${ALLOWED_ENVIRONMENTS.join(', ')}`);
12
+ }
13
+ const { apiUrl } = ENV_CONFIG[environment];
14
+ this.socket = io(apiUrl, { transports: ['websocket', 'polling'] });
15
+ }
16
+ get connected() {
17
+ return this.socket.connected;
18
+ }
19
+ onConnect(callback) {
20
+ this.socket.on('connect', callback);
21
+ }
22
+ onDisconnect(callback) {
23
+ this.socket.on('disconnect', callback);
24
+ }
25
+ /**
26
+ * Subscribe to the enter-option event for a specific market.
27
+ * Fires when a user buys into a market option.
28
+ * Returns an unsubscribe function — call it to stop listening.
29
+ */
30
+ onEnterOption(marketId, callback) {
31
+ const event = `enter-option/${marketId}`;
32
+ const handler = (raw) => {
33
+ const investments = (raw?.enterOption?.investment ?? []).map((inv) => ({
34
+ choiceIndex: inv?.choiceIndex ?? inv?.option ?? 0,
35
+ amount: String(inv?.amount ?? inv?.investment ?? '0'),
36
+ optionName: inv?.optionName,
37
+ }));
38
+ callback({
39
+ poolId: raw?.pool?._id ?? marketId,
40
+ investments,
41
+ totalInvestmentWei: String(raw?.enterOption?.totalInvestment ?? '0'),
42
+ tokenDecimals: raw?.pool?.token?.tokenDecimals ?? 6,
43
+ });
44
+ };
45
+ this.socket.on(event, handler);
46
+ return () => this.socket.off(event, handler);
47
+ }
48
+ /**
49
+ * Subscribe to the order-created event for a specific market.
50
+ * Fires when a new limit order is placed.
51
+ */
52
+ onOrderCreated(marketId, callback) {
53
+ const event = `order-created/${marketId}`;
54
+ const handler = (raw) => {
55
+ callback({
56
+ poolId: raw?.pool?._id ?? marketId,
57
+ order: raw?.order ?? {},
58
+ tokenDecimals: raw?.pool?.token?.tokenDecimals ?? 6,
59
+ });
60
+ };
61
+ this.socket.on(event, handler);
62
+ return () => this.socket.off(event, handler);
63
+ }
64
+ /**
65
+ * Subscribe to the order-cancelled event for a specific market.
66
+ * Fires when an open order is cancelled.
67
+ */
68
+ onOrderCancelled(marketId, callback) {
69
+ const event = `order-cancelled/${marketId}`;
70
+ const handler = (raw) => {
71
+ callback({
72
+ poolId: raw?.pool?._id ?? marketId,
73
+ order: raw?.order ?? {},
74
+ tokenDecimals: raw?.pool?.token?.tokenDecimals ?? 6,
75
+ });
76
+ };
77
+ this.socket.on(event, handler);
78
+ return () => this.socket.off(event, handler);
79
+ }
80
+ /**
81
+ * Subscribe to the order-filled event for a specific market.
82
+ * Fires when an order is partially or fully filled.
83
+ */
84
+ onOrderFilled(marketId, callback) {
85
+ const event = `order-filled/${marketId}`;
86
+ const handler = (raw) => {
87
+ callback({ poolId: raw?.pool?._id ?? marketId, raw });
88
+ };
89
+ this.socket.on(event, handler);
90
+ return () => this.socket.off(event, handler);
91
+ }
92
+ /**
93
+ * Subscribe to the dispute-opened event for a specific market.
94
+ */
95
+ onDisputeOpened(marketId, callback) {
96
+ const event = `dispute-opened/${marketId}`;
97
+ const handler = (raw) => {
98
+ callback({
99
+ poolId: raw?.pool?._id ?? marketId,
100
+ isDisputed: raw?.pool?.isDisputed ?? false,
101
+ isAiResolver: raw?.pool?.isAiResolver ?? false,
102
+ status: raw?.pool?.status ?? '',
103
+ disputeData: raw?.pool?.disputeData ?? null,
104
+ winnerDecidedBy: raw?.pool?.winnerOption?.decidedBy ?? '',
105
+ });
106
+ };
107
+ this.socket.on(event, handler);
108
+ return () => this.socket.off(event, handler);
109
+ }
110
+ /**
111
+ * Subscribe to the appeal-opened event for a specific market.
112
+ */
113
+ onAppealOpened(marketId, callback) {
114
+ const event = `appeal-opened/${marketId}`;
115
+ const handler = (raw) => {
116
+ callback({
117
+ poolId: raw?.pool?._id ?? marketId,
118
+ eventType: raw?.eventType ?? '',
119
+ isAppealed: raw?.pool?.isAppealed ?? false,
120
+ });
121
+ };
122
+ this.socket.on(event, handler);
123
+ return () => this.socket.off(event, handler);
124
+ }
125
+ /**
126
+ * Subscribe to the dispute-time-extented event for a specific market.
127
+ * Note: event name matches the backend spelling ("extented").
128
+ */
129
+ onDisputeTimeExtended(marketId, callback) {
130
+ const event = `dispute-time-extented/${marketId}`;
131
+ const handler = (raw) => {
132
+ callback({
133
+ poolId: raw?.pool?._id ?? marketId,
134
+ eventType: raw?.eventType ?? '',
135
+ newEndTime: raw?.newEndTime ?? '',
136
+ });
137
+ };
138
+ this.socket.on(event, handler);
139
+ return () => this.socket.off(event, handler);
140
+ }
141
+ /**
142
+ * Subscribe to the dispute-winner event for a specific market.
143
+ * Fires when a dispute winner is decided.
144
+ */
145
+ onDisputeWinner(marketId, callback) {
146
+ const event = `dispute-winner/${marketId}`;
147
+ const handler = (raw) => {
148
+ callback({
149
+ poolId: raw?.pool?._id ?? marketId,
150
+ status: raw?.pool?.status ?? '',
151
+ winnerOption: {
152
+ decidedBy: raw?.pool?.winnerOption?.decidedBy ?? '',
153
+ choiceIndex: raw?.pool?.winnerOption?.choiceIndex ?? 0,
154
+ createdAt: raw?.pool?.winnerOption?.createdAt ?? '',
155
+ },
156
+ });
157
+ };
158
+ this.socket.on(event, handler);
159
+ return () => this.socket.off(event, handler);
160
+ }
161
+ /**
162
+ * Subscribe to the appeal-winner event for a specific market.
163
+ * Fires when an appeal winner is finalized.
164
+ */
165
+ onAppealWinner(marketId, callback) {
166
+ const event = `appeal-winner/${marketId}`;
167
+ const handler = (raw) => {
168
+ callback({
169
+ poolId: raw?.pool?._id ?? marketId,
170
+ eventType: raw?.eventType ?? '',
171
+ winnerFinalized: raw?.winnerFinalized ?? null,
172
+ winnerOption: raw?.pool?.winnerOption ?? null,
173
+ });
174
+ };
175
+ this.socket.on(event, handler);
176
+ return () => this.socket.off(event, handler);
177
+ }
178
+ /**
179
+ * Subscribe to the claim-reward event for a specific market and user.
180
+ * Fires when a user's reward claim is confirmed.
181
+ */
182
+ onClaimReward(marketId, userId, callback) {
183
+ const event = `claim-reward/${marketId}/${userId}`;
184
+ const handler = (_raw) => {
185
+ callback({ poolId: marketId, userId });
186
+ };
187
+ this.socket.on(event, handler);
188
+ return () => this.socket.off(event, handler);
189
+ }
190
+ disconnect() {
191
+ this.socket.disconnect();
192
+ }
193
+ }
@@ -0,0 +1,65 @@
1
+ export interface EnterOptionEventData {
2
+ poolId: string;
3
+ investments: {
4
+ choiceIndex: number;
5
+ amount: string;
6
+ optionName?: string;
7
+ }[];
8
+ totalInvestmentWei: string;
9
+ tokenDecimals: number;
10
+ }
11
+ export interface OrderEventData {
12
+ poolId: string;
13
+ order: {
14
+ _id: string;
15
+ orderType: 'buy' | 'sell';
16
+ option: number;
17
+ optionName?: string;
18
+ pricePerShare: string;
19
+ quantity: string;
20
+ createdAt: string;
21
+ [key: string]: any;
22
+ };
23
+ tokenDecimals: number;
24
+ }
25
+ export interface OrderFilledEventData {
26
+ poolId: string;
27
+ raw: any;
28
+ }
29
+ export interface DisputeOpenedEventData {
30
+ poolId: string;
31
+ isDisputed: boolean;
32
+ isAiResolver: boolean;
33
+ status: string;
34
+ disputeData: any;
35
+ winnerDecidedBy: string;
36
+ }
37
+ export interface AppealOpenedEventData {
38
+ poolId: string;
39
+ eventType: string;
40
+ isAppealed: boolean;
41
+ }
42
+ export interface DisputeTimeExtendedEventData {
43
+ poolId: string;
44
+ eventType: string;
45
+ newEndTime: string | number;
46
+ }
47
+ export interface DisputeWinnerEventData {
48
+ poolId: string;
49
+ status: string;
50
+ winnerOption: {
51
+ decidedBy: string;
52
+ choiceIndex: number;
53
+ createdAt: string;
54
+ };
55
+ }
56
+ export interface AppealWinnerEventData {
57
+ poolId: string;
58
+ eventType: string;
59
+ winnerFinalized: any;
60
+ winnerOption: any;
61
+ }
62
+ export interface ClaimRewardEventData {
63
+ poolId: string;
64
+ userId: string;
65
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -30,21 +30,33 @@ export async function buildCloseMarketRawTx(params) {
30
30
  if (proposedOutcome === undefined) {
31
31
  throw new Error("proposedOutcome is required for V2 markets");
32
32
  }
33
- txs.push({
34
- to: contractAddress,
35
- data: encodeFunctionData({
36
- abi: TradePoolAbi,
37
- functionName: CLOSE_POOL,
38
- }),
39
- });
40
- txs.push({
41
- to: contractAddress,
42
- data: encodeFunctionData({
43
- abi: TradePoolAbi,
44
- functionName: CHOOSE_WINNER,
45
- args: [BigInt(proposedOutcome)],
46
- }),
47
- });
33
+ if (isAiResolver) {
34
+ // AI resolver: closePool() with no args
35
+ txs.push({
36
+ to: contractAddress,
37
+ data: encodeFunctionData({
38
+ abi: TradePoolAbi,
39
+ functionName: CLOSE_POOL,
40
+ }),
41
+ });
42
+ }
43
+ else {
44
+ txs.push({
45
+ to: contractAddress,
46
+ data: encodeFunctionData({
47
+ abi: TradePoolAbi,
48
+ functionName: CLOSE_POOL,
49
+ }),
50
+ });
51
+ txs.push({
52
+ to: contractAddress,
53
+ data: encodeFunctionData({
54
+ abi: TradePoolAbi,
55
+ functionName: CHOOSE_WINNER,
56
+ args: [BigInt(proposedOutcome)],
57
+ }),
58
+ });
59
+ }
48
60
  }
49
61
  else {
50
62
  // V3 flow: optional approvals → closePool() or closePool(proposedOutcome)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rainprotocolsdk/sdk",
3
- "version": "2.1.1",
3
+ "version": "2.2.0",
4
4
  "type": "module",
5
5
  "description": "Rain SDK",
6
6
  "main": "dist/index.js",
@@ -38,7 +38,8 @@
38
38
  "@account-kit/wallet-client": "^0.1.0-alpha.10",
39
39
  "@alchemy/aa-alchemy": "^3.0.0",
40
40
  "@alchemy/aa-core": "^3.0.0",
41
- "ethers": "^6.16.0"
41
+ "ethers": "^6.16.0",
42
+ "socket.io-client": "^4.8.3"
42
43
  },
43
44
  "devDependencies": {
44
45
  "typescript": "^5.9.3"