UNPKG

@ixily/activ

Version:

Alpha Capture Trade Idea Verification. Blockchain ownership proven trade ideas and strategies.

305 lines (301 loc) 11.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.PricingModule = exports.getValidPriceFromLitPriceResult = exports.getSigLogPriceResponseFromLitResult = exports.getValidPriceFromSigLogPrice = exports.extractPricingKeysArrayFromPriceLogs = void 0; const __1 = require("../.."); const parallel_chunks_module_1 = require("./parallel-chunks.module"); const lit_actions_1 = require("../../constants/lit-actions"); const CROUPIER_URL = 'https://croupier.ixily.io'; // 'https://api.ixily.io/v1/activ/croupier' const state = { useProxyUntrusted: false, }; const config = (params) => { if (params.useProxyUntrusted !== undefined) { state.useProxyUntrusted = params.useProxyUntrusted; } }; const extractPricingKeysArrayFromPriceLogs = (logs) => { const keysArray = []; for (const logi of logs) { const loga = logi.split('\n'); // console.log('loga') // console.log(loga) const logo = loga[loga.length - 2]; // console.log('logo') // console.log(logo) const logp = logo.split(':'); // console.log('logp') // console.log(logp) if (logp[1] !== 'myidpass') { throw new Error('Error Decoding Node Key for a node, for pricing'); } const nodeId = logp[2]; // console.log('nodeId') // console.log(nodeId) const nodeKey = logp[3]; // console.log('nodeKey') // console.log(nodeKey) keysArray.push([nodeId, nodeKey]); } // sort key arrays alphabetically by first entry of the double array keysArray.sort((a, b) => { if (a[0] < b[0]) { return -1; } else if (a[0] > b[0]) { return 1; } else { return 0; } }); return keysArray; }; exports.extractPricingKeysArrayFromPriceLogs = extractPricingKeysArrayFromPriceLogs; const getValidPriceFromSigLogPrice = (litResult) => { const keys = (0, exports.extractPricingKeysArrayFromPriceLogs)(litResult.logs); return { response: litResult.response, signature: litResult.signatures.priceSignature, keys, }; }; exports.getValidPriceFromSigLogPrice = getValidPriceFromSigLogPrice; const getSigLogPriceResponseFromLitResult = (litResult) => { // console.log('litResult') // console.log(litResult) const sigLogResponse = { signatures: litResult.signatures, response: litResult.response, logs: litResult.debug.allNodeLogs, }; return sigLogResponse; }; exports.getSigLogPriceResponseFromLitResult = getSigLogPriceResponseFromLitResult; const getValidPriceFromLitPriceResult = (litResult) => { const sigLogResponse = (0, exports.getSigLogPriceResponseFromLitResult)(litResult); const validPrice = (0, exports.getValidPriceFromSigLogPrice)(sigLogResponse); return validPrice; }; exports.getValidPriceFromLitPriceResult = getValidPriceFromLitPriceResult; const LIT_PRICING_LIT_JS_TASK = 'litpri'; const LIT_PRICING_LIT_JS_TASK_CHUNKS = 5; parallel_chunks_module_1.ParallelChunksModule.defineTaskChunks(LIT_PRICING_LIT_JS_TASK, LIT_PRICING_LIT_JS_TASK_CHUNKS); const getPriceDetails = async (params, mockPricing = false, customSettings) => { // only for debug/testing if (mockPricing) { const price = await testPricingAsset(params); return { response: [JSON.stringify(price)], signature: null, keys: [], }; } // pricing code // let litAction: ILitActionV2 let litAction; if (params.provider === 'Binance') { __1.LogModule.dev('set to ask binance'); litAction = __1.LIT_ACTIONS.GET_PRICE_BINANCE_V2; } else if (params.provider === 'IEX') { __1.LogModule.dev('set to ask iex'); litAction = __1.LIT_ACTIONS.GET_PRICE_IEX_V2; } else if (params.provider === 'IG Group') { __1.LogModule.dev('set to ask IG'); litAction = __1.LIT_ACTIONS.GET_PRICE_IG_V2; const trueCreds = await (0, lit_actions_1.IG_PRE_AUTHENTICATE)(params.auth.key, params.auth.secret, params.auth.secret2); params.auth.key = trueCreds.key; params.auth.secret = trueCreds.secret; params.auth.secret2 = trueCreds.secret2; } else if (params.provider === 'Alpaca') { __1.LogModule.dev('set to ask alpaca'); litAction = __1.LIT_ACTIONS.GET_PRICE_ALPACA_V2; } else if (params.provider === 'Tradestation') { __1.LogModule.dev('set to ask tradestation'); litAction = __1.LIT_ACTIONS.GET_PRICE_TRADESTATION_V2; } else if (params.provider === 'CoinMarketCap') { __1.LogModule.dev('set to ask coinmarketcap'); litAction = __1.LIT_ACTIONS.GET_PRICE_COINMARKETCAP_V2; } else if (params.provider === 'CryptoCompare') { __1.LogModule.dev('set to ask cryptocompare'); if (params.params.symbol.indexOf('=') === -1) { throw new Error('Pricing Error: For CryptoCompare, please separate the assets with "=". For instance: "BTC=USD"'); } litAction = __1.LIT_ACTIONS.GET_PRICE_CRYPTOCOMPARE_V2; } else { // throw new Error( // 'PricingModule: getPriceDetails: Price Provider not supported: ' + // params.provider, // ) throw new Error('Unsupported provider'); } // trying the first time to load this and force await (0, __1.loop)(async () => { __1.LogModule.dev(`The ipfs content to this id "${litAction.ipfsId}" was loaded successfully.`); __1.LogModule.dev(`GET https://ipfs.io/ipfs/${litAction.ipfsId}`); }, async () => { let isPassed = false; try { __1.LogModule.dev(`Calling the ipfs content to "${params.provider} lit action" with id "${litAction.ipfsId}". loading...`); const result = await fetch(`https://ipfs.io/ipfs/${litAction.ipfsId}`); if (result.status === 200) { isPassed = true; } } catch (er) { __1.LogModule.dev('er:', er); isPassed = false; } return isPassed; }, { loopTimeInMs: 10000, limitTimeSecond: 300, }, async (err) => { __1.LogModule.dev('err', err); }); // note: define one interface for this (LitModule.runJS<...>) // let dt: any = null // This is no the best place for a retry function as such. // try { // dt = await retryFunctionHelper({ // maxRetries: 3, // retryCallback: async () => { // console.log( // 'ACTIV: PricingModule: getPriceDetails: tracker: ', // customSettings?.tracker, // ) const litActionParams = { symbol: params.params.symbol, apiKey: params.auth?.key || '', apiSecret: params.auth?.secret || '', apiSecret2: params.auth?.secret2 || '', ipfsId: litAction.ipfsId, uniqueExecutionId: (0, __1.generateUUID)(), croupierUrl: customSettings?.croupierUrl || CROUPIER_URL, tracker: customSettings?.tracker, attempts: customSettings?.attempts, useProxy: state.useProxyUntrusted === true ? true : false, }; // console.log('[pricing.module] getPriceDetails (litActionParams)', litActionParams) const dt = await parallel_chunks_module_1.ParallelChunksModule.run(LIT_PRICING_LIT_JS_TASK, async () => { return __1.LitModule.runJS( // litAction.fetcher.ipfsId, litAction.ipfsId, // litAction.fetcher.publicKey, litAction.publicKey, litActionParams); }); // }, // notificationCallback: async (errMsg: string, retryInx: number) => { // log.dev( // `[${params.provider}] getPriceDetails LitModule.runJS (retry #${retryInx})`, // errMsg, // ) // }, // rejectOnMaxRetries: true, // }) // } catch (err: any) { // We do prefer to show error for sure and we do prefer the uggly; // cause the uggly is actually pretty and says everythin we want to know. // the pretty is a code that tells us nothing. /* let errorCode = err.message let prettyError = null as any const showCode = false if (showCode) { switch (errorCode) { case 'There was an error getting the signing shares from the nodes': errorCode = 'LA-001' break case 'There was a timeout error executing the Javascript for this action': errorCode = 'LA-002' break case 'LitNodeClient is not ready. Please call await litNodeClient.connect() first.': errorCode = 'LA-003' break // we need catch and define the case for this error default: errorCode = 'LA-000' break } prettyError = `Request failed (${errorCode})` } throw new Error(prettyError || errorCode) */ // We do prefer to show error for sure and we do prefer the uggly; // cause the uggly is actually pretty and says everythin we want to know. // the pretty is a code that tells us nothing. // throw err // } // console.log('dt') // console.log(dt) return (0, exports.getValidPriceFromLitPriceResult)(dt); }; const testPricingAsset = async (request) => { try { switch (request.provider) { case 'Binance': const binanceParams = { symbol: request.params.symbol, key: request.auth?.key, secret: request.auth?.secret, useProxy: undefined, }; if (state.useProxyUntrusted === true) { binanceParams.useProxy = true; } return __1.LIT_ACTIONS.GET_PRICE_BINANCE_V2.testPricing(binanceParams); case 'IEX': return __1.LIT_ACTIONS.GET_PRICE_IEX_V2.testPricing({ symbol: request.params.symbol, key: request.auth?.key, }); case 'IG Group': return __1.LIT_ACTIONS.GET_PRICE_IG_V2.testPricing({ key: request.auth?.key, secret: request.auth?.secret, secret2: request.auth?.secret2, symbol: request.params.symbol, }); case 'CoinMarketCap': return __1.LIT_ACTIONS.GET_PRICE_COINMARKETCAP_V2.testPricing({ symbol: request.params.symbol, key: request.auth?.key, }); case 'CryptoCompare': return __1.LIT_ACTIONS.GET_PRICE_CRYPTOCOMPARE_V2.testPricing({ symbol: request.params.symbol, key: request.auth?.key, }); case 'Alpaca': return __1.LIT_ACTIONS.GET_PRICE_ALPACA_V2.testPricing({ symbol: request.params.symbol, key: request.auth?.key, secret: request.auth?.secret, }); case 'Tradestation': return __1.LIT_ACTIONS.GET_PRICE_TRADESTATION_V2.testPricing({ symbol: request.params.symbol, key: request.auth?.key, }); default: throw new Error('Provider not supported'); } } catch (err) { ; err.message = 'PRICING MODULE ERROR: ' + err.message; throw err; } }; exports.PricingModule = { config, getPriceDetails, testPricingAsset, }; //# sourceMappingURL=pricing.module.js.map