@rainprotocolsdk/sdk 1.0.4 → 1.1.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 +64 -9
- package/dist/Rain.d.ts +2 -1
- package/dist/Rain.js +4 -1
- package/dist/constants/contractmethods.d.ts +3 -0
- package/dist/constants/contractmethods.js +3 -0
- package/dist/tx/buildApprovalRawTx.js +2 -1
- package/dist/tx/buildRawTransactions.d.ts +2 -1
- package/dist/tx/buildRawTransactions.js +39 -1
- package/dist/tx/types.d.ts +7 -0
- package/dist/utils/helpers.d.ts +1 -0
- package/dist/utils/helpers.js +4 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,6 +71,37 @@ const markets = await rain.getPublicMarkets({
|
|
|
71
71
|
|
|
72
72
|
---
|
|
73
73
|
|
|
74
|
+
## buildApprovalTx
|
|
75
|
+
|
|
76
|
+
```
|
|
77
|
+
Builds a raw ERC20 approval transaction if needed.
|
|
78
|
+
|
|
79
|
+
This function prepares an unsigned approve(spender, amount) transaction and does not execute it.
|
|
80
|
+
|
|
81
|
+
If amount is not provided, a default large allowance is approved.
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### Return Type
|
|
85
|
+
|
|
86
|
+
```ts
|
|
87
|
+
interface RawTransaction {
|
|
88
|
+
to: `0x${string}`;
|
|
89
|
+
data: `0x${string}`;
|
|
90
|
+
}
|
|
91
|
+
```
|
|
92
|
+
|
|
93
|
+
### Example
|
|
94
|
+
|
|
95
|
+
```ts
|
|
96
|
+
const approvalTx = rain.buildApprovalTx({
|
|
97
|
+
tokenAddress: `0x${string}`, // Approval token address
|
|
98
|
+
spender: `0x${string}`, // Market contract address
|
|
99
|
+
amount?: 1000000000000000000n // optional parameter
|
|
100
|
+
});
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
---
|
|
104
|
+
|
|
74
105
|
## buildBuyOptionRawTx
|
|
75
106
|
|
|
76
107
|
Builds a **raw EVM transaction** for entering a market option.
|
|
@@ -113,15 +144,38 @@ const rawTx = rain.buildBuyOptionRawTx({
|
|
|
113
144
|
```
|
|
114
145
|
---
|
|
115
146
|
|
|
116
|
-
##
|
|
147
|
+
## buildLimitBuyOptionTx
|
|
117
148
|
|
|
149
|
+
Builds a **raw EVM transaction** for placing a limit buy order on a Rain market.
|
|
150
|
+
|
|
151
|
+
This function **does not send the transaction** — it only prepares calldata.
|
|
152
|
+
|
|
153
|
+
### Method Signature
|
|
154
|
+
|
|
155
|
+
```ts
|
|
156
|
+
buildLimitBuyOptionTx(params: EnterLimitOptionTxParams): RawTransaction
|
|
118
157
|
```
|
|
119
|
-
Builds a raw ERC20 approval transaction if needed.
|
|
120
158
|
|
|
121
|
-
|
|
159
|
+
### Parameters
|
|
122
160
|
|
|
123
|
-
|
|
161
|
+
```ts
|
|
162
|
+
interface EnterLimitOptionTxParams {
|
|
163
|
+
marketContractAddress: `0x${string}`; // market contract address
|
|
164
|
+
selectedOption: number; // Option index
|
|
165
|
+
pricePerShare: bigint; // price per share
|
|
166
|
+
buyAmountInWei: bigint; // total buy amount (already converted to token wei)
|
|
167
|
+
tokenDecimals?: number; // token decimals optional (default: `6`)
|
|
168
|
+
}
|
|
124
169
|
```
|
|
170
|
+
### Validations
|
|
171
|
+
|
|
172
|
+
| Field | Type | Required | Description |
|
|
173
|
+
| ----------------------- | ------------- | -------- | ------------------------------------------------------ |
|
|
174
|
+
| `marketContractAddress` | `0x${string}` | ✅ | Address of the market contract |
|
|
175
|
+
| `selectedOption` | `number` | ✅ | Option index to place the buy order for |
|
|
176
|
+
| `pricePerShare` | `number` | ✅ | Limit price per share (between `0` and `1`) |
|
|
177
|
+
| `buyAmountInWei` | `bigint` | ✅ | Total amount to spend (already converted to token wei) |
|
|
178
|
+
| `tokenDecimals` | `number` | ❌ | Token decimals optional (default: `6`) |
|
|
125
179
|
|
|
126
180
|
### Return Type
|
|
127
181
|
|
|
@@ -135,11 +189,12 @@ interface RawTransaction {
|
|
|
135
189
|
### Example
|
|
136
190
|
|
|
137
191
|
```ts
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
192
|
+
rain.buildLimitBuyOptionTx({
|
|
193
|
+
marketContractAddress: `0x${string}`,
|
|
194
|
+
buyAmountInWei: 1000000,
|
|
195
|
+
pricePerShare: 0.1,
|
|
196
|
+
selectedOption: 1,
|
|
197
|
+
})
|
|
143
198
|
```
|
|
144
199
|
|
|
145
200
|
---
|
package/dist/Rain.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import { GetMarketsParams, Market } from './markets/types.js';
|
|
2
|
-
import { ApproveTxParams, EnterOptionTxParams, RawTransaction } from './tx/types.js';
|
|
2
|
+
import { ApproveTxParams, EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './tx/types.js';
|
|
3
3
|
export declare class Rain {
|
|
4
4
|
getPublicMarkets(params: GetMarketsParams): Promise<Market[]>;
|
|
5
5
|
buildApprovalTx(params: ApproveTxParams): RawTransaction | Error;
|
|
6
6
|
buildBuyOptionRawTx(params: EnterOptionTxParams): RawTransaction;
|
|
7
|
+
buildLimitBuyOptionTx(params: EnterLimitOptionTxParams): RawTransaction;
|
|
7
8
|
}
|
package/dist/Rain.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { getMarkets } from './markets/getMarkets.js';
|
|
2
|
-
import { buildEnterOptionRawTx } from './tx/buildRawTransactions.js';
|
|
2
|
+
import { buildEnterOptionRawTx, buildLimitBuyOrderRawTx } from './tx/buildRawTransactions.js';
|
|
3
3
|
import { buildApproveRawTx } from './tx/buildApprovalRawTx.js';
|
|
4
4
|
export class Rain {
|
|
5
5
|
async getPublicMarkets(params) {
|
|
@@ -11,4 +11,7 @@ export class Rain {
|
|
|
11
11
|
buildBuyOptionRawTx(params) {
|
|
12
12
|
return buildEnterOptionRawTx(params);
|
|
13
13
|
}
|
|
14
|
+
buildLimitBuyOptionTx(params) {
|
|
15
|
+
return buildLimitBuyOrderRawTx(params);
|
|
16
|
+
}
|
|
14
17
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { encodeFunctionData } from "viem";
|
|
2
2
|
import { ERC20Abi } from "../abi/ERC20Abi.js";
|
|
3
3
|
import { ethers } from "ethers";
|
|
4
|
+
import { APPROVE_TOKEN } from "../constants/contractmethods.js";
|
|
4
5
|
const DEFAULT_APPROVE_AMOUNT = ethers.MaxUint256;
|
|
5
6
|
export function buildApproveRawTx(params) {
|
|
6
7
|
const { tokenAddress, spender, amount = DEFAULT_APPROVE_AMOUNT, } = params;
|
|
@@ -12,7 +13,7 @@ export function buildApproveRawTx(params) {
|
|
|
12
13
|
to: tokenAddress,
|
|
13
14
|
data: encodeFunctionData({
|
|
14
15
|
abi: ERC20Abi,
|
|
15
|
-
functionName:
|
|
16
|
+
functionName: APPROVE_TOKEN,
|
|
16
17
|
args: [spender, amount],
|
|
17
18
|
}),
|
|
18
19
|
value: 0n,
|
|
@@ -1,2 +1,3 @@
|
|
|
1
|
-
import { EnterOptionTxParams, RawTransaction } from './types.js';
|
|
1
|
+
import { EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './types.js';
|
|
2
2
|
export declare function buildEnterOptionRawTx(params: EnterOptionTxParams): RawTransaction;
|
|
3
|
+
export declare function buildLimitBuyOrderRawTx(params: EnterLimitOptionTxParams): RawTransaction;
|
|
@@ -1,5 +1,7 @@
|
|
|
1
1
|
import { encodeFunctionData } from 'viem';
|
|
2
2
|
import { TradePoolAbi } from '../abi/TradeMarketsAbi.js';
|
|
3
|
+
import { convertToWeiEthers } from '../utils/helpers.js';
|
|
4
|
+
import { ENTER_OPTION, PLACE_BUY_ORDER } from '../constants/contractmethods.js';
|
|
3
5
|
export function buildEnterOptionRawTx(params) {
|
|
4
6
|
const { marketContractAddress, selectedOption, buyAmountInWei } = params;
|
|
5
7
|
if (!marketContractAddress)
|
|
@@ -12,8 +14,44 @@ export function buildEnterOptionRawTx(params) {
|
|
|
12
14
|
to: marketContractAddress,
|
|
13
15
|
data: encodeFunctionData({
|
|
14
16
|
abi: TradePoolAbi,
|
|
15
|
-
functionName:
|
|
17
|
+
functionName: ENTER_OPTION,
|
|
16
18
|
args: [selectedOption, buyAmountInWei],
|
|
17
19
|
}),
|
|
18
20
|
};
|
|
19
21
|
}
|
|
22
|
+
export function buildLimitBuyOrderRawTx(params) {
|
|
23
|
+
const { marketContractAddress, selectedOption, pricePerShare, buyAmountInWei, tokenDecimals } = params;
|
|
24
|
+
const decimals = tokenDecimals ?? 6;
|
|
25
|
+
const oneTokenInWei = 10n ** BigInt(decimals);
|
|
26
|
+
if (!marketContractAddress) {
|
|
27
|
+
throw new Error("market address is required");
|
|
28
|
+
}
|
|
29
|
+
if (selectedOption === undefined) {
|
|
30
|
+
throw new Error("selectedOption is required");
|
|
31
|
+
}
|
|
32
|
+
if (!pricePerShare) {
|
|
33
|
+
throw new Error("price per share is required");
|
|
34
|
+
}
|
|
35
|
+
if (pricePerShare <= 0 || pricePerShare >= 1) {
|
|
36
|
+
throw new Error("price per share should be in between 0 to 1, make sure to convert to correct decimals");
|
|
37
|
+
}
|
|
38
|
+
if (!buyAmountInWei) {
|
|
39
|
+
throw new Error("buy amount in wei is required");
|
|
40
|
+
}
|
|
41
|
+
if (buyAmountInWei < oneTokenInWei) {
|
|
42
|
+
throw new Error("order amount should be more then $1");
|
|
43
|
+
}
|
|
44
|
+
const pricePerShareInEther = convertToWeiEthers(pricePerShare, 18);
|
|
45
|
+
return {
|
|
46
|
+
to: marketContractAddress,
|
|
47
|
+
data: encodeFunctionData({
|
|
48
|
+
abi: TradePoolAbi,
|
|
49
|
+
functionName: PLACE_BUY_ORDER,
|
|
50
|
+
args: [
|
|
51
|
+
BigInt(selectedOption),
|
|
52
|
+
pricePerShareInEther,
|
|
53
|
+
buyAmountInWei,
|
|
54
|
+
],
|
|
55
|
+
}),
|
|
56
|
+
};
|
|
57
|
+
}
|
package/dist/tx/types.d.ts
CHANGED
|
@@ -13,3 +13,10 @@ export type ApproveTxParams = {
|
|
|
13
13
|
spender: `0x${string}`;
|
|
14
14
|
amount?: bigint;
|
|
15
15
|
};
|
|
16
|
+
export interface EnterLimitOptionTxParams {
|
|
17
|
+
marketContractAddress: `0x${string}`;
|
|
18
|
+
selectedOption: number;
|
|
19
|
+
pricePerShare: bigint;
|
|
20
|
+
buyAmountInWei: bigint;
|
|
21
|
+
tokenDecimals?: number;
|
|
22
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const convertToWeiEthers: (value: string | bigint, decimals: number) => bigint;
|