Helper functions

Helper Functions

This document provides detailed information about the various helper functions available to simplify the creation of adapters. Each section includes example implementations and links to real adapters using these functions.

Token Tracking Helpers

addTokensReceived

Tracks ERC20 token transfers received by specified addresses. Supports filtering by sender/receiver and custom token transformations. It attempts to use an indexer first for performance, with a fallback to log processing.

import { addTokensReceived } from '../../helpers/token';

const fetch: any = async (options: FetchOptions) => {
  const dailyFees = await addTokensReceived({
    options,
    tokens: ["0x4200000000000000000000000000000000000006"], // WETH on Base
    targets: ["0xbcb4a982d3c2786e69a0fdc0f0c4f2db1a04e875"] // Treasury
  })

  return { dailyFees, dailyRevenue: dailyFees }
}

Example Implementation - Synthetix

addGasTokensReceived

Tracks native token transfers (like ETH) received by specified multisig addresses. This helper is particularly useful for protocols that collect fees in the chain's native token.

getETHReceived

Tracks native token transfers on EVM chains, supporting multiple chains through Allium database queries.

Example Implementation - DexTools

getSolanaReceived

Fetches token transfers to specified Solana addresses within a given time range, with ability to exclude specific sender addresses or transaction signers.

Example Implementation - Jito

EVM Data Helpers

getLogs

Retrieves event logs from blockchains based on specified filters like target addresses, event signatures, and topics. Essential for tracking on-chain events.

Example Implementation - Ostium

queryIndexer

Executes queries against DeFiLlama's indexers to retrieve token transfers, events, and other blockchain data.

Example Implementation - Sudoswap V2

Protocol-Specific Helpers

compoundV2Export

Creates an adapter for Compound V2-like protocols, taking config parameters and returning an object that tracks fees, revenue, and distribution among holders and suppliers.

Example Implementation - Strike

uniV2Exports / getUniV2LogAdapter

Generates adapter configurations for Uniswap V2-style DEXes across multiple chains.

Example Implementation - Nile Exchange V1

Example Implementation - Hydrometer

Example Implementation - ABCDEFX

uniV3Exports

Creates adapters for Uniswap V3-style DEXes, supporting variable fees and multiple pools.

Example Implementation - 2thick

Query Engine Helpers

queryDuneSql

Executes SQL queries against the Dune Analytics database.

Example Implementation - Pumpswap

queryAllium

Queries the Allium database for blockchain data across multiple chains.

getGraphDimensions2

Alternative implementation of getGraphDimensions with a simplified approach for newer adapter versions.

Example Implementation - Quickswap

Chain-Specific Helpers

fetchTransactionFees

Retrieves the total transaction fees for a specific blockchain network within a given time range.

Helper Function Reference

You can find the full source code for these helper functions in the DefiLlama GitHub repository:

Exact day timestamp

Some data sources are only able to return data given a 00:00:00 day timestamp. In that case you can use the parameter startOfDay to get the timestamp of the specific day. For example passing 1663718399 (2022-09-20T23:59:59.000Z) timestamp will return 1663632000000 (2022-09-20T00:00:00.000Z)

Last updated

Was this helpful?