@gvnrdao/dh-sdk 0.0.166 → 0.0.205

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.
Files changed (71) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +86 -5
  3. package/browser/dist/397.browser.js +1 -1
  4. package/browser/dist/833.browser.js +1 -1
  5. package/browser/dist/browser.js +1 -13
  6. package/browser/dist/browser.js.LICENSE.txt +1 -70
  7. package/browser/dist/index.d.ts +3 -4
  8. package/browser/dist/index.d.ts.map +1 -1
  9. package/browser/dist/index.js +4 -3
  10. package/dist/constants/chunks/contract-abis.d.ts +7 -0
  11. package/dist/constants/chunks/deployment-addresses.d.ts +68 -0
  12. package/dist/constants/chunks/encrypted-provider-params.d.ts +21 -0
  13. package/dist/constants/chunks/environment.browser.d.ts +45 -0
  14. package/dist/constants/chunks/environment.d.ts +57 -0
  15. package/dist/constants/chunks/network-configs.d.ts +65 -0
  16. package/dist/constants/chunks/sdk-config.d.ts +33 -0
  17. package/dist/constants/chunks/sdk-limits.d.ts +66 -0
  18. package/dist/constants/index.d.ts +15 -0
  19. package/dist/graphs/client.d.ts +19 -0
  20. package/dist/graphs/diamond-hands.d.ts +248 -0
  21. package/dist/index.d.ts +47 -7391
  22. package/dist/index.js +4525 -16860
  23. package/dist/index.mjs +4489 -16801
  24. package/dist/interfaces/chunks/btc.i.d.ts +36 -0
  25. package/dist/interfaces/chunks/config.i.d.ts +250 -0
  26. package/dist/interfaces/chunks/contract-interactions.i.d.ts +64 -0
  27. package/dist/interfaces/chunks/contract-types.i.d.ts +165 -0
  28. package/dist/interfaces/chunks/lit-actions-results.i.d.ts +165 -0
  29. package/dist/interfaces/chunks/lit-actions.i.d.ts +98 -0
  30. package/dist/interfaces/chunks/loan-operations.i.d.ts +332 -0
  31. package/dist/interfaces/chunks/pkp-integration.i.d.ts +87 -0
  32. package/dist/interfaces/chunks/position-query.i.d.ts +76 -0
  33. package/dist/interfaces/chunks/requests.i.d.ts +55 -0
  34. package/dist/interfaces/chunks/ucd-minting.i.d.ts +34 -0
  35. package/dist/interfaces/chunks/utility.i.d.ts +64 -0
  36. package/dist/interfaces/index.d.ts +17 -0
  37. package/dist/modules/bitcoin/bitcoin-operations.module.d.ts +223 -0
  38. package/dist/modules/cache/cache-manager.module.d.ts +92 -0
  39. package/dist/modules/contract/contract-manager.module.d.ts +136 -0
  40. package/dist/modules/diamond-hands-sdk.d.ts +669 -0
  41. package/dist/modules/loan/loan-creator.module.d.ts +143 -0
  42. package/dist/modules/loan/loan-query.module.d.ts +206 -0
  43. package/dist/modules/mock/mock-token-manager.module.d.ts +83 -0
  44. package/dist/modules/pkp/pkp-manager.module.d.ts +136 -0
  45. package/dist/protocol/protocol-pause.d.ts +19 -0
  46. package/dist/server.d.ts +17 -0
  47. package/dist/server.js +285 -0
  48. package/dist/server.mjs +242 -0
  49. package/dist/types/authorization-params.d.ts +160 -0
  50. package/dist/types/branded/domain-values.d.ts +138 -0
  51. package/dist/types/branded/ids.d.ts +23 -0
  52. package/dist/types/event-types.d.ts +235 -0
  53. package/dist/types/graph-dtos.d.ts +228 -0
  54. package/dist/types/loanStatus.d.ts +10 -0
  55. package/dist/types/result.d.ts +120 -0
  56. package/dist/utils/bitcoin-address-cache.utils.d.ts +87 -0
  57. package/dist/utils/bitcoin-provider.utils.d.ts +48 -0
  58. package/dist/utils/bitcoin-signature.d.ts +20 -0
  59. package/dist/utils/chunks/bitcoin-utils.d.ts +75 -0
  60. package/dist/utils/chunks/eip1559-broadcast.utils.d.ts +24 -0
  61. package/dist/utils/error-handler.d.ts +106 -0
  62. package/dist/utils/ethers-interop.utils.d.ts +146 -0
  63. package/dist/utils/extend-authorization.utils.d.ts +61 -0
  64. package/dist/utils/lit-signature.utils.d.ts +6 -0
  65. package/dist/utils/logger.utils.d.ts +142 -0
  66. package/dist/utils/mint-authorization.utils.d.ts +224 -0
  67. package/dist/utils/quantum-timing.d.ts +75 -0
  68. package/dist/utils/signature-tempering.utils.d.ts +31 -0
  69. package/dist/utils/telegram-messaging.utils.d.ts +188 -0
  70. package/package.json +43 -22
  71. package/dist/index.d.mts +0 -7392
@@ -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,223 @@
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(): {
188
+ size: number;
189
+ maxSize: number;
190
+ ttlMs: number;
191
+ } | null;
192
+ /**
193
+ * Get address cache statistics
194
+ */
195
+ getAddressCacheStats(): {
196
+ size: number;
197
+ maxSize: number;
198
+ ttlMs: number;
199
+ } | null;
200
+ /**
201
+ * Get current network configuration
202
+ */
203
+ getNetwork(): BitcoinNetwork;
204
+ /**
205
+ * Check if consensus mode is enabled
206
+ */
207
+ isConsensusEnabled(): boolean;
208
+ /**
209
+ * Get current BTC price using LIT Protocol price oracle
210
+ *
211
+ * Queries the decentralized price oracle via LIT Actions.
212
+ * Supports fast mode (single source) and full mode (consensus).
213
+ *
214
+ * @param mode - Price fetch mode: "fast" (single source) or "full" (consensus)
215
+ * @param sign - Whether to request PKP signature on the price
216
+ * @returns BTC price result with timestamp and source
217
+ */
218
+ getPrice(mode?: "fast" | "full", sign?: boolean): Promise<Result<BTCPriceResult, SDKError>>;
219
+ }
220
+ /**
221
+ * Factory function to create a BitcoinOperations instance
222
+ */
223
+ export declare function createBitcoinOperations(config: BitcoinOperationsConfig): Result<BitcoinOperations, SDKError>;
@@ -0,0 +1,92 @@
1
+ /**
2
+ * Cache Manager Module
3
+ *
4
+ * Provides a simple cache factory for SDK modules.
5
+ * Each cache is an independent LRU cache with TTL support.
6
+ */
7
+ export interface CacheConfig {
8
+ maxSize: number;
9
+ ttlMs: number;
10
+ }
11
+ export interface CacheStats {
12
+ size: number;
13
+ maxSize: number;
14
+ ttlMs: number;
15
+ }
16
+ /**
17
+ * Simple LRU Cache with TTL support
18
+ */
19
+ export declare class Cache<T = any> {
20
+ private cache;
21
+ private readonly maxSize;
22
+ private readonly ttlMs;
23
+ constructor(config: CacheConfig);
24
+ /**
25
+ * Get value from cache
26
+ */
27
+ get(key: string): T | undefined;
28
+ /**
29
+ * Set value in cache
30
+ */
31
+ set(key: string, value: T, ttl?: number): void;
32
+ /**
33
+ * Check if key exists in cache
34
+ */
35
+ has(key: string): boolean;
36
+ /**
37
+ * Delete key from cache
38
+ */
39
+ delete(key: string): boolean;
40
+ /**
41
+ * Clear all cache entries
42
+ */
43
+ clear(): void;
44
+ /**
45
+ * Clean expired entries
46
+ */
47
+ cleanExpired(): number;
48
+ /**
49
+ * Get cache statistics
50
+ */
51
+ getStats(): {
52
+ size: number;
53
+ maxSize: number;
54
+ ttlMs: number;
55
+ };
56
+ }
57
+ /**
58
+ * Cache Manager
59
+ *
60
+ * Factory for creating named caches with specific configurations
61
+ */
62
+ export declare class CacheManager {
63
+ private caches;
64
+ private readonly debug;
65
+ constructor(config?: {
66
+ debug?: boolean;
67
+ });
68
+ /**
69
+ * Get or create a cache instance
70
+ */
71
+ getCache<T = any>(name: string, config: CacheConfig): Cache<T>;
72
+ /**
73
+ * Clear all caches
74
+ */
75
+ clearAll(): void;
76
+ /**
77
+ * Clean expired entries from all caches
78
+ */
79
+ cleanAllExpired(): number;
80
+ /**
81
+ * Get statistics for all caches
82
+ */
83
+ getAllStats(): Record<string, ReturnType<Cache["getStats"]>>;
84
+ /**
85
+ * Destroy cache manager
86
+ */
87
+ destroy(): void;
88
+ }
89
+ export { Cache as LRUCache };
90
+ export declare function createCacheManager(config?: {
91
+ debug?: boolean;
92
+ }): CacheManager;
@@ -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>;