@rainprotocolsdk/sdk 1.1.0 → 1.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.
@@ -0,0 +1,18 @@
1
+ export declare const ALLOWED_ENVIRONMENTS: readonly ["development", "stage", "production"];
2
+ export declare const ENV_CONFIG: {
3
+ readonly development: {
4
+ readonly apiUrl: "https://dev-api.rain.one";
5
+ readonly market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313";
6
+ readonly dispute_initial_timer: number;
7
+ };
8
+ readonly stage: {
9
+ readonly apiUrl: "https://stg-api.rain.one";
10
+ readonly market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561";
11
+ readonly dispute_initial_timer: number;
12
+ };
13
+ readonly production: {
14
+ readonly apiUrl: "https://prod-api.rain.one";
15
+ readonly market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1";
16
+ readonly dispute_initial_timer: number;
17
+ };
18
+ };
@@ -0,0 +1,18 @@
1
+ export const ALLOWED_ENVIRONMENTS = ["development", "stage", "production"];
2
+ export const ENV_CONFIG = {
3
+ development: {
4
+ apiUrl: "https://dev-api.rain.one",
5
+ market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313",
6
+ dispute_initial_timer: 1 * 60
7
+ },
8
+ stage: {
9
+ apiUrl: "https://stg-api.rain.one",
10
+ market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561",
11
+ dispute_initial_timer: 1 * 60
12
+ },
13
+ production: {
14
+ apiUrl: "https://prod-api.rain.one",
15
+ market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1",
16
+ dispute_initial_timer: 120 * 60
17
+ },
18
+ };
@@ -1,3 +1,4 @@
1
1
  export declare const ENTER_OPTION = "enterOption";
2
2
  export declare const PLACE_BUY_ORDER = "placeBuyOrder";
3
3
  export declare const APPROVE_TOKEN = "approve";
4
+ export declare const CREATE_MARKET = "createPool";
@@ -1,3 +1,4 @@
1
1
  export const ENTER_OPTION = 'enterOption';
2
2
  export const PLACE_BUY_ORDER = 'placeBuyOrder';
3
3
  export const APPROVE_TOKEN = 'approve';
4
+ export const CREATE_MARKET = 'createPool';
@@ -1,7 +1,9 @@
1
- import { API_BASE_URL } from '../config/api.js';
2
1
  import { MARKET_SORT_BY, MARKET_STATUS } from './constants.js';
3
2
  export async function getMarkets(params) {
4
3
  const query = new URLSearchParams();
4
+ if (!params?.apiUrl) {
5
+ throw new Error("Environemnt is not set properly, api url is missing");
6
+ }
5
7
  if (params?.limit)
6
8
  query.append('limit', params.limit.toString());
7
9
  if (params?.offset)
@@ -10,7 +12,7 @@ export async function getMarkets(params) {
10
12
  query.append('sortBy', MARKET_SORT_BY[params.sortBy]);
11
13
  if (params?.status)
12
14
  query.append('status', MARKET_STATUS[params.status]);
13
- const res = await fetch(`${API_BASE_URL}/pools/public-pools?${query.toString()}`);
15
+ const res = await fetch(`${params.apiUrl}/pools/public-pools?${query.toString()}`);
14
16
  if (!res.ok) {
15
17
  throw new Error(`Failed to fetch markets: ${res.status}`);
16
18
  }
@@ -5,6 +5,7 @@ export interface GetMarketsParams {
5
5
  offset?: number;
6
6
  sortBy?: MarketSortBy;
7
7
  status?: MarketStatus;
8
+ apiUrl?: string;
8
9
  }
9
10
  export interface Market {
10
11
  id: string;
@@ -0,0 +1,2 @@
1
+ import { CreateMarketTxParams, RawTransaction } from "../types.js";
2
+ export declare function buildCreateMarketRawTx(params: CreateMarketTxParams): RawTransaction;
@@ -0,0 +1,36 @@
1
+ import { encodeFunctionData } from "viem";
2
+ import { CreateMarketAbi } from "../../abi/CreateMarketAbi.js";
3
+ import { CREATE_MARKET } from "../../constants/contractmethods.js";
4
+ import { validateCreateMarketParams } from "./createMarketValidation.js";
5
+ import { normalizeBarValues } from "./helpers.js";
6
+ const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
7
+ export function buildCreateMarketRawTx(params) {
8
+ const { isPublic, isPublicPoolResolverAi, creator, startTime, endTime, options, disputeTimer, ipfsUrl, inputAmountWei, barValues, baseToken, factoryContractAddress, } = params;
9
+ if (!factoryContractAddress)
10
+ throw new Error("environment is not set correctly, factory contract address is missing");
11
+ validateCreateMarketParams(params);
12
+ const normalizeBarValue = normalizeBarValues(barValues);
13
+ const createMarketParams = [
14
+ isPublic,
15
+ isPublicPoolResolverAi,
16
+ creator,
17
+ ZERO_ADDRESS,
18
+ startTime,
19
+ endTime,
20
+ options,
21
+ disputeTimer,
22
+ ipfsUrl,
23
+ inputAmountWei,
24
+ normalizeBarValue,
25
+ creator,
26
+ baseToken,
27
+ ];
28
+ return {
29
+ to: factoryContractAddress,
30
+ data: encodeFunctionData({
31
+ abi: CreateMarketAbi,
32
+ functionName: CREATE_MARKET,
33
+ args: [createMarketParams],
34
+ }),
35
+ };
36
+ }
@@ -0,0 +1,2 @@
1
+ import { CreateMarketTxParams } from "../types.js";
2
+ export declare function validateCreateMarketParams(params: CreateMarketTxParams): boolean;
@@ -0,0 +1,34 @@
1
+ export function validateCreateMarketParams(params) {
2
+ const { isPublic, isPublicPoolResolverAi, creator, startTime, endTime, options, ipfsUrl, inputAmountWei, barValues, baseToken, factoryContractAddress, tokenDecimals, } = params;
3
+ // Required field validations
4
+ if (typeof isPublic !== "boolean")
5
+ throw new Error("isPublic is required and must be a boolean");
6
+ if (typeof isPublicPoolResolverAi !== "boolean")
7
+ throw new Error("isPublicPoolResolverAi is required and must be a boolean");
8
+ if (!creator)
9
+ throw new Error("creator address is required");
10
+ if (!startTime)
11
+ throw new Error("startTime is required");
12
+ if (!endTime)
13
+ throw new Error("endTime is required");
14
+ if (!options)
15
+ throw new Error("number of options is required and cannot be empty");
16
+ if (!ipfsUrl || typeof ipfsUrl !== "string")
17
+ throw new Error("ipfsUrl is required");
18
+ if (!inputAmountWei)
19
+ throw new Error("inputAmountWei is required");
20
+ if (!barValues || !Array.isArray(barValues) || barValues.length === 0)
21
+ throw new Error("barValues array is required and cannot be empty");
22
+ if (!baseToken)
23
+ throw new Error("baseToken address is required");
24
+ if (!factoryContractAddress)
25
+ throw new Error("factoryContractAddress is required");
26
+ const decimals = tokenDecimals ?? 6;
27
+ const oneTokenInWei = 10n ** BigInt(decimals);
28
+ if (inputAmountWei < oneTokenInWei * 10n) {
29
+ throw new Error("Market cannot be opened: inputAmountWei must be at least $10");
30
+ }
31
+ if (startTime >= endTime)
32
+ throw new Error("startTime must be earlier than endTime");
33
+ return true;
34
+ }
@@ -0,0 +1 @@
1
+ export declare function normalizeBarValues(values: (number)[]): number[];
@@ -0,0 +1,11 @@
1
+ export function normalizeBarValues(values) {
2
+ const transformedBarValues = [
3
+ ...values.map((value) => Math.floor(value * 100)),
4
+ ];
5
+ const totalRounded = transformedBarValues.reduce((sum, val) => sum + val, 0);
6
+ const difference = 10000 - totalRounded;
7
+ if (difference !== 0) {
8
+ transformedBarValues[transformedBarValues.length - 1] += difference;
9
+ }
10
+ return transformedBarValues;
11
+ }
@@ -20,3 +20,18 @@ export interface EnterLimitOptionTxParams {
20
20
  buyAmountInWei: bigint;
21
21
  tokenDecimals?: number;
22
22
  }
23
+ export interface CreateMarketTxParams {
24
+ isPublic: boolean;
25
+ isPublicPoolResolverAi: boolean;
26
+ creator: `0x${string}`;
27
+ startTime: bigint;
28
+ endTime: bigint;
29
+ options: bigint;
30
+ disputeTimer: number;
31
+ ipfsUrl: string;
32
+ inputAmountWei: bigint;
33
+ barValues: number[];
34
+ baseToken: `0x${string}`;
35
+ tokenDecimals?: number;
36
+ factoryContractAddress?: `0x${string}`;
37
+ }
package/dist/types.d.ts CHANGED
@@ -7,3 +7,7 @@ export interface RainConfig {
7
7
  chain: Chain;
8
8
  rpcUrl?: string;
9
9
  }
10
+ export type RainEnvironment = "development" | "stage" | "production";
11
+ export interface RainCoreConfig {
12
+ environment?: RainEnvironment;
13
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rainprotocolsdk/sdk",
3
- "version": "1.1.0",
3
+ "version": "1.2.0",
4
4
  "type": "module",
5
5
  "description": "Rain SDK",
6
6
  "main": "dist/index.js",
@@ -12,7 +12,8 @@
12
12
  }
13
13
  },
14
14
  "files": [
15
- "dist"
15
+ "dist",
16
+ "README.md"
16
17
  ],
17
18
  "scripts": {
18
19
  "build": "tsc",
@@ -1 +0,0 @@
1
- export declare const API_BASE_URL = "https://dev-api.rain.one";
@@ -1 +0,0 @@
1
- export const API_BASE_URL = "https://dev-api.rain.one";