Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions fees/ethereum-wip/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
import { Row } from "@clickhouse/client"
import { FetchOptions, ProtocolType, Adapter } from "../../adapters/types";
import { METRIC } from "../../helpers/metrics";
import { queryClickhouse } from "../../helpers/clickhouse";
import { CHAIN } from "../../helpers/chains";

type FeesRow = Row & {
total_fees_wei: string;
base_burn_wei: string;
};

export const SQL_BLOCK_RANGE = `
SELECT
CAST(
sum(toDecimal256(effective_gas_price, 0) * toDecimal256(gas_used, 0))
AS String
) AS total_fees_wei,
CAST(
sum(toDecimal256(base_fee_per_gas, 0) * toDecimal256(gas_used, 0))
AS String
) AS base_burn_wei
FROM evm_indexer.transactions
WHERE
chain = {chain:UInt64}
AND block_number >= {fromBlock:UInt32}
AND block_number < {toBlock:UInt32}
`;

export const fetch = async (options: FetchOptions) => {
const chainId = options.api.chainId
const fromBlock = await options.getFromBlock()
const _toBlock = await options.getToBlock()
const safeBlock = _toBlock - 150

if (safeBlock <= fromBlock) {
const dailyFees = options.createBalances();
const dailyRevenue = options.createBalances();
return { dailyFees, dailyRevenue, dailyHoldersRevenue: dailyRevenue };
}

const rows = await queryClickhouse<FeesRow>(SQL_BLOCK_RANGE, {
chain: chainId,
fromBlock,
toBlock: safeBlock,
});

const totalFeesWei = BigInt(rows?.[0]?.total_fees_wei ?? "0");
const baseFeesWei = BigInt(rows?.[0]?.base_burn_wei ?? "0");
const priorityWei = totalFeesWei - baseFeesWei;

const dailyFees = options.createBalances();
const dailyRevenue = options.createBalances();

dailyFees.addGasToken(baseFeesWei, METRIC.TRANSACTION_BASE_FEES);
dailyFees.addGasToken(priorityWei, METRIC.TRANSACTION_PRIORITY_FEES);
dailyRevenue.addGasToken(baseFeesWei, METRIC.TRANSACTION_BASE_FEES);

return { dailyFees, dailyRevenue, dailyHoldersRevenue: dailyRevenue };
}

const adapter: Adapter = {
version: 2,
adapter: {
[CHAIN.ETHEREUM]: {
fetch,
start: '2015-07-30',
},
},
protocolType: ProtocolType.CHAIN,
methodology: {
Fees: 'Total ETH gas fees (including base fees and priority fees) paid by users',
Revenue: 'Amount of ETH base fees that were burned',
HoldersRevenue: 'Amount of ETH base fees that were burned',
},
breakdownMethodology: {
Fees: {
[METRIC.TRANSACTION_BASE_FEES]: 'Total ETH base fees paid by users',
[METRIC.TRANSACTION_PRIORITY_FEES]: 'Total ETH priority fees paid by users',
},
Revenue: {
[METRIC.TRANSACTION_BASE_FEES]: 'Total ETH base fees will be burned',
},
HoldersRevenue: {
[METRIC.TRANSACTION_BASE_FEES]: 'Total ETH base fees will be burned',
},
}
}

export default adapter;
95 changes: 95 additions & 0 deletions helpers/clickhouse.ts
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wondering if should move this to @defillama/sdk repo

this way, code is not repeated

Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { ClickHouseClient, createClient, Row } from "@clickhouse/client";

let client: ClickHouseClient | null = null;
let connectionPromise: Promise<ClickHouseClient> | null = null;
let hooksInstalled = false;

type ClickhouseConfig = {
host: string;
port: number | string;
database: string;
username: string;
password: string;
};

const DEFAULT_TIMEOUT = 180_000;
const DEFAULT_MAX_CONN = 10;
const DEFAULT_KEEPALIVE_TTL = 180_000;

function readConfig(): ClickhouseConfig {
const raw = process.env.CLICKHOUSE_CONFIG;
if (!raw) throw new Error("Missing CLICKHOUSE_CONFIG.");

const cfg = JSON.parse(raw) as Partial<ClickhouseConfig>;

if (!cfg.host || !cfg.port || !cfg.database || !cfg.username || !cfg.password) {
throw new Error('CLICKHOUSE_CONFIG must include "host","port","database","username","password".');
}

return {
host: cfg.host,
port: cfg.port,
database: cfg.database,
username: cfg.username,
password: cfg.password,
};
}

function buildUrl(cfg: ClickhouseConfig): string {
return `http://${cfg.host}:${cfg.port}`;
}


function installShutdownHooks() {
if (hooksInstalled) return;
hooksInstalled = true;

const cleanup = async () => { try { await disconnectClickhouse(); } catch {} };

["SIGINT", "SIGTERM", "SIGHUP"].forEach((sig) => process.once(sig, () => { void cleanup().then(() => process.exit(0)) }));
process.once("beforeExit", () => { void cleanup() });
process.once("uncaughtException", () => { void cleanup().then(() => process.exit(1)) });
process.once("unhandledRejection", () => { void cleanup().then(() => process.exit(1)) });
}

export async function connectClickhouse(): Promise<ClickHouseClient> {
if (client) return client;
if (connectionPromise) return connectionPromise;

installShutdownHooks();

connectionPromise = (async () => {
const cfg = readConfig();
const url = buildUrl(cfg);

const _client = createClient({
url,
username: cfg.username,
password: cfg.password,
database: cfg.database,
request_timeout: DEFAULT_TIMEOUT,
max_open_connections: DEFAULT_MAX_CONN,
keep_alive: { enabled: true, idle_socket_ttl: DEFAULT_KEEPALIVE_TTL },
compression: { response: true, request: false },
});

await _client.ping();
client = _client;
return _client;
})();

return connectionPromise;
}

export async function queryClickhouse<T extends Row>(sql: string, params?: Record<string, unknown>): Promise<T[]> {
const c = await connectClickhouse();
const rs = await c.query({ query: sql, query_params: params, format: "JSONEachRow" });
return rs.json<T>();
}

export async function disconnectClickhouse() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add auto disconnect on exit event

if (!client) return;
try { await client.close(); }
finally { client = null; connectionPromise = null; }
}

5 changes: 5 additions & 0 deletions helpers/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ export const ENV_KEYS = new Set([
'DUNE_BULK_MODE',
'DUNE_BULK_MODE_BATCH_TIME',
'LLAMA_HL_INDEXER',
'CLICKHOUSE_HOST',
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer a single env var, can be a json string, check what we are doing in server repo

'CLICKHOUSE_PORT',
'CLICKHOUSE_USERNAME',
'CLICKHOUSE_PASSWORD',
'CLICKHOUSE_DATABASE'
])

// This is done to support both ZEROx_API_KEY and ZEROX_API_KEY
Expand Down
Loading
Loading