@gvnrdao/dh-sdk 0.0.172 → 0.0.206
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/LICENSE +21 -0
- package/README.md +86 -5
- package/browser/dist/index.d.ts +3 -4
- package/browser/dist/index.d.ts.map +1 -1
- package/browser/dist/index.js +4 -3
- package/dist/constants/chunks/contract-abis.d.ts +7 -0
- package/dist/constants/chunks/deployment-addresses.d.ts +68 -0
- package/dist/constants/chunks/encrypted-provider-params.d.ts +21 -0
- package/dist/constants/chunks/environment.browser.d.ts +45 -0
- package/dist/constants/chunks/environment.d.ts +57 -0
- package/dist/constants/chunks/network-configs.d.ts +65 -0
- package/dist/constants/chunks/sdk-config.d.ts +33 -0
- package/dist/constants/chunks/sdk-limits.d.ts +66 -0
- package/dist/constants/index.d.ts +15 -0
- package/dist/graphs/client.d.ts +19 -0
- package/dist/graphs/diamond-hands.d.ts +248 -0
- package/dist/index.d.ts +47 -7319
- package/dist/index.js +4439 -16783
- package/dist/index.mjs +4452 -16773
- package/dist/interfaces/chunks/btc.i.d.ts +36 -0
- package/dist/interfaces/chunks/config.i.d.ts +245 -0
- package/dist/interfaces/chunks/contract-interactions.i.d.ts +64 -0
- package/dist/interfaces/chunks/contract-types.i.d.ts +165 -0
- package/dist/interfaces/chunks/lit-actions-results.i.d.ts +165 -0
- package/dist/interfaces/chunks/lit-actions.i.d.ts +98 -0
- package/dist/interfaces/chunks/loan-operations.i.d.ts +332 -0
- package/dist/interfaces/chunks/pkp-integration.i.d.ts +87 -0
- package/dist/interfaces/chunks/position-query.i.d.ts +76 -0
- package/dist/interfaces/chunks/requests.i.d.ts +55 -0
- package/dist/interfaces/chunks/ucd-minting.i.d.ts +34 -0
- package/dist/interfaces/chunks/utility.i.d.ts +64 -0
- package/dist/interfaces/index.d.ts +17 -0
- package/dist/modules/bitcoin/bitcoin-operations.module.d.ts +215 -0
- package/dist/modules/cache/cache-manager.module.d.ts +251 -0
- package/dist/modules/contract/contract-manager.module.d.ts +136 -0
- package/dist/modules/diamond-hands-sdk.d.ts +666 -0
- package/dist/modules/loan/loan-creator.module.d.ts +143 -0
- package/dist/modules/loan/loan-query.module.d.ts +204 -0
- package/dist/modules/mock/mock-token-manager.module.d.ts +83 -0
- package/dist/modules/pkp/pkp-manager.module.d.ts +132 -0
- package/dist/protocol/protocol-pause.d.ts +19 -0
- package/dist/server.d.ts +17 -0
- package/dist/server.js +285 -0
- package/dist/server.mjs +242 -0
- package/dist/types/authorization-params.d.ts +160 -0
- package/dist/types/branded/domain-values.d.ts +138 -0
- package/dist/types/branded/ids.d.ts +23 -0
- package/dist/types/event-types.d.ts +235 -0
- package/dist/types/graph-dtos.d.ts +228 -0
- package/dist/types/loanStatus.d.ts +10 -0
- package/dist/types/result.d.ts +120 -0
- package/dist/utils/bitcoin-address-cache.utils.d.ts +87 -0
- package/dist/utils/bitcoin-provider.utils.d.ts +48 -0
- package/dist/utils/bitcoin-signature.d.ts +20 -0
- package/dist/utils/chunks/bitcoin-utils.d.ts +75 -0
- package/dist/utils/chunks/eip1559-broadcast.utils.d.ts +24 -0
- package/dist/utils/error-handler.d.ts +106 -0
- package/dist/utils/ethers-interop.utils.d.ts +146 -0
- package/dist/utils/extend-authorization.utils.d.ts +61 -0
- package/dist/utils/lit-signature.utils.d.ts +6 -0
- package/dist/utils/logger.utils.d.ts +142 -0
- package/dist/utils/mint-authorization.utils.d.ts +231 -0
- package/dist/utils/quantum-timing.d.ts +75 -0
- package/dist/utils/signature-tempering.utils.d.ts +31 -0
- package/dist/utils/telegram-messaging.utils.d.ts +188 -0
- package/package.json +43 -23
- package/browser/dist/20.browser.js +0 -1
- package/browser/dist/397.browser.js +0 -2
- package/browser/dist/397.browser.js.LICENSE.txt +0 -1
- package/browser/dist/833.browser.js +0 -2
- package/browser/dist/833.browser.js.LICENSE.txt +0 -1
- package/browser/dist/browser.js +0 -14
- package/browser/dist/browser.js.LICENSE.txt +0 -90
- package/dist/index.d.mts +0 -7320
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Utility Type Interfaces
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Generic Result Interface
|
|
6
|
+
*/
|
|
7
|
+
export interface Result<T = any> {
|
|
8
|
+
success: boolean;
|
|
9
|
+
error?: string;
|
|
10
|
+
data?: T;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Pagination Interface
|
|
14
|
+
*/
|
|
15
|
+
export interface Pagination {
|
|
16
|
+
page: number;
|
|
17
|
+
limit: number;
|
|
18
|
+
total?: number;
|
|
19
|
+
hasNext?: boolean;
|
|
20
|
+
hasPrev?: boolean;
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Paginated Result Interface
|
|
24
|
+
*/
|
|
25
|
+
export interface PaginatedResult<T = any> extends Result<T[]> {
|
|
26
|
+
pagination?: Pagination;
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* Audit Trail Interface
|
|
30
|
+
*/
|
|
31
|
+
export interface AuditTrail {
|
|
32
|
+
operation: string;
|
|
33
|
+
timestamp: number;
|
|
34
|
+
duration?: number;
|
|
35
|
+
success: boolean;
|
|
36
|
+
error?: string;
|
|
37
|
+
metadata?: Record<string, any>;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Debug Information Interface
|
|
41
|
+
*/
|
|
42
|
+
export interface DebugInfo {
|
|
43
|
+
logs: string[];
|
|
44
|
+
metrics?: {
|
|
45
|
+
startTime: number;
|
|
46
|
+
endTime: number;
|
|
47
|
+
duration: number;
|
|
48
|
+
};
|
|
49
|
+
networkCalls?: {
|
|
50
|
+
url: string;
|
|
51
|
+
method: string;
|
|
52
|
+
duration: number;
|
|
53
|
+
success: boolean;
|
|
54
|
+
}[];
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Retry Configuration Interface
|
|
58
|
+
*/
|
|
59
|
+
export interface RetryConfig {
|
|
60
|
+
maxAttempts: number;
|
|
61
|
+
delay: number;
|
|
62
|
+
backoffMultiplier?: number;
|
|
63
|
+
maxDelay?: number;
|
|
64
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diamond Hands SDK Interfaces
|
|
3
|
+
* All TypeScript interfaces and types used in the SDK package
|
|
4
|
+
*
|
|
5
|
+
* Following the golden source pattern with organized chunks
|
|
6
|
+
*/
|
|
7
|
+
export * from "./chunks/config.i";
|
|
8
|
+
export * from "./chunks/contract-types.i";
|
|
9
|
+
export * from "./chunks/loan-operations.i";
|
|
10
|
+
export * from "./chunks/pkp-integration.i";
|
|
11
|
+
export * from "./chunks/contract-interactions.i";
|
|
12
|
+
export * from "./chunks/utility.i";
|
|
13
|
+
export * from "./chunks/btc.i";
|
|
14
|
+
export * from "./chunks/lit-actions.i";
|
|
15
|
+
export * from "./chunks/ucd-minting.i";
|
|
16
|
+
export * from "./chunks/requests.i";
|
|
17
|
+
export * from "./chunks/position-query.i";
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bitcoin Operations Module
|
|
3
|
+
*
|
|
4
|
+
* Responsible for:
|
|
5
|
+
* - Deriving Bitcoin addresses from PKP public keys
|
|
6
|
+
* - Querying Bitcoin balances (with multi-provider consensus)
|
|
7
|
+
* - Validating Bitcoin transactions
|
|
8
|
+
* - Caching Bitcoin data for performance
|
|
9
|
+
*
|
|
10
|
+
* Uses branded types (Satoshis) for type-safe Bitcoin amounts
|
|
11
|
+
*/
|
|
12
|
+
import { Result } from "../../types/result";
|
|
13
|
+
import { Satoshis } from "../../types/branded/domain-values";
|
|
14
|
+
import { SDKError } from "../../utils/error-handler";
|
|
15
|
+
import type { Cache } from "../cache/cache-manager.module";
|
|
16
|
+
import type { BitcoinAddresses } from "../../interfaces/chunks/loan-operations.i";
|
|
17
|
+
import type { BitcoinBalanceResult, BitcoinUTXO } from "../../interfaces/chunks/position-query.i";
|
|
18
|
+
import type { BitcoinProviderConfig, PriceProviderConfig } from "../../interfaces/chunks/config.i";
|
|
19
|
+
/**
|
|
20
|
+
* Bitcoin network type
|
|
21
|
+
*/
|
|
22
|
+
export type BitcoinNetwork = "mainnet" | "testnet" | "regtest";
|
|
23
|
+
/**
|
|
24
|
+
* Bitcoin Operations configuration
|
|
25
|
+
*/
|
|
26
|
+
export interface BitcoinOperationsConfig {
|
|
27
|
+
/** Bitcoin network to use */
|
|
28
|
+
network: BitcoinNetwork;
|
|
29
|
+
/** Bitcoin provider configurations (supports 1-3 for consensus) */
|
|
30
|
+
providers: BitcoinProviderConfig[];
|
|
31
|
+
/** Consensus mode: single or majority */
|
|
32
|
+
consensusMode?: "single" | "majority";
|
|
33
|
+
/** Cache for balance results (keyed by address) */
|
|
34
|
+
balanceCache?: Cache<BitcoinBalanceResult>;
|
|
35
|
+
/** Cache for address derivations (keyed by PKP public key) */
|
|
36
|
+
addressCache?: Cache<BitcoinAddresses>;
|
|
37
|
+
/** LitOps instance for price oracle queries (optional) */
|
|
38
|
+
litOps?: any;
|
|
39
|
+
/** Signer for LIT Protocol operations (required for getBTCPrice) */
|
|
40
|
+
signer?: any;
|
|
41
|
+
/** Optional PKP Ethereum address for signed price oracle (Lit jsParams pkpId) */
|
|
42
|
+
priceOraclePkpId?: string;
|
|
43
|
+
/** Price feed API credentials passed to the price-oracle Lit Action (needs 3 sources for getBTCPrice) */
|
|
44
|
+
priceProviders?: PriceProviderConfig[];
|
|
45
|
+
/** Enable debug logging */
|
|
46
|
+
debug?: boolean;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Bitcoin balance with enhanced metadata
|
|
50
|
+
*/
|
|
51
|
+
export interface EnrichedBitcoinBalance {
|
|
52
|
+
/** Bitcoin address */
|
|
53
|
+
address: string;
|
|
54
|
+
/** Balance in satoshis (branded type) */
|
|
55
|
+
balance: Satoshis;
|
|
56
|
+
/** UTXOs for this address */
|
|
57
|
+
utxos: BitcoinUTXO[];
|
|
58
|
+
/** Minimum confirmations across UTXOs */
|
|
59
|
+
confirmations: number;
|
|
60
|
+
/** Provider name that returned this data */
|
|
61
|
+
provider: string;
|
|
62
|
+
/** Timestamp when fetched */
|
|
63
|
+
timestamp: number;
|
|
64
|
+
/** Whether this data came from cache */
|
|
65
|
+
cached: boolean;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Bitcoin price result from LIT Protocol price oracle
|
|
69
|
+
*/
|
|
70
|
+
export interface BTCPriceResult {
|
|
71
|
+
/** Price with 8 decimals (e.g., "10679200000000" for $106,792) */
|
|
72
|
+
priceE8: string;
|
|
73
|
+
/** Price in USD as a number */
|
|
74
|
+
price: number;
|
|
75
|
+
/** Unix timestamp when price was fetched */
|
|
76
|
+
timestamp: number;
|
|
77
|
+
/** Source: "fast" (single source), "consensus" (multiple sources), or "fast-fallback" */
|
|
78
|
+
source: string;
|
|
79
|
+
/** Optional PKP signature on the price data */
|
|
80
|
+
signature?: string;
|
|
81
|
+
/** Optional signer address */
|
|
82
|
+
signer?: string;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Bitcoin Operations Module
|
|
86
|
+
*
|
|
87
|
+
* Handles all Bitcoin-related operations with type safety and caching
|
|
88
|
+
*/
|
|
89
|
+
export declare class BitcoinOperations {
|
|
90
|
+
private readonly config;
|
|
91
|
+
private readonly provider;
|
|
92
|
+
private readonly litOps?;
|
|
93
|
+
constructor(config: BitcoinOperationsConfig);
|
|
94
|
+
/**
|
|
95
|
+
* Derive Bitcoin addresses from PKP public key
|
|
96
|
+
*
|
|
97
|
+
* Generates addresses for all networks (mainnet, testnet, regtest)
|
|
98
|
+
* and caches the result.
|
|
99
|
+
*
|
|
100
|
+
* @param publicKey - PKP public key (hex string)
|
|
101
|
+
* @returns Bitcoin addresses for all networks
|
|
102
|
+
*/
|
|
103
|
+
deriveAddresses(publicKey: string): Promise<Result<BitcoinAddresses, SDKError>>;
|
|
104
|
+
/**
|
|
105
|
+
* Get Bitcoin address for current network
|
|
106
|
+
*
|
|
107
|
+
* Convenience method that returns only the address for the configured network.
|
|
108
|
+
*
|
|
109
|
+
* @param publicKey - PKP public key
|
|
110
|
+
* @returns Bitcoin address for current network
|
|
111
|
+
*/
|
|
112
|
+
getAddressForNetwork(publicKey: string): Promise<Result<string, SDKError>>;
|
|
113
|
+
/**
|
|
114
|
+
* Get Bitcoin balance for an address
|
|
115
|
+
*
|
|
116
|
+
* Queries balance using configured providers with consensus if enabled.
|
|
117
|
+
* Results are cached for performance.
|
|
118
|
+
*
|
|
119
|
+
* @param address - Bitcoin address
|
|
120
|
+
* @param skipCache - Skip cache and force fresh query
|
|
121
|
+
* @returns Enriched balance information
|
|
122
|
+
*/
|
|
123
|
+
getBalance(address: string, skipCache?: boolean): Promise<Result<EnrichedBitcoinBalance, SDKError>>;
|
|
124
|
+
/**
|
|
125
|
+
* Get balance with consensus across multiple providers
|
|
126
|
+
*
|
|
127
|
+
* Only available if multiple providers are configured.
|
|
128
|
+
*
|
|
129
|
+
* @param address - Bitcoin address
|
|
130
|
+
* @returns Consensus balance result
|
|
131
|
+
*/
|
|
132
|
+
getBalanceWithConsensus(address: string): Promise<Result<EnrichedBitcoinBalance, SDKError>>;
|
|
133
|
+
/**
|
|
134
|
+
* Get balances for multiple addresses
|
|
135
|
+
*
|
|
136
|
+
* Efficiently queries balances for multiple addresses in parallel.
|
|
137
|
+
*
|
|
138
|
+
* @param addresses - Array of Bitcoin addresses
|
|
139
|
+
* @returns Array of balance results
|
|
140
|
+
*/
|
|
141
|
+
getBalances(addresses: string[]): Promise<Result<EnrichedBitcoinBalance[], SDKError>>;
|
|
142
|
+
/**
|
|
143
|
+
* Get balance for a PKP public key
|
|
144
|
+
*
|
|
145
|
+
* Convenience method that derives address and queries balance.
|
|
146
|
+
*
|
|
147
|
+
* @param publicKey - PKP public key
|
|
148
|
+
* @returns Balance for the PKP's Bitcoin address
|
|
149
|
+
*/
|
|
150
|
+
getBalanceForPKP(publicKey: string): Promise<Result<EnrichedBitcoinBalance, SDKError>>;
|
|
151
|
+
/**
|
|
152
|
+
* Validate a Bitcoin address
|
|
153
|
+
*
|
|
154
|
+
* Checks if an address has a valid format.
|
|
155
|
+
*
|
|
156
|
+
* @param address - Bitcoin address
|
|
157
|
+
* @returns Whether address is valid
|
|
158
|
+
*/
|
|
159
|
+
validateAddress(address: string): boolean;
|
|
160
|
+
/**
|
|
161
|
+
* Clear balance cache for an address
|
|
162
|
+
*
|
|
163
|
+
* Forces next balance query to fetch fresh data.
|
|
164
|
+
*
|
|
165
|
+
* @param address - Bitcoin address
|
|
166
|
+
*/
|
|
167
|
+
clearBalanceCache(address: string): void;
|
|
168
|
+
/**
|
|
169
|
+
* Clear all balance cache
|
|
170
|
+
*/
|
|
171
|
+
clearAllBalanceCache(): void;
|
|
172
|
+
/**
|
|
173
|
+
* Clear address cache for a public key
|
|
174
|
+
*
|
|
175
|
+
* Forces next address derivation to compute fresh.
|
|
176
|
+
*
|
|
177
|
+
* @param publicKey - PKP public key
|
|
178
|
+
*/
|
|
179
|
+
clearAddressCache(publicKey: string): void;
|
|
180
|
+
/**
|
|
181
|
+
* Clear all address cache
|
|
182
|
+
*/
|
|
183
|
+
clearAllAddressCache(): void;
|
|
184
|
+
/**
|
|
185
|
+
* Get balance cache statistics
|
|
186
|
+
*/
|
|
187
|
+
getBalanceCacheStats(): import("../..").CacheStats | null;
|
|
188
|
+
/**
|
|
189
|
+
* Get address cache statistics
|
|
190
|
+
*/
|
|
191
|
+
getAddressCacheStats(): import("../..").CacheStats | null;
|
|
192
|
+
/**
|
|
193
|
+
* Get current network configuration
|
|
194
|
+
*/
|
|
195
|
+
getNetwork(): BitcoinNetwork;
|
|
196
|
+
/**
|
|
197
|
+
* Check if consensus mode is enabled
|
|
198
|
+
*/
|
|
199
|
+
isConsensusEnabled(): boolean;
|
|
200
|
+
/**
|
|
201
|
+
* Get current BTC price using LIT Protocol price oracle
|
|
202
|
+
*
|
|
203
|
+
* Queries the decentralized price oracle via LIT Actions.
|
|
204
|
+
* Supports fast mode (single source) and full mode (consensus).
|
|
205
|
+
*
|
|
206
|
+
* @param mode - Price fetch mode: "fast" (single source) or "full" (consensus)
|
|
207
|
+
* @param sign - Whether to request PKP signature on the price
|
|
208
|
+
* @returns BTC price result with timestamp and source
|
|
209
|
+
*/
|
|
210
|
+
getPrice(mode?: "fast" | "full", sign?: boolean): Promise<Result<BTCPriceResult, SDKError>>;
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Factory function to create a BitcoinOperations instance
|
|
214
|
+
*/
|
|
215
|
+
export declare function createBitcoinOperations(config: BitcoinOperationsConfig): Result<BitcoinOperations, SDKError>;
|
|
@@ -0,0 +1,251 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Generic Cache Manager Module
|
|
3
|
+
*
|
|
4
|
+
* Provides a type-safe, generic LRU cache with TTL support.
|
|
5
|
+
* Can be used for any data type (Bitcoin balances, addresses, query results, etc.)
|
|
6
|
+
*
|
|
7
|
+
* Features:
|
|
8
|
+
* - Generic type support
|
|
9
|
+
* - TTL-based expiration
|
|
10
|
+
* - LRU eviction policy
|
|
11
|
+
* - Hit/miss statistics
|
|
12
|
+
* - Memory-efficient
|
|
13
|
+
* - Thread-safe operations
|
|
14
|
+
*/
|
|
15
|
+
import { Result } from '../../types/result';
|
|
16
|
+
import { SDKError } from '../../utils/error-handler';
|
|
17
|
+
/**
|
|
18
|
+
* Cache entry with metadata
|
|
19
|
+
*/
|
|
20
|
+
interface CacheEntry<T> {
|
|
21
|
+
/** Cached value */
|
|
22
|
+
value: T;
|
|
23
|
+
/** Unix timestamp when entry was created (ms) */
|
|
24
|
+
timestamp: number;
|
|
25
|
+
/** Number of times this entry was accessed */
|
|
26
|
+
hits: number;
|
|
27
|
+
/** Unix timestamp of last access (ms) */
|
|
28
|
+
lastAccessed: number;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Cache statistics
|
|
32
|
+
*/
|
|
33
|
+
export interface CacheStats {
|
|
34
|
+
/** Current number of entries in cache */
|
|
35
|
+
size: number;
|
|
36
|
+
/** Total cache hits */
|
|
37
|
+
hits: number;
|
|
38
|
+
/** Total cache misses */
|
|
39
|
+
misses: number;
|
|
40
|
+
/** Total evictions performed */
|
|
41
|
+
evictions: number;
|
|
42
|
+
/** Timestamp of oldest entry (ms) */
|
|
43
|
+
oldestEntry: number;
|
|
44
|
+
/** Timestamp of newest entry (ms) */
|
|
45
|
+
newestEntry: number;
|
|
46
|
+
/** Hit rate as percentage (0-100) */
|
|
47
|
+
hitRate: number;
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Cache configuration
|
|
51
|
+
*/
|
|
52
|
+
export interface CacheConfig {
|
|
53
|
+
/** Maximum number of entries (default: 1000) */
|
|
54
|
+
maxSize?: number;
|
|
55
|
+
/** Time-to-live in milliseconds (default: 60000 = 1 minute) */
|
|
56
|
+
ttlMs?: number;
|
|
57
|
+
/** Enable debug logging (default: false) */
|
|
58
|
+
debug?: boolean;
|
|
59
|
+
/** Cache name for logging (default: 'Cache') */
|
|
60
|
+
name?: string;
|
|
61
|
+
}
|
|
62
|
+
/**
|
|
63
|
+
* Generic LRU Cache with TTL support
|
|
64
|
+
*
|
|
65
|
+
* @template K - Key type (usually string)
|
|
66
|
+
* @template V - Value type
|
|
67
|
+
*
|
|
68
|
+
* @example
|
|
69
|
+
* ```typescript
|
|
70
|
+
* // Create a cache for Bitcoin balances
|
|
71
|
+
* const balanceCache = new LRUCache<string, BitcoinBalance>({
|
|
72
|
+
* maxSize: 500,
|
|
73
|
+
* ttlMs: 60000,
|
|
74
|
+
* name: 'BitcoinBalance'
|
|
75
|
+
* });
|
|
76
|
+
*
|
|
77
|
+
* // Set value
|
|
78
|
+
* balanceCache.set('bc1q...', { balance: Satoshis(50000000n) });
|
|
79
|
+
*
|
|
80
|
+
* // Get value
|
|
81
|
+
* const balance = balanceCache.get('bc1q...');
|
|
82
|
+
* if (balance) {
|
|
83
|
+
* console.log('Cached balance:', balance.balance);
|
|
84
|
+
* }
|
|
85
|
+
* ```
|
|
86
|
+
*/
|
|
87
|
+
export declare class LRUCache<K, V> {
|
|
88
|
+
private cache;
|
|
89
|
+
private readonly maxSize;
|
|
90
|
+
private readonly ttlMs;
|
|
91
|
+
private readonly debug;
|
|
92
|
+
private readonly name;
|
|
93
|
+
private stats;
|
|
94
|
+
constructor(config?: CacheConfig);
|
|
95
|
+
/**
|
|
96
|
+
* Get value from cache
|
|
97
|
+
*
|
|
98
|
+
* Returns null if:
|
|
99
|
+
* - Key not found
|
|
100
|
+
* - Entry has expired
|
|
101
|
+
*
|
|
102
|
+
* @param key - Cache key
|
|
103
|
+
* @returns Cached value or null
|
|
104
|
+
*/
|
|
105
|
+
get(key: K): V | null;
|
|
106
|
+
/**
|
|
107
|
+
* Get value from cache with Result wrapper
|
|
108
|
+
*
|
|
109
|
+
* Useful when you want to distinguish between "not found" and "expired"
|
|
110
|
+
*/
|
|
111
|
+
getResult(key: K): Result<V, SDKError>;
|
|
112
|
+
/**
|
|
113
|
+
* Set value in cache
|
|
114
|
+
*
|
|
115
|
+
* If cache is full, evicts the least recently used entry
|
|
116
|
+
*
|
|
117
|
+
* @param key - Cache key
|
|
118
|
+
* @param value - Value to cache
|
|
119
|
+
* @param ttl - Optional custom TTL for this entry (ms)
|
|
120
|
+
*/
|
|
121
|
+
set(key: K, value: V, ttl?: number): void;
|
|
122
|
+
/**
|
|
123
|
+
* Set value in cache with Result wrapper
|
|
124
|
+
*/
|
|
125
|
+
setResult(key: K, value: V, ttl?: number): Result<void, SDKError>;
|
|
126
|
+
/**
|
|
127
|
+
* Check if key exists in cache (without affecting stats)
|
|
128
|
+
*/
|
|
129
|
+
has(key: K): boolean;
|
|
130
|
+
/**
|
|
131
|
+
* Delete specific key from cache
|
|
132
|
+
*/
|
|
133
|
+
delete(key: K): boolean;
|
|
134
|
+
/**
|
|
135
|
+
* Clear entire cache
|
|
136
|
+
*/
|
|
137
|
+
clear(): void;
|
|
138
|
+
/**
|
|
139
|
+
* Get current cache size
|
|
140
|
+
*/
|
|
141
|
+
size(): number;
|
|
142
|
+
/**
|
|
143
|
+
* Get cache statistics
|
|
144
|
+
*/
|
|
145
|
+
getStats(): CacheStats;
|
|
146
|
+
/**
|
|
147
|
+
* Get hit rate percentage
|
|
148
|
+
*/
|
|
149
|
+
getHitRate(): number;
|
|
150
|
+
/**
|
|
151
|
+
* Get all cached keys (for debugging)
|
|
152
|
+
*/
|
|
153
|
+
getKeys(): K[];
|
|
154
|
+
/**
|
|
155
|
+
* Get all cached values (for debugging)
|
|
156
|
+
*/
|
|
157
|
+
getValues(): V[];
|
|
158
|
+
/**
|
|
159
|
+
* Get all cache entries with metadata (for debugging)
|
|
160
|
+
*/
|
|
161
|
+
getEntries(): Array<{
|
|
162
|
+
key: K;
|
|
163
|
+
value: V;
|
|
164
|
+
metadata: Omit<CacheEntry<V>, 'value'>;
|
|
165
|
+
}>;
|
|
166
|
+
/**
|
|
167
|
+
* Clean up expired entries
|
|
168
|
+
*
|
|
169
|
+
* Useful for periodic maintenance
|
|
170
|
+
*
|
|
171
|
+
* @returns Number of entries cleaned
|
|
172
|
+
*/
|
|
173
|
+
cleanExpired(): number;
|
|
174
|
+
/**
|
|
175
|
+
* Check if cache entry is expired
|
|
176
|
+
*/
|
|
177
|
+
private isExpired;
|
|
178
|
+
/**
|
|
179
|
+
* Evict least recently used entry
|
|
180
|
+
*/
|
|
181
|
+
private evictLRU;
|
|
182
|
+
/**
|
|
183
|
+
* Get or compute value
|
|
184
|
+
*
|
|
185
|
+
* If key exists in cache, returns cached value.
|
|
186
|
+
* Otherwise, computes value using provided function and caches it.
|
|
187
|
+
*
|
|
188
|
+
* @param key - Cache key
|
|
189
|
+
* @param compute - Function to compute value if not in cache
|
|
190
|
+
* @param ttl - Optional custom TTL for this entry
|
|
191
|
+
* @returns Cached or computed value
|
|
192
|
+
*/
|
|
193
|
+
getOrCompute(key: K, compute: () => Promise<V>, ttl?: number): Promise<V>;
|
|
194
|
+
/**
|
|
195
|
+
* Get or compute value with Result wrapper
|
|
196
|
+
*/
|
|
197
|
+
getOrComputeResult(key: K, compute: () => Promise<Result<V, SDKError>>, ttl?: number): Promise<Result<V, SDKError>>;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Generic cache interface for type safety
|
|
201
|
+
*/
|
|
202
|
+
export interface Cache<T> {
|
|
203
|
+
get(key: string): T | null | undefined;
|
|
204
|
+
set(key: string, value: T): void;
|
|
205
|
+
delete(key: string): boolean;
|
|
206
|
+
clear(): void;
|
|
207
|
+
has(key: string): boolean;
|
|
208
|
+
size(): number;
|
|
209
|
+
getStats(): CacheStats;
|
|
210
|
+
}
|
|
211
|
+
/**
|
|
212
|
+
* Cache Manager - Factory for creating specialized caches
|
|
213
|
+
*/
|
|
214
|
+
export declare class CacheManager {
|
|
215
|
+
private caches;
|
|
216
|
+
private readonly globalConfig;
|
|
217
|
+
constructor(globalConfig?: CacheConfig);
|
|
218
|
+
/**
|
|
219
|
+
* Create or get a named cache
|
|
220
|
+
*
|
|
221
|
+
* @param name - Unique cache name
|
|
222
|
+
* @param config - Optional cache-specific configuration
|
|
223
|
+
* @returns LRU cache instance
|
|
224
|
+
*/
|
|
225
|
+
getCache<K, V>(name: string, config?: CacheConfig): LRUCache<K, V>;
|
|
226
|
+
/**
|
|
227
|
+
* Clear all caches
|
|
228
|
+
*/
|
|
229
|
+
clearAll(): void;
|
|
230
|
+
/**
|
|
231
|
+
* Clean expired entries from all caches
|
|
232
|
+
*/
|
|
233
|
+
cleanAllExpired(): number;
|
|
234
|
+
/**
|
|
235
|
+
* Get statistics for all caches
|
|
236
|
+
*/
|
|
237
|
+
getAllStats(): Record<string, CacheStats>;
|
|
238
|
+
/**
|
|
239
|
+
* Get list of all cache names
|
|
240
|
+
*/
|
|
241
|
+
getCacheNames(): string[];
|
|
242
|
+
/**
|
|
243
|
+
* Delete a named cache
|
|
244
|
+
*/
|
|
245
|
+
deleteCache(name: string): boolean;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Factory function to create a CacheManager instance
|
|
249
|
+
*/
|
|
250
|
+
export declare function createCacheManager(config?: CacheConfig): CacheManager;
|
|
251
|
+
export {};
|
|
@@ -0,0 +1,136 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Contract Manager Module
|
|
3
|
+
*
|
|
4
|
+
* Responsible for:
|
|
5
|
+
* - Managing contract instances and their lifecycle
|
|
6
|
+
* - Providing typed contract interfaces
|
|
7
|
+
* - Handling contract address resolution
|
|
8
|
+
* - Managing provider/signer connections
|
|
9
|
+
*
|
|
10
|
+
* This module centralizes all contract interaction setup and provides
|
|
11
|
+
* a clean interface for other modules to access contracts.
|
|
12
|
+
*/
|
|
13
|
+
import { ethers as ethers5 } from "ethers";
|
|
14
|
+
import type { ContractAddresses } from "../../interfaces/chunks/config.i";
|
|
15
|
+
import { Result } from "../../types/result";
|
|
16
|
+
import { SDKError } from "../../utils/error-handler";
|
|
17
|
+
import type { PositionManager, ILoanOperationsManager, IUCDController, IPriceFeedConsumer, ITermManager, CircuitBreakerModule, ILiquidationManager } from "../../interfaces/chunks/contract-types.i";
|
|
18
|
+
/**
|
|
19
|
+
* Type alias for ethers v5 provider (used throughout SDK)
|
|
20
|
+
*/
|
|
21
|
+
export type Provider = ethers5.providers.Provider;
|
|
22
|
+
/**
|
|
23
|
+
* Type alias for ethers v5 signer
|
|
24
|
+
*/
|
|
25
|
+
export type Signer = ethers5.Signer;
|
|
26
|
+
/**
|
|
27
|
+
* Type alias for contract runner (provider or signer)
|
|
28
|
+
*/
|
|
29
|
+
export type ContractRunner = Provider | Signer;
|
|
30
|
+
/**
|
|
31
|
+
* Configuration for Contract Manager
|
|
32
|
+
*/
|
|
33
|
+
export interface ContractManagerConfig {
|
|
34
|
+
/** Ethers v5 provider for reading blockchain data */
|
|
35
|
+
provider: Provider;
|
|
36
|
+
/** Ethers v5 signer for writing transactions (optional) */
|
|
37
|
+
signer?: Signer;
|
|
38
|
+
/** Contract addresses for the target network */
|
|
39
|
+
contractAddresses: ContractAddresses;
|
|
40
|
+
/** SDK mode: 'service' or 'standalone' */
|
|
41
|
+
mode?: "service" | "standalone";
|
|
42
|
+
/** Enable debug logging */
|
|
43
|
+
debug?: boolean;
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Managed contract instances with proper typing
|
|
47
|
+
*/
|
|
48
|
+
export interface ManagedContracts {
|
|
49
|
+
positionManager: PositionManager;
|
|
50
|
+
loanOperationsManager: ILoanOperationsManager;
|
|
51
|
+
ucdController: IUCDController;
|
|
52
|
+
priceFeed: IPriceFeedConsumer;
|
|
53
|
+
termManager: ITermManager;
|
|
54
|
+
circuitBreaker: CircuitBreakerModule;
|
|
55
|
+
liquidationManager: ILiquidationManager;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Contract Manager Module
|
|
59
|
+
*
|
|
60
|
+
* Centralized contract instance management with proper typing
|
|
61
|
+
*/
|
|
62
|
+
export declare class ContractManager {
|
|
63
|
+
private readonly config;
|
|
64
|
+
private contracts;
|
|
65
|
+
constructor(config: ContractManagerConfig);
|
|
66
|
+
/**
|
|
67
|
+
* Initialize all contract instances
|
|
68
|
+
*
|
|
69
|
+
* This is called lazily on first access to any contract
|
|
70
|
+
*/
|
|
71
|
+
private initializeContracts;
|
|
72
|
+
/**
|
|
73
|
+
* Validate that all required contract addresses are present and valid
|
|
74
|
+
*/
|
|
75
|
+
private validateContractAddresses;
|
|
76
|
+
/**
|
|
77
|
+
* Get all managed contract instances
|
|
78
|
+
*/
|
|
79
|
+
getContracts(): Result<ManagedContracts, SDKError>;
|
|
80
|
+
/**
|
|
81
|
+
* Get Position Manager contract
|
|
82
|
+
*/
|
|
83
|
+
getPositionManager(): Result<PositionManager, SDKError>;
|
|
84
|
+
/**
|
|
85
|
+
* Get Loan Operations Manager contract
|
|
86
|
+
*/
|
|
87
|
+
getLoanOperationsManager(): Result<ILoanOperationsManager, SDKError>;
|
|
88
|
+
/**
|
|
89
|
+
* Get UCD Controller contract
|
|
90
|
+
*/
|
|
91
|
+
getUCDController(): Result<IUCDController, SDKError>;
|
|
92
|
+
/**
|
|
93
|
+
* Get Price Feed contract
|
|
94
|
+
*/
|
|
95
|
+
getPriceFeed(): Result<IPriceFeedConsumer, SDKError>;
|
|
96
|
+
/**
|
|
97
|
+
* Get Term Manager contract
|
|
98
|
+
*/
|
|
99
|
+
getTermManager(): Result<ITermManager, SDKError>;
|
|
100
|
+
/**
|
|
101
|
+
* Get Circuit Breaker contract
|
|
102
|
+
*/
|
|
103
|
+
getCircuitBreaker(): Result<CircuitBreakerModule, SDKError>;
|
|
104
|
+
/**
|
|
105
|
+
* Get Liquidation Manager contract
|
|
106
|
+
*/
|
|
107
|
+
getLiquidationManager(): Result<ILiquidationManager, SDKError>;
|
|
108
|
+
/**
|
|
109
|
+
* Get the provider instance
|
|
110
|
+
*/
|
|
111
|
+
getProvider(): Provider;
|
|
112
|
+
/**
|
|
113
|
+
* Get the signer instance (if available)
|
|
114
|
+
*/
|
|
115
|
+
getSigner(): Result<Signer, SDKError>;
|
|
116
|
+
/**
|
|
117
|
+
* Check if a signer is available for write operations
|
|
118
|
+
*/
|
|
119
|
+
hasSigner(): boolean;
|
|
120
|
+
/**
|
|
121
|
+
* Get contract addresses
|
|
122
|
+
*/
|
|
123
|
+
getContractAddresses(): ContractAddresses;
|
|
124
|
+
/**
|
|
125
|
+
* Get the network chain ID from the provider
|
|
126
|
+
*/
|
|
127
|
+
getChainId(): Promise<Result<number, SDKError>>;
|
|
128
|
+
/**
|
|
129
|
+
* Verify that the provider is connected and responsive
|
|
130
|
+
*/
|
|
131
|
+
verifyConnection(): Promise<Result<boolean, SDKError>>;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Factory function to create a ContractManager instance
|
|
135
|
+
*/
|
|
136
|
+
export declare function createContractManager(config: ContractManagerConfig): Result<ContractManager, SDKError>;
|