@rainprotocolsdk/sdk 1.3.0 → 2.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 +652 -102
- package/dist/Rain.d.ts +15 -3
- package/dist/Rain.js +42 -2
- package/dist/abi/OracleAbi.d.ts +15 -0
- package/dist/abi/OracleAbi.js +20 -0
- package/dist/abi/TradeMarketsAbi.d.ts +66 -0
- package/dist/abi/TradeMarketsAbi.js +85 -0
- package/dist/auth/login.d.ts +4 -0
- package/dist/auth/login.js +24 -0
- package/dist/auth/types.d.ts +16 -0
- package/dist/auth/types.js +1 -0
- package/dist/config/environments.d.ts +5 -0
- package/dist/config/environments.js +8 -3
- package/dist/constants/contractmethods.d.ts +8 -0
- package/dist/constants/contractmethods.js +8 -0
- package/dist/index.d.ts +1 -0
- package/dist/markets/getMarketById.d.ts +2 -0
- package/dist/markets/getMarketById.js +12 -0
- package/dist/markets/getUserInvestments.d.ts +2 -0
- package/dist/markets/getUserInvestments.js +23 -0
- package/dist/markets/types.d.ts +21 -0
- package/dist/tx/CloseMarket/buildCloseMarketRawTx.d.ts +2 -0
- package/dist/tx/CloseMarket/buildCloseMarketRawTx.js +97 -0
- package/dist/tx/CloseMarket/helpers.d.ts +2 -0
- package/dist/tx/CloseMarket/helpers.js +16 -0
- package/dist/tx/CreateMarket/buildCreateMarketRawTx.d.ts +1 -1
- package/dist/tx/CreateMarket/buildCreateMarketRawTx.js +17 -6
- package/dist/tx/CreateMarket/createMarketValidation.js +18 -4
- package/dist/tx/CreateMarket/helpers.d.ts +2 -0
- package/dist/tx/CreateMarket/helpers.js +33 -0
- package/dist/tx/Dispute/buildDisputeRawTx.d.ts +3 -0
- package/dist/tx/Dispute/buildDisputeRawTx.js +59 -0
- package/dist/tx/Dispute/helpers.d.ts +1 -0
- package/dist/tx/Dispute/helpers.js +9 -0
- package/dist/tx/ExtendTime/buildExtendTimeRawTx.d.ts +2 -0
- package/dist/tx/ExtendTime/buildExtendTimeRawTx.js +46 -0
- package/dist/tx/buildApprovalRawTx.d.ts +1 -1
- package/dist/tx/buildCancelAllOpenOrdersRawTx.d.ts +2 -0
- package/dist/tx/buildCancelAllOpenOrdersRawTx.js +35 -0
- package/dist/tx/buildCancelOrdersRawTx.d.ts +2 -0
- package/dist/tx/buildCancelOrdersRawTx.js +42 -0
- package/dist/tx/buildRawTransactions.d.ts +2 -1
- package/dist/tx/buildRawTransactions.js +35 -1
- package/dist/tx/types.d.ts +67 -2
- package/dist/utils/helpers.d.ts +2 -0
- package/dist/utils/helpers.js +14 -1
- package/package.json +2 -2
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { Contract, JsonRpcProvider } from "ethers";
|
|
2
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
3
|
+
import { ERC20Abi } from "../../abi/ERC20Abi.js";
|
|
4
|
+
import { GET_RESOLVER_BOND_AMOUNT } from "../../constants/contractmethods.js";
|
|
5
|
+
export async function getResolverBondAmount(contractAddress, rpcUrl) {
|
|
6
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
7
|
+
const contract = new Contract(contractAddress, TradePoolAbi, provider);
|
|
8
|
+
const bondAmount = await contract[GET_RESOLVER_BOND_AMOUNT]();
|
|
9
|
+
return BigInt(bondAmount);
|
|
10
|
+
}
|
|
11
|
+
export async function getTokenAllowance(tokenAddress, owner, spender, rpcUrl) {
|
|
12
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
13
|
+
const contract = new Contract(tokenAddress, ERC20Abi, provider);
|
|
14
|
+
const allowance = await contract.allowance(owner, spender);
|
|
15
|
+
return BigInt(allowance);
|
|
16
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { CreateMarketTxParams, RawTransaction } from "../types.js";
|
|
2
|
-
export declare function buildCreateMarketRawTx(params: CreateMarketTxParams): RawTransaction
|
|
2
|
+
export declare function buildCreateMarketRawTx(params: CreateMarketTxParams): Promise<RawTransaction[]>;
|
|
@@ -2,14 +2,23 @@ import { encodeFunctionData } from "viem";
|
|
|
2
2
|
import { CreateMarketAbi } from "../../abi/CreateMarketAbi.js";
|
|
3
3
|
import { CREATE_MARKET } from "../../constants/contractmethods.js";
|
|
4
4
|
import { validateCreateMarketParams } from "./createMarketValidation.js";
|
|
5
|
-
import { normalizeBarValues } from "./helpers.js";
|
|
5
|
+
import { normalizeBarValues, uploadMetaData } from "./helpers.js";
|
|
6
|
+
import { getUserAllownace } from "../../utils/helpers.js";
|
|
7
|
+
import { buildApproveRawTx } from "../buildApprovalRawTx.js";
|
|
6
8
|
const ZERO_ADDRESS = "0x0000000000000000000000000000000000000000";
|
|
7
|
-
export function buildCreateMarketRawTx(params) {
|
|
8
|
-
const { isPublic, isPublicPoolResolverAi, creator, startTime, endTime,
|
|
9
|
+
export async function buildCreateMarketRawTx(params) {
|
|
10
|
+
const { isPublic, isPublicPoolResolverAi, creator, startTime, endTime, no_of_options, disputeTimer, inputAmountWei, barValues, baseToken, factoryContractAddress, } = params;
|
|
9
11
|
if (!factoryContractAddress)
|
|
10
12
|
throw new Error("environment is not set correctly, factory contract address is missing");
|
|
11
13
|
validateCreateMarketParams(params);
|
|
14
|
+
const allowance = await getUserAllownace(params);
|
|
15
|
+
const createMarketTransactions = [];
|
|
16
|
+
if (BigInt(allowance) < BigInt(inputAmountWei)) {
|
|
17
|
+
const approveTx = buildApproveRawTx({ spender: factoryContractAddress, tokenAddress: baseToken });
|
|
18
|
+
createMarketTransactions.push(approveTx);
|
|
19
|
+
}
|
|
12
20
|
const normalizeBarValue = normalizeBarValues(barValues);
|
|
21
|
+
const ipfsUrl = await uploadMetaData(params);
|
|
13
22
|
const createMarketParams = [
|
|
14
23
|
isPublic,
|
|
15
24
|
isPublicPoolResolverAi,
|
|
@@ -17,7 +26,7 @@ export function buildCreateMarketRawTx(params) {
|
|
|
17
26
|
ZERO_ADDRESS,
|
|
18
27
|
startTime,
|
|
19
28
|
endTime,
|
|
20
|
-
|
|
29
|
+
no_of_options,
|
|
21
30
|
disputeTimer,
|
|
22
31
|
ipfsUrl,
|
|
23
32
|
inputAmountWei,
|
|
@@ -25,12 +34,14 @@ export function buildCreateMarketRawTx(params) {
|
|
|
25
34
|
creator,
|
|
26
35
|
baseToken,
|
|
27
36
|
];
|
|
28
|
-
|
|
37
|
+
createMarketTransactions.push({
|
|
29
38
|
to: factoryContractAddress,
|
|
30
39
|
data: encodeFunctionData({
|
|
31
40
|
abi: CreateMarketAbi,
|
|
32
41
|
functionName: CREATE_MARKET,
|
|
33
42
|
args: [createMarketParams],
|
|
34
43
|
}),
|
|
35
|
-
|
|
44
|
+
value: 0n,
|
|
45
|
+
});
|
|
46
|
+
return createMarketTransactions;
|
|
36
47
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
export function validateCreateMarketParams(params) {
|
|
2
|
-
const { isPublic, isPublicPoolResolverAi, creator, startTime, endTime,
|
|
2
|
+
const { marketQuestion, marketOptions, marketTags, marketDescription, isPublic, isPublicPoolResolverAi, creator, startTime, endTime, no_of_options, inputAmountWei, barValues, baseToken, factoryContractAddress, tokenDecimals, } = params;
|
|
3
3
|
// Required field validations
|
|
4
4
|
if (typeof isPublic !== "boolean")
|
|
5
5
|
throw new Error("isPublic is required and must be a boolean");
|
|
@@ -7,14 +7,28 @@ export function validateCreateMarketParams(params) {
|
|
|
7
7
|
throw new Error("isPublicPoolResolverAi is required and must be a boolean");
|
|
8
8
|
if (!creator)
|
|
9
9
|
throw new Error("creator address is required");
|
|
10
|
+
if (!marketQuestion)
|
|
11
|
+
throw new Error("question is required");
|
|
12
|
+
if (!marketDescription)
|
|
13
|
+
throw new Error("description is required");
|
|
14
|
+
if (!Array.isArray(marketOptions) || marketOptions.length < 2 || marketOptions.length > 26) {
|
|
15
|
+
throw new Error("options must be between 2 and 26");
|
|
16
|
+
}
|
|
17
|
+
if (marketOptions.some(opt => !opt?.toString().trim())) {
|
|
18
|
+
throw new Error("options cannot contain empty values");
|
|
19
|
+
}
|
|
20
|
+
if (!Array.isArray(marketTags) || marketTags.length < 1 || marketTags.length > 3) {
|
|
21
|
+
throw new Error("tags must be between 1 and 3");
|
|
22
|
+
}
|
|
23
|
+
if (marketTags.some(tag => !tag?.toString().trim())) {
|
|
24
|
+
throw new Error("tags cannot contain empty values");
|
|
25
|
+
}
|
|
10
26
|
if (!startTime)
|
|
11
27
|
throw new Error("startTime is required");
|
|
12
28
|
if (!endTime)
|
|
13
29
|
throw new Error("endTime is required");
|
|
14
|
-
if (!
|
|
30
|
+
if (!no_of_options)
|
|
15
31
|
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
32
|
if (!inputAmountWei)
|
|
19
33
|
throw new Error("inputAmountWei is required");
|
|
20
34
|
if (!barValues || !Array.isArray(barValues) || barValues.length === 0)
|
|
@@ -9,3 +9,36 @@ export function normalizeBarValues(values) {
|
|
|
9
9
|
}
|
|
10
10
|
return transformedBarValues;
|
|
11
11
|
}
|
|
12
|
+
export async function uploadMetaData(params) {
|
|
13
|
+
const { marketQuestion, marketOptions, marketTags, marketDescription, isPublic, isPublicPoolResolverAi, startTime, endTime, no_of_options, creator, apiUrl, } = params;
|
|
14
|
+
const formattedStartDate = new Date(Number(startTime) * 1000).toISOString();
|
|
15
|
+
const formattedEndDate = new Date(Number(endTime) * 1000).toISOString();
|
|
16
|
+
const metadata = {
|
|
17
|
+
question: marketQuestion,
|
|
18
|
+
options: Array.isArray(marketOptions) ? marketOptions : [],
|
|
19
|
+
tags: marketTags,
|
|
20
|
+
isPrivate: !isPublic,
|
|
21
|
+
startDate: formattedStartDate,
|
|
22
|
+
endDate: formattedEndDate,
|
|
23
|
+
poolDescription: marketDescription,
|
|
24
|
+
isAiResolver: isPublicPoolResolverAi,
|
|
25
|
+
contractData: {
|
|
26
|
+
pool_owner: creator,
|
|
27
|
+
start_time: startTime.toString(),
|
|
28
|
+
end_time: endTime.toString(),
|
|
29
|
+
no_of_options: no_of_options.toString(),
|
|
30
|
+
},
|
|
31
|
+
};
|
|
32
|
+
const res = await fetch(`${apiUrl}/ipfs/upload`, {
|
|
33
|
+
method: "POST",
|
|
34
|
+
headers: {
|
|
35
|
+
"Content-Type": "application/json",
|
|
36
|
+
},
|
|
37
|
+
body: JSON.stringify(metadata),
|
|
38
|
+
});
|
|
39
|
+
if (!res.ok) {
|
|
40
|
+
throw new Error(`Failed to upload metadata: ${res.status}`);
|
|
41
|
+
}
|
|
42
|
+
const data = await res.json();
|
|
43
|
+
return data?.data?.ipfsHash;
|
|
44
|
+
}
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import { CreateDisputeTxParams, CreateAppealTxParams, RawTransaction } from "../types.js";
|
|
2
|
+
export declare function buildCreateDisputeRawTx(params: CreateDisputeTxParams): Promise<RawTransaction[]>;
|
|
3
|
+
export declare function buildCreateAppealRawTx(params: CreateAppealTxParams): Promise<RawTransaction[]>;
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
3
|
+
import { getMarket, isRpcValid } from "../ClaimFunds/helpers.js";
|
|
4
|
+
import { getTokenAllowance } from "../CloseMarket/helpers.js";
|
|
5
|
+
import { buildApproveRawTx } from "../buildApprovalRawTx.js";
|
|
6
|
+
import { OPEN_DISPUTE } from "../../constants/contractmethods.js";
|
|
7
|
+
import { getDisputeAppealFee } from "./helpers.js";
|
|
8
|
+
async function buildOpenDisputeTxs(params) {
|
|
9
|
+
const { marketId, walletAddress, usdtTokenAddress, rainTokenAddress, usdtSymbol, apiUrl, rpcUrl, } = params;
|
|
10
|
+
if (!apiUrl)
|
|
11
|
+
throw new Error("Environment is not set properly, api url is missing");
|
|
12
|
+
if (!rpcUrl)
|
|
13
|
+
throw new Error("rpcUrl is required");
|
|
14
|
+
const isRpcWorking = await isRpcValid(rpcUrl);
|
|
15
|
+
if (!isRpcWorking)
|
|
16
|
+
throw new Error("Provided RPC URL is not valid or not working");
|
|
17
|
+
if (!marketId)
|
|
18
|
+
throw new Error("marketId is required");
|
|
19
|
+
if (!walletAddress)
|
|
20
|
+
throw new Error("walletAddress is required");
|
|
21
|
+
const { data } = await getMarket({ marketId, apiUrl });
|
|
22
|
+
const contractAddress = data?.contractAddress;
|
|
23
|
+
const marketTokenSymbol = data?.token?.tokenSymbol ?? "";
|
|
24
|
+
if (!contractAddress)
|
|
25
|
+
throw new Error("Market contract address not found");
|
|
26
|
+
const feeAmount = await getDisputeAppealFee(contractAddress, rpcUrl);
|
|
27
|
+
const isUsdtMarket = usdtSymbol ? marketTokenSymbol === usdtSymbol : false;
|
|
28
|
+
const txs = [];
|
|
29
|
+
if (isUsdtMarket) {
|
|
30
|
+
if (!usdtTokenAddress)
|
|
31
|
+
throw new Error("usdtTokenAddress is required for USDT markets");
|
|
32
|
+
const usdtAllowance = await getTokenAllowance(usdtTokenAddress, walletAddress, contractAddress, rpcUrl);
|
|
33
|
+
if (usdtAllowance < feeAmount) {
|
|
34
|
+
txs.push(buildApproveRawTx({ tokenAddress: usdtTokenAddress, spender: contractAddress }));
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
else {
|
|
38
|
+
if (!rainTokenAddress)
|
|
39
|
+
throw new Error("rainTokenAddress is required for RAIN markets");
|
|
40
|
+
const rainAllowance = await getTokenAllowance(rainTokenAddress, walletAddress, contractAddress, rpcUrl);
|
|
41
|
+
if (rainAllowance < feeAmount) {
|
|
42
|
+
txs.push(buildApproveRawTx({ tokenAddress: rainTokenAddress, spender: contractAddress }));
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
txs.push({
|
|
46
|
+
to: contractAddress,
|
|
47
|
+
data: encodeFunctionData({
|
|
48
|
+
abi: TradePoolAbi,
|
|
49
|
+
functionName: OPEN_DISPUTE,
|
|
50
|
+
}),
|
|
51
|
+
});
|
|
52
|
+
return txs;
|
|
53
|
+
}
|
|
54
|
+
export async function buildCreateDisputeRawTx(params) {
|
|
55
|
+
return buildOpenDisputeTxs(params);
|
|
56
|
+
}
|
|
57
|
+
export async function buildCreateAppealRawTx(params) {
|
|
58
|
+
return buildOpenDisputeTxs(params);
|
|
59
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function getDisputeAppealFee(contractAddress: `0x${string}`, rpcUrl: string): Promise<bigint>;
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { Contract, JsonRpcProvider } from "ethers";
|
|
2
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
3
|
+
import { GET_DISPUTE_APPEAL_FEE } from "../../constants/contractmethods.js";
|
|
4
|
+
export async function getDisputeAppealFee(contractAddress, rpcUrl) {
|
|
5
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
6
|
+
const contract = new Contract(contractAddress, TradePoolAbi, provider);
|
|
7
|
+
const fee = await contract[GET_DISPUTE_APPEAL_FEE]();
|
|
8
|
+
return BigInt(fee);
|
|
9
|
+
}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { Contract, JsonRpcProvider } from "ethers";
|
|
3
|
+
import { OracleAbi } from "../../abi/OracleAbi.js";
|
|
4
|
+
import { TradePoolAbi } from "../../abi/TradeMarketsAbi.js";
|
|
5
|
+
async function getOracleAddress(marketContractAddress, rpcUrl) {
|
|
6
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
7
|
+
const contract = new Contract(marketContractAddress, TradePoolAbi, provider);
|
|
8
|
+
const resolver = await contract.resolver();
|
|
9
|
+
return resolver;
|
|
10
|
+
}
|
|
11
|
+
async function fetchExtendTimeSignature(oracleContractAddress, walletAddress, accessToken, apiUrl) {
|
|
12
|
+
const res = await fetch(`${apiUrl}/pools/sign-oracles-extend-time?contractAddress=${oracleContractAddress}&walletAddress=${walletAddress}`, { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
13
|
+
if (!res.ok)
|
|
14
|
+
throw new Error(`Failed to fetch extend time signature: ${res.status}`);
|
|
15
|
+
const data = await res.json();
|
|
16
|
+
if (!data?.epoch || !data?.signature)
|
|
17
|
+
throw new Error("Invalid response from extend time signature endpoint");
|
|
18
|
+
return {
|
|
19
|
+
epoch: BigInt(data.epoch),
|
|
20
|
+
signature: data.signature,
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
export async function buildExtendTimeRawTx(params) {
|
|
24
|
+
const { marketContractAddress, walletAddress, accessToken, apiUrl, rpcUrl } = params;
|
|
25
|
+
if (!apiUrl)
|
|
26
|
+
throw new Error("Environment is not set properly, api url is missing");
|
|
27
|
+
if (!rpcUrl)
|
|
28
|
+
throw new Error("rpcUrl is required");
|
|
29
|
+
if (!marketContractAddress)
|
|
30
|
+
throw new Error("marketContractAddress is required");
|
|
31
|
+
if (!walletAddress)
|
|
32
|
+
throw new Error("walletAddress is required");
|
|
33
|
+
if (!accessToken)
|
|
34
|
+
throw new Error("accessToken is required");
|
|
35
|
+
const oracleContractAddress = await getOracleAddress(marketContractAddress, rpcUrl);
|
|
36
|
+
const { epoch, signature } = await fetchExtendTimeSignature(oracleContractAddress, walletAddress, accessToken, apiUrl);
|
|
37
|
+
return {
|
|
38
|
+
to: oracleContractAddress,
|
|
39
|
+
data: encodeFunctionData({
|
|
40
|
+
abi: OracleAbi,
|
|
41
|
+
functionName: "extendTime",
|
|
42
|
+
args: [epoch, signature],
|
|
43
|
+
}),
|
|
44
|
+
value: 0n,
|
|
45
|
+
};
|
|
46
|
+
}
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { ApproveTxParams, RawTransaction } from "./types.js";
|
|
2
|
-
export declare function buildApproveRawTx(params: ApproveTxParams): RawTransaction
|
|
2
|
+
export declare function buildApproveRawTx(params: ApproveTxParams): RawTransaction;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { ethers } from "ethers";
|
|
2
|
+
import { buildCancelOrdersRawTx } from "./buildCancelOrdersRawTx.js";
|
|
3
|
+
const PRICE_DECIMALS = 18;
|
|
4
|
+
async function fetchOpenOrders(marketId, walletAddress, accessToken, apiUrl) {
|
|
5
|
+
const res = await fetch(`${apiUrl}/orders/get-user-order-by-poolId/${marketId}?filter=open`, { headers: { Authorization: `Bearer ${accessToken}` } });
|
|
6
|
+
if (!res.ok)
|
|
7
|
+
throw new Error(`Failed to fetch open orders: ${res.status}`);
|
|
8
|
+
const data = await res.json();
|
|
9
|
+
return data?.data ?? data ?? [];
|
|
10
|
+
}
|
|
11
|
+
export async function buildCancelAllOpenOrdersRawTx(params) {
|
|
12
|
+
const { marketId, marketContractAddress, walletAddress, accessToken, apiUrl } = params;
|
|
13
|
+
if (!apiUrl)
|
|
14
|
+
throw new Error("Environment is not set properly, api url is missing");
|
|
15
|
+
if (!marketId)
|
|
16
|
+
throw new Error("marketId is required");
|
|
17
|
+
if (!marketContractAddress)
|
|
18
|
+
throw new Error("marketContractAddress is required");
|
|
19
|
+
if (!walletAddress)
|
|
20
|
+
throw new Error("walletAddress is required");
|
|
21
|
+
if (!accessToken)
|
|
22
|
+
throw new Error("accessToken is required");
|
|
23
|
+
const orders = await fetchOpenOrders(marketId, walletAddress, accessToken, apiUrl);
|
|
24
|
+
// Filter to only this wallet's orders in case the API returns all
|
|
25
|
+
const userOrders = orders.filter((o) => !o.user || o.user?.toLowerCase() === walletAddress.toLowerCase());
|
|
26
|
+
if (userOrders.length === 0)
|
|
27
|
+
return [];
|
|
28
|
+
const mapped = userOrders.map((o) => ({
|
|
29
|
+
orderType: o.orderType,
|
|
30
|
+
option: BigInt(o.orderOption),
|
|
31
|
+
pricePerShare: ethers.parseUnits(String(o.pricePerShare), PRICE_DECIMALS),
|
|
32
|
+
orderId: BigInt(o.externalID),
|
|
33
|
+
}));
|
|
34
|
+
return buildCancelOrdersRawTx({ marketContractAddress, orders: mapped });
|
|
35
|
+
}
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { encodeFunctionData } from "viem";
|
|
2
|
+
import { TradePoolAbi } from "../abi/TradeMarketsAbi.js";
|
|
3
|
+
import { CANCEL_BUY_ORDERS, CANCEL_SELL_ORDERS } from "../constants/contractmethods.js";
|
|
4
|
+
export function buildCancelOrdersRawTx(params) {
|
|
5
|
+
const { marketContractAddress, orders } = params;
|
|
6
|
+
if (!marketContractAddress)
|
|
7
|
+
throw new Error("marketContractAddress is required");
|
|
8
|
+
if (!orders || orders.length === 0)
|
|
9
|
+
throw new Error("orders array must not be empty");
|
|
10
|
+
const sellOrders = orders.filter(o => o.orderType === 'sell');
|
|
11
|
+
const buyOrders = orders.filter(o => o.orderType === 'buy');
|
|
12
|
+
const txs = [];
|
|
13
|
+
if (sellOrders.length > 0) {
|
|
14
|
+
txs.push({
|
|
15
|
+
to: marketContractAddress,
|
|
16
|
+
data: encodeFunctionData({
|
|
17
|
+
abi: TradePoolAbi,
|
|
18
|
+
functionName: CANCEL_SELL_ORDERS,
|
|
19
|
+
args: [
|
|
20
|
+
sellOrders.map(o => o.option),
|
|
21
|
+
sellOrders.map(o => o.pricePerShare),
|
|
22
|
+
sellOrders.map(o => o.orderId),
|
|
23
|
+
],
|
|
24
|
+
}),
|
|
25
|
+
});
|
|
26
|
+
}
|
|
27
|
+
if (buyOrders.length > 0) {
|
|
28
|
+
txs.push({
|
|
29
|
+
to: marketContractAddress,
|
|
30
|
+
data: encodeFunctionData({
|
|
31
|
+
abi: TradePoolAbi,
|
|
32
|
+
functionName: CANCEL_BUY_ORDERS,
|
|
33
|
+
args: [
|
|
34
|
+
buyOrders.map(o => o.option),
|
|
35
|
+
buyOrders.map(o => o.pricePerShare),
|
|
36
|
+
buyOrders.map(o => o.orderId),
|
|
37
|
+
],
|
|
38
|
+
}),
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return txs;
|
|
42
|
+
}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
-
import { EnterLimitOptionTxParams, EnterOptionTxParams, RawTransaction } from './types.js';
|
|
1
|
+
import { EnterLimitOptionTxParams, EnterOptionTxParams, LimitSellOptionTxParams, RawTransaction } from './types.js';
|
|
2
2
|
export declare function buildEnterOptionRawTx(params: EnterOptionTxParams): RawTransaction;
|
|
3
3
|
export declare function buildLimitBuyOrderRawTx(params: EnterLimitOptionTxParams): RawTransaction;
|
|
4
|
+
export declare function buildLimitSellOrderRawTx(params: LimitSellOptionTxParams): RawTransaction;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { encodeFunctionData } from 'viem';
|
|
2
2
|
import { TradePoolAbi } from '../abi/TradeMarketsAbi.js';
|
|
3
3
|
import { convertToWeiEthers } from '../utils/helpers.js';
|
|
4
|
-
import { ENTER_OPTION, PLACE_BUY_ORDER } from '../constants/contractmethods.js';
|
|
4
|
+
import { ENTER_OPTION, PLACE_BUY_ORDER, PLACE_SELL_ORDER } from '../constants/contractmethods.js';
|
|
5
5
|
export function buildEnterOptionRawTx(params) {
|
|
6
6
|
const { marketContractAddress, selectedOption, buyAmountInWei } = params;
|
|
7
7
|
if (!marketContractAddress)
|
|
@@ -55,3 +55,37 @@ export function buildLimitBuyOrderRawTx(params) {
|
|
|
55
55
|
}),
|
|
56
56
|
};
|
|
57
57
|
}
|
|
58
|
+
export function buildLimitSellOrderRawTx(params) {
|
|
59
|
+
const { marketContractAddress, selectedOption, pricePerShare, sharesAmountWei } = params;
|
|
60
|
+
if (!marketContractAddress) {
|
|
61
|
+
throw new Error("market address is required");
|
|
62
|
+
}
|
|
63
|
+
if (selectedOption === undefined) {
|
|
64
|
+
throw new Error("selectedOption is required");
|
|
65
|
+
}
|
|
66
|
+
if (!pricePerShare) {
|
|
67
|
+
throw new Error("price per share is required");
|
|
68
|
+
}
|
|
69
|
+
if (pricePerShare <= 0 || pricePerShare >= 1) {
|
|
70
|
+
throw new Error("price per share should be in between 0 to 1, make sure to convert to correct decimals");
|
|
71
|
+
}
|
|
72
|
+
if (!sharesAmountWei) {
|
|
73
|
+
throw new Error("shares amount is required");
|
|
74
|
+
}
|
|
75
|
+
if (sharesAmountWei <= 0n) {
|
|
76
|
+
throw new Error("shares amount must be greater than 0");
|
|
77
|
+
}
|
|
78
|
+
const pricePerShareInEther = convertToWeiEthers(pricePerShare, 18);
|
|
79
|
+
return {
|
|
80
|
+
to: marketContractAddress,
|
|
81
|
+
data: encodeFunctionData({
|
|
82
|
+
abi: TradePoolAbi,
|
|
83
|
+
functionName: PLACE_SELL_ORDER,
|
|
84
|
+
args: [
|
|
85
|
+
BigInt(selectedOption),
|
|
86
|
+
pricePerShareInEther,
|
|
87
|
+
sharesAmountWei,
|
|
88
|
+
],
|
|
89
|
+
}),
|
|
90
|
+
};
|
|
91
|
+
}
|
package/dist/tx/types.d.ts
CHANGED
|
@@ -20,20 +20,32 @@ export interface EnterLimitOptionTxParams {
|
|
|
20
20
|
buyAmountInWei: bigint;
|
|
21
21
|
tokenDecimals?: number;
|
|
22
22
|
}
|
|
23
|
+
export interface LimitSellOptionTxParams {
|
|
24
|
+
marketContractAddress: `0x${string}`;
|
|
25
|
+
selectedOption: number;
|
|
26
|
+
pricePerShare: bigint;
|
|
27
|
+
sharesAmountWei: bigint;
|
|
28
|
+
tokenDecimals?: number;
|
|
29
|
+
}
|
|
23
30
|
export interface CreateMarketTxParams {
|
|
31
|
+
marketQuestion: string;
|
|
32
|
+
marketOptions: string[];
|
|
33
|
+
marketTags: string[];
|
|
34
|
+
marketDescription: string;
|
|
24
35
|
isPublic: boolean;
|
|
25
36
|
isPublicPoolResolverAi: boolean;
|
|
26
37
|
creator: `0x${string}`;
|
|
27
38
|
startTime: bigint;
|
|
28
39
|
endTime: bigint;
|
|
29
|
-
|
|
40
|
+
no_of_options: bigint;
|
|
30
41
|
disputeTimer: number;
|
|
31
|
-
ipfsUrl: string;
|
|
32
42
|
inputAmountWei: bigint;
|
|
33
43
|
barValues: number[];
|
|
34
44
|
baseToken: `0x${string}`;
|
|
35
45
|
tokenDecimals?: number;
|
|
36
46
|
factoryContractAddress?: `0x${string}`;
|
|
47
|
+
apiUrl?: string;
|
|
48
|
+
rpcUrl?: string;
|
|
37
49
|
}
|
|
38
50
|
export interface ClaimTxParams {
|
|
39
51
|
marketId: string;
|
|
@@ -41,6 +53,59 @@ export interface ClaimTxParams {
|
|
|
41
53
|
apiUrl?: string;
|
|
42
54
|
rpcUrl?: string;
|
|
43
55
|
}
|
|
56
|
+
export interface CreateDisputeTxParams {
|
|
57
|
+
marketId: string;
|
|
58
|
+
walletAddress: `0x${string}`;
|
|
59
|
+
usdtTokenAddress?: `0x${string}`;
|
|
60
|
+
rainTokenAddress?: `0x${string}`;
|
|
61
|
+
usdtSymbol?: string;
|
|
62
|
+
apiUrl?: string;
|
|
63
|
+
rpcUrl?: string;
|
|
64
|
+
}
|
|
65
|
+
export interface CreateAppealTxParams {
|
|
66
|
+
marketId: string;
|
|
67
|
+
walletAddress: `0x${string}`;
|
|
68
|
+
usdtTokenAddress?: `0x${string}`;
|
|
69
|
+
rainTokenAddress?: `0x${string}`;
|
|
70
|
+
usdtSymbol?: string;
|
|
71
|
+
apiUrl?: string;
|
|
72
|
+
rpcUrl?: string;
|
|
73
|
+
}
|
|
74
|
+
export interface CloseMarketTxParams {
|
|
75
|
+
marketId: string;
|
|
76
|
+
walletAddress: `0x${string}`;
|
|
77
|
+
proposedOutcome?: number;
|
|
78
|
+
usdtTokenAddress?: `0x${string}`;
|
|
79
|
+
rainTokenAddress?: `0x${string}`;
|
|
80
|
+
usdtSymbol?: string;
|
|
81
|
+
tokenDecimals?: number;
|
|
82
|
+
apiUrl?: string;
|
|
83
|
+
rpcUrl?: string;
|
|
84
|
+
}
|
|
85
|
+
export interface OrderToCancel {
|
|
86
|
+
orderType: 'buy' | 'sell';
|
|
87
|
+
option: bigint;
|
|
88
|
+
pricePerShare: bigint;
|
|
89
|
+
orderId: bigint;
|
|
90
|
+
}
|
|
91
|
+
export interface CancelOrdersTxParams {
|
|
92
|
+
marketContractAddress: `0x${string}`;
|
|
93
|
+
orders: OrderToCancel[];
|
|
94
|
+
}
|
|
95
|
+
export interface CancelAllOpenOrdersTxParams {
|
|
96
|
+
marketId: string;
|
|
97
|
+
marketContractAddress: `0x${string}`;
|
|
98
|
+
walletAddress: `0x${string}`;
|
|
99
|
+
accessToken: string;
|
|
100
|
+
apiUrl?: string;
|
|
101
|
+
}
|
|
102
|
+
export interface ExtendTimeTxParams {
|
|
103
|
+
marketContractAddress: `0x${string}`;
|
|
104
|
+
walletAddress: `0x${string}`;
|
|
105
|
+
accessToken: string;
|
|
106
|
+
apiUrl?: string;
|
|
107
|
+
rpcUrl?: string;
|
|
108
|
+
}
|
|
44
109
|
export interface GetUserOptionSharesParams {
|
|
45
110
|
options: [{
|
|
46
111
|
choiceIndex: number;
|
package/dist/utils/helpers.d.ts
CHANGED
package/dist/utils/helpers.js
CHANGED
|
@@ -1,4 +1,17 @@
|
|
|
1
|
-
import { ethers } from "ethers";
|
|
1
|
+
import { ethers, JsonRpcProvider, Contract } from "ethers";
|
|
2
|
+
import { ERC20Abi } from "../abi/ERC20Abi.js";
|
|
3
|
+
import { isRpcValid } from "../tx/ClaimFunds/helpers.js";
|
|
2
4
|
export const convertToWeiEthers = (value, decimals) => {
|
|
3
5
|
return ethers.parseUnits(value.toString(), decimals);
|
|
4
6
|
};
|
|
7
|
+
export async function getUserAllownace(params) {
|
|
8
|
+
const { factoryContractAddress, baseToken, creator, rpcUrl } = params;
|
|
9
|
+
const isRpcWorking = await isRpcValid(rpcUrl);
|
|
10
|
+
if (!rpcUrl || !isRpcWorking) {
|
|
11
|
+
throw new Error("Provided RPC URL is not valid or not working");
|
|
12
|
+
}
|
|
13
|
+
const provider = new JsonRpcProvider(rpcUrl);
|
|
14
|
+
const ERC20ApprovalContract = new Contract(baseToken, ERC20Abi, provider);
|
|
15
|
+
const userAllowance = await ERC20ApprovalContract.allowance(creator, factoryContractAddress);
|
|
16
|
+
return userAllowance;
|
|
17
|
+
}
|
package/package.json
CHANGED