Skip to main content
Schema: somnia.defi Table: ez_lending_repayments Type: View

What

This table contains loan repayment transactions across lending protocols. Repayments reduce or eliminate outstanding debt positions, with amounts including both principal and accrued interest. Understanding repayment patterns helps analyze user behavior and protocol health.

Key Use Cases

  • Tracking loan lifecycle and duration analysis
  • Calculating interest paid on borrowed positions
  • Understanding repayment patterns by user segment
  • Monitoring protocol revenue from interest
  • Analyzing third-party repayment activity

Important Relationships

  • Links to ez_lending_borrows for original loan details
  • Connects to ez_lending_liquidations (forced repayments)
  • May reference ez_lending_deposits for collateral release
  • Uses price.ez_prices_hourly for USD valuations

Commonly-used Fields

  • borrower: Address with the loan
  • payer: Address making the payment (may differ)
  • platform: Lending protocol
  • token_address/token_symbol: Repaid asset
  • amount/amount_usd: Repayment quantity

Sample queries

-- Daily repayment volume and metrics
SELECT 
    DATE_TRUNC('day', block_timestamp) AS date,
    platform,
    COUNT(*) AS repayment_count,
    COUNT(DISTINCT borrower) AS unique_borrowers,
    SUM(amount_usd) AS total_repaid_usd,
    AVG(amount_usd) AS avg_repayment_size_usd
FROM <blockchain_name>.defi.ez_lending_repayments
WHERE block_timestamp >= CURRENT_DATE - 30
    AND amount_usd IS NOT NULL
GROUP BY 1, 2
ORDER BY 1 DESC, 5 DESC;

-- Loan duration analysis
WITH loan_lifecycles AS (
    SELECT 
        b.borrower,
        b.platform,
        b.token_symbol,
        b.block_timestamp AS borrow_time,
        MIN(r.block_timestamp) AS first_repayment_time,
        SUM(b.amount_usd) AS borrowed_usd,
        SUM(r.amount_usd) AS total_repaid_usd
    FROM <blockchain_name>.defi.ez_lending_borrows b
    INNER JOIN <blockchain_name>.defi.ez_lending_repayments r
        ON b.borrower = r.borrower
        AND b.platform = r.platform
        AND b.token_address = r.token_address
        AND r.block_timestamp > b.block_timestamp
    WHERE b.block_timestamp >= CURRENT_DATE - 90
        AND b.amount_usd IS NOT NULL
        AND r.amount_usd IS NOT NULL
    GROUP BY 1, 2, 3, 4
)
SELECT 
    platform,
    token_symbol,
    AVG(DATEDIFF('day', borrow_time, first_repayment_time)) AS avg_days_to_first_repayment,
    PERCENTILE_CONT(0.5) WITHIN GROUP (ORDER BY DATEDIFF('day', borrow_time, first_repayment_time)) AS median_days,
    COUNT(*) AS loan_count,
    AVG(total_repaid_usd / NULLIF(borrowed_usd, 0) - 1) * 100 AS avg_interest_paid_pct
FROM loan_lifecycles
GROUP BY 1, 2
HAVING COUNT(*) > 10
ORDER BY 3;

-- Repayment patterns by user segment
WITH user_repayment_stats AS (
    SELECT 
        borrower,
        COUNT(*) AS repayment_count,
        SUM(amount_usd) AS total_repaid_usd,
        AVG(amount_usd) AS avg_repayment_size,
        COUNT(DISTINCT DATE_TRUNC('month', block_timestamp)) AS active_months,
        COUNT(DISTINCT token_address) AS unique_assets_repaid
    FROM <blockchain_name>.defi.ez_lending_repayments
    WHERE block_timestamp >= CURRENT_DATE - 180
        AND amount_usd IS NOT NULL
    GROUP BY 1
)
SELECT 
    CASE 
        WHEN total_repaid_usd < 1000 THEN 'Micro (<$1K)'
        WHEN total_repaid_usd < 10000 THEN 'Small ($1K-$10K)'
        WHEN total_repaid_usd < 100000 THEN 'Medium ($10K-$100K)'
        WHEN total_repaid_usd < 1000000 THEN 'Large ($100K-$1M)'
        ELSE 'Whale (>$1M)'
    END AS borrower_segment,
    COUNT(*) AS borrower_count,
    AVG(repayment_count) AS avg_repayments_per_user,
    AVG(avg_repayment_size) AS avg_repayment_size,
    AVG(active_months) AS avg_active_months
FROM user_repayment_stats
GROUP BY 1
ORDER BY 2 DESC;

-- Asset-specific repayment velocity
SELECT 
    token_symbol,
    platform,
    COUNT(*) AS repayment_transactions,
    COUNT(DISTINCT borrower) AS unique_repayers,
    SUM(amount_usd) AS total_usd_repaid,
    AVG(amount_usd) AS avg_repayment_usd,
    SUM(amount_usd) / COUNT(DISTINCT DATE_TRUNC('day', block_timestamp)) AS daily_velocity_usd
FROM <blockchain_name>.defi.ez_lending_repayments
WHERE block_timestamp >= CURRENT_DATE - 30
    AND token_symbol IS NOT NULL
    AND amount_usd IS NOT NULL
GROUP BY 1, 2
HAVING COUNT(*) > 50
ORDER BY 8 DESC;

-- Large repayments monitoring
SELECT 
    block_timestamp,
    tx_hash,
    platform,
    borrower,
    payer,
    token_symbol,
    amount_usd,
    CASE WHEN borrower = payer THEN 'Self' ELSE 'Third-party' END AS repayment_type
FROM <blockchain_name>.defi.ez_lending_repayments
WHERE amount_usd > 500000
    AND block_timestamp >= CURRENT_DATE - 7
ORDER BY amount_usd DESC;

Columns

Column NameData TypeDescription
BLOCK_NUMBERNUMBERSequential counter representing the position of a block in the blockchain since genesis (block 0). Key Facts: Immutable once finalized Primary ordering mechanism for blockchain data Increments by 1 for each new block Used as a proxy for time in many analyses Usage in Queries: Important: Block numbers are chain-specific. Block 15000000 on Ethereum ≠ block 15000000 on Polygon.
BLOCK_TIMESTAMPTIMESTAMP_NTZUTC timestamp when the block was produced by validators/miners. Format: TIMESTAMP_NTZ (no timezone) Precision: Second-level accuracy Reliability: Set by block producer Can have minor variations (±15 seconds) Always increasing (newer blocks = later timestamps) Best Practices: Note: Use for time-series analysis, but be aware that block production rates vary by chain.
ORIGIN_FROM_ADDRESSTEXTThe externally-owned account (EOA) or contract address that initiated the transaction. Key Points: Always 42 characters (0x + 40 hex chars) Lowercase normalized in all tables Cannot be NULL for valid transactions For contract creation: sender of creation transaction Common Patterns: EOA → EOA: Simple transfer EOA → Contract: User interaction Contract → Contract: Internal calls (see fact_traces) Known addresses: Exchange hot wallets, protocol deployers Query Examples:
ORIGIN_TO_ADDRESSTEXTThe destination address for the transaction - either an EOA or contract address. Special Cases: NULL: Contract creation transaction Contract address: Interacting with smart contract EOA address: Simple transfer or receiving funds Important Patterns: Note: For token transfers, this is the token contract, not the recipient. See eztokentransfers tables for recipient.
ORIGIN_FUNCTION_SIGNATURETEXTFunction signature (first 4 bytes) of the called method. Format: 0x + 8 hex characters Common Signatures: 0xa9059cbb: transfer(address,uint256) 0x095ea7b3: approve(address,uint256) 0x23b872dd: transferFrom(address,address,uint256) Note: NULL for simple transfers or invalid calls
TX_HASHTEXTUnique 66-character identifier for the transaction. Format: 0x + 64 hexadecimal characters Usage: Primary key for transaction lookups Join key for traces, logs, and token transfers Immutable once confirmed Example: 0x5c504ed432cb51138bcf09aa5e8a410dd4a1e204ef84bfed1be16dfba1b22060
EVENT_INDEXNUMBERZero-based sequential position of the event within a transaction’s execution. Key Facts: Starts at 0 for first event Increments across all contracts in transaction Preserves execution order Essential for deterministic event ordering Usage Example:
CONTRACT_ADDRESSTEXTSmart contract address that emitted this event or received the transaction. Key Points: Always the immediate event emitter for logs May differ from transaction to_address Lowercase normalized format Never NULL for valid events
EVENT_NAMETEXTThe event name as defined in the contract’s ABI. Format: PascalCase event identifier Examples: Transfer - Token transfers Swap - DEX trades OwnershipTransferred - Admin changes Approval - Token approvals Usage Pattern:
PLATFORMTEXTThe lending protocol where the transaction occurred. Example: ‘aave’
PAYERTEXTThe address that paid the loan or deposit. Example: ‘0x5555555555555555555555555555555555555555’
BORROWERTEXTThe address that initiated a borrow or repayment transaction. Example: ‘0x1234567890123456789012345678901234567890’
PROTOCOL_MARKETTEXTThe lending protocol’s receipt token issued to depositors. Example: ‘0xfedcbafedcbafedcbafedcbafedcbafedcbafed’
TOKEN_ADDRESSTEXTThe contract address of the underlying asset being lent or borrowed. Example: ‘0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48’
TOKEN_SYMBOLTEXTThe ticker symbol of the asset involved in the lending transaction. Example: ‘USDC’
AMOUNT_UNADJNUMBERThe raw amount of tokens borrowed or repaid without decimal adjustment. Example: 1000000000
AMOUNTFLOATThe decimal-adjusted quantity of tokens in the transaction. Example: 1000.5
AMOUNT_USDFLOATThe USD value of tokens at transaction time. Example: 1500.75
EZ_LENDING_REPAYMENTS_IDTEXTPrimary key - unique identifier for each row ensuring data integrity. Format: Usually VARCHAR containing composite key generated using MD5 hash of the relevant columns. Example: MD5(blocknumber, txhash, trace_index) Usage: Deduplication in incremental loads Join operations for data quality checks Troubleshooting specific records Important: Implementation varies by table - check table-specific documentation.
INSERTED_TIMESTAMPTIMESTAMP_NTZUTC timestamp when the record was first added to the Flipside database. Format: TIMESTAMP_NTZ Use Cases: Data freshness monitoring Incremental processing markers Debugging data pipeline issues SLA tracking Query Example:
MODIFIED_TIMESTAMPTIMESTAMP_NTZUTC timestamp of the most recent update to this record. Format: TIMESTAMP_NTZ Triggers for Updates: Data corrections Enrichment additions Reprocessing for accuracy Schema migrations Monitoring Usage: