perps-sdk-ts 1.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.
- package/.claude/settings.local.json +11 -0
- package/CONTRACT_METHOD_FIXES.md +189 -0
- package/INTEGRATION_SUMMARY.md +219 -0
- package/OPTIMIZATION_GUIDE.md +238 -0
- package/README.md +384 -0
- package/SNAPSHOT_FIX_SUMMARY.md +161 -0
- package/SNAPSHOT_OPTIMIZATION_SUMMARY.md +199 -0
- package/dist/abis/Referral.d.ts +36 -0
- package/dist/abis/Referral.js +4 -0
- package/dist/abis/Trading.d.ts +57 -0
- package/dist/abis/Trading.js +742 -0
- package/dist/abis/erc20.d.ts +51 -0
- package/dist/abis/erc20.js +4 -0
- package/dist/abis/index.d.ts +8 -0
- package/dist/abis/index.js +24 -0
- package/dist/abis/multicall.d.ts +85 -0
- package/dist/abis/multicall.js +4 -0
- package/dist/abis/pairInfos.d.ts +77 -0
- package/dist/abis/pairInfos.js +4 -0
- package/dist/abis/pairStorage.d.ts +124 -0
- package/dist/abis/pairStorage.js +4 -0
- package/dist/abis/priceAggregator.d.ts +77 -0
- package/dist/abis/priceAggregator.js +4 -0
- package/dist/abis/tardingStorage.d.ts +97 -0
- package/dist/abis/tardingStorage.js +1295 -0
- package/dist/abis.d.ts +623 -0
- package/dist/abis.js +49 -0
- package/dist/client.d.ts +118 -0
- package/dist/client.js +224 -0
- package/dist/config.d.ts +43 -0
- package/dist/config.js +42 -0
- package/dist/crypto/spki.d.ts +55 -0
- package/dist/crypto/spki.js +160 -0
- package/dist/feed/feed_client.d.ts +68 -0
- package/dist/feed/feed_client.js +239 -0
- package/dist/index.d.ts +28 -0
- package/dist/index.js +87 -0
- package/dist/rpc/asset_parameters.d.ts +62 -0
- package/dist/rpc/asset_parameters.js +169 -0
- package/dist/rpc/blended.d.ts +23 -0
- package/dist/rpc/blended.js +55 -0
- package/dist/rpc/category_parameters.d.ts +34 -0
- package/dist/rpc/category_parameters.js +105 -0
- package/dist/rpc/delegation.d.ts +81 -0
- package/dist/rpc/delegation.js +180 -0
- package/dist/rpc/fee_parameters.d.ts +46 -0
- package/dist/rpc/fee_parameters.js +113 -0
- package/dist/rpc/multicall.d.ts +83 -0
- package/dist/rpc/multicall.js +117 -0
- package/dist/rpc/pair_info_queries.d.ts +101 -0
- package/dist/rpc/pair_info_queries.js +161 -0
- package/dist/rpc/pairs_cache.d.ts +62 -0
- package/dist/rpc/pairs_cache.js +240 -0
- package/dist/rpc/referral_operations.d.ts +67 -0
- package/dist/rpc/referral_operations.js +143 -0
- package/dist/rpc/snapshot.d.ts +49 -0
- package/dist/rpc/snapshot.js +162 -0
- package/dist/rpc/trade.d.ts +84 -0
- package/dist/rpc/trade.js +249 -0
- package/dist/rpc/trading_operations.d.ts +103 -0
- package/dist/rpc/trading_operations.js +295 -0
- package/dist/rpc/trading_parameters.d.ts +49 -0
- package/dist/rpc/trading_parameters.js +94 -0
- package/dist/signers/base.d.ts +24 -0
- package/dist/signers/base.js +10 -0
- package/dist/signers/kms.d.ts +47 -0
- package/dist/signers/kms.js +172 -0
- package/dist/signers/local.d.ts +43 -0
- package/dist/signers/local.js +64 -0
- package/dist/types.d.ts +1419 -0
- package/dist/types.js +245 -0
- package/dist/utils.d.ts +52 -0
- package/dist/utils.js +134 -0
- package/examples/advanced-queries.ts +181 -0
- package/examples/basic-usage.ts +78 -0
- package/examples/delegation-and-referrals.ts +130 -0
- package/examples/get-pyth-ids.ts +61 -0
- package/examples/kms-signer.ts +31 -0
- package/examples/optimized-snapshot.ts +153 -0
- package/examples/price-feed-with-sdk-ids.ts +97 -0
- package/examples/price-feed.ts +36 -0
- package/examples/trading-operations.ts +149 -0
- package/package.json +41 -0
- package/src/abis/Referral.ts +3 -0
- package/src/abis/Trading.ts +741 -0
- package/src/abis/erc20.ts +3 -0
- package/src/abis/index.ts +8 -0
- package/src/abis/multicall.ts +3 -0
- package/src/abis/pairInfos.ts +3 -0
- package/src/abis/pairStorage.ts +3 -0
- package/src/abis/priceAggregator.ts +3 -0
- package/src/abis/tardingStorage.ts +1294 -0
- package/src/abis.ts +56 -0
- package/src/client.ts +373 -0
- package/src/config.ts +62 -0
- package/src/crypto/spki.ts +197 -0
- package/src/feed/feed_client.ts +288 -0
- package/src/index.ts +114 -0
- package/src/rpc/asset_parameters.ts +217 -0
- package/src/rpc/blended.ts +77 -0
- package/src/rpc/category_parameters.ts +128 -0
- package/src/rpc/delegation.ts +225 -0
- package/src/rpc/fee_parameters.ts +150 -0
- package/src/rpc/multicall.ts +164 -0
- package/src/rpc/pair_info_queries.ts +208 -0
- package/src/rpc/pairs_cache.ts +268 -0
- package/src/rpc/referral_operations.ts +164 -0
- package/src/rpc/snapshot.ts +210 -0
- package/src/rpc/trade.ts +306 -0
- package/src/rpc/trading_operations.ts +378 -0
- package/src/rpc/trading_parameters.ts +127 -0
- package/src/signers/base.ts +27 -0
- package/src/signers/kms.ts +212 -0
- package/src/signers/local.ts +70 -0
- package/src/types.ts +410 -0
- package/src/utils.ts +155 -0
- package/tsconfig.json +18 -0
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
import { Contract, Provider, TransactionRequest } from 'ethers';
|
|
2
|
+
import { TradeInput, TradeResponse, MarginUpdateType } from '../types';
|
|
3
|
+
import { PairsCache } from './pairs_cache';
|
|
4
|
+
/**
|
|
5
|
+
* RPC module for trading operations
|
|
6
|
+
*/
|
|
7
|
+
export declare class TradeRPC {
|
|
8
|
+
private provider;
|
|
9
|
+
private tradingContract;
|
|
10
|
+
private tradingStorageContract;
|
|
11
|
+
private pairsCache;
|
|
12
|
+
constructor(provider: Provider, tradingContract: Contract, tradingStorageContract: Contract, pairsCache: PairsCache);
|
|
13
|
+
/**
|
|
14
|
+
* Build transaction to open a trade
|
|
15
|
+
* @param tradeInput - Trade input parameters
|
|
16
|
+
* @returns Transaction request
|
|
17
|
+
*/
|
|
18
|
+
buildTradeOpenTx(tradeInput: TradeInput, trader: string): Promise<TransactionRequest>;
|
|
19
|
+
/**
|
|
20
|
+
* Build transaction to open a trade via delegation
|
|
21
|
+
* @param tradeInput - Trade input parameters
|
|
22
|
+
* @param trader - Trader address
|
|
23
|
+
* @returns Transaction request
|
|
24
|
+
*/
|
|
25
|
+
buildTradeOpenTxDelegate(tradeInput: TradeInput, trader: string): Promise<TransactionRequest>;
|
|
26
|
+
/**
|
|
27
|
+
* Build transaction to close a trade
|
|
28
|
+
* @param pairIndex - Pair index
|
|
29
|
+
* @param tradeIndex - Trade index
|
|
30
|
+
* @returns Transaction request
|
|
31
|
+
*/
|
|
32
|
+
buildTradeCloseTx(pairIndex: number, tradeIndex: number): Promise<TransactionRequest>;
|
|
33
|
+
/**
|
|
34
|
+
* Build transaction to close a trade via delegation
|
|
35
|
+
* @param pairIndex - Pair index
|
|
36
|
+
* @param tradeIndex - Trade index
|
|
37
|
+
* @param trader - Trader address
|
|
38
|
+
* @returns Transaction request
|
|
39
|
+
*/
|
|
40
|
+
buildTradeCloseTxDelegate(pairIndex: number, tradeIndex: number, trader: string): Promise<TransactionRequest>;
|
|
41
|
+
/**
|
|
42
|
+
* Build transaction to cancel a pending order
|
|
43
|
+
* @param pairIndex - Pair index
|
|
44
|
+
* @param orderIndex - Order index
|
|
45
|
+
* @returns Transaction request
|
|
46
|
+
*/
|
|
47
|
+
buildOrderCancelTx(pairIndex: number, orderIndex: number): Promise<TransactionRequest>;
|
|
48
|
+
/**
|
|
49
|
+
* Build transaction to update trade margin
|
|
50
|
+
* @param pairIndex - Pair index
|
|
51
|
+
* @param tradeIndex - Trade index
|
|
52
|
+
* @param marginDelta - Margin change amount (USDC)
|
|
53
|
+
* @param updateType - Deposit or withdraw
|
|
54
|
+
* @returns Transaction request
|
|
55
|
+
*/
|
|
56
|
+
buildTradeMarginUpdateTx(pairIndex: number, tradeIndex: number, marginDelta: number, updateType: MarginUpdateType): Promise<TransactionRequest>;
|
|
57
|
+
/**
|
|
58
|
+
* Build transaction to update take profit and stop loss
|
|
59
|
+
* @param pairIndex - Pair index
|
|
60
|
+
* @param tradeIndex - Trade index
|
|
61
|
+
* @param tp - Take profit price
|
|
62
|
+
* @param sl - Stop loss price
|
|
63
|
+
* @returns Transaction request
|
|
64
|
+
*/
|
|
65
|
+
buildTradeTpSlUpdateTx(pairIndex: number, tradeIndex: number, tp: number, sl: number): Promise<TransactionRequest>;
|
|
66
|
+
/**
|
|
67
|
+
* Get trade execution fee
|
|
68
|
+
* @returns Execution fee in native token (wei)
|
|
69
|
+
*/
|
|
70
|
+
getTradeExecutionFee(): Promise<bigint>;
|
|
71
|
+
/**
|
|
72
|
+
* Get all trades for a trader
|
|
73
|
+
* @param traderAddress - Trader address
|
|
74
|
+
* @param maxPairs - Maximum pair indices to check (default: 100)
|
|
75
|
+
* @returns Array of trades
|
|
76
|
+
*/
|
|
77
|
+
getTrades(traderAddress: string, maxPairs?: number): Promise<TradeResponse[]>;
|
|
78
|
+
/**
|
|
79
|
+
* Convert order type string to numeric value
|
|
80
|
+
* @param orderType - Order type string
|
|
81
|
+
* @returns Numeric order type
|
|
82
|
+
*/
|
|
83
|
+
private getOrderTypeValue;
|
|
84
|
+
}
|
|
@@ -0,0 +1,249 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TradeRPC = void 0;
|
|
4
|
+
const ethers_1 = require("ethers");
|
|
5
|
+
const types_1 = require("../types");
|
|
6
|
+
/**
|
|
7
|
+
* RPC module for trading operations
|
|
8
|
+
*/
|
|
9
|
+
class TradeRPC {
|
|
10
|
+
constructor(provider, tradingContract, tradingStorageContract, pairsCache) {
|
|
11
|
+
this.provider = provider;
|
|
12
|
+
this.tradingContract = tradingContract;
|
|
13
|
+
this.tradingStorageContract = tradingStorageContract;
|
|
14
|
+
this.pairsCache = pairsCache;
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Build transaction to open a trade
|
|
18
|
+
* @param tradeInput - Trade input parameters
|
|
19
|
+
* @returns Transaction request
|
|
20
|
+
*/
|
|
21
|
+
async buildTradeOpenTx(tradeInput, trader) {
|
|
22
|
+
// const pairIndex = await this.pairsCache.getPairIndex(tradeInput.pair);
|
|
23
|
+
const pairIndex = 1;
|
|
24
|
+
console.log("pairIndex", pairIndex);
|
|
25
|
+
if (pairIndex === undefined) {
|
|
26
|
+
throw new Error(`Pair ${tradeInput.pair} not found`);
|
|
27
|
+
}
|
|
28
|
+
const sl = (0, types_1.toBlockchain10)(tradeInput.sl);
|
|
29
|
+
console.log("sl", sl);
|
|
30
|
+
const tp = (0, types_1.toBlockchain10)(tradeInput.tp);
|
|
31
|
+
console.log("tp", tp);
|
|
32
|
+
const leverage = (0, types_1.toBlockchain10)(tradeInput.leverage);
|
|
33
|
+
console.log("leverage", leverage);
|
|
34
|
+
const openPrice = (0, types_1.toBlockchain10)(tradeInput.openPrice);
|
|
35
|
+
console.log("openPrice", openPrice);
|
|
36
|
+
// positionSizeUSDC is actually the collateral amount, not position size
|
|
37
|
+
const positionSizeUsdc = (0, types_1.toBlockchain6)(tradeInput.collateralInTrade);
|
|
38
|
+
console.log("positionSizeUsdc", positionSizeUsdc);
|
|
39
|
+
const trade = {
|
|
40
|
+
trader,
|
|
41
|
+
pairIndex: pairIndex,
|
|
42
|
+
index: 0,
|
|
43
|
+
initialPosToken: 0,
|
|
44
|
+
positionSizeUSDC: positionSizeUsdc,
|
|
45
|
+
openPrice: openPrice,
|
|
46
|
+
buy: tradeInput.isLong,
|
|
47
|
+
leverage: leverage,
|
|
48
|
+
tp: tp,
|
|
49
|
+
sl: sl,
|
|
50
|
+
timestamp: 0,
|
|
51
|
+
};
|
|
52
|
+
console.log("trade", trade);
|
|
53
|
+
const orderType = this.getOrderTypeValue(tradeInput.orderType);
|
|
54
|
+
console.log("orderType", orderType);
|
|
55
|
+
const slippageP = (0, types_1.toBlockchain10)(tradeInput.maxSlippageP);
|
|
56
|
+
console.log("slippageP", slippageP);
|
|
57
|
+
// const executionFee = await this.getTradeExecutionFee();
|
|
58
|
+
const value = ethers_1.ethers.parseEther("0.00035");
|
|
59
|
+
console.log("execution fee (wei)", value);
|
|
60
|
+
return {
|
|
61
|
+
to: await this.tradingContract.getAddress(),
|
|
62
|
+
data: this.tradingContract.interface.encodeFunctionData('openTrade', [
|
|
63
|
+
trade,
|
|
64
|
+
orderType,
|
|
65
|
+
slippageP,
|
|
66
|
+
// tradeInput.referrer || '0x0000000000000000000000000000000000000000',
|
|
67
|
+
]),
|
|
68
|
+
value: value,
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Build transaction to open a trade via delegation
|
|
73
|
+
* @param tradeInput - Trade input parameters
|
|
74
|
+
* @param trader - Trader address
|
|
75
|
+
* @returns Transaction request
|
|
76
|
+
*/
|
|
77
|
+
async buildTradeOpenTxDelegate(tradeInput, trader) {
|
|
78
|
+
const tx = await this.buildTradeOpenTx(tradeInput, trader);
|
|
79
|
+
// Add delegation logic if needed
|
|
80
|
+
return tx;
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Build transaction to close a trade
|
|
84
|
+
* @param pairIndex - Pair index
|
|
85
|
+
* @param tradeIndex - Trade index
|
|
86
|
+
* @returns Transaction request
|
|
87
|
+
*/
|
|
88
|
+
async buildTradeCloseTx(pairIndex, tradeIndex) {
|
|
89
|
+
return {
|
|
90
|
+
to: await this.tradingContract.getAddress(),
|
|
91
|
+
data: this.tradingContract.interface.encodeFunctionData('closeTradeMarket', [
|
|
92
|
+
pairIndex,
|
|
93
|
+
tradeIndex,
|
|
94
|
+
]),
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* Build transaction to close a trade via delegation
|
|
99
|
+
* @param pairIndex - Pair index
|
|
100
|
+
* @param tradeIndex - Trade index
|
|
101
|
+
* @param trader - Trader address
|
|
102
|
+
* @returns Transaction request
|
|
103
|
+
*/
|
|
104
|
+
async buildTradeCloseTxDelegate(pairIndex, tradeIndex, trader) {
|
|
105
|
+
const tx = await this.buildTradeCloseTx(pairIndex, tradeIndex);
|
|
106
|
+
// Add delegation logic if needed
|
|
107
|
+
return tx;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Build transaction to cancel a pending order
|
|
111
|
+
* @param pairIndex - Pair index
|
|
112
|
+
* @param orderIndex - Order index
|
|
113
|
+
* @returns Transaction request
|
|
114
|
+
*/
|
|
115
|
+
async buildOrderCancelTx(pairIndex, orderIndex) {
|
|
116
|
+
return {
|
|
117
|
+
to: await this.tradingContract.getAddress(),
|
|
118
|
+
data: this.tradingContract.interface.encodeFunctionData('cancelOpenOrder', [
|
|
119
|
+
pairIndex,
|
|
120
|
+
orderIndex,
|
|
121
|
+
]),
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Build transaction to update trade margin
|
|
126
|
+
* @param pairIndex - Pair index
|
|
127
|
+
* @param tradeIndex - Trade index
|
|
128
|
+
* @param marginDelta - Margin change amount (USDC)
|
|
129
|
+
* @param updateType - Deposit or withdraw
|
|
130
|
+
* @returns Transaction request
|
|
131
|
+
*/
|
|
132
|
+
async buildTradeMarginUpdateTx(pairIndex, tradeIndex, marginDelta, updateType) {
|
|
133
|
+
const isDeposit = updateType === types_1.MarginUpdateType.DEPOSIT;
|
|
134
|
+
return {
|
|
135
|
+
to: await this.tradingContract.getAddress(),
|
|
136
|
+
data: this.tradingContract.interface.encodeFunctionData('updateMargin', [
|
|
137
|
+
pairIndex,
|
|
138
|
+
tradeIndex,
|
|
139
|
+
(0, types_1.toBlockchain6)(marginDelta),
|
|
140
|
+
isDeposit,
|
|
141
|
+
]),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
/**
|
|
145
|
+
* Build transaction to update take profit and stop loss
|
|
146
|
+
* @param pairIndex - Pair index
|
|
147
|
+
* @param tradeIndex - Trade index
|
|
148
|
+
* @param tp - Take profit price
|
|
149
|
+
* @param sl - Stop loss price
|
|
150
|
+
* @returns Transaction request
|
|
151
|
+
*/
|
|
152
|
+
async buildTradeTpSlUpdateTx(pairIndex, tradeIndex, tp, sl) {
|
|
153
|
+
return {
|
|
154
|
+
to: await this.tradingContract.getAddress(),
|
|
155
|
+
data: this.tradingContract.interface.encodeFunctionData('updateTpSl', [
|
|
156
|
+
pairIndex,
|
|
157
|
+
tradeIndex,
|
|
158
|
+
(0, types_1.toBlockchain10)(tp),
|
|
159
|
+
(0, types_1.toBlockchain10)(sl),
|
|
160
|
+
]),
|
|
161
|
+
};
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Get trade execution fee
|
|
165
|
+
* @returns Execution fee in native token (wei)
|
|
166
|
+
*/
|
|
167
|
+
async getTradeExecutionFee() {
|
|
168
|
+
try {
|
|
169
|
+
return await this.tradingContract.getExecutionFee();
|
|
170
|
+
}
|
|
171
|
+
catch (error) {
|
|
172
|
+
console.error('Error getting execution fee:', error);
|
|
173
|
+
return BigInt(0);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
/**
|
|
177
|
+
* Get all trades for a trader
|
|
178
|
+
* @param traderAddress - Trader address
|
|
179
|
+
* @param maxPairs - Maximum pair indices to check (default: 100)
|
|
180
|
+
* @returns Array of trades
|
|
181
|
+
*/
|
|
182
|
+
async getTrades(traderAddress, maxPairs = 100) {
|
|
183
|
+
try {
|
|
184
|
+
const trades = [];
|
|
185
|
+
// Check pairs concurrently for better performance
|
|
186
|
+
const pairPromises = [];
|
|
187
|
+
for (let pairIndex = 0; pairIndex < maxPairs; pairIndex++) {
|
|
188
|
+
const pairPromise = (async () => {
|
|
189
|
+
try {
|
|
190
|
+
// Get the count of trades for this trader on this pair
|
|
191
|
+
const count = await this.tradingStorageContract.openTradesCount(traderAddress, pairIndex);
|
|
192
|
+
if (count === 0n || count === 0)
|
|
193
|
+
return;
|
|
194
|
+
// Fetch all trades for this pair
|
|
195
|
+
const tradePromises = [];
|
|
196
|
+
for (let index = 0; index < Number(count); index++) {
|
|
197
|
+
tradePromises.push(this.tradingStorageContract.openTrades(traderAddress, pairIndex, index)
|
|
198
|
+
.then((trade) => {
|
|
199
|
+
// Only add if the trade exists (leverage > 0)
|
|
200
|
+
if (trade.leverage > 0) {
|
|
201
|
+
trades.push({
|
|
202
|
+
trader: trade.trader,
|
|
203
|
+
pairIndex: Number(trade.pairIndex),
|
|
204
|
+
index: Number(trade.index),
|
|
205
|
+
initialPosUsdc: (0, types_1.fromBlockchain6)(trade.initialPosToken || trade.positionSizeUSDC),
|
|
206
|
+
openPrice: (0, types_1.fromBlockchain10)(trade.openPrice),
|
|
207
|
+
buy: trade.buy,
|
|
208
|
+
leverage: Number((0, types_1.fromBlockchain10)(trade.leverage)),
|
|
209
|
+
tp: (0, types_1.fromBlockchain10)(trade.tp),
|
|
210
|
+
sl: (0, types_1.fromBlockchain10)(trade.sl),
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
})
|
|
214
|
+
.catch(() => {
|
|
215
|
+
// Skip if trade doesn't exist at this index
|
|
216
|
+
}));
|
|
217
|
+
}
|
|
218
|
+
await Promise.all(tradePromises);
|
|
219
|
+
}
|
|
220
|
+
catch (err) {
|
|
221
|
+
// Skip if no trades for this pair
|
|
222
|
+
}
|
|
223
|
+
})();
|
|
224
|
+
pairPromises.push(pairPromise);
|
|
225
|
+
}
|
|
226
|
+
await Promise.all(pairPromises);
|
|
227
|
+
return trades;
|
|
228
|
+
}
|
|
229
|
+
catch (error) {
|
|
230
|
+
console.error('Error getting trades:', error);
|
|
231
|
+
return [];
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Convert order type string to numeric value
|
|
236
|
+
* @param orderType - Order type string
|
|
237
|
+
* @returns Numeric order type
|
|
238
|
+
*/
|
|
239
|
+
getOrderTypeValue(orderType) {
|
|
240
|
+
const orderTypes = {
|
|
241
|
+
market: 0,
|
|
242
|
+
limit: 1,
|
|
243
|
+
stop_limit: 2,
|
|
244
|
+
market_zero_fee: 3,
|
|
245
|
+
};
|
|
246
|
+
return orderTypes[orderType] || 0;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
exports.TradeRPC = TradeRPC;
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { Contract, TransactionReceipt } from 'ethers';
|
|
2
|
+
import { Trade, TradeInfo, OpenLimitOrder, TradeInputOrderType, MarginUpdateType } from '../types';
|
|
3
|
+
import { BaseSigner } from '../signers/base';
|
|
4
|
+
/**
|
|
5
|
+
* Trading Operations RPC
|
|
6
|
+
* Handles all trading contract interactions including opening/closing trades,
|
|
7
|
+
* updating margins, and managing limit orders.
|
|
8
|
+
*/
|
|
9
|
+
export declare class TradingOperationsRPC {
|
|
10
|
+
private tradingContract;
|
|
11
|
+
private tradingStorageContract;
|
|
12
|
+
private signer?;
|
|
13
|
+
constructor(tradingContract: Contract, tradingStorageContract: Contract, signer?: BaseSigner | undefined);
|
|
14
|
+
/**
|
|
15
|
+
* Get execution fee required for trading operations
|
|
16
|
+
* @returns Execution fee in ETH
|
|
17
|
+
*/
|
|
18
|
+
getExecutionFee(): Promise<number>;
|
|
19
|
+
/**
|
|
20
|
+
* Open a new trade position
|
|
21
|
+
* @param trade - Trade parameters
|
|
22
|
+
* @param orderType - Order type (MARKET, LIMIT, etc.)
|
|
23
|
+
* @param slippageP - Slippage percentage (e.g., 1 for 1%)
|
|
24
|
+
* @param executionFeeEth - Execution fee in ETH (optional, will fetch if not provided)
|
|
25
|
+
* @returns Transaction receipt
|
|
26
|
+
*/
|
|
27
|
+
openTrade(trade: Omit<Trade, 'timestamp'>, orderType: TradeInputOrderType, slippageP: number, executionFeeEth?: number): Promise<TransactionReceipt | null>;
|
|
28
|
+
/**
|
|
29
|
+
* Close a trade position at market price
|
|
30
|
+
* @param pairIndex - Trading pair index
|
|
31
|
+
* @param index - Trade index
|
|
32
|
+
* @param closeAmount - Amount of collateral to close (USDC)
|
|
33
|
+
* @param executionFeeEth - Execution fee in ETH (optional)
|
|
34
|
+
* @returns Transaction receipt
|
|
35
|
+
*/
|
|
36
|
+
closeTradeMarket(pairIndex: number, index: number, closeAmount: number, executionFeeEth?: number): Promise<TransactionReceipt | null>;
|
|
37
|
+
/**
|
|
38
|
+
* Update margin for an existing trade
|
|
39
|
+
* @param pairIndex - Trading pair index
|
|
40
|
+
* @param index - Trade index
|
|
41
|
+
* @param updateType - DEPOSIT or WITHDRAW
|
|
42
|
+
* @param amount - Amount to add/remove (USDC)
|
|
43
|
+
* @param priceUpdateData - Price oracle update data
|
|
44
|
+
* @returns Transaction receipt
|
|
45
|
+
*/
|
|
46
|
+
updateMargin(pairIndex: number, index: number, updateType: MarginUpdateType, amount: number, priceUpdateData?: string[]): Promise<TransactionReceipt | null>;
|
|
47
|
+
/**
|
|
48
|
+
* Update take profit and stop loss for a trade
|
|
49
|
+
* @param pairIndex - Trading pair index
|
|
50
|
+
* @param index - Trade index
|
|
51
|
+
* @param newSl - New stop loss price
|
|
52
|
+
* @param newTp - New take profit price
|
|
53
|
+
* @param priceUpdateData - Price oracle update data
|
|
54
|
+
* @returns Transaction receipt
|
|
55
|
+
*/
|
|
56
|
+
updateTpAndSl(pairIndex: number, index: number, newSl: number, newTp: number, priceUpdateData?: string[]): Promise<TransactionReceipt | null>;
|
|
57
|
+
/**
|
|
58
|
+
* Cancel a pending limit order
|
|
59
|
+
* @param pairIndex - Trading pair index
|
|
60
|
+
* @param index - Order index
|
|
61
|
+
* @returns Transaction receipt
|
|
62
|
+
*/
|
|
63
|
+
cancelOpenLimitOrder(pairIndex: number, index: number): Promise<TransactionReceipt | null>;
|
|
64
|
+
/**
|
|
65
|
+
* Get open trade information
|
|
66
|
+
* @param trader - Trader address
|
|
67
|
+
* @param pairIndex - Trading pair index
|
|
68
|
+
* @param index - Trade index
|
|
69
|
+
* @returns Trade information
|
|
70
|
+
*/
|
|
71
|
+
getOpenTrade(trader: string, pairIndex: number, index: number): Promise<Trade>;
|
|
72
|
+
/**
|
|
73
|
+
* Get additional trade information
|
|
74
|
+
* @param trader - Trader address
|
|
75
|
+
* @param pairIndex - Trading pair index
|
|
76
|
+
* @param index - Trade index
|
|
77
|
+
* @returns Trade info
|
|
78
|
+
*/
|
|
79
|
+
getOpenTradeInfo(trader: string, pairIndex: number, index: number): Promise<TradeInfo>;
|
|
80
|
+
/**
|
|
81
|
+
* Get number of open trades for a trader on a pair
|
|
82
|
+
* @param trader - Trader address
|
|
83
|
+
* @param pairIndex - Trading pair index
|
|
84
|
+
* @returns Number of open trades
|
|
85
|
+
*/
|
|
86
|
+
getOpenTradesCount(trader: string, pairIndex: number): Promise<number>;
|
|
87
|
+
/**
|
|
88
|
+
* Get limit order information
|
|
89
|
+
* @param trader - Trader address
|
|
90
|
+
* @param pairIndex - Trading pair index
|
|
91
|
+
* @param index - Order index
|
|
92
|
+
* @returns Limit order information
|
|
93
|
+
*/
|
|
94
|
+
getOpenLimitOrder(trader: string, pairIndex: number, index: number): Promise<OpenLimitOrder>;
|
|
95
|
+
/**
|
|
96
|
+
* Helper method to sign and send transactions
|
|
97
|
+
*/
|
|
98
|
+
private signAndSend;
|
|
99
|
+
/**
|
|
100
|
+
* Set signer for transactions
|
|
101
|
+
*/
|
|
102
|
+
setSigner(signer: BaseSigner): void;
|
|
103
|
+
}
|