UNPKG

@guaritos/tracer-engine

Version:

A highly performant and scalable multi-hop, time-aware tracer for account-based blockchain transactions, designed for off-chain risk assessment and flow analysis.

180 lines 6.08 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.AggregatedEdgeProfit = exports.AggregatedEdge = exports.Profit = exports.WeightedEdge = exports.Edge = void 0; class Edge { constructor(_from = "", _to = "", _symbol = "", _value = 0, _type = "", _timestamp = 0, _hash = "", _metadata) { /** * Edge is a class that represents a financial transaction edge in the TTR strategy. * It contains the from address, to address, symbol, value, and an optional timestamp * associated with the edge. * * @param _from - The address from which the transaction originates. * @param _to - The address to which the transaction is directed. * @param _symbol - The symbol of the asset being transferred. * @param _value - The value of the asset being transferred. * @param _hash - The hash of the transaction. * @param _timestamp - The timestamp of the transaction. * @param _metadata - Extra info. */ this.from = ""; this.to = ""; this.symbol = ""; this.value = 0; this.hash = ""; this.timestamp = 0; this.from = _from; this.to = _to; this.symbol = _symbol; this.value = _value; this.hash = _hash; this.timestamp = _timestamp; this.metadata = _metadata; } } exports.Edge = Edge; class WeightedEdge { constructor(params) { this.from = params.from; this.to = params.to; this.weight = params.weight; this.symbol = params.symbol; this.hash = params.hash; this.timestamp = params.timestamp; this.metadata = params.metadata; } } exports.WeightedEdge = WeightedEdge; class Profit { constructor(symbol, value, timestamp) { this.symbol = symbol; this.value = value; this.timestamp = timestamp; this.symbol = symbol; this.value = value; this.timestamp = timestamp; } } exports.Profit = Profit; class AggregatedEdge { constructor(_hash, _profits, _aggregated_edges, _metadata) { this.hash = _hash; this.profits = _profits; this.aggregated_edges = _aggregated_edges; this.metadata = _metadata; } aggregate(aggregated_edge) { if (aggregated_edge === null || aggregated_edge === undefined) { return this; } if (!(aggregated_edge instanceof AggregatedEdge)) { throw new Error("Invalid aggregated edge"); } // 1. collect all edges this.aggregated_edges.push(...aggregated_edge.aggregated_edges); // 2. according to symbol to classify profit and aggregate const aggregated_profits = new Map(); for (const profit of [...this.profits, ...aggregated_edge.profits]) { const key = (profit.symbol, profit.address); let _profit = aggregated_profits.get(key); if (_profit === undefined) { if (profit.value !== 0) { aggregated_profits.set(key, profit); } continue; } if (_profit.value + profit.value === 0) { aggregated_profits.delete(key); continue; } let sgn = _profit.value > 0 ? 1 : -1; sgn *= _profit.value + profit.value > 0 ? 1 : -1; const aggregated_value = _profit.value + profit.value; if (sgn < 0) { _profit = profit; } _profit.value = aggregated_value; aggregated_profits.set(key, _profit); } this.profits = Array.from(aggregated_profits.values()); return this; } get_input_profit(symbol) { for (const profit of this.profits) { if (profit.symbol === symbol && profit.value > 0) { return profit; } } return null; } get_output_profit(symbol) { for (const profit of this.profits) { if (profit.symbol === symbol && profit.value < 0) { return profit; } } return null; } get_output_profits() { const rlt = []; for (const profit of this.profits) { if (profit.value < 0) { rlt.push(profit); } } return rlt; } get_input_profits() { const rlt = []; for (const profit of this.profits) { if (profit.value > 0) { rlt.push(profit); } } return rlt; } get_output_symbols() { const symbols = new Set(); for (const profit of this.profits) { if (profit.value < 0) { symbols.add(profit.symbol); } } return symbols; } get_input_symbols() { const symbols = new Set(); for (const profit of this.profits) { if (profit.value > 0) { symbols.add(profit.symbol); } } return symbols; } get_timestamp() { let timestamp = 0; if (this.profits.length > 0) { timestamp = this.profits[0].timestamp; } return timestamp; } } exports.AggregatedEdge = AggregatedEdge; class AggregatedEdgeProfit { /** * AggregatedEdgeProfit is a class that represents a profit associated with an aggregated edge. * It contains the address, value, timestamp, and symbol of the profit. * * @param _address - The address associated with the profit. * @param _value - The value of the profit. * @param _timestamp - The timestamp of the profit. * @param _symbol - The symbol associated with the profit. */ constructor(_address, _value, _timestamp, _symbol) { this.address = _address; this.value = _value; this.timestamp = _timestamp; this.symbol = _symbol; } } exports.AggregatedEdgeProfit = AggregatedEdgeProfit; //# sourceMappingURL=ttr_defs.js.map