@rainprotocolsdk/sdk 1.3.0 → 2.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.
Files changed (43) hide show
  1. package/README.md +600 -103
  2. package/dist/Rain.d.ts +14 -3
  3. package/dist/Rain.js +38 -2
  4. package/dist/abi/TradeMarketsAbi.d.ts +66 -0
  5. package/dist/abi/TradeMarketsAbi.js +85 -0
  6. package/dist/auth/login.d.ts +4 -0
  7. package/dist/auth/login.js +24 -0
  8. package/dist/auth/types.d.ts +16 -0
  9. package/dist/auth/types.js +1 -0
  10. package/dist/config/environments.d.ts +5 -0
  11. package/dist/config/environments.js +8 -3
  12. package/dist/constants/contractmethods.d.ts +8 -0
  13. package/dist/constants/contractmethods.js +8 -0
  14. package/dist/index.d.ts +1 -0
  15. package/dist/markets/getMarketById.d.ts +2 -0
  16. package/dist/markets/getMarketById.js +12 -0
  17. package/dist/markets/getUserInvestments.d.ts +2 -0
  18. package/dist/markets/getUserInvestments.js +23 -0
  19. package/dist/markets/types.d.ts +21 -0
  20. package/dist/tx/CloseMarket/buildCloseMarketRawTx.d.ts +2 -0
  21. package/dist/tx/CloseMarket/buildCloseMarketRawTx.js +97 -0
  22. package/dist/tx/CloseMarket/helpers.d.ts +2 -0
  23. package/dist/tx/CloseMarket/helpers.js +16 -0
  24. package/dist/tx/CreateMarket/buildCreateMarketRawTx.d.ts +1 -1
  25. package/dist/tx/CreateMarket/buildCreateMarketRawTx.js +17 -6
  26. package/dist/tx/CreateMarket/createMarketValidation.js +18 -4
  27. package/dist/tx/CreateMarket/helpers.d.ts +2 -0
  28. package/dist/tx/CreateMarket/helpers.js +33 -0
  29. package/dist/tx/Dispute/buildDisputeRawTx.d.ts +3 -0
  30. package/dist/tx/Dispute/buildDisputeRawTx.js +59 -0
  31. package/dist/tx/Dispute/helpers.d.ts +1 -0
  32. package/dist/tx/Dispute/helpers.js +9 -0
  33. package/dist/tx/buildApprovalRawTx.d.ts +1 -1
  34. package/dist/tx/buildCancelAllOpenOrdersRawTx.d.ts +2 -0
  35. package/dist/tx/buildCancelAllOpenOrdersRawTx.js +35 -0
  36. package/dist/tx/buildCancelOrdersRawTx.d.ts +2 -0
  37. package/dist/tx/buildCancelOrdersRawTx.js +42 -0
  38. package/dist/tx/buildRawTransactions.d.ts +2 -1
  39. package/dist/tx/buildRawTransactions.js +35 -1
  40. package/dist/tx/types.d.ts +60 -2
  41. package/dist/utils/helpers.d.ts +2 -0
  42. package/dist/utils/helpers.js +14 -1
  43. package/package.json +2 -2
package/dist/Rain.d.ts CHANGED
@@ -1,17 +1,28 @@
1
- import { GetMarketsParams, Market } from './markets/types.js';
2
- import { ApproveTxParams, ClaimTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './tx/types.js';
1
+ import { GetMarketByIdParams, GetMarketsParams, GetUserInvestmentsParams, Market, UserInvestment } from './markets/types.js';
2
+ import { ApproveTxParams, CancelAllOpenOrdersTxParams, CancelOrdersTxParams, ClaimTxParams, CloseMarketTxParams, CreateDisputeTxParams, CreateAppealTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, LimitSellOptionTxParams, RawTransaction } from './tx/types.js';
3
3
  import { RainCoreConfig, RainEnvironment } from './types.js';
4
+ import { LoginParams, LoginResult } from './auth/types.js';
4
5
  export declare class Rain {
5
6
  readonly environment: RainEnvironment;
6
7
  private readonly marketFactory;
7
8
  private readonly apiUrl;
8
9
  private readonly distute_initial_timer;
9
10
  private readonly rpcUrl?;
11
+ private readonly usdtSymbol;
10
12
  constructor(config?: RainCoreConfig);
11
13
  getPublicMarkets(params: GetMarketsParams): Promise<Market[]>;
14
+ getMarketById(params: GetMarketByIdParams): Promise<Market>;
15
+ getUserInvestments(params: GetUserInvestmentsParams): Promise<UserInvestment[]>;
12
16
  buildApprovalTx(params: ApproveTxParams): RawTransaction | Error;
13
17
  buildBuyOptionRawTx(params: EnterOptionTxParams): RawTransaction;
14
18
  buildLimitBuyOptionTx(params: EnterLimitOptionTxParams): RawTransaction;
15
- buildCreateMarketTx(params: CreateMarketTxParams): RawTransaction;
19
+ buildLimitSellOptionTx(params: LimitSellOptionTxParams): RawTransaction;
20
+ buildCancelOrdersTx(params: CancelOrdersTxParams): RawTransaction[];
21
+ buildCancelAllOpenOrdersTx(params: CancelAllOpenOrdersTxParams): Promise<RawTransaction[]>;
22
+ buildCreateMarketTx(params: CreateMarketTxParams): Promise<RawTransaction[]>;
16
23
  buildClaimTx(params: ClaimTxParams): Promise<RawTransaction>;
24
+ buildCloseMarketTx(params: CloseMarketTxParams): Promise<RawTransaction[]>;
25
+ buildCreateDisputeTx(params: CreateDisputeTxParams): Promise<RawTransaction[]>;
26
+ buildCreateAppealTx(params: CreateAppealTxParams): Promise<RawTransaction[]>;
27
+ login(params: LoginParams): Promise<LoginResult>;
17
28
  }
package/dist/Rain.js CHANGED
@@ -1,15 +1,23 @@
1
1
  import { getMarkets } from './markets/getMarkets.js';
2
- import { buildEnterOptionRawTx, buildLimitBuyOrderRawTx } from './tx/buildRawTransactions.js';
2
+ import { getMarketById } from './markets/getMarketById.js';
3
+ import { getUserInvestments } from './markets/getUserInvestments.js';
4
+ import { buildEnterOptionRawTx, buildLimitBuyOrderRawTx, buildLimitSellOrderRawTx } from './tx/buildRawTransactions.js';
5
+ import { buildCancelOrdersRawTx } from './tx/buildCancelOrdersRawTx.js';
6
+ import { buildCancelAllOpenOrdersRawTx } from './tx/buildCancelAllOpenOrdersRawTx.js';
7
+ import { buildCloseMarketRawTx } from './tx/CloseMarket/buildCloseMarketRawTx.js';
8
+ import { buildCreateDisputeRawTx, buildCreateAppealRawTx } from './tx/Dispute/buildDisputeRawTx.js';
3
9
  import { buildApproveRawTx } from './tx/buildApprovalRawTx.js';
4
10
  import { buildCreateMarketRawTx } from './tx/CreateMarket/buildCreateMarketRawTx.js';
5
11
  import { ALLOWED_ENVIRONMENTS, ENV_CONFIG, getRandomRpc } from './config/environments.js';
6
12
  import { buildClaimRawTx } from './tx/ClaimFunds/buildClaimFundsRawTx.js';
13
+ import { loginUser } from './auth/login.js';
7
14
  export class Rain {
8
15
  environment;
9
16
  marketFactory;
10
17
  apiUrl;
11
18
  distute_initial_timer;
12
19
  rpcUrl;
20
+ usdtSymbol;
13
21
  constructor(config = {}) {
14
22
  const { environment = "development", rpcUrl } = config;
15
23
  function isValidEnvironment(env) {
@@ -24,10 +32,17 @@ export class Rain {
24
32
  this.marketFactory = envConfig.market_factory_address;
25
33
  this.apiUrl = envConfig.apiUrl;
26
34
  this.distute_initial_timer = envConfig.dispute_initial_timer;
35
+ this.usdtSymbol = envConfig.usdt_symbol;
27
36
  }
28
37
  async getPublicMarkets(params) {
29
38
  return getMarkets({ ...params, apiUrl: this.apiUrl });
30
39
  }
40
+ async getMarketById(params) {
41
+ return getMarketById({ ...params, apiUrl: this.apiUrl });
42
+ }
43
+ async getUserInvestments(params) {
44
+ return getUserInvestments({ ...params, apiUrl: this.apiUrl });
45
+ }
31
46
  buildApprovalTx(params) {
32
47
  return buildApproveRawTx(params);
33
48
  }
@@ -37,10 +52,31 @@ export class Rain {
37
52
  buildLimitBuyOptionTx(params) {
38
53
  return buildLimitBuyOrderRawTx(params);
39
54
  }
55
+ buildLimitSellOptionTx(params) {
56
+ return buildLimitSellOrderRawTx(params);
57
+ }
58
+ buildCancelOrdersTx(params) {
59
+ return buildCancelOrdersRawTx(params);
60
+ }
61
+ async buildCancelAllOpenOrdersTx(params) {
62
+ return buildCancelAllOpenOrdersRawTx({ ...params, apiUrl: this.apiUrl });
63
+ }
40
64
  buildCreateMarketTx(params) {
41
- return buildCreateMarketRawTx({ ...params, factoryContractAddress: this.marketFactory, disputeTimer: this.distute_initial_timer });
65
+ return buildCreateMarketRawTx({ ...params, factoryContractAddress: this.marketFactory, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl, disputeTimer: this.distute_initial_timer });
42
66
  }
43
67
  async buildClaimTx(params) {
44
68
  return buildClaimRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl });
45
69
  }
70
+ async buildCloseMarketTx(params) {
71
+ return buildCloseMarketRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl, usdtSymbol: this.usdtSymbol });
72
+ }
73
+ async buildCreateDisputeTx(params) {
74
+ return buildCreateDisputeRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl, usdtSymbol: this.usdtSymbol });
75
+ }
76
+ async buildCreateAppealTx(params) {
77
+ return buildCreateAppealRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl, usdtSymbol: this.usdtSymbol });
78
+ }
79
+ async login(params) {
80
+ return loginUser({ ...params, apiUrl: this.apiUrl });
81
+ }
46
82
  }
@@ -267,12 +267,78 @@ export declare const TradePoolAbi: readonly [{
267
267
  readonly internalType: "bool";
268
268
  }];
269
269
  readonly stateMutability: "view";
270
+ }, {
271
+ readonly type: "function";
272
+ readonly name: "openDispute";
273
+ readonly inputs: readonly [];
274
+ readonly outputs: readonly [];
275
+ readonly stateMutability: "nonpayable";
276
+ }, {
277
+ readonly type: "function";
278
+ readonly name: "getDisputeAppealFee";
279
+ readonly inputs: readonly [];
280
+ readonly outputs: readonly [{
281
+ readonly name: "disputeFee";
282
+ readonly type: "uint256";
283
+ readonly internalType: "uint256";
284
+ }];
285
+ readonly stateMutability: "view";
286
+ }, {
287
+ readonly type: "function";
288
+ readonly name: "DISPUTE_FEE_MIN";
289
+ readonly inputs: readonly [];
290
+ readonly outputs: readonly [{
291
+ readonly name: "";
292
+ readonly type: "uint256";
293
+ readonly internalType: "uint256";
294
+ }];
295
+ readonly stateMutability: "view";
296
+ }, {
297
+ readonly type: "function";
298
+ readonly name: "DISPUTE_FEE_MAX";
299
+ readonly inputs: readonly [];
300
+ readonly outputs: readonly [{
301
+ readonly name: "";
302
+ readonly type: "uint256";
303
+ readonly internalType: "uint256";
304
+ }];
305
+ readonly stateMutability: "view";
306
+ }, {
307
+ readonly type: "function";
308
+ readonly name: "APPEAL_FEE_MIN";
309
+ readonly inputs: readonly [];
310
+ readonly outputs: readonly [{
311
+ readonly name: "";
312
+ readonly type: "uint256";
313
+ readonly internalType: "uint256";
314
+ }];
315
+ readonly stateMutability: "view";
270
316
  }, {
271
317
  readonly type: "function";
272
318
  readonly name: "closePool";
273
319
  readonly inputs: readonly [];
274
320
  readonly outputs: readonly [];
275
321
  readonly stateMutability: "nonpayable";
322
+ }, {
323
+ readonly type: "function";
324
+ readonly name: "closePool";
325
+ readonly inputs: readonly [{
326
+ readonly name: "proposedWinner";
327
+ readonly type: "uint256";
328
+ readonly internalType: "uint256";
329
+ }];
330
+ readonly outputs: readonly [];
331
+ readonly stateMutability: "nonpayable";
332
+ }, {
333
+ readonly type: "function";
334
+ readonly name: "getResolverBondAmount";
335
+ readonly inputs: readonly [];
336
+ readonly outputs: readonly [{
337
+ readonly name: "resolverBondAmount";
338
+ readonly type: "uint256";
339
+ readonly internalType: "uint256";
340
+ }];
341
+ readonly stateMutability: "view";
276
342
  }, {
277
343
  readonly type: "function";
278
344
  readonly name: "creatorFee";
@@ -347,6 +347,65 @@ export const TradePoolAbi = [
347
347
  ],
348
348
  "stateMutability": "view"
349
349
  },
350
+ {
351
+ "type": "function",
352
+ "name": "openDispute",
353
+ "inputs": [],
354
+ "outputs": [],
355
+ "stateMutability": "nonpayable"
356
+ },
357
+ {
358
+ "type": "function",
359
+ "name": "getDisputeAppealFee",
360
+ "inputs": [],
361
+ "outputs": [
362
+ {
363
+ "name": "disputeFee",
364
+ "type": "uint256",
365
+ "internalType": "uint256"
366
+ }
367
+ ],
368
+ "stateMutability": "view"
369
+ },
370
+ {
371
+ "type": "function",
372
+ "name": "DISPUTE_FEE_MIN",
373
+ "inputs": [],
374
+ "outputs": [
375
+ {
376
+ "name": "",
377
+ "type": "uint256",
378
+ "internalType": "uint256"
379
+ }
380
+ ],
381
+ "stateMutability": "view"
382
+ },
383
+ {
384
+ "type": "function",
385
+ "name": "DISPUTE_FEE_MAX",
386
+ "inputs": [],
387
+ "outputs": [
388
+ {
389
+ "name": "",
390
+ "type": "uint256",
391
+ "internalType": "uint256"
392
+ }
393
+ ],
394
+ "stateMutability": "view"
395
+ },
396
+ {
397
+ "type": "function",
398
+ "name": "APPEAL_FEE_MIN",
399
+ "inputs": [],
400
+ "outputs": [
401
+ {
402
+ "name": "",
403
+ "type": "uint256",
404
+ "internalType": "uint256"
405
+ }
406
+ ],
407
+ "stateMutability": "view"
408
+ },
350
409
  {
351
410
  "type": "function",
352
411
  "name": "closePool",
@@ -354,6 +413,32 @@ export const TradePoolAbi = [
354
413
  "outputs": [],
355
414
  "stateMutability": "nonpayable"
356
415
  },
416
+ {
417
+ "type": "function",
418
+ "name": "closePool",
419
+ "inputs": [
420
+ {
421
+ "name": "proposedWinner",
422
+ "type": "uint256",
423
+ "internalType": "uint256"
424
+ }
425
+ ],
426
+ "outputs": [],
427
+ "stateMutability": "nonpayable"
428
+ },
429
+ {
430
+ "type": "function",
431
+ "name": "getResolverBondAmount",
432
+ "inputs": [],
433
+ "outputs": [
434
+ {
435
+ "name": "resolverBondAmount",
436
+ "type": "uint256",
437
+ "internalType": "uint256"
438
+ }
439
+ ],
440
+ "stateMutability": "view"
441
+ },
357
442
  {
358
443
  "type": "function",
359
444
  "name": "creatorFee",
@@ -0,0 +1,4 @@
1
+ import { LoginParams, LoginResult } from './types.js';
2
+ export declare function loginUser(params: LoginParams & {
3
+ apiUrl: string;
4
+ }): Promise<LoginResult>;
@@ -0,0 +1,24 @@
1
+ export async function loginUser(params) {
2
+ const { signature, walletAddress, smartWalletAddress, referredBy, apiUrl } = params;
3
+ const res = await fetch(`${apiUrl}/auth/login-or-register-with-walletAddress`, {
4
+ method: 'POST',
5
+ headers: { 'Content-Type': 'application/json' },
6
+ body: JSON.stringify({
7
+ sign: signature,
8
+ walletAddress,
9
+ userSmartAddress: smartWalletAddress,
10
+ ...(referredBy ? { referredBy } : {}),
11
+ }),
12
+ });
13
+ if (!res.ok) {
14
+ const text = await res.text().catch(() => res.statusText);
15
+ throw new Error(`Login failed (${res.status}): ${text}`);
16
+ }
17
+ const data = await res.json();
18
+ const accessToken = data?.token;
19
+ const userId = data?.user?._id;
20
+ if (!accessToken || !userId) {
21
+ throw new Error('Login response missing token or user id');
22
+ }
23
+ return { accessToken, userId };
24
+ }
@@ -0,0 +1,16 @@
1
+ export interface LoginParams {
2
+ /** Signed message (personal_sign of the lowercased wallet address) */
3
+ signature: string;
4
+ /** EOA wallet address */
5
+ walletAddress: string;
6
+ /** Smart account / AA wallet address */
7
+ smartWalletAddress: string;
8
+ /** Optional referral code */
9
+ referredBy?: string;
10
+ }
11
+ export interface LoginResult {
12
+ /** JWT access token */
13
+ accessToken: string;
14
+ /** Backend user ID */
15
+ userId: string;
16
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -1,20 +1,25 @@
1
1
  export declare const ALLOWED_ENVIRONMENTS: readonly ["development", "stage", "production"];
2
2
  export declare const DEFAULT_RPCS: string[];
3
3
  export declare function getRandomRpc(): string;
4
+ export declare const USDT_SYMBOL_DEV = "USDTm";
5
+ export declare const USDT_SYMBOL_PROD = "USD\u20AE0";
4
6
  export declare const ENV_CONFIG: {
5
7
  readonly development: {
6
8
  readonly apiUrl: "https://dev-api.rain.one";
7
9
  readonly market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313";
8
10
  readonly dispute_initial_timer: number;
11
+ readonly usdt_symbol: "USDTm";
9
12
  };
10
13
  readonly stage: {
11
14
  readonly apiUrl: "https://stg-api.rain.one";
12
15
  readonly market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561";
13
16
  readonly dispute_initial_timer: number;
17
+ readonly usdt_symbol: "USD₮0";
14
18
  };
15
19
  readonly production: {
16
20
  readonly apiUrl: "https://prod-api.rain.one";
17
21
  readonly market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1";
18
22
  readonly dispute_initial_timer: number;
23
+ readonly usdt_symbol: "USD₮0";
19
24
  };
20
25
  };
@@ -8,20 +8,25 @@ export function getRandomRpc() {
8
8
  const index = Math.floor(Math.random() * DEFAULT_RPCS.length);
9
9
  return DEFAULT_RPCS[index];
10
10
  }
11
+ export const USDT_SYMBOL_DEV = "USDTm";
12
+ export const USDT_SYMBOL_PROD = "USD₮0";
11
13
  export const ENV_CONFIG = {
12
14
  development: {
13
15
  apiUrl: "https://dev-api.rain.one",
14
16
  market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313",
15
- dispute_initial_timer: 1 * 60
17
+ dispute_initial_timer: 1 * 60,
18
+ usdt_symbol: USDT_SYMBOL_DEV,
16
19
  },
17
20
  stage: {
18
21
  apiUrl: "https://stg-api.rain.one",
19
22
  market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561",
20
- dispute_initial_timer: 1 * 60
23
+ dispute_initial_timer: 1 * 60,
24
+ usdt_symbol: USDT_SYMBOL_PROD,
21
25
  },
22
26
  production: {
23
27
  apiUrl: "https://prod-api.rain.one",
24
28
  market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1",
25
- dispute_initial_timer: 120 * 60
29
+ dispute_initial_timer: 120 * 60,
30
+ usdt_symbol: USDT_SYMBOL_PROD,
26
31
  },
27
32
  };
@@ -1,5 +1,13 @@
1
1
  export declare const ENTER_OPTION = "enterOption";
2
2
  export declare const PLACE_BUY_ORDER = "placeBuyOrder";
3
+ export declare const PLACE_SELL_ORDER = "placeSellOrder";
3
4
  export declare const APPROVE_TOKEN = "approve";
4
5
  export declare const CREATE_MARKET = "createPool";
5
6
  export declare const CLAIM = "claim";
7
+ export declare const CLOSE_POOL = "closePool";
8
+ export declare const CHOOSE_WINNER = "chooseWinner";
9
+ export declare const GET_RESOLVER_BOND_AMOUNT = "getResolverBondAmount";
10
+ export declare const OPEN_DISPUTE = "openDispute";
11
+ export declare const GET_DISPUTE_APPEAL_FEE = "getDisputeAppealFee";
12
+ export declare const CANCEL_BUY_ORDERS = "cancelBuyOrders";
13
+ export declare const CANCEL_SELL_ORDERS = "cancelSellOrders";
@@ -1,5 +1,13 @@
1
1
  export const ENTER_OPTION = 'enterOption';
2
2
  export const PLACE_BUY_ORDER = 'placeBuyOrder';
3
+ export const PLACE_SELL_ORDER = 'placeSellOrder';
3
4
  export const APPROVE_TOKEN = 'approve';
4
5
  export const CREATE_MARKET = 'createPool';
5
6
  export const CLAIM = "claim";
7
+ export const CLOSE_POOL = 'closePool';
8
+ export const CHOOSE_WINNER = 'chooseWinner';
9
+ export const GET_RESOLVER_BOND_AMOUNT = 'getResolverBondAmount';
10
+ export const OPEN_DISPUTE = 'openDispute';
11
+ export const GET_DISPUTE_APPEAL_FEE = 'getDisputeAppealFee';
12
+ export const CANCEL_BUY_ORDERS = 'cancelBuyOrders';
13
+ export const CANCEL_SELL_ORDERS = 'cancelSellOrders';
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
1
  export { Rain } from './Rain.js';
2
2
  export { RainAA } from './RainAA.js';
3
3
  export * from './types.js';
4
+ export type { LoginParams, LoginResult } from './auth/types.js';
@@ -0,0 +1,2 @@
1
+ import { GetMarketByIdParams, Market } from './types.js';
2
+ export declare function getMarketById(params: GetMarketByIdParams): Promise<Market>;
@@ -0,0 +1,12 @@
1
+ export async function getMarketById(params) {
2
+ if (!params?.apiUrl)
3
+ throw new Error("Environment is not set properly, api url is missing");
4
+ if (!params?.marketId)
5
+ throw new Error("marketId is required");
6
+ const res = await fetch(`${params.apiUrl}/pools/pool/${params.marketId}`);
7
+ if (!res.ok) {
8
+ throw new Error(`Failed to fetch market: ${res.status}`);
9
+ }
10
+ const data = await res.json();
11
+ return data;
12
+ }
@@ -0,0 +1,2 @@
1
+ import { GetUserInvestmentsParams, UserInvestment } from './types.js';
2
+ export declare function getUserInvestments(params: GetUserInvestmentsParams): Promise<UserInvestment[]>;
@@ -0,0 +1,23 @@
1
+ import { MARKET_STATUS } from './constants.js';
2
+ export async function getUserInvestments(params) {
3
+ if (!params?.apiUrl)
4
+ throw new Error("Environment is not set properly, api url is missing");
5
+ if (!params?.walletAddress)
6
+ throw new Error("walletAddress is required");
7
+ if (!params?.accessToken)
8
+ throw new Error("accessToken is required");
9
+ const query = new URLSearchParams();
10
+ query.append('walletAddress', params.walletAddress);
11
+ if (params?.limit)
12
+ query.append('limit', params.limit.toString());
13
+ if (params?.offset)
14
+ query.append('offset', params.offset.toString());
15
+ if (params?.status)
16
+ query.append('status', MARKET_STATUS[params.status]);
17
+ const res = await fetch(`${params.apiUrl}/investments/user-invested-pools?${query.toString()}`, { headers: { Authorization: `Bearer ${params.accessToken}` } });
18
+ if (!res.ok) {
19
+ throw new Error(`Failed to fetch user investments: ${res.status}`);
20
+ }
21
+ const data = await res.json();
22
+ return data?.data ?? data;
23
+ }
@@ -13,3 +13,24 @@ export interface Market {
13
13
  totalVolume: string;
14
14
  status: MarketStatus;
15
15
  }
16
+ export interface GetMarketByIdParams {
17
+ marketId: string;
18
+ apiUrl?: string;
19
+ }
20
+ export interface GetUserInvestmentsParams {
21
+ walletAddress: string;
22
+ accessToken: string;
23
+ limit?: number;
24
+ offset?: number;
25
+ status?: MarketStatus;
26
+ apiUrl?: string;
27
+ }
28
+ export interface UserInvestment {
29
+ _id: string;
30
+ poolId: string;
31
+ walletAddress: string;
32
+ investment: any[];
33
+ totalInvestment: string;
34
+ pool: Record<string, any>;
35
+ [key: string]: any;
36
+ }
@@ -0,0 +1,2 @@
1
+ import { CloseMarketTxParams, RawTransaction } from "../types.js";
2
+ export declare function buildCloseMarketRawTx(params: CloseMarketTxParams): Promise<RawTransaction[]>;
@@ -0,0 +1,97 @@
1
+ import { encodeFunctionData } from "viem";
2
+ import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
3
+ import { getMarket, isRpcValid } from "../ClaimFunds/helpers.js";
4
+ import { buildApproveRawTx } from "../buildApprovalRawTx.js";
5
+ import { CLOSE_POOL, CHOOSE_WINNER } from "../../constants/contractmethods.js";
6
+ import { getResolverBondAmount, getTokenAllowance } from "./helpers.js";
7
+ export async function buildCloseMarketRawTx(params) {
8
+ const { marketId, walletAddress, proposedOutcome, usdtTokenAddress, rainTokenAddress, usdtSymbol, apiUrl, rpcUrl, } = params;
9
+ if (!apiUrl)
10
+ throw new Error("Environment is not set properly, api url is missing");
11
+ if (!rpcUrl)
12
+ throw new Error("rpcUrl is required");
13
+ const isRpcWorking = await isRpcValid(rpcUrl);
14
+ if (!isRpcWorking)
15
+ throw new Error("Provided RPC URL is not valid or not working");
16
+ if (!marketId)
17
+ throw new Error("marketId is required");
18
+ if (!walletAddress)
19
+ throw new Error("walletAddress is required");
20
+ const { data } = await getMarket({ marketId, apiUrl });
21
+ const contractAddress = data?.contractAddress;
22
+ const version = data?.version ?? 2;
23
+ const isAiResolver = data?.isAiResolver ?? false;
24
+ const marketTokenSymbol = data?.token?.tokenSymbol ?? "";
25
+ if (!contractAddress)
26
+ throw new Error("Market contract address not found");
27
+ const txs = [];
28
+ if (version <= 2) {
29
+ // V2 flow: closePool() (no args) → chooseWinner(proposedOutcome)
30
+ if (proposedOutcome === undefined) {
31
+ throw new Error("proposedOutcome is required for V2 markets");
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
+ });
48
+ }
49
+ else {
50
+ // V3 flow: optional approvals → closePool() or closePool(proposedOutcome)
51
+ const isUsdtMarket = usdtSymbol ? marketTokenSymbol === usdtSymbol : false;
52
+ const bondAmount = await getResolverBondAmount(contractAddress, rpcUrl);
53
+ if (isUsdtMarket) {
54
+ // Market uses USDT — only check and approve USDT
55
+ if (!usdtTokenAddress)
56
+ throw new Error("usdtTokenAddress is required for V3 USDT markets");
57
+ const usdtAllowance = await getTokenAllowance(usdtTokenAddress, walletAddress, contractAddress, rpcUrl);
58
+ if (usdtAllowance < bondAmount) {
59
+ txs.push(buildApproveRawTx({ tokenAddress: usdtTokenAddress, spender: contractAddress }));
60
+ }
61
+ }
62
+ else {
63
+ // Market uses RAIN — only check and approve RAIN
64
+ if (!rainTokenAddress)
65
+ throw new Error("rainTokenAddress is required for V3 RAIN markets");
66
+ const rainAllowance = await getTokenAllowance(rainTokenAddress, walletAddress, contractAddress, rpcUrl);
67
+ if (rainAllowance < bondAmount) {
68
+ txs.push(buildApproveRawTx({ tokenAddress: rainTokenAddress, spender: contractAddress }));
69
+ }
70
+ }
71
+ if (isAiResolver) {
72
+ // AI resolver: closePool() with no args
73
+ txs.push({
74
+ to: contractAddress,
75
+ data: encodeFunctionData({
76
+ abi: TradePoolAbi,
77
+ functionName: CLOSE_POOL,
78
+ }),
79
+ });
80
+ }
81
+ else {
82
+ // Manual resolver: closePool(proposedOutcome)
83
+ if (proposedOutcome === undefined) {
84
+ throw new Error("proposedOutcome is required for V3 manual resolver markets");
85
+ }
86
+ txs.push({
87
+ to: contractAddress,
88
+ data: encodeFunctionData({
89
+ abi: TradePoolAbi,
90
+ functionName: CLOSE_POOL,
91
+ args: [BigInt(proposedOutcome)],
92
+ }),
93
+ });
94
+ }
95
+ }
96
+ return txs;
97
+ }
@@ -0,0 +1,2 @@
1
+ export declare function getResolverBondAmount(contractAddress: `0x${string}`, rpcUrl: string): Promise<bigint>;
2
+ export declare function getTokenAllowance(tokenAddress: `0x${string}`, owner: `0x${string}`, spender: `0x${string}`, rpcUrl: string): Promise<bigint>;
@@ -0,0 +1,16 @@
1
+ import { Contract, JsonRpcProvider } from "ethers";
2
+ import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
3
+ import { ERC20Abi } from "../../abi/ERC20Abi.js";
4
+ import { GET_RESOLVER_BOND_AMOUNT } from "../../constants/contractmethods.js";
5
+ export async function getResolverBondAmount(contractAddress, rpcUrl) {
6
+ const provider = new JsonRpcProvider(rpcUrl);
7
+ const contract = new Contract(contractAddress, TradePoolAbi, provider);
8
+ const bondAmount = await contract[GET_RESOLVER_BOND_AMOUNT]();
9
+ return BigInt(bondAmount);
10
+ }
11
+ export async function getTokenAllowance(tokenAddress, owner, spender, rpcUrl) {
12
+ const provider = new JsonRpcProvider(rpcUrl);
13
+ const contract = new Contract(tokenAddress, ERC20Abi, provider);
14
+ const allowance = await contract.allowance(owner, spender);
15
+ return BigInt(allowance);
16
+ }
@@ -1,2 +1,2 @@
1
1
  import { CreateMarketTxParams, RawTransaction } from "../types.js";
2
- export declare function buildCreateMarketRawTx(params: CreateMarketTxParams): RawTransaction;
2
+ export declare function buildCreateMarketRawTx(params: CreateMarketTxParams): Promise<RawTransaction[]>;