UNPKG

@crl-technologies/sdk

Version:

CRL White Label API - TypeScript SDK with HMAC authentication

113 lines (112 loc) 4.3 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.CRLApiClient = void 0; const crypto_1 = __importDefault(require("crypto")); class CRLApiClient { constructor(config) { this.keyId = config.keyId; this.secretKey = config.secretKey; this.baseUrl = config.baseUrl || 'https://api.crl-api.com'; } async calculateLong(S0, ST, K, L, premium) { return this.calculate({ mode: 'long', S0, ST, K, L, premium, side: 'buy' }); } async calculate(request) { const endpoint = '/v1/calc'; const body = JSON.stringify(request); const timestamp = Math.floor(Date.now() / 1000).toString(); const signature = this.generateSignature('POST', endpoint, timestamp, body); const response = await fetch(`${this.baseUrl}${endpoint}`, { method: 'POST', headers: { 'Content-Type': 'application/json', 'X-Key-Id': this.keyId, 'X-Timestamp': timestamp, 'X-Signature': signature }, body }); if (!response.ok) { throw new Error(`API error: ${response.status}`); } return response.json(); } // ========================================== // BEST-EXECUTION API METHODS (CL-16) // ========================================== /** * Export best-execution data for compliance reporting * @param params Export parameters (dates, format, position IDs) */ async exportBestExecution(params) { const queryParams = new URLSearchParams(); queryParams.append('start_date', params.startDate); if (params.endDate) queryParams.append('end_date', params.endDate); if (params.format) queryParams.append('format', params.format); if (params.positionIds) queryParams.append('position_ids', params.positionIds.join(',')); const endpoint = `/api/v1/best-execution/export?${queryParams}`; return this.makeRequest('GET', endpoint); } /** * Get venue performance analytics * @param startDate Start date for analysis (ISO 8601) */ async getVenuePerformance(startDate) { const endpoint = `/api/v1/best-execution/venue-performance?start_date=${startDate}`; return this.makeRequest('GET', endpoint); } /** * Export compliance data in regulatory format * @param startDate Start date (ISO 8601) * @param endDate End date (ISO 8601) */ async exportCompliance(startDate, endDate) { const endpoint = `/api/v1/best-execution/compliance-export?start_date=${startDate}&end_date=${endDate}`; return this.makeRequest('GET', endpoint); } /** * Generic request method with HMAC authentication */ async makeRequest(method, endpoint, body) { const bodyStr = body ? JSON.stringify(body) : ''; const timestamp = Math.floor(Date.now() / 1000).toString(); const signature = this.generateSignature(method, endpoint, timestamp, bodyStr); const options = { method, headers: { 'Content-Type': 'application/json', 'X-Key-Id': this.keyId, 'X-Timestamp': timestamp, 'X-Signature': signature } }; if (body) options.body = bodyStr; const response = await fetch(`${this.baseUrl}${endpoint}`, options); if (!response.ok) { throw new Error(`API error: ${response.status}`); } return response.json(); } generateSignature(method, path, timestamp, body) { const bodyHash = crypto_1.default.createHash('sha256').update(body).digest('base64'); const signatureString = `${method}\n${path}\n${timestamp}\n${bodyHash}`; const signature = crypto_1.default.createHmac('sha256', this.secretKey).update(signatureString).digest('base64'); return signature; } } exports.CRLApiClient = CRLApiClient;