@rainprotocolsdk/sdk 2.2.0 → 2.3.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/README.md +27 -1
- package/dist/Rain.d.ts +2 -1
- package/dist/Rain.js +8 -0
- package/dist/config/environments.d.ts +6 -0
- package/dist/config/environments.js +6 -0
- package/dist/tx/AddLiquidity/buildAddLiquidityRawTx.d.ts +8 -0
- package/dist/tx/AddLiquidity/buildAddLiquidityRawTx.js +51 -0
- package/dist/tx/types.d.ts +6 -0
- package/package.json +12 -6
package/README.md
CHANGED
|
@@ -295,7 +295,24 @@ This single transaction sends the user's original stake plus their winnings dire
|
|
|
295
295
|
|
|
296
296
|
Every market on Rain needs liquidity to function. Because trades execute against an Automated Market Maker (AMM), the pool requires an initial supply of funds to price shares and settle outcomes.
|
|
297
297
|
|
|
298
|
-
|
|
298
|
+
### Adding Liquidity
|
|
299
|
+
|
|
300
|
+
Use `buildAddLiquidityTx` to let users supply funds to an existing market. The SDK reads the market's base token on-chain and only requests the approval needed — either USDT or RAIN, depending on the market.
|
|
301
|
+
|
|
302
|
+
```ts
|
|
303
|
+
const txs = await rain.buildAddLiquidityTx({
|
|
304
|
+
marketContractAddress: '0x...',
|
|
305
|
+
walletAddress: '0x...',
|
|
306
|
+
amount: 100, // human-readable amount — decimals auto-detected (6 for USDT, 18 for RAIN)
|
|
307
|
+
});
|
|
308
|
+
|
|
309
|
+
// Execute sequentially
|
|
310
|
+
for (const tx of txs) {
|
|
311
|
+
await yourProvider.sendTransaction(tx);
|
|
312
|
+
}
|
|
313
|
+
```
|
|
314
|
+
|
|
315
|
+
Returns `[approveTx, enterLiquidityTx]` if the allowance is insufficient, or `[enterLiquidityTx]` if already approved.
|
|
299
316
|
|
|
300
317
|
### How LPs Earn Fees
|
|
301
318
|
|
|
@@ -693,6 +710,14 @@ const closeTx = await rain.buildCloseMarketTx({
|
|
|
693
710
|
accessToken: 'your-access-token',
|
|
694
711
|
});
|
|
695
712
|
|
|
713
|
+
// buildAddLiquidityTx(params) — Add liquidity to a market
|
|
714
|
+
const addLiqTxs = await rain.buildAddLiquidityTx({
|
|
715
|
+
marketContractAddress: '0x...',
|
|
716
|
+
walletAddress: '0x...',
|
|
717
|
+
amount: 100, // human-readable — reads baseToken() on-chain, uses 6 decimals for USDT or 18 for RAIN
|
|
718
|
+
});
|
|
719
|
+
// Returns [approveTx, enterLiquidityTx] or [enterLiquidityTx] if already approved.
|
|
720
|
+
|
|
696
721
|
// buildClaimTx(params) — Claim winnings after market settles
|
|
697
722
|
const claimTx = await rain.buildClaimTx({
|
|
698
723
|
marketId: '698c8f116e985bbfacc7fc01',
|
|
@@ -839,6 +864,7 @@ A complete directory of all available methods in the Rain SDK.
|
|
|
839
864
|
| `buildLimitSellOptionTx(params)` | `RawTransaction` | No |
|
|
840
865
|
| `buildCancelOrdersTx(params)` | `RawTransaction[]` | No |
|
|
841
866
|
| `buildCancelAllOpenOrdersTx(params)` | `RawTransaction[]` | Yes |
|
|
867
|
+
| `buildAddLiquidityTx(params)` | `RawTransaction[]` | Yes |
|
|
842
868
|
| `buildClaimTx(params)` | `RawTransaction` | Yes |
|
|
843
869
|
| `buildCloseMarketTx(params)` | `RawTransaction[]` | Yes |
|
|
844
870
|
| `buildCreateDisputeTx(params)` | `RawTransaction[]` | Yes |
|
package/dist/Rain.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { GetMarketByIdParams, GetMarketsParams, GetUserInvestmentsParams, Market, UserInvestment } from './markets/types.js';
|
|
2
|
-
import { ApproveTxParams, CancelAllOpenOrdersTxParams, CancelOrdersTxParams, ClaimTxParams, CloseMarketTxParams, CreateDisputeTxParams, CreateAppealTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, ExtendTimeTxParams, LimitSellOptionTxParams, RawTransaction } from './tx/types.js';
|
|
2
|
+
import { AddLiquidityTxParams, ApproveTxParams, CancelAllOpenOrdersTxParams, CancelOrdersTxParams, ClaimTxParams, CloseMarketTxParams, CreateDisputeTxParams, CreateAppealTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, ExtendTimeTxParams, LimitSellOptionTxParams, RawTransaction } from './tx/types.js';
|
|
3
3
|
import { RainCoreConfig, RainEnvironment } from './types.js';
|
|
4
4
|
import { LoginParams, LoginResult } from './auth/types.js';
|
|
5
5
|
export declare class Rain {
|
|
@@ -25,5 +25,6 @@ export declare class Rain {
|
|
|
25
25
|
buildCreateDisputeTx(params: CreateDisputeTxParams): Promise<RawTransaction[]>;
|
|
26
26
|
buildCreateAppealTx(params: CreateAppealTxParams): Promise<RawTransaction[]>;
|
|
27
27
|
buildExtendTimeTx(params: ExtendTimeTxParams): Promise<RawTransaction>;
|
|
28
|
+
buildAddLiquidityTx(params: AddLiquidityTxParams): Promise<RawTransaction[]>;
|
|
28
29
|
login(params: LoginParams): Promise<LoginResult>;
|
|
29
30
|
}
|
package/dist/Rain.js
CHANGED
|
@@ -11,6 +11,7 @@ import { buildCreateMarketRawTx } from './tx/CreateMarket/buildCreateMarketRawTx
|
|
|
11
11
|
import { ALLOWED_ENVIRONMENTS, ENV_CONFIG, getRandomRpc } from './config/environments.js';
|
|
12
12
|
import { buildClaimRawTx } from './tx/ClaimFunds/buildClaimFundsRawTx.js';
|
|
13
13
|
import { buildExtendTimeRawTx } from './tx/ExtendTime/buildExtendTimeRawTx.js';
|
|
14
|
+
import { buildAddLiquidityRawTx } from './tx/AddLiquidity/buildAddLiquidityRawTx.js';
|
|
14
15
|
import { loginUser } from './auth/login.js';
|
|
15
16
|
export class Rain {
|
|
16
17
|
environment;
|
|
@@ -80,6 +81,13 @@ export class Rain {
|
|
|
80
81
|
async buildExtendTimeTx(params) {
|
|
81
82
|
return buildExtendTimeRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl });
|
|
82
83
|
}
|
|
84
|
+
async buildAddLiquidityTx(params) {
|
|
85
|
+
return buildAddLiquidityRawTx({
|
|
86
|
+
...params,
|
|
87
|
+
environment: this.environment,
|
|
88
|
+
rpcUrl: params.rpcUrl ?? this.rpcUrl,
|
|
89
|
+
});
|
|
90
|
+
}
|
|
83
91
|
async login(params) {
|
|
84
92
|
return loginUser({ ...params, apiUrl: this.apiUrl });
|
|
85
93
|
}
|
|
@@ -9,17 +9,23 @@ export declare const ENV_CONFIG: {
|
|
|
9
9
|
readonly market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313";
|
|
10
10
|
readonly dispute_initial_timer: number;
|
|
11
11
|
readonly usdt_symbol: "USDTm";
|
|
12
|
+
readonly usdt_token: `0x${string}`;
|
|
13
|
+
readonly rain_token: `0x${string}`;
|
|
12
14
|
};
|
|
13
15
|
readonly stage: {
|
|
14
16
|
readonly apiUrl: "https://stg-api.rain.one";
|
|
15
17
|
readonly market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561";
|
|
16
18
|
readonly dispute_initial_timer: number;
|
|
17
19
|
readonly usdt_symbol: "USD₮0";
|
|
20
|
+
readonly usdt_token: `0x${string}`;
|
|
21
|
+
readonly rain_token: `0x${string}`;
|
|
18
22
|
};
|
|
19
23
|
readonly production: {
|
|
20
24
|
readonly apiUrl: "https://prod-api.rain.one";
|
|
21
25
|
readonly market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1";
|
|
22
26
|
readonly dispute_initial_timer: number;
|
|
23
27
|
readonly usdt_symbol: "USD₮0";
|
|
28
|
+
readonly usdt_token: `0x${string}`;
|
|
29
|
+
readonly rain_token: `0x${string}`;
|
|
24
30
|
};
|
|
25
31
|
};
|
|
@@ -16,17 +16,23 @@ export const ENV_CONFIG = {
|
|
|
16
16
|
market_factory_address: "0x148DA7F2039B2B00633AC2ab566f59C8a4C86313",
|
|
17
17
|
dispute_initial_timer: 1 * 60,
|
|
18
18
|
usdt_symbol: USDT_SYMBOL_DEV,
|
|
19
|
+
usdt_token: "0xCa4f77A38d8552Dd1D5E44e890173921B67725F4",
|
|
20
|
+
rain_token: "0x25118290e6A5f4139381D072181157035864099d",
|
|
19
21
|
},
|
|
20
22
|
stage: {
|
|
21
23
|
apiUrl: "https://stg-api.rain.one",
|
|
22
24
|
market_factory_address: "0x6109c9f28FE3Ad84c51368f7Ef2d487ca020c561",
|
|
23
25
|
dispute_initial_timer: 1 * 60,
|
|
24
26
|
usdt_symbol: USDT_SYMBOL_PROD,
|
|
27
|
+
usdt_token: "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9",
|
|
28
|
+
rain_token: "0x43976a124e6834b541840Ce741243dAD3dd538DA",
|
|
25
29
|
},
|
|
26
30
|
production: {
|
|
27
31
|
apiUrl: "https://prod-api.rain.one",
|
|
28
32
|
market_factory_address: "0xccCB3C03D9355B01883779EF15C1Be09cf3623F1",
|
|
29
33
|
dispute_initial_timer: 120 * 60,
|
|
30
34
|
usdt_symbol: USDT_SYMBOL_PROD,
|
|
35
|
+
usdt_token: "0xFd086bC7CD5C481DCC9C85ebE478A1C0b69FCbb9",
|
|
36
|
+
rain_token: "0x43976a124e6834b541840Ce741243dAD3dd538DA",
|
|
31
37
|
},
|
|
32
38
|
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { AddLiquidityTxParams, RawTransaction } from "../types.js";
|
|
2
|
+
import { RainEnvironment } from "../../types.js";
|
|
3
|
+
interface AddLiquidityRawTxInternalParams extends AddLiquidityTxParams {
|
|
4
|
+
environment: RainEnvironment;
|
|
5
|
+
rpcUrl: string;
|
|
6
|
+
}
|
|
7
|
+
export declare function buildAddLiquidityRawTx(params: AddLiquidityRawTxInternalParams): Promise<RawTransaction[]>;
|
|
8
|
+
export {};
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { Contract, JsonRpcProvider, ethers } from "ethers";
|
|
3
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
4
|
+
import { buildApproveRawTx } from "../buildApprovalRawTx.js";
|
|
5
|
+
import { getTokenAllowance } from "../CloseMarket/helpers.js";
|
|
6
|
+
import { isRpcValid } from "../ClaimFunds/helpers.js";
|
|
7
|
+
import { ENV_CONFIG, ALLOWED_ENVIRONMENTS } from "../../config/environments.js";
|
|
8
|
+
async function getMarketBaseToken(marketContractAddress, rpcUrl) {
|
|
9
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
10
|
+
const contract = new Contract(marketContractAddress, TradePoolAbi, provider);
|
|
11
|
+
return (await contract.baseToken()).toLowerCase();
|
|
12
|
+
}
|
|
13
|
+
export async function buildAddLiquidityRawTx(params) {
|
|
14
|
+
const { marketContractAddress, walletAddress, amount, environment, rpcUrl } = params;
|
|
15
|
+
if (!marketContractAddress)
|
|
16
|
+
throw new Error("marketContractAddress is required");
|
|
17
|
+
if (!walletAddress)
|
|
18
|
+
throw new Error("walletAddress is required");
|
|
19
|
+
if (!amount)
|
|
20
|
+
throw new Error("amount is required");
|
|
21
|
+
if (!ALLOWED_ENVIRONMENTS.includes(environment))
|
|
22
|
+
throw new Error(`Invalid environment: ${environment}`);
|
|
23
|
+
const isRpcWorking = await isRpcValid(rpcUrl);
|
|
24
|
+
if (!isRpcWorking)
|
|
25
|
+
throw new Error("Provided RPC URL is not valid or not working");
|
|
26
|
+
const { usdt_token, rain_token } = ENV_CONFIG[environment];
|
|
27
|
+
// Read base token from the market contract
|
|
28
|
+
const baseToken = await getMarketBaseToken(marketContractAddress, rpcUrl);
|
|
29
|
+
const isUsdtMarket = baseToken === usdt_token.toLowerCase();
|
|
30
|
+
const tokenAddress = isUsdtMarket ? usdt_token : rain_token;
|
|
31
|
+
// USDT uses 6 decimals, RAIN uses 18
|
|
32
|
+
const decimals = isUsdtMarket ? 6 : 18;
|
|
33
|
+
const amountInWei = ethers.parseUnits(amount.toString(), decimals);
|
|
34
|
+
const txs = [];
|
|
35
|
+
// Check allowance for the correct token only
|
|
36
|
+
const allowance = await getTokenAllowance(tokenAddress, walletAddress, marketContractAddress, rpcUrl);
|
|
37
|
+
if (BigInt(allowance) < amountInWei) {
|
|
38
|
+
txs.push(buildApproveRawTx({ tokenAddress, spender: marketContractAddress }));
|
|
39
|
+
}
|
|
40
|
+
// enterLiquidity tx on the market contract
|
|
41
|
+
txs.push({
|
|
42
|
+
to: marketContractAddress,
|
|
43
|
+
data: encodeFunctionData({
|
|
44
|
+
abi: TradePoolAbi,
|
|
45
|
+
functionName: "enterLiquidity",
|
|
46
|
+
args: [amountInWei],
|
|
47
|
+
}),
|
|
48
|
+
value: 0n,
|
|
49
|
+
});
|
|
50
|
+
return txs;
|
|
51
|
+
}
|
package/dist/tx/types.d.ts
CHANGED
|
@@ -99,6 +99,12 @@ export interface CancelAllOpenOrdersTxParams {
|
|
|
99
99
|
accessToken: string;
|
|
100
100
|
apiUrl?: string;
|
|
101
101
|
}
|
|
102
|
+
export interface AddLiquidityTxParams {
|
|
103
|
+
marketContractAddress: `0x${string}`;
|
|
104
|
+
walletAddress: `0x${string}`;
|
|
105
|
+
amount: string | number;
|
|
106
|
+
rpcUrl?: string;
|
|
107
|
+
}
|
|
102
108
|
export interface ExtendTimeTxParams {
|
|
103
109
|
marketContractAddress: `0x${string}`;
|
|
104
110
|
walletAddress: `0x${string}`;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rainprotocolsdk/sdk",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.3.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"description": "Rain SDK",
|
|
6
6
|
"main": "dist/index.js",
|
|
@@ -31,15 +31,21 @@
|
|
|
31
31
|
"author": "Rain Protocol",
|
|
32
32
|
"license": "MIT",
|
|
33
33
|
"peerDependencies": {
|
|
34
|
-
"viem": "^2.0.0"
|
|
35
|
-
},
|
|
36
|
-
"dependencies": {
|
|
34
|
+
"viem": "^2.0.0",
|
|
37
35
|
"@account-kit/infra": "^4.48.0",
|
|
38
36
|
"@account-kit/wallet-client": "^0.1.0-alpha.10",
|
|
39
37
|
"@alchemy/aa-alchemy": "^3.0.0",
|
|
40
|
-
"@alchemy/aa-core": "^3.0.0"
|
|
38
|
+
"@alchemy/aa-core": "^3.0.0"
|
|
39
|
+
},
|
|
40
|
+
"peerDependenciesMeta": {
|
|
41
|
+
"@account-kit/infra": { "optional": true },
|
|
42
|
+
"@account-kit/wallet-client": { "optional": true },
|
|
43
|
+
"@alchemy/aa-alchemy": { "optional": true },
|
|
44
|
+
"@alchemy/aa-core": { "optional": true }
|
|
45
|
+
},
|
|
46
|
+
"dependencies": {
|
|
41
47
|
"ethers": "^6.16.0",
|
|
42
|
-
|
|
48
|
+
"socket.io-client": "^4.8.3"
|
|
43
49
|
},
|
|
44
50
|
"devDependencies": {
|
|
45
51
|
"typescript": "^5.9.3"
|