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,83 @@
|
|
|
1
|
+
import { Contract } from 'ethers';
|
|
2
|
+
/**
|
|
3
|
+
* Call structure for multicall
|
|
4
|
+
*/
|
|
5
|
+
export interface MulticallCall {
|
|
6
|
+
target: string;
|
|
7
|
+
callData: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Result structure from multicall
|
|
11
|
+
*/
|
|
12
|
+
export interface MulticallResult {
|
|
13
|
+
blockNumber: number;
|
|
14
|
+
returnData: string[];
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* Multicall RPC
|
|
18
|
+
* Allows batching multiple contract view calls into a single request
|
|
19
|
+
*/
|
|
20
|
+
export declare class MulticallRPC {
|
|
21
|
+
private multicallContract;
|
|
22
|
+
constructor(multicallContract: Contract);
|
|
23
|
+
/**
|
|
24
|
+
* Execute multiple calls in a single transaction
|
|
25
|
+
* @param calls - Array of calls to execute
|
|
26
|
+
* @returns Block number and return data from each call
|
|
27
|
+
*/
|
|
28
|
+
aggregate(calls: MulticallCall[]): Promise<MulticallResult>;
|
|
29
|
+
/**
|
|
30
|
+
* Helper: Create call data for a contract function
|
|
31
|
+
* @param contract - Contract instance
|
|
32
|
+
* @param functionName - Function name
|
|
33
|
+
* @param args - Function arguments
|
|
34
|
+
* @returns Call data object
|
|
35
|
+
*/
|
|
36
|
+
createCall(contract: Contract, functionName: string, args?: any[]): MulticallCall;
|
|
37
|
+
/**
|
|
38
|
+
* Helper: Decode return data from a multicall result
|
|
39
|
+
* @param contract - Contract instance used for decoding
|
|
40
|
+
* @param functionName - Function name that was called
|
|
41
|
+
* @param returnData - Raw return data from multicall
|
|
42
|
+
* @returns Decoded result
|
|
43
|
+
*/
|
|
44
|
+
decodeResult(contract: Contract, functionName: string, returnData: string): any;
|
|
45
|
+
/**
|
|
46
|
+
* Execute multicall and decode results
|
|
47
|
+
* @param calls - Array of call definitions with contract and function info
|
|
48
|
+
* @returns Decoded results
|
|
49
|
+
*/
|
|
50
|
+
aggregateAndDecode(calls: Array<{
|
|
51
|
+
contract: Contract;
|
|
52
|
+
functionName: string;
|
|
53
|
+
args?: any[];
|
|
54
|
+
}>): Promise<{
|
|
55
|
+
blockNumber: number;
|
|
56
|
+
results: any[];
|
|
57
|
+
}>;
|
|
58
|
+
/**
|
|
59
|
+
* Batch read multiple open trades
|
|
60
|
+
* @param tradingStorageContract - TradingStorage contract instance
|
|
61
|
+
* @param trader - Trader address
|
|
62
|
+
* @param pairIndex - Pair index
|
|
63
|
+
* @param indices - Array of trade indices
|
|
64
|
+
* @returns Array of decoded trade data
|
|
65
|
+
*/
|
|
66
|
+
batchGetOpenTrades(tradingStorageContract: Contract, trader: string, pairIndex: number, indices: number[]): Promise<any[]>;
|
|
67
|
+
/**
|
|
68
|
+
* Batch read multiple trade infos
|
|
69
|
+
* @param tradingStorageContract - TradingStorage contract instance
|
|
70
|
+
* @param trader - Trader address
|
|
71
|
+
* @param pairIndex - Pair index
|
|
72
|
+
* @param indices - Array of trade indices
|
|
73
|
+
* @returns Array of decoded trade info data
|
|
74
|
+
*/
|
|
75
|
+
batchGetOpenTradesInfo(tradingStorageContract: Contract, trader: string, pairIndex: number, indices: number[]): Promise<any[]>;
|
|
76
|
+
/**
|
|
77
|
+
* Batch read pair information
|
|
78
|
+
* @param pairStorageContract - PairStorage contract instance
|
|
79
|
+
* @param pairIndices - Array of pair indices
|
|
80
|
+
* @returns Array of decoded pair data
|
|
81
|
+
*/
|
|
82
|
+
batchGetPairs(pairStorageContract: Contract, pairIndices: number[]): Promise<any[]>;
|
|
83
|
+
}
|
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.MulticallRPC = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Multicall RPC
|
|
6
|
+
* Allows batching multiple contract view calls into a single request
|
|
7
|
+
*/
|
|
8
|
+
class MulticallRPC {
|
|
9
|
+
constructor(multicallContract) {
|
|
10
|
+
this.multicallContract = multicallContract;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Execute multiple calls in a single transaction
|
|
14
|
+
* @param calls - Array of calls to execute
|
|
15
|
+
* @returns Block number and return data from each call
|
|
16
|
+
*/
|
|
17
|
+
async aggregate(calls) {
|
|
18
|
+
const result = await this.multicallContract.aggregate(calls);
|
|
19
|
+
return {
|
|
20
|
+
blockNumber: Number(result.blockNumber || result[0]),
|
|
21
|
+
returnData: Array.isArray(result.returnData) ? result.returnData : result[1],
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Helper: Create call data for a contract function
|
|
26
|
+
* @param contract - Contract instance
|
|
27
|
+
* @param functionName - Function name
|
|
28
|
+
* @param args - Function arguments
|
|
29
|
+
* @returns Call data object
|
|
30
|
+
*/
|
|
31
|
+
createCall(contract, functionName, args = []) {
|
|
32
|
+
return {
|
|
33
|
+
target: contract.target,
|
|
34
|
+
callData: contract.interface.encodeFunctionData(functionName, args),
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* Helper: Decode return data from a multicall result
|
|
39
|
+
* @param contract - Contract instance used for decoding
|
|
40
|
+
* @param functionName - Function name that was called
|
|
41
|
+
* @param returnData - Raw return data from multicall
|
|
42
|
+
* @returns Decoded result
|
|
43
|
+
*/
|
|
44
|
+
decodeResult(contract, functionName, returnData) {
|
|
45
|
+
return contract.interface.decodeFunctionResult(functionName, returnData);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Execute multicall and decode results
|
|
49
|
+
* @param calls - Array of call definitions with contract and function info
|
|
50
|
+
* @returns Decoded results
|
|
51
|
+
*/
|
|
52
|
+
async aggregateAndDecode(calls) {
|
|
53
|
+
// Create multicall calls
|
|
54
|
+
const multicallCalls = calls.map((call) => this.createCall(call.contract, call.functionName, call.args || []));
|
|
55
|
+
// Execute multicall
|
|
56
|
+
const { blockNumber, returnData } = await this.aggregate(multicallCalls);
|
|
57
|
+
// Decode results
|
|
58
|
+
const results = returnData.map((data, index) => {
|
|
59
|
+
const call = calls[index];
|
|
60
|
+
return this.decodeResult(call.contract, call.functionName, data);
|
|
61
|
+
});
|
|
62
|
+
return {
|
|
63
|
+
blockNumber,
|
|
64
|
+
results,
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Batch read multiple open trades
|
|
69
|
+
* @param tradingStorageContract - TradingStorage contract instance
|
|
70
|
+
* @param trader - Trader address
|
|
71
|
+
* @param pairIndex - Pair index
|
|
72
|
+
* @param indices - Array of trade indices
|
|
73
|
+
* @returns Array of decoded trade data
|
|
74
|
+
*/
|
|
75
|
+
async batchGetOpenTrades(tradingStorageContract, trader, pairIndex, indices) {
|
|
76
|
+
const calls = indices.map((index) => ({
|
|
77
|
+
contract: tradingStorageContract,
|
|
78
|
+
functionName: 'openTrades',
|
|
79
|
+
args: [trader, pairIndex, index],
|
|
80
|
+
}));
|
|
81
|
+
const { results } = await this.aggregateAndDecode(calls);
|
|
82
|
+
return results;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Batch read multiple trade infos
|
|
86
|
+
* @param tradingStorageContract - TradingStorage contract instance
|
|
87
|
+
* @param trader - Trader address
|
|
88
|
+
* @param pairIndex - Pair index
|
|
89
|
+
* @param indices - Array of trade indices
|
|
90
|
+
* @returns Array of decoded trade info data
|
|
91
|
+
*/
|
|
92
|
+
async batchGetOpenTradesInfo(tradingStorageContract, trader, pairIndex, indices) {
|
|
93
|
+
const calls = indices.map((index) => ({
|
|
94
|
+
contract: tradingStorageContract,
|
|
95
|
+
functionName: 'openTradesInfo',
|
|
96
|
+
args: [trader, pairIndex, index],
|
|
97
|
+
}));
|
|
98
|
+
const { results } = await this.aggregateAndDecode(calls);
|
|
99
|
+
return results;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Batch read pair information
|
|
103
|
+
* @param pairStorageContract - PairStorage contract instance
|
|
104
|
+
* @param pairIndices - Array of pair indices
|
|
105
|
+
* @returns Array of decoded pair data
|
|
106
|
+
*/
|
|
107
|
+
async batchGetPairs(pairStorageContract, pairIndices) {
|
|
108
|
+
const calls = pairIndices.map((pairIndex) => ({
|
|
109
|
+
contract: pairStorageContract,
|
|
110
|
+
functionName: 'pairs',
|
|
111
|
+
args: [pairIndex],
|
|
112
|
+
}));
|
|
113
|
+
const { results } = await this.aggregateAndDecode(calls);
|
|
114
|
+
return results;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
exports.MulticallRPC = MulticallRPC;
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import { Contract } from 'ethers';
|
|
2
|
+
import { Trade } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* PairInfo Queries RPC
|
|
5
|
+
* Handles querying pair-related information like price impact, skew, fees, and loss protection
|
|
6
|
+
*/
|
|
7
|
+
export declare class PairInfoQueriesRPC {
|
|
8
|
+
private pairInfosContract;
|
|
9
|
+
private priceAggregatorContract;
|
|
10
|
+
constructor(pairInfosContract: Contract, priceAggregatorContract: Contract);
|
|
11
|
+
/**
|
|
12
|
+
* Get loss protection tier for a trade
|
|
13
|
+
* @param trade - Trade information
|
|
14
|
+
* @param isPnl - Whether calculating for PnL
|
|
15
|
+
* @returns Loss protection tier
|
|
16
|
+
*/
|
|
17
|
+
getLossProtectionTier(trade: Trade, isPnl?: boolean): Promise<number>;
|
|
18
|
+
/**
|
|
19
|
+
* Get price impact spread for a position
|
|
20
|
+
* @param pairIndex - Trading pair index
|
|
21
|
+
* @param isLong - True for long position
|
|
22
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
23
|
+
* @param isOpen - Whether opening or closing
|
|
24
|
+
* @returns Price impact spread percentage
|
|
25
|
+
*/
|
|
26
|
+
getPriceImpactSpread(pairIndex: number, isLong: boolean, positionSizeUsdc: number, isOpen: boolean): Promise<number>;
|
|
27
|
+
/**
|
|
28
|
+
* Get skew impact spread for a position
|
|
29
|
+
* @param pairIndex - Trading pair index
|
|
30
|
+
* @param isLong - True for long position
|
|
31
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
32
|
+
* @param isOpen - Whether opening or closing
|
|
33
|
+
* @returns Skew impact spread percentage
|
|
34
|
+
*/
|
|
35
|
+
getSkewImpactSpread(pairIndex: number, isLong: boolean, positionSizeUsdc: number, isOpen: boolean): Promise<number>;
|
|
36
|
+
/**
|
|
37
|
+
* Get price impact percentage for a position
|
|
38
|
+
* @param pairIndex - Trading pair index
|
|
39
|
+
* @param isLong - True for long position
|
|
40
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
41
|
+
* @returns Price impact percentage
|
|
42
|
+
*/
|
|
43
|
+
getPriceImpactP(pairIndex: number, isLong: boolean, positionSizeUsdc: number): Promise<number>;
|
|
44
|
+
/**
|
|
45
|
+
* Get opening fee in USDC for a position
|
|
46
|
+
* @param pairIndex - Trading pair index
|
|
47
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
48
|
+
* @param isLong - True for long position
|
|
49
|
+
* @returns Opening fee in USDC
|
|
50
|
+
*/
|
|
51
|
+
getOpenFeeUsdc(pairIndex: number, positionSizeUsdc: number, isLong: boolean): Promise<number>;
|
|
52
|
+
/**
|
|
53
|
+
* Get opening fee percentage for a position (from PriceAggregator)
|
|
54
|
+
* @param pairIndex - Trading pair index
|
|
55
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
56
|
+
* @param isLong - True for long position
|
|
57
|
+
* @returns Opening fee percentage (12 decimals)
|
|
58
|
+
*/
|
|
59
|
+
getOpenFeeP(pairIndex: number, positionSizeUsdc: number, isLong: boolean): Promise<number>;
|
|
60
|
+
/**
|
|
61
|
+
* Get pair margin fee percentage
|
|
62
|
+
* @param pairIndex - Trading pair index
|
|
63
|
+
* @returns Margin fee percentage
|
|
64
|
+
*/
|
|
65
|
+
getPairMarginFeeP(pairIndex: number): Promise<number>;
|
|
66
|
+
/**
|
|
67
|
+
* Get loss protection tier for a pair and position size
|
|
68
|
+
* @param pairIndex - Trading pair index
|
|
69
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
70
|
+
* @returns Loss protection tier
|
|
71
|
+
*/
|
|
72
|
+
getLossProtectionTierForSize(pairIndex: number, positionSizeUsdc: number): Promise<number>;
|
|
73
|
+
/**
|
|
74
|
+
* Get loss protection percentage for a pair and tier
|
|
75
|
+
* @param pairIndex - Trading pair index
|
|
76
|
+
* @param tier - Loss protection tier
|
|
77
|
+
* @returns Loss protection percentage
|
|
78
|
+
*/
|
|
79
|
+
getLossProtectionP(pairIndex: number, tier: number): Promise<number>;
|
|
80
|
+
/**
|
|
81
|
+
* Get 1% depth above (for longs) in USDC
|
|
82
|
+
* @param pairIndex - Trading pair index
|
|
83
|
+
* @returns Depth in USDC
|
|
84
|
+
*/
|
|
85
|
+
getOnePercentDepthAboveUsdc(pairIndex: number): Promise<number>;
|
|
86
|
+
/**
|
|
87
|
+
* Get 1% depth below (for shorts) in USDC
|
|
88
|
+
* @param pairIndex - Trading pair index
|
|
89
|
+
* @returns Depth in USDC
|
|
90
|
+
*/
|
|
91
|
+
getOnePercentDepthBelowUsdc(pairIndex: number): Promise<number>;
|
|
92
|
+
/**
|
|
93
|
+
* Get both depth values for a pair
|
|
94
|
+
* @param pairIndex - Trading pair index
|
|
95
|
+
* @returns Object with depth above and below
|
|
96
|
+
*/
|
|
97
|
+
getDepth(pairIndex: number): Promise<{
|
|
98
|
+
above: number;
|
|
99
|
+
below: number;
|
|
100
|
+
}>;
|
|
101
|
+
}
|
|
@@ -0,0 +1,161 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.PairInfoQueriesRPC = void 0;
|
|
4
|
+
const types_1 = require("../types");
|
|
5
|
+
/**
|
|
6
|
+
* PairInfo Queries RPC
|
|
7
|
+
* Handles querying pair-related information like price impact, skew, fees, and loss protection
|
|
8
|
+
*/
|
|
9
|
+
class PairInfoQueriesRPC {
|
|
10
|
+
constructor(pairInfosContract, priceAggregatorContract) {
|
|
11
|
+
this.pairInfosContract = pairInfosContract;
|
|
12
|
+
this.priceAggregatorContract = priceAggregatorContract;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* Get loss protection tier for a trade
|
|
16
|
+
* @param trade - Trade information
|
|
17
|
+
* @param isPnl - Whether calculating for PnL
|
|
18
|
+
* @returns Loss protection tier
|
|
19
|
+
*/
|
|
20
|
+
async getLossProtectionTier(trade, isPnl = false) {
|
|
21
|
+
// Convert trade to blockchain format
|
|
22
|
+
const tradeStruct = {
|
|
23
|
+
trader: trade.trader,
|
|
24
|
+
pairIndex: trade.pairIndex,
|
|
25
|
+
index: trade.index,
|
|
26
|
+
initialPosToken: (0, types_1.toBlockchain6)(trade.initialPosToken),
|
|
27
|
+
positionSizeUSDC: (0, types_1.toBlockchain6)(trade.positionSizeUSDC),
|
|
28
|
+
openPrice: (0, types_1.toBlockchain10)(trade.openPrice),
|
|
29
|
+
buy: trade.buy,
|
|
30
|
+
leverage: (0, types_1.toBlockchain10)(trade.leverage),
|
|
31
|
+
tp: (0, types_1.toBlockchain10)(trade.tp),
|
|
32
|
+
sl: (0, types_1.toBlockchain10)(trade.sl),
|
|
33
|
+
timestamp: trade.timestamp,
|
|
34
|
+
};
|
|
35
|
+
const tier = await this.pairInfosContract.lossProtectionTier(tradeStruct, isPnl);
|
|
36
|
+
return Number(tier);
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Get price impact spread for a position
|
|
40
|
+
* @param pairIndex - Trading pair index
|
|
41
|
+
* @param isLong - True for long position
|
|
42
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
43
|
+
* @param isOpen - Whether opening or closing
|
|
44
|
+
* @returns Price impact spread percentage
|
|
45
|
+
*/
|
|
46
|
+
async getPriceImpactSpread(pairIndex, isLong, positionSizeUsdc, isOpen) {
|
|
47
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
48
|
+
const spread = await this.pairInfosContract.getPriceImpactSpread(pairIndex, isLong, positionSize, isOpen);
|
|
49
|
+
return (0, types_1.fromBlockchain10)(spread);
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Get skew impact spread for a position
|
|
53
|
+
* @param pairIndex - Trading pair index
|
|
54
|
+
* @param isLong - True for long position
|
|
55
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
56
|
+
* @param isOpen - Whether opening or closing
|
|
57
|
+
* @returns Skew impact spread percentage
|
|
58
|
+
*/
|
|
59
|
+
async getSkewImpactSpread(pairIndex, isLong, positionSizeUsdc, isOpen) {
|
|
60
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
61
|
+
const spread = await this.pairInfosContract.getSkewImpactSpread(pairIndex, isLong, positionSize, isOpen);
|
|
62
|
+
return (0, types_1.fromBlockchain10)(spread);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Get price impact percentage for a position
|
|
66
|
+
* @param pairIndex - Trading pair index
|
|
67
|
+
* @param isLong - True for long position
|
|
68
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
69
|
+
* @returns Price impact percentage
|
|
70
|
+
*/
|
|
71
|
+
async getPriceImpactP(pairIndex, isLong, positionSizeUsdc) {
|
|
72
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
73
|
+
const impact = await this.pairInfosContract.getPriceImpactP(pairIndex, isLong, positionSize);
|
|
74
|
+
return (0, types_1.fromBlockchain10)(impact);
|
|
75
|
+
}
|
|
76
|
+
/**
|
|
77
|
+
* Get opening fee in USDC for a position
|
|
78
|
+
* @param pairIndex - Trading pair index
|
|
79
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
80
|
+
* @param isLong - True for long position
|
|
81
|
+
* @returns Opening fee in USDC
|
|
82
|
+
*/
|
|
83
|
+
async getOpenFeeUsdc(pairIndex, positionSizeUsdc, isLong) {
|
|
84
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
85
|
+
const fee = await this.pairInfosContract.getOpenFeeUsdc(pairIndex, positionSize, isLong);
|
|
86
|
+
return (0, types_1.fromBlockchain6)(fee);
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* Get opening fee percentage for a position (from PriceAggregator)
|
|
90
|
+
* @param pairIndex - Trading pair index
|
|
91
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
92
|
+
* @param isLong - True for long position
|
|
93
|
+
* @returns Opening fee percentage (12 decimals)
|
|
94
|
+
*/
|
|
95
|
+
async getOpenFeeP(pairIndex, positionSizeUsdc, isLong) {
|
|
96
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
97
|
+
const fee = await this.priceAggregatorContract.openFeeP(pairIndex, positionSize, isLong);
|
|
98
|
+
return (0, types_1.fromBlockchain12)(fee);
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Get pair margin fee percentage
|
|
102
|
+
* @param pairIndex - Trading pair index
|
|
103
|
+
* @returns Margin fee percentage
|
|
104
|
+
*/
|
|
105
|
+
async getPairMarginFeeP(pairIndex) {
|
|
106
|
+
const fee = await this.pairInfosContract.getPairMarginFeeP(pairIndex);
|
|
107
|
+
return (0, types_1.fromBlockchain10)(fee);
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Get loss protection tier for a pair and position size
|
|
111
|
+
* @param pairIndex - Trading pair index
|
|
112
|
+
* @param positionSizeUsdc - Position size in USDC
|
|
113
|
+
* @returns Loss protection tier
|
|
114
|
+
*/
|
|
115
|
+
async getLossProtectionTierForSize(pairIndex, positionSizeUsdc) {
|
|
116
|
+
const positionSize = (0, types_1.toBlockchain6)(positionSizeUsdc);
|
|
117
|
+
const tier = await this.pairInfosContract.getLossProtectionTier(pairIndex, positionSize);
|
|
118
|
+
return Number(tier);
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Get loss protection percentage for a pair and tier
|
|
122
|
+
* @param pairIndex - Trading pair index
|
|
123
|
+
* @param tier - Loss protection tier
|
|
124
|
+
* @returns Loss protection percentage
|
|
125
|
+
*/
|
|
126
|
+
async getLossProtectionP(pairIndex, tier) {
|
|
127
|
+
const protection = await this.pairInfosContract.getLossProtectionP(pairIndex, tier);
|
|
128
|
+
return (0, types_1.fromBlockchain10)(protection);
|
|
129
|
+
}
|
|
130
|
+
/**
|
|
131
|
+
* Get 1% depth above (for longs) in USDC
|
|
132
|
+
* @param pairIndex - Trading pair index
|
|
133
|
+
* @returns Depth in USDC
|
|
134
|
+
*/
|
|
135
|
+
async getOnePercentDepthAboveUsdc(pairIndex) {
|
|
136
|
+
const depth = await this.pairInfosContract.onePercentDepthAboveUsdc(pairIndex);
|
|
137
|
+
return (0, types_1.fromBlockchain6)(depth);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Get 1% depth below (for shorts) in USDC
|
|
141
|
+
* @param pairIndex - Trading pair index
|
|
142
|
+
* @returns Depth in USDC
|
|
143
|
+
*/
|
|
144
|
+
async getOnePercentDepthBelowUsdc(pairIndex) {
|
|
145
|
+
const depth = await this.pairInfosContract.onePercentDepthBelowUsdc(pairIndex);
|
|
146
|
+
return (0, types_1.fromBlockchain6)(depth);
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Get both depth values for a pair
|
|
150
|
+
* @param pairIndex - Trading pair index
|
|
151
|
+
* @returns Object with depth above and below
|
|
152
|
+
*/
|
|
153
|
+
async getDepth(pairIndex) {
|
|
154
|
+
const [above, below] = await Promise.all([
|
|
155
|
+
this.getOnePercentDepthAboveUsdc(pairIndex),
|
|
156
|
+
this.getOnePercentDepthBelowUsdc(pairIndex),
|
|
157
|
+
]);
|
|
158
|
+
return { above, below };
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
exports.PairInfoQueriesRPC = PairInfoQueriesRPC;
|
|
@@ -0,0 +1,62 @@
|
|
|
1
|
+
import { Contract, Provider } from 'ethers';
|
|
2
|
+
import { ContractPairInfo, PairInfo, PairsBackendReturn } from '../types';
|
|
3
|
+
/**
|
|
4
|
+
* RPC module for caching and managing trading pair information
|
|
5
|
+
*/
|
|
6
|
+
export declare class PairsCache {
|
|
7
|
+
private provider;
|
|
8
|
+
private pairStorageContract;
|
|
9
|
+
private pairsCache?;
|
|
10
|
+
private pairNameToIndexMap?;
|
|
11
|
+
constructor(provider: Provider, pairStorageContract: Contract);
|
|
12
|
+
/**
|
|
13
|
+
* Get all trading pairs from blockchain (with caching)
|
|
14
|
+
* @param forceRefresh - Force refresh from blockchain
|
|
15
|
+
* @returns Map of pair index to PairInfo
|
|
16
|
+
*/
|
|
17
|
+
getPairsInfo(forceRefresh?: boolean): Promise<Map<number, PairInfo>>;
|
|
18
|
+
getPairBackend(index: number): Promise<PairsBackendReturn>;
|
|
19
|
+
getPairInfoNameFromIndex: (index: number) => Promise<{
|
|
20
|
+
from: string;
|
|
21
|
+
to: string;
|
|
22
|
+
}>;
|
|
23
|
+
getOtherPairInfoFromIndex: (index: number) => Promise<ContractPairInfo>;
|
|
24
|
+
/**
|
|
25
|
+
* Get pair information from socket API
|
|
26
|
+
* @returns Pair information from API
|
|
27
|
+
*/
|
|
28
|
+
getPairInfoFromSocket(): Promise<any>;
|
|
29
|
+
/**
|
|
30
|
+
* Get pair index from pair name
|
|
31
|
+
* @param pairName - Pair name (e.g., "BTC/USD")
|
|
32
|
+
* @returns Pair index or undefined if not found
|
|
33
|
+
*/
|
|
34
|
+
getPairIndex(pairName: string): Promise<number | undefined>;
|
|
35
|
+
/**
|
|
36
|
+
* Get pair name from index
|
|
37
|
+
* @param pairIndex - Pair index
|
|
38
|
+
* @returns Pair name or undefined if not found
|
|
39
|
+
*/
|
|
40
|
+
getPairName(pairIndex: number): Promise<string | undefined>;
|
|
41
|
+
/**
|
|
42
|
+
* Get all unique group indexes
|
|
43
|
+
* @returns Array of group indexes
|
|
44
|
+
*/
|
|
45
|
+
getGroupIndexes(): Promise<number[]>;
|
|
46
|
+
/**
|
|
47
|
+
* Get all pairs in a specific group
|
|
48
|
+
* @param groupIndex - Group index
|
|
49
|
+
* @returns Array of pair indexes in the group
|
|
50
|
+
*/
|
|
51
|
+
getPairsInGroup(groupIndex: number): Promise<number[]>;
|
|
52
|
+
/**
|
|
53
|
+
* Get pair info by index
|
|
54
|
+
* @param pairIndex - Pair index
|
|
55
|
+
* @returns PairInfo or undefined
|
|
56
|
+
*/
|
|
57
|
+
getPairByIndex(pairIndex: number): Promise<PairInfo | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* Clear the cache
|
|
60
|
+
*/
|
|
61
|
+
clearCache(): void;
|
|
62
|
+
}
|