UNPKG

@shogun-sdk/money-legos

Version:

Shogun Money Legos: clients and types for quotes, memes, prices, balances, fees, validations, etc.

138 lines 4.85 kB
import { InfoAPI } from './rest/info.js'; import { ExchangeAPI } from './rest/exchange.js'; import { RateLimiter } from './utils/rateLimiter.js'; import * as CONSTANTS from './types/constants.js'; import { CustomOperations } from './rest/custom.js'; import { ethers } from 'ethers'; import { SymbolConversion } from './utils/symbolConversion.js'; import { AuthenticationError } from './utils/errors.js'; export class Hyperliquid { constructor(params = {}) { Object.defineProperty(this, "info", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "exchange", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "custom", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "symbolConversion", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "rateLimiter", { enumerable: true, configurable: true, writable: true, value: void 0 }); Object.defineProperty(this, "isValidPrivateKey", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "_initialized", { enumerable: true, configurable: true, writable: true, value: false }); Object.defineProperty(this, "_initializing", { enumerable: true, configurable: true, writable: true, value: null }); Object.defineProperty(this, "vaultAddress", { enumerable: true, configurable: true, writable: true, value: null }); const { privateKey, vaultAddress } = params; const baseURL = CONSTANTS.BASE_URL; this.rateLimiter = new RateLimiter(); this.symbolConversion = new SymbolConversion(baseURL, this.rateLimiter); this.vaultAddress = vaultAddress || null; // Initialize info API this.info = new InfoAPI(baseURL, this.rateLimiter, this.symbolConversion, this); // Create proxy objects for exchange and custom this.exchange = this.createAuthenticatedProxy(ExchangeAPI); this.custom = this.createAuthenticatedProxy(CustomOperations); if (privateKey) { this.initializePrivateKey(privateKey); } } async connect() { if (!this._initialized) { if (!this._initializing) { this._initializing = this.initialize(); } await this._initializing; } } async initialize() { if (this._initialized) return; try { // Initialize symbol conversion first await this.symbolConversion.initialize(); this._initialized = true; this._initializing = null; } catch (error) { this._initializing = null; throw error; } } async ensureInitialized() { await this.connect(); } initializePrivateKey(privateKey) { try { const formattedPrivateKey = privateKey.startsWith('0x') ? privateKey : `0x${privateKey}`; new ethers.Wallet(formattedPrivateKey); // Validate the private key this.exchange = new ExchangeAPI(formattedPrivateKey, this.rateLimiter, this.symbolConversion, this, this.vaultAddress); this.custom = new CustomOperations(this.exchange, this.info, formattedPrivateKey, this.symbolConversion, this); this.isValidPrivateKey = true; } catch (error) { console.warn('Invalid private key provided. Some functionalities will be limited.'); this.isValidPrivateKey = false; } } createAuthenticatedProxy(_Class) { return new Proxy({}, { get: (target, prop) => { if (!this.isValidPrivateKey) { throw new AuthenticationError('Invalid or missing private key. This method requires authentication.'); } return target[prop]; }, }); } // Modify existing methods to check initialization isAuthenticated() { this.ensureInitialized(); return this.isValidPrivateKey; } disconnect() { this.ensureInitialized(); } } export * from './types/index.js'; export * from './utils/signing.js'; //# sourceMappingURL=index.js.map