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
package/src/utils.ts
ADDED
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
import { Interface, Result } from 'ethers';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Utility functions for Web3 decoding and data processing
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Check if ABI type is a tuple
|
|
9
|
+
*/
|
|
10
|
+
export function isTupleType(abiType: string): boolean {
|
|
11
|
+
return abiType.startsWith('tuple');
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* Check if ABI type is an array
|
|
16
|
+
*/
|
|
17
|
+
export function isArrayType(abiType: string): boolean {
|
|
18
|
+
return abiType.endsWith('[]');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Process ABI outputs and convert Result to object with named fields
|
|
23
|
+
*/
|
|
24
|
+
export function processOutputTypes(
|
|
25
|
+
outputs: readonly any[],
|
|
26
|
+
decoded: Result
|
|
27
|
+
): Record<string, any> {
|
|
28
|
+
const result: Record<string, any> = {};
|
|
29
|
+
|
|
30
|
+
outputs.forEach((output, index) => {
|
|
31
|
+
const name = output.name || `output${index}`;
|
|
32
|
+
let value = decoded[index];
|
|
33
|
+
|
|
34
|
+
// Handle tuple types
|
|
35
|
+
if (isTupleType(output.type) && output.components) {
|
|
36
|
+
value = processOutputTypes(output.components, value as Result);
|
|
37
|
+
}
|
|
38
|
+
// Handle array of tuples
|
|
39
|
+
else if (isArrayType(output.type) && output.baseType === 'tuple' && output.components) {
|
|
40
|
+
value = (value as any[]).map((item) =>
|
|
41
|
+
processOutputTypes(output.components, item as Result)
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
result[name] = value;
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* Decode contract function output using ABI
|
|
53
|
+
* @param abi - Contract ABI
|
|
54
|
+
* @param functionName - Function name to decode
|
|
55
|
+
* @param output - Raw output data
|
|
56
|
+
* @returns Decoded output as object with named fields
|
|
57
|
+
*/
|
|
58
|
+
export function decoder(
|
|
59
|
+
abi: any[],
|
|
60
|
+
functionName: string,
|
|
61
|
+
output: any
|
|
62
|
+
): Record<string, any> | null {
|
|
63
|
+
try {
|
|
64
|
+
const iface = new Interface(abi);
|
|
65
|
+
const fragment = iface.getFunction(functionName);
|
|
66
|
+
|
|
67
|
+
if (!fragment) {
|
|
68
|
+
throw new Error(`Function ${functionName} not found in ABI`);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
// If output is already decoded (Result type), process it
|
|
72
|
+
if (typeof output === 'object' && !Array.isArray(output) && output.toArray) {
|
|
73
|
+
return processOutputTypes(fragment.outputs, output);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// Decode the output
|
|
77
|
+
const decoded = iface.decodeFunctionResult(fragment, output);
|
|
78
|
+
|
|
79
|
+
// Process and return with named fields
|
|
80
|
+
return processOutputTypes(fragment.outputs, decoded);
|
|
81
|
+
} catch (error) {
|
|
82
|
+
console.error(`Error decoding function ${functionName}:`, error);
|
|
83
|
+
return null;
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
/**
|
|
88
|
+
* Convert hex string to number
|
|
89
|
+
*/
|
|
90
|
+
export function hexToNumber(hex: string): number {
|
|
91
|
+
return parseInt(hex, 16);
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* Convert number to hex string
|
|
96
|
+
*/
|
|
97
|
+
export function numberToHex(num: number): string {
|
|
98
|
+
return '0x' + num.toString(16);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Validate Ethereum address
|
|
103
|
+
*/
|
|
104
|
+
export function isValidAddress(address: string): boolean {
|
|
105
|
+
return /^0x[a-fA-F0-9]{40}$/.test(address);
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
/**
|
|
109
|
+
* Format address to checksummed version
|
|
110
|
+
*/
|
|
111
|
+
export function toChecksumAddress(address: string): string {
|
|
112
|
+
if (!isValidAddress(address)) {
|
|
113
|
+
throw new Error('Invalid Ethereum address');
|
|
114
|
+
}
|
|
115
|
+
// ethers.js getAddress() returns checksummed address
|
|
116
|
+
return address;
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Sleep for specified milliseconds
|
|
121
|
+
*/
|
|
122
|
+
export function sleep(ms: number): Promise<void> {
|
|
123
|
+
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Retry function with exponential backoff
|
|
128
|
+
* @param fn - Async function to retry
|
|
129
|
+
* @param maxRetries - Maximum number of retries
|
|
130
|
+
* @param baseDelay - Base delay in milliseconds (default: 1000)
|
|
131
|
+
* @returns Result of the function
|
|
132
|
+
*/
|
|
133
|
+
export async function retryWithBackoff<T>(
|
|
134
|
+
fn: () => Promise<T>,
|
|
135
|
+
maxRetries: number = 3,
|
|
136
|
+
baseDelay: number = 1000
|
|
137
|
+
): Promise<T> {
|
|
138
|
+
let lastError: Error;
|
|
139
|
+
|
|
140
|
+
for (let i = 0; i <= maxRetries; i++) {
|
|
141
|
+
try {
|
|
142
|
+
return await fn();
|
|
143
|
+
} catch (error) {
|
|
144
|
+
lastError = error as Error;
|
|
145
|
+
|
|
146
|
+
if (i < maxRetries) {
|
|
147
|
+
const delay = baseDelay * Math.pow(2, i);
|
|
148
|
+
console.log(`Retry ${i + 1}/${maxRetries} after ${delay}ms...`);
|
|
149
|
+
await sleep(delay);
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
throw lastError!;
|
|
155
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
{
|
|
2
|
+
"compilerOptions": {
|
|
3
|
+
"target": "ES2020",
|
|
4
|
+
"module": "commonjs",
|
|
5
|
+
"lib": ["ES2020"],
|
|
6
|
+
"declaration": true,
|
|
7
|
+
"outDir": "./dist",
|
|
8
|
+
"rootDir": "./src",
|
|
9
|
+
"strict": true,
|
|
10
|
+
"esModuleInterop": true,
|
|
11
|
+
"skipLibCheck": true,
|
|
12
|
+
"forceConsistentCasingInFileNames": true,
|
|
13
|
+
"resolveJsonModule": true,
|
|
14
|
+
"moduleResolution": "node"
|
|
15
|
+
},
|
|
16
|
+
"include": ["src/**/*"],
|
|
17
|
+
"exclude": ["node_modules", "dist", "**/*.test.ts"]
|
|
18
|
+
}
|