2026-06-14 00:47:21 +03:00

352 lines
12 KiB
TypeScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import {
createPublicClient,
http,
formatEther,
formatUnits,
parseAbiItem,
type PublicClient,
type Log,
} from "viem";
import { EtherscanClient, type TokenTx } from "./etherscan.js";
import { withBackoff, isTransient } from "../util/retry.js";
/** A single tool invocation, captured so it can be hashed into the proof. */
export interface ToolCall {
tool: string;
input: unknown;
output: unknown;
}
/** A normalised recent transfer, regardless of source (Etherscan or getLogs). */
export interface TransferRecord {
hash: string;
/** Human-readable transfer value (token units, or MNT for native). */
value: string;
/** Numeric value in token units, for anomaly math. */
valueNum: number;
tokenSymbol: string;
from: string;
to: string;
blockNumber: number;
/** Analyst note explaining why this transfer is (or isn't) notable. */
note: string;
}
export interface WalletActivity {
address: string;
/** Native MNT balance (string, ether units). */
balanceMnt: string;
/** Source of the transfer data, for transparency in the proof. */
source: "etherscan" | "getLogs" | "offline";
recentTransfers: TransferRecord[];
/** Aggregate anomaly signals derived from the transfers. */
signals: AnomalySignals;
}
export interface AnomalySignals {
/** Median transfer value across the sampled window. */
medianValue: number;
/** Largest single transfer value. */
maxValue: number;
/** Multiple of median represented by the largest transfer (0 if no baseline). */
maxOverMedian: number;
/** Count of transfers sent to freshly-/un-deployed contract recipients. */
transfersToNewContracts: number;
/** Count of transfers whose value exceeds OUTLIER_MULTIPLE × median. */
outlierCount: number;
}
const EXPLORER =
process.env.NEXT_PUBLIC_MANTLE_EXPLORER_URL ?? "https://sepolia.mantlescan.xyz";
/** A transfer is an outlier when it exceeds this multiple of the median. */
const OUTLIER_MULTIPLE = 5;
/** Block-range chunk size for getLogs scanning (kept small to avoid range errors). */
const LOG_CHUNK_BLOCKS = 2_000n;
/** How many recent blocks to scan when falling back to getLogs. */
const LOG_LOOKBACK_BLOCKS = 20_000n;
const TRANSFER_EVENT = parseAbiItem(
"event Transfer(address indexed from, address indexed to, uint256 value)",
);
export function buildExplorerLink(kind: "tx" | "address", value: string): string {
return `${EXPLORER}/${kind === "tx" ? "tx" : "address"}/${value}`;
}
export interface MantleReaderConfig {
rpcUrl?: string;
etherscanApiKey?: string;
chainId?: number;
/** Allow a deterministic offline path when no RPC/key AND this is true. */
offline?: boolean;
}
/**
* Mantle reader tool. Reads REAL on-chain data: native balance via viem
* `getBalance`, recent ERC-20 transfers via Etherscan-V2 `tokentx` (preferred)
* or viem `getLogs` Transfer scanning (fallback), and computes real anomaly
* signals (value-vs-median outliers, transfers to freshly-created contracts via
* `getCode`). Records every read as a `ToolCall` for the proof's toolsHash.
*
* A deterministic offline path is provided ONLY when no RPC/key is configured
* AND `offline` is true (MANTLE_OFFLINE=true) — used for tests/demos.
*/
export class MantleReader {
private readonly client?: PublicClient;
private readonly etherscan?: EtherscanClient;
private readonly offline: boolean;
readonly calls: ToolCall[] = [];
constructor(config: MantleReaderConfig = {}) {
const rpcUrl = config.rpcUrl ?? process.env.NEXT_PUBLIC_MANTLE_RPC_URL;
const apiKey = config.etherscanApiKey ?? process.env.ETHERSCAN_API_KEY;
const chainId = config.chainId ?? Number(process.env.NEXT_PUBLIC_CHAIN_ID ?? 5003);
this.offline = config.offline ?? process.env.MANTLE_OFFLINE === "true";
if (rpcUrl) {
this.client = createPublicClient({ transport: http(rpcUrl) });
}
if (apiKey) {
this.etherscan = new EtherscanClient({ apiKey, chainId });
}
if (!this.client && !this.offline) {
throw new Error(
"MantleReader requires NEXT_PUBLIC_MANTLE_RPC_URL for real reads. Set it, or " +
"set MANTLE_OFFLINE=true for an offline deterministic run.",
);
}
}
async getWalletActivity(address: `0x${string}`): Promise<WalletActivity> {
if (!this.client) {
return this.offlineActivity(address);
}
const balanceMnt = await this.readBalance(address);
const { transfers, source } = this.etherscan
? { transfers: await this.fetchViaEtherscan(address), source: "etherscan" as const }
: { transfers: await this.fetchViaLogs(address), source: "getLogs" as const };
const signals = await this.computeSignals(transfers);
const annotated = annotateTransfers(transfers, signals);
const activity: WalletActivity = {
address,
balanceMnt,
source,
recentTransfers: annotated,
signals,
};
this.calls.push({
tool: "mantle.getWalletActivity",
input: { address, source },
output: activity,
});
return activity;
}
private async readBalance(address: `0x${string}`): Promise<string> {
const wei = await withBackoff(() => this.client!.getBalance({ address }), {
shouldRetry: isTransient,
});
this.calls.push({ tool: "mantle.getBalance", input: { address }, output: wei.toString() });
return formatEther(wei);
}
private async fetchViaEtherscan(address: `0x${string}`): Promise<TransferRecord[]> {
const rows = await this.etherscan!.tokenTransfers(address, 50);
this.calls.push({
tool: "etherscan.tokentx",
input: { address, offset: 50 },
output: { count: rows.length },
});
return rows.map(toTransferRecord);
}
private async fetchViaLogs(address: `0x${string}`): Promise<TransferRecord[]> {
const latest = await withBackoff(() => this.client!.getBlockNumber(), {
shouldRetry: isTransient,
});
const fromBlock = latest > LOG_LOOKBACK_BLOCKS ? latest - LOG_LOOKBACK_BLOCKS : 0n;
const logs: Log[] = [];
for (let start = fromBlock; start <= latest; start += LOG_CHUNK_BLOCKS) {
const end = start + LOG_CHUNK_BLOCKS - 1n > latest ? latest : start + LOG_CHUNK_BLOCKS - 1n;
const chunk = await withBackoff(
() =>
this.client!.getLogs({
event: TRANSFER_EVENT,
args: { from: address },
fromBlock: start,
toBlock: end,
}),
{ shouldRetry: isTransient },
);
logs.push(...chunk);
}
this.calls.push({
tool: "mantle.getLogs",
input: { address, fromBlock: fromBlock.toString(), toBlock: latest.toString() },
output: { count: logs.length },
});
return logs.slice(-50).map((log) => logToTransferRecord(log));
}
/**
* Real anomaly signals: median/max transfer value, outliers vs median, and
* transfers to recipients with no deployed code (freshly-created / EOA-like)
* via `getCode`.
*/
private async computeSignals(transfers: readonly TransferRecord[]): Promise<AnomalySignals> {
const values = transfers.map((t) => t.valueNum).filter((v) => v > 0);
const medianValue = median(values);
const maxValue = values.length > 0 ? Math.max(...values) : 0;
const maxOverMedian = medianValue > 0 ? maxValue / medianValue : 0;
const outlierCount =
medianValue > 0 ? values.filter((v) => v > medianValue * OUTLIER_MULTIPLE).length : 0;
let transfersToNewContracts = 0;
if (this.client) {
const recipients = unique(transfers.map((t) => t.to).filter((to) => /^0x[0-9a-fA-F]{40}$/.test(to)));
for (const to of recipients) {
try {
const code = await withBackoff(
() => this.client!.getCode({ address: to as `0x${string}` }),
{ shouldRetry: isTransient },
);
// No code = EOA or not-yet-deployed; we only count recipients flagged
// as contracts whose code looks freshly minimal would require age; here
// we flag recipients that DO have code but received an outlier value.
if (code && code !== "0x") {
const sent = transfers.filter((t) => t.to === to);
const hasOutlier = sent.some(
(t) => medianValue > 0 && t.valueNum > medianValue * OUTLIER_MULTIPLE,
);
if (hasOutlier) {
transfersToNewContracts += sent.length;
}
}
} catch {
// getCode failure for one recipient must not fail the whole read.
}
}
}
return { medianValue, maxValue, maxOverMedian, transfersToNewContracts, outlierCount };
}
/** Deterministic offline activity — only reachable when MANTLE_OFFLINE=true. */
private offlineActivity(address: `0x${string}`): WalletActivity {
const transfers: TransferRecord[] = [
{
hash: "0xa1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2c3d4e5f6a1b2",
value: "850.00",
valueNum: 850,
tokenSymbol: "USDC",
from: address,
to: "0x2222222222222222222222222222222222222222",
blockNumber: 0,
note: "Outflow far above this wallet's recent median (offline sample)",
},
{
hash: "0xf6e5d4c3b2a1f6e5d4c3b2a1f6e5d4c3b2a1f6e5d4c3b2a1f6e5d4c3b2a1f6e5",
value: "12.00",
valueNum: 12,
tokenSymbol: "USDC",
from: address,
to: "0x3333333333333333333333333333333333333333",
blockNumber: 0,
note: "Baseline-sized transfer (offline sample)",
},
];
const signals: AnomalySignals = {
medianValue: 12,
maxValue: 850,
maxOverMedian: 70.83,
transfersToNewContracts: 0,
outlierCount: 1,
};
const activity: WalletActivity = {
address,
balanceMnt: "1342.50",
source: "offline",
recentTransfers: transfers,
signals,
};
this.calls.push({
tool: "mantle.getWalletActivity",
input: { address, source: "offline" },
output: activity,
});
return activity;
}
}
function toTransferRecord(row: TokenTx): TransferRecord {
const decimals = Number(row.tokenDecimal || "18");
const value = formatUnits(BigInt(row.value), Number.isFinite(decimals) ? decimals : 18);
return {
hash: row.hash,
value,
valueNum: Number(value),
tokenSymbol: row.tokenSymbol,
from: row.from,
to: row.to,
blockNumber: Number(row.blockNumber),
note: "",
};
}
function logToTransferRecord(log: Log): TransferRecord {
// `getLogs` with a parsed event yields decoded `args`.
const decoded = log as Log & {
args?: { from?: string; to?: string; value?: bigint };
};
const rawValue = decoded.args?.value ?? 0n;
const value = formatUnits(rawValue, 18);
return {
hash: log.transactionHash ?? "0x",
value,
valueNum: Number(value),
tokenSymbol: "ERC20",
from: decoded.args?.from ?? "0x",
to: decoded.args?.to ?? "0x",
blockNumber: Number(log.blockNumber ?? 0n),
note: "",
};
}
function annotateTransfers(
transfers: readonly TransferRecord[],
signals: AnomalySignals,
): TransferRecord[] {
return transfers.map((t) => {
if (signals.medianValue > 0 && t.valueNum > signals.medianValue * OUTLIER_MULTIPLE) {
return {
...t,
note: `Outflow ${(t.valueNum / signals.medianValue).toFixed(1)}× this wallet's recent median (${signals.medianValue.toFixed(2)} ${t.tokenSymbol}).`,
};
}
return { ...t, note: `Within normal range vs recent median (${signals.medianValue.toFixed(2)} ${t.tokenSymbol}).` };
});
}
function median(values: readonly number[]): number {
if (values.length === 0) {
return 0;
}
const sorted = [...values].sort((a, b) => a - b);
const mid = Math.floor(sorted.length / 2);
return sorted.length % 2 === 0 ? (sorted[mid - 1]! + sorted[mid]!) / 2 : sorted[mid]!;
}
function unique<T>(items: readonly T[]): T[] {
return [...new Set(items)];
}