UNPKG

hm-aftermath-ts-sdk

Version:
176 lines (175 loc) 7.43 kB
"use strict"; var _a; Object.defineProperty(exports, "__esModule", { value: true }); exports.DcaApiCasting = void 0; const utils_1 = require("../../../general/utils"); class DcaApiCasting { } exports.DcaApiCasting = DcaApiCasting; _a = DcaApiCasting; // ========================================================================= // Chain Event objects // ========================================================================= DcaApiCasting.createdDcaOrderEventFromOnChain = (eventOnChain) => { const fields = eventOnChain.parsedJson; return { orderId: utils_1.Helpers.addLeadingZeroesToType(fields.order_id), owner: utils_1.Helpers.addLeadingZeroesToType(fields.user), inputValue: BigInt(fields.input_amount), inputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.input_type).toString()), outputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.output_type).toString()), gasValue: BigInt(fields.gas_amount), frequencyMs: Number(fields.frequency_ms), startTimestampMs: Number(fields.start_timestamp_ms), amountPerTrade: BigInt(fields.amount_per_trade), maxAllowableSlippageBps: BigInt(fields.max_allowable_slippage_bps), minAmountOut: BigInt(fields.min_amount_out), maxAmountOut: BigInt(fields.max_amount_out), remainingTrades: BigInt(fields.remaining_trades), timestamp: eventOnChain.timestampMs, txnDigest: eventOnChain.id.txDigest, type: eventOnChain.type, recipient: utils_1.Helpers.addLeadingZeroesToType(fields.recipient), }; }; DcaApiCasting.closedDcaOrderEventFromOnChain = (eventOnChain) => { const fields = eventOnChain.parsedJson; return { orderId: utils_1.Helpers.addLeadingZeroesToType(fields.order_id), owner: utils_1.Helpers.addLeadingZeroesToType(fields.user), remainingValue: BigInt(fields.remaining_amount), inputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.input_type).toString()), outputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.output_type).toString()), gasValue: BigInt(fields.gas_amount), frequencyMs: Number(fields.frequency_ms), lastTradeTimestampMs: Number(fields.last_trade_timestamp_ms), amountPerTrade: BigInt(fields.amount_per_trade), maxAllowableSlippageBps: BigInt(fields.max_allowable_slippage_bps), minAmountOut: BigInt(fields.min_amount_out), maxAmountOut: BigInt(fields.max_amount_out), remainingTrades: BigInt(fields.remaining_trades), timestamp: eventOnChain.timestampMs, txnDigest: eventOnChain.id.txDigest, type: eventOnChain.type, }; }; DcaApiCasting.executedTradeEventFromOnChain = (eventOnChain) => { const fields = eventOnChain.parsedJson; return { orderId: utils_1.Helpers.addLeadingZeroesToType(fields.order_id), user: utils_1.Helpers.addLeadingZeroesToType(fields.user), inputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.input_type).toString()), inputAmount: BigInt(fields.input_amount), outputType: utils_1.Helpers.addLeadingZeroesToType("0x" + Buffer.from(fields.output_type).toString()), outputAmount: BigInt(fields.output_amount), timestamp: eventOnChain.timestampMs, txnDigest: eventOnChain.id.txDigest, type: eventOnChain.type, }; }; // ========================================================================= // Indexer // ========================================================================= DcaApiCasting.createdOrderEventOnIndexer = (response) => { const totalOrdersAmount = Number(response.orders_num); const ordersLeft = Number(response.orders_left); const progress = totalOrdersAmount > 0 ? (totalOrdersAmount - ordersLeft) / totalOrdersAmount : 1; const inputCoinType = utils_1.Helpers.addLeadingZeroesToType(String(response.coin_sell)); const outputCoinType = utils_1.Helpers.addLeadingZeroesToType(String(response.coin_buy)); const strategy = BigInt(response.min_amount_out) === BigInt(0) && BigInt(response.max_amount_out) === utils_1.Casting.u64MaxBigInt ? undefined : { minPrice: BigInt(response.min_amount_out), maxPrice: BigInt(response.max_amount_out), }; const { totalSpent, totalBought } = response.trades.reduce((total, order) => { total.totalSpent += BigInt(order.input_amount); total.totalBought += BigInt(order.output_amount); return total; }, { totalSpent: BigInt(0), totalBought: BigInt(0), }); const tradesPrepared = response.trades.map((trade) => { return _a.createdOrderTradeEventOnIndexer(trade, inputCoinType, outputCoinType); }); const started = tradesPrepared.length > 0 ? { timestamp: tradesPrepared[0].tnxDate, digest: tradesPrepared[0].tnxDigest, } : { timestamp: Number(response.next_execution_timestamp_ms), digest: "", }; const lastTrade = tradesPrepared.length > 0 ? { timestamp: tradesPrepared[tradesPrepared.length - 1].tnxDate, digest: tradesPrepared[tradesPrepared.length - 1] .tnxDigest, } : undefined; const failed = response.failed.map((element) => { return { timestamp: element.timestamp_ms, reason: element.reason, }; }); return { objectId: utils_1.Helpers.addLeadingZeroesToType(response.order_object_id), overview: { allocatedCoin: { coin: inputCoinType, amount: BigInt(response.coin_sell_amount), }, buyCoin: { coin: outputCoinType, amount: totalBought, }, totalSpent: totalSpent, intervalMs: Number(response.frequency_ms), totalTrades: totalOrdersAmount, tradesRemaining: ordersLeft, maxSlippageBps: Number(response.slippage), strategy: strategy, progress: progress, recipient: utils_1.Helpers.addLeadingZeroesToType(response.recipient), created: { time: response.created.timestamp, tnxDigest: response.created.tx_digest, }, nextTrade: { time: started.timestamp, tnxDigest: started.digest, }, lastExecutedTrade: lastTrade ? { time: lastTrade.timestamp, tnxDigest: lastTrade.digest, } : undefined, }, failed: failed, trades: tradesPrepared, }; }; DcaApiCasting.createdOrderTradeEventOnIndexer = (response, inputCoinType, outputCoinType) => { const inputAmount = BigInt(response.input_amount); const outputAmount = BigInt(response.output_amount); return { allocatedCoin: { coin: inputCoinType, amount: inputAmount, }, buyCoin: { coin: outputCoinType, amount: outputAmount, }, tnxDigest: response.event.tx_digest, tnxDate: response.event.timestamp, rate: undefined, }; };