@rainprotocolsdk/sdk 1.2.0 → 1.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 +49 -1
- package/dist/Rain.d.ts +3 -1
- package/dist/Rain.js +8 -2
- package/dist/config/environments.d.ts +2 -0
- package/dist/config/environments.js +9 -0
- package/dist/constants/contractmethods.d.ts +1 -0
- package/dist/constants/contractmethods.js +1 -0
- package/dist/tx/ClaimFunds/buildClaimFundsRawTx.d.ts +2 -0
- package/dist/tx/ClaimFunds/buildClaimFundsRawTx.js +34 -0
- package/dist/tx/ClaimFunds/helpers.d.ts +8 -0
- package/dist/tx/ClaimFunds/helpers.js +37 -0
- package/dist/tx/types.d.ts +15 -0
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -150,7 +150,7 @@ interface CreateMarketTxParams {
|
|
|
150
150
|
|
|
151
151
|
### Minimum Liquidity Rule
|
|
152
152
|
|
|
153
|
-
|
|
153
|
+
#### inputAmountWei >= 10 tokens
|
|
154
154
|
|
|
155
155
|
### Return Type
|
|
156
156
|
|
|
@@ -287,6 +287,54 @@ rain.buildLimitBuyOptionTx({
|
|
|
287
287
|
|
|
288
288
|
---
|
|
289
289
|
|
|
290
|
+
## buildClaimTx
|
|
291
|
+
|
|
292
|
+
Builds a **raw EVM transaction** to claim the funds from a Rain market.
|
|
293
|
+
|
|
294
|
+
This function **does not send the transaction** — it only prepares calldata.
|
|
295
|
+
|
|
296
|
+
### Method Signature
|
|
297
|
+
|
|
298
|
+
```ts
|
|
299
|
+
buildClaimTx(params: ClaimTxParams): Promise<RawTransaction>
|
|
300
|
+
```
|
|
301
|
+
|
|
302
|
+
### Parameters
|
|
303
|
+
|
|
304
|
+
```ts
|
|
305
|
+
interface ClaimTxParams {
|
|
306
|
+
marketId: string;
|
|
307
|
+
walletAddress: `0x${string}`;
|
|
308
|
+
}
|
|
309
|
+
```
|
|
310
|
+
|
|
311
|
+
### Validations
|
|
312
|
+
|
|
313
|
+
| Parameter | Type | Required | Description |
|
|
314
|
+
| --------------- | ------------- | -------- | ---------------------------------- |
|
|
315
|
+
| `marketId` | `string` | ✅ | Unique identifier of the market |
|
|
316
|
+
| `walletAddress` | `0x${string}` | ✅ | Address of the user claiming funds |
|
|
317
|
+
|
|
318
|
+
### Return Type
|
|
319
|
+
|
|
320
|
+
```ts
|
|
321
|
+
interface RawTransaction {
|
|
322
|
+
to: `0x${string}`;
|
|
323
|
+
data: `0x${string}`;
|
|
324
|
+
}
|
|
325
|
+
```
|
|
326
|
+
|
|
327
|
+
### Example
|
|
328
|
+
|
|
329
|
+
```ts
|
|
330
|
+
rain.buildClaimTx({
|
|
331
|
+
marketId: "698c8f116e985bbfacc7fc01",
|
|
332
|
+
walletAddress: '0x996ea23940f4a01610181D04bdB6F862719b63f0'
|
|
333
|
+
})
|
|
334
|
+
```
|
|
335
|
+
|
|
336
|
+
---
|
|
337
|
+
|
|
290
338
|
## RainAA Class (Account Abstraction)
|
|
291
339
|
|
|
292
340
|
`RainAA` is responsible for:
|
package/dist/Rain.d.ts
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { GetMarketsParams, Market } from './markets/types.js';
|
|
2
|
-
import { ApproveTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './tx/types.js';
|
|
2
|
+
import { ApproveTxParams, ClaimTxParams, CreateMarketTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './tx/types.js';
|
|
3
3
|
import { RainCoreConfig, RainEnvironment } from './types.js';
|
|
4
4
|
export declare class Rain {
|
|
5
5
|
readonly environment: RainEnvironment;
|
|
6
6
|
private readonly marketFactory;
|
|
7
7
|
private readonly apiUrl;
|
|
8
8
|
private readonly distute_initial_timer;
|
|
9
|
+
private readonly rpcUrl?;
|
|
9
10
|
constructor(config?: RainCoreConfig);
|
|
10
11
|
getPublicMarkets(params: GetMarketsParams): Promise<Market[]>;
|
|
11
12
|
buildApprovalTx(params: ApproveTxParams): RawTransaction | Error;
|
|
12
13
|
buildBuyOptionRawTx(params: EnterOptionTxParams): RawTransaction;
|
|
13
14
|
buildLimitBuyOptionTx(params: EnterLimitOptionTxParams): RawTransaction;
|
|
14
15
|
buildCreateMarketTx(params: CreateMarketTxParams): RawTransaction;
|
|
16
|
+
buildClaimTx(params: ClaimTxParams): Promise<RawTransaction>;
|
|
15
17
|
}
|
package/dist/Rain.js
CHANGED
|
@@ -2,14 +2,16 @@ import { getMarkets } from './markets/getMarkets.js';
|
|
|
2
2
|
import { buildEnterOptionRawTx, buildLimitBuyOrderRawTx } from './tx/buildRawTransactions.js';
|
|
3
3
|
import { buildApproveRawTx } from './tx/buildApprovalRawTx.js';
|
|
4
4
|
import { buildCreateMarketRawTx } from './tx/CreateMarket/buildCreateMarketRawTx.js';
|
|
5
|
-
import { ALLOWED_ENVIRONMENTS, ENV_CONFIG } from './config/environments.js';
|
|
5
|
+
import { ALLOWED_ENVIRONMENTS, ENV_CONFIG, getRandomRpc } from './config/environments.js';
|
|
6
|
+
import { buildClaimRawTx } from './tx/ClaimFunds/buildClaimFundsRawTx.js';
|
|
6
7
|
export class Rain {
|
|
7
8
|
environment;
|
|
8
9
|
marketFactory;
|
|
9
10
|
apiUrl;
|
|
10
11
|
distute_initial_timer;
|
|
12
|
+
rpcUrl;
|
|
11
13
|
constructor(config = {}) {
|
|
12
|
-
const { environment = "development" } = config;
|
|
14
|
+
const { environment = "development", rpcUrl } = config;
|
|
13
15
|
function isValidEnvironment(env) {
|
|
14
16
|
return ALLOWED_ENVIRONMENTS.includes(env);
|
|
15
17
|
}
|
|
@@ -17,6 +19,7 @@ export class Rain {
|
|
|
17
19
|
throw new Error(`Invalid environment "${environment}". Allowed values: ${ALLOWED_ENVIRONMENTS.join(", ")}`);
|
|
18
20
|
}
|
|
19
21
|
this.environment = environment;
|
|
22
|
+
this.rpcUrl = rpcUrl ?? getRandomRpc();
|
|
20
23
|
const envConfig = ENV_CONFIG[this.environment];
|
|
21
24
|
this.marketFactory = envConfig.market_factory_address;
|
|
22
25
|
this.apiUrl = envConfig.apiUrl;
|
|
@@ -37,4 +40,7 @@ export class Rain {
|
|
|
37
40
|
buildCreateMarketTx(params) {
|
|
38
41
|
return buildCreateMarketRawTx({ ...params, factoryContractAddress: this.marketFactory, disputeTimer: this.distute_initial_timer });
|
|
39
42
|
}
|
|
43
|
+
async buildClaimTx(params) {
|
|
44
|
+
return buildClaimRawTx({ ...params, apiUrl: this.apiUrl, rpcUrl: this.rpcUrl });
|
|
45
|
+
}
|
|
40
46
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export declare const ALLOWED_ENVIRONMENTS: readonly ["development", "stage", "production"];
|
|
2
|
+
export declare const DEFAULT_RPCS: string[];
|
|
3
|
+
export declare function getRandomRpc(): string;
|
|
2
4
|
export declare const ENV_CONFIG: {
|
|
3
5
|
readonly development: {
|
|
4
6
|
readonly apiUrl: "https://dev-api.rain.one";
|
|
@@ -1,4 +1,13 @@
|
|
|
1
1
|
export const ALLOWED_ENVIRONMENTS = ["development", "stage", "production"];
|
|
2
|
+
export const DEFAULT_RPCS = [
|
|
3
|
+
"https://arbitrum.meowrpc.com",
|
|
4
|
+
"https://rpc.sentio.xyz/arbitrum-one",
|
|
5
|
+
"https://rpc.owlracle.info/arb/70d38ce1826c4a60bb2a8e05a6c8b20f"
|
|
6
|
+
];
|
|
7
|
+
export function getRandomRpc() {
|
|
8
|
+
const index = Math.floor(Math.random() * DEFAULT_RPCS.length);
|
|
9
|
+
return DEFAULT_RPCS[index];
|
|
10
|
+
}
|
|
2
11
|
export const ENV_CONFIG = {
|
|
3
12
|
development: {
|
|
4
13
|
apiUrl: "https://dev-api.rain.one",
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
3
|
+
import { getMarket, getUserOptionShares, isRpcValid } from "./helpers.js";
|
|
4
|
+
import { MARKET_STATUS } from "../../markets/constants.js";
|
|
5
|
+
import { CLAIM } from "../../constants/contractmethods.js";
|
|
6
|
+
export async function buildClaimRawTx(params) {
|
|
7
|
+
const { marketId, walletAddress, apiUrl, rpcUrl } = params;
|
|
8
|
+
if (!apiUrl) {
|
|
9
|
+
throw new Error("Environemnt is not set properly, api url is missing");
|
|
10
|
+
}
|
|
11
|
+
const isRpcWorking = await isRpcValid(rpcUrl);
|
|
12
|
+
if (!rpcUrl || !isRpcWorking) {
|
|
13
|
+
throw new Error("Provided RPC URL is not valid or not working");
|
|
14
|
+
}
|
|
15
|
+
if (!marketId)
|
|
16
|
+
throw new Error("marketContractAddress is required");
|
|
17
|
+
if (!walletAddress)
|
|
18
|
+
throw new Error("walletAddress is required");
|
|
19
|
+
const { data } = await getMarket({ marketId, apiUrl });
|
|
20
|
+
if (data?.status !== MARKET_STATUS.Closed)
|
|
21
|
+
throw new Error("Market is not closed yet. Please wait until the market is closed to claim your funds.");
|
|
22
|
+
const marketContractAddress = data?.contractAddress;
|
|
23
|
+
const options = data?.options || 0;
|
|
24
|
+
const userShares = await getUserOptionShares({ marketContractAddress: marketContractAddress, walletAddress: walletAddress, options: options, rpcUrl: rpcUrl });
|
|
25
|
+
if (userShares?.length <= 0)
|
|
26
|
+
throw new Error("No shares to claim for this market");
|
|
27
|
+
return {
|
|
28
|
+
to: marketContractAddress,
|
|
29
|
+
data: encodeFunctionData({
|
|
30
|
+
abi: TradePoolAbi,
|
|
31
|
+
functionName: CLAIM,
|
|
32
|
+
}),
|
|
33
|
+
};
|
|
34
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { Market } from "../../markets/types.js";
|
|
2
|
+
import { GetUserOptionSharesParams } from "../types.js";
|
|
3
|
+
export declare function getMarket(params: {
|
|
4
|
+
marketId: string;
|
|
5
|
+
apiUrl: string;
|
|
6
|
+
}): Promise<Market>;
|
|
7
|
+
export declare function isRpcValid(rpcUrl: string | undefined): Promise<boolean>;
|
|
8
|
+
export declare function getUserOptionShares(params: GetUserOptionSharesParams): Promise<number[]>;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { Contract, JsonRpcProvider } from "ethers";
|
|
2
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
3
|
+
export async function getMarket(params) {
|
|
4
|
+
if (!params?.apiUrl) {
|
|
5
|
+
throw new Error("Environemnt is not set properly, api url is missing");
|
|
6
|
+
}
|
|
7
|
+
const res = await fetch(`${params.apiUrl}/pools/pool/${params.marketId}`);
|
|
8
|
+
if (!res.ok) {
|
|
9
|
+
throw new Error(`Failed to fetch markets: ${res.status}`);
|
|
10
|
+
}
|
|
11
|
+
const data = await res.json();
|
|
12
|
+
return data;
|
|
13
|
+
}
|
|
14
|
+
export async function isRpcValid(rpcUrl) {
|
|
15
|
+
if (!rpcUrl)
|
|
16
|
+
return false;
|
|
17
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
18
|
+
try {
|
|
19
|
+
await provider.getNetwork();
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
return false;
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
export async function getUserOptionShares(params) {
|
|
27
|
+
const { marketContractAddress, options, walletAddress, rpcUrl } = params;
|
|
28
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
29
|
+
const userShares = [];
|
|
30
|
+
for (let i = 0; i < options.length; i++) {
|
|
31
|
+
const choiceIndex = options[i].choiceIndex;
|
|
32
|
+
const contract = new Contract(marketContractAddress, TradePoolAbi, provider);
|
|
33
|
+
const userVotes = await contract.userVotes(choiceIndex, walletAddress);
|
|
34
|
+
Number(userVotes) > 0 && userShares.push(Number(userVotes));
|
|
35
|
+
}
|
|
36
|
+
return userShares;
|
|
37
|
+
}
|
package/dist/tx/types.d.ts
CHANGED
|
@@ -35,3 +35,18 @@ export interface CreateMarketTxParams {
|
|
|
35
35
|
tokenDecimals?: number;
|
|
36
36
|
factoryContractAddress?: `0x${string}`;
|
|
37
37
|
}
|
|
38
|
+
export interface ClaimTxParams {
|
|
39
|
+
marketId: string;
|
|
40
|
+
walletAddress: `0x${string}`;
|
|
41
|
+
apiUrl?: string;
|
|
42
|
+
rpcUrl?: string;
|
|
43
|
+
}
|
|
44
|
+
export interface GetUserOptionSharesParams {
|
|
45
|
+
options: [{
|
|
46
|
+
choiceIndex: number;
|
|
47
|
+
optionName: string;
|
|
48
|
+
}];
|
|
49
|
+
walletAddress: `0x${string}`;
|
|
50
|
+
marketContractAddress: `0x${string}`;
|
|
51
|
+
rpcUrl: string;
|
|
52
|
+
}
|
package/dist/types.d.ts
CHANGED