rain-sdk-v2 1.2.1 → 2.0.1

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.
@@ -1,10 +1,13 @@
1
- import { encodeFunctionData } from "viem";
1
+ import { encodeFunctionData, createPublicClient, http } from "viem";
2
+ import { arbitrum } from "viem/chains";
2
3
  import { MarketsAbi } from "../abi/MarketsAbi.js";
3
4
  import { ENTER_OPTION } from "../constants/contractmethods.js";
4
5
  import { checkMarketTokenAllowance } from "../utils/helpers.js";
5
6
  import { buildApproveRawTx } from "./buildApprovalRawTx.js";
7
+ const DEFAULT_SLIPPAGE = 5n; // 5%
8
+ const DEFAULT_DEADLINE_SECONDS = 600n; // 10 minutes
6
9
  export async function buildEnterOptionRawTx(params) {
7
- const { marketContractAddress, selectedOption, optionSide, buyAmountInWei, walletAddress, rpcUrl } = params;
10
+ const { marketContractAddress, selectedOption, optionSide, buyAmountInWei, minSharesOut, slippageTolerance, deadline, walletAddress, rpcUrl } = params;
8
11
  if (!marketContractAddress)
9
12
  throw new Error("marketContractAddress is required");
10
13
  if (selectedOption === undefined || selectedOption === null)
@@ -20,12 +23,27 @@ export async function buildEnterOptionRawTx(params) {
20
23
  if (allowance < buyAmountInWei) {
21
24
  txs.push(buildApproveRawTx({ tokenAddress: baseToken, spender: marketContractAddress, amount: buyAmountInWei }));
22
25
  }
26
+ let effectiveMinShares = minSharesOut;
27
+ if (effectiveMinShares === undefined || effectiveMinShares === null) {
28
+ const client = createPublicClient({
29
+ chain: arbitrum,
30
+ transport: http(rpcUrl),
31
+ });
32
+ const [expectedShares] = await client.readContract({
33
+ address: marketContractAddress,
34
+ abi: MarketsAbi,
35
+ functionName: "getEntryShares",
36
+ args: [selectedOption, optionSide, buyAmountInWei],
37
+ });
38
+ const slippage = slippageTolerance ?? DEFAULT_SLIPPAGE;
39
+ effectiveMinShares = expectedShares * (100n - slippage) / 100n;
40
+ }
23
41
  txs.push({
24
42
  to: marketContractAddress,
25
43
  data: encodeFunctionData({
26
44
  abi: MarketsAbi,
27
45
  functionName: ENTER_OPTION,
28
- args: [selectedOption, optionSide, buyAmountInWei],
46
+ args: [selectedOption, optionSide, buyAmountInWei, effectiveMinShares, deadline ?? DEFAULT_DEADLINE_SECONDS],
29
47
  }),
30
48
  value: 0n,
31
49
  });
@@ -1,2 +1,4 @@
1
1
  import { RemoveLiquidityTxParams, RawTransaction } from "./types.js";
2
- export declare function buildRemoveLiquidityRawTx(params: RemoveLiquidityTxParams): RawTransaction;
2
+ export declare function buildRemoveLiquidityRawTx(params: RemoveLiquidityTxParams & {
3
+ rpcUrl: string;
4
+ }): Promise<RawTransaction>;
@@ -1,8 +1,11 @@
1
- import { encodeFunctionData } from "viem";
1
+ import { encodeFunctionData, createPublicClient, http } from "viem";
2
+ import { arbitrum } from "viem/chains";
2
3
  import { MarketsAbi } from "../abi/MarketsAbi.js";
3
4
  import { REMOVE_LIQUIDITY } from "../constants/contractmethods.js";
4
- export function buildRemoveLiquidityRawTx(params) {
5
- const { marketContractAddress, option, lpShares } = params;
5
+ const DEFAULT_SLIPPAGE = 5n; // 5%
6
+ const DEFAULT_DEADLINE_SECONDS = 600n; // 10 minutes
7
+ export async function buildRemoveLiquidityRawTx(params) {
8
+ const { marketContractAddress, option, lpShares, minYesOut, minNoOut, slippageTolerance, deadline, rpcUrl } = params;
6
9
  if (!marketContractAddress)
7
10
  throw new Error("marketContractAddress is required");
8
11
  if (option === undefined || option === null)
@@ -11,12 +14,29 @@ export function buildRemoveLiquidityRawTx(params) {
11
14
  throw new Error("lpShares is required");
12
15
  if (lpShares <= 0n)
13
16
  throw new Error("lpShares must be greater than 0");
17
+ let effectiveMinYes = minYesOut;
18
+ let effectiveMinNo = minNoOut;
19
+ if ((effectiveMinYes === undefined || effectiveMinYes === null) && (effectiveMinNo === undefined || effectiveMinNo === null)) {
20
+ const client = createPublicClient({
21
+ chain: arbitrum,
22
+ transport: http(rpcUrl),
23
+ });
24
+ const [yesBack, noBack] = await client.readContract({
25
+ address: marketContractAddress,
26
+ abi: MarketsAbi,
27
+ functionName: "getRemovedLiquidity",
28
+ args: [option, lpShares],
29
+ });
30
+ const slippage = slippageTolerance ?? DEFAULT_SLIPPAGE;
31
+ effectiveMinYes = yesBack * (100n - slippage) / 100n;
32
+ effectiveMinNo = noBack * (100n - slippage) / 100n;
33
+ }
14
34
  return {
15
35
  to: marketContractAddress,
16
36
  data: encodeFunctionData({
17
37
  abi: MarketsAbi,
18
38
  functionName: REMOVE_LIQUIDITY,
19
- args: [option, lpShares],
39
+ args: [option, lpShares, effectiveMinYes ?? 0n, effectiveMinNo ?? 0n, deadline ?? DEFAULT_DEADLINE_SECONDS],
20
40
  }),
21
41
  value: 0n,
22
42
  };
@@ -21,6 +21,9 @@ export interface EnterOptionTxParams {
21
21
  selectedOption: bigint;
22
22
  optionSide: OptionSide;
23
23
  buyAmountInWei: bigint;
24
+ minSharesOut?: bigint;
25
+ slippageTolerance?: bigint;
26
+ deadline?: bigint;
24
27
  }
25
28
  export interface PlaceBuyOrderTxParams {
26
29
  marketContractAddress: `0x${string}`;
@@ -86,11 +89,19 @@ export interface AddLiquidityTxParams {
86
89
  marketContractAddress: `0x${string}`;
87
90
  option: bigint;
88
91
  totalAmountInWei: bigint;
92
+ minYesToDeposit?: bigint;
93
+ minNoToDeposit?: bigint;
94
+ slippageTolerance?: bigint;
95
+ deadline?: bigint;
89
96
  }
90
97
  export interface RemoveLiquidityTxParams {
91
98
  marketContractAddress: `0x${string}`;
92
99
  option: bigint;
93
100
  lpShares: bigint;
101
+ minYesOut?: bigint;
102
+ minNoOut?: bigint;
103
+ slippageTolerance?: bigint;
104
+ deadline?: bigint;
94
105
  }
95
106
  export interface CreateMarketTxParams {
96
107
  marketQuestion: string;
@@ -108,6 +119,7 @@ export interface CreateMarketTxParams {
108
119
  barValues: number[];
109
120
  baseToken: `0x${string}`;
110
121
  tradingModel?: TradingModel;
122
+ initialYesPrices?: bigint[];
111
123
  marketImage: string;
112
124
  tokenDecimals?: number;
113
125
  factoryContractAddress?: `0x${string}`;
package/dist/types.d.ts CHANGED
@@ -7,7 +7,7 @@ export interface RainConfig {
7
7
  chain: Chain;
8
8
  rpcUrl?: string;
9
9
  }
10
- export type RainEnvironment = "development" | "stage";
10
+ export type RainEnvironment = "development" | "stage" | "production";
11
11
  export interface RainCoreConfig {
12
12
  environment?: RainEnvironment;
13
13
  rpcUrl?: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "rain-sdk-v2",
3
- "version": "1.2.1",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "description": "Rain SDK V2 — TypeScript SDK for Rain prediction markets on Arbitrum. Market creation, trading, liquidity, order book, split/merge, dispute, and smart account support.",
6
6
  "main": "dist/index.js",