### Handle Decimal Conversions with Scale and SmartScale (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Explains the usage of `Scale` and `SmartScale` for managing decimal places in blockchain data. `Scale` uses a fixed number of decimals, while `SmartScale` dynamically fetches decimals from the token contract. The example shows creating instances of both and using `SmartScale` with `ContractCallMetric`. Dependencies include `decimal`, `brownie`, and `evm_contract_exporter`. ```python from decimal import Decimal from evm_contract_exporter import Scale, SmartScale, ContractCallMetric from brownie import Contract # Fixed scale (10^18 for 18 decimal tokens) fixed_scale = Scale(decimals=18) value = fixed_scale() # Returns Decimal(1000000000000000000) # Smart scale - fetches decimals from contract async def use_smart_scale(): # SmartScale is a singleton per address usdc_scale = SmartScale("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") scale_value = await usdc_scale.coroutine() # Returns Decimal(1000000) for 6 decimals # Use with ContractCallMetric contract = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") metric = ContractCallMetric( contract.totalSupply, scale=SmartScale("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") ) ``` -------------------------------- ### Create and Use ContractCallMetric for Data Export (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt This example shows how to create a `ContractCallMetric` to wrap a brownie `ContractCall` for exportable data. It supports automatic decimal scaling using `Scale` or auto-detection, and allows producing values at specific timestamps. It also illustrates accessing fields from struct return types and indices from tuple return types. ```python from datetime import datetime, timedelta from brownie import Contract from evm_contract_exporter import ContractCallMetric, Scale # Load a contract contract = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") # USDC # Create a metric from a view method with auto-scaling total_supply_metric = ContractCallMetric( contract.totalSupply, scale=True # Auto-detect decimals from contract ) # Or with explicit scale factor (10^6 for USDC) total_supply_metric = ContractCallMetric( contract.totalSupply, scale=Scale(6) # 6 decimals ) # Produce value at a specific timestamp async def get_supply_at_time(): timestamp = datetime(2024, 1, 1, 0, 0, 0) value = await total_supply_metric.produce(timestamp) print(f"USDC total supply at {timestamp}: {value}") # Output: USDC total supply at 2024-01-01 00:00:00: 24567890123.456789 # For methods returning structs, access fields by name # reserves_metric = ContractCallMetric(contract.getReserves) # reserve0 = reserves_metric["reserve0"] # StructDerivedMetric # For methods returning tuples, access by index # first_value = reserves_metric[0] # TupleDerivedMetric ``` -------------------------------- ### Export Token Prices with Price and PriceExporter (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Shows how to use the `Price` metric and `PriceExporter` class to fetch historical token prices. It demonstrates creating a single `Price` object, retrieving a price at a specific timestamp, and configuring `PriceExporter` to export prices for multiple tokens. This utility relies on `ypricemagic` for price lookups and handles failures gracefully by returning 0. Dependencies include `asyncio`, `datetime`, `brownie`, and `evm_contract_exporter.examples`. ```python import asyncio from datetime import datetime, timedelta from brownie import network from evm_contract_exporter.examples import Price from evm_contract_exporter.examples.price import PriceExporter network.connect('mainnet') async def export_token_prices(): # Single price metric weth_price = Price("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") # Get price at specific timestamp timestamp = datetime(2024, 1, 15, 12, 0, 0) price = await weth_price.produce(timestamp) print(f"WETH price at {timestamp}: ${price}") # Output: WETH price at 2024-01-15 12:00:00: 2456.78 # Export prices for multiple tokens exporter = PriceExporter( "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", # WETH "0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599", # WBTC "0x6B175474E89094C44Da98b954EescdeCB5", # DAI interval=timedelta(hours=1), concurrency=20, sync=False ) await exporter asyncio.run(export_token_prices()) ``` -------------------------------- ### Export EVM Contract Data via CLI Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Command-line interface for exporting EVM contract data with automated Docker-based Grafana visualization. Supports single or multiple contract exports with configurable network and data intervals. The CLI manages PostgreSQL and Grafana containers automatically. ```bash # Export a single contract with daily interval evm_contract_exporter --contract 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 --network mainnet --interval 1d # Export multiple contracts with hourly data evm_contract_exporter \ --contract 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 \ --contract 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2 \ --network mainnet \ --interval 1h \ --grafana-port 3000 # Supported interval formats: # 1d - 1 day # 12h - 12 hours # 30m - 30 minutes # 60s - 60 seconds # Supported networks: Any brownie network ID # mainnet, goerli, polygon-main, arbitrum-main, etc. ``` -------------------------------- ### Load and Wrap EVM Contract with Python Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Loads an EVM contract and wraps its methods to be accessible as metrics. The `wrap_contract` function automatically scales view methods into `ContractCallMetric` instances, allowing them to be called directly or as time-series data producers. ```python from brownie import Contract from evm_contract_exporter import wrap_contract # Load and wrap a contract contract = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") wrapped = wrap_contract(contract, scale=True) # Auto-scale all methods # Now all view methods are ContractCallMetric instances # wrapped.totalSupply is a ContractCallMetric # wrapped.balanceOf is a ContractCallMetric # Access methods as metrics async def use_wrapped(): from datetime import datetime timestamp = datetime(2024, 1, 1) # Call as normal supply = wrapped.totalSupply() # Or use as metric supply_at_time = await wrapped.totalSupply.produce(timestamp) ``` -------------------------------- ### Export All Numeric View Methods from a Contract (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt This snippet demonstrates how to use `GenericContractExporter` to automatically discover and export all numeric view methods from a specified contract address. It handles data extraction at configurable intervals and supports asynchronous execution. Dependencies include `asyncio`, `datetime`, `brownie`, and `evm_contract_exporter`. ```python import asyncio from datetime import timedelta from brownie import network from evm_contract_exporter import GenericContractExporter # Connect to network network.connect('mainnet') # Export all numeric view methods from a contract async def export_contract_data(): # Initialize exporter for a USDC contract exporter = GenericContractExporter( "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", # USDC address interval=timedelta(hours=1), # Export hourly data concurrency=100, # Max concurrent RPC calls sync=False # Run asynchronously ) # Start exporting (runs until complete historical data is exported) await exporter # Or use the task property for background execution # task = exporter.task # await task asyncio.run(export_contract_data()) ``` -------------------------------- ### Export Custom Contract Metrics with ContractMetricExporter (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Demonstrates how to use `ContractMetricExporter` to export custom metrics from an EVM contract. It shows the creation of `ContractCallMetric` objects, wrapping them in `TimeSeries` or `WideTimeSeries`, and configuring the exporter with chain ID, interval, and concurrency. Dependencies include `asyncio`, `brownie`, and `evm_contract_exporter`. ```python import asyncio from datetime import timedelta from brownie import chain, network from evm_contract_exporter import ( ContractMetricExporter, ContractCallMetric, TimeSeries, WideTimeSeries, Scale ) from brownie import Contract network.connect('mainnet') async def export_custom_metrics(): # Create multiple metrics contract = Contract("0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984") # UNI total_supply = ContractCallMetric(contract.totalSupply, scale=Scale(18)) # Single metric with TimeSeries single_ts = TimeSeries(total_supply, sync=False) # Or multiple metrics with WideTimeSeries # wide_ts = WideTimeSeries(metric1, metric2, metric3, sync=False) exporter = ContractMetricExporter( chainid=chain.id, timeseries=single_ts, interval=timedelta(hours=6), concurrency=100, sync=False ) await exporter asyncio.run(export_custom_metrics()) ``` -------------------------------- ### Export Specific Contract Methods with ViewMethodExporter (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt This Python snippet demonstrates using `ViewMethodExporter` to export data from one or more specific contract methods. It provides granular control over method selection and scaling, allowing for the creation of custom metrics for multiple contracts and methods. Dependencies include `asyncio`, `datetime`, `brownie`, and `evm_contract_exporter`. ```python import asyncio from datetime import timedelta from brownie import Contract, network from evm_contract_exporter import ViewMethodExporter, ContractCallMetric, Scale network.connect('mainnet') async def export_specific_methods(): # Load contracts usdc = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") weth = Contract("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") # Create metrics for specific methods usdc_supply = ContractCallMetric(usdc.totalSupply, scale=Scale(6)) weth_supply = ContractCallMetric(weth.totalSupply, scale=Scale(18)) # Export multiple methods together exporter = ViewMethodExporter( usdc_supply, weth_supply, interval=timedelta(days=1), # Daily snapshots concurrency=50, sync=False ) # Run the exporter await exporter asyncio.run(export_specific_methods()) ``` -------------------------------- ### Enable y.stuck? Logger for Debugging Price Lookups (Python) Source: https://github.com/bobthebuidler/evm_contract_exporter/blob/master/CONTRIBUTING.md This Python code snippet demonstrates how to enable the 'y.stuck?' logger to DEBUG level. This is useful for identifying and debugging slow asynchronous price lookup calls made by exporters that rely on ypricemagic. It requires the standard Python 'logging' module. ```python import logging logging.basicConfig(level=logging.INFO) logging.getLogger("y.stuck?").setLevel(logging.DEBUG) ``` -------------------------------- ### Manage Time Series Data with TimeSeries and WideTimeSeries (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Details the `TimeSeries` and `WideTimeSeries` classes for managing metric data over time. `TimeSeries` handles a single metric, while `WideTimeSeries` aggregates multiple metrics. Both support slicing with datetime ranges and intervals to create query plans. Dependencies include `datetime`, `brownie`, and `evm_contract_exporter`. ```python from datetime import datetime, timedelta from brownie import Contract from evm_contract_exporter import ( TimeSeries, WideTimeSeries, ContractCallMetric, Scale ) contract = Contract("0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48") # Create metric supply_metric = ContractCallMetric(contract.totalSupply, scale=Scale(6)) # Single time series ts = TimeSeries(supply_metric, sync=False) print(ts.address) # 0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48 # Create a query plan with slicing syntax # ts[start:end:interval] returns a QueryPlan start = datetime(2024, 1, 1) end = datetime(2024, 1, 31) interval = timedelta(days=1) query_plan = ts[start:end:interval] # Multiple metrics in WideTimeSeries weth = Contract("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2") weth_supply = ContractCallMetric(weth.totalSupply, scale=Scale(18)) wide_ts = WideTimeSeries( supply_metric, weth_supply, sync=False ) ``` -------------------------------- ### Wrap Brownie Contracts with wrap_contract Utility (Python) Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Introduces the `wrap_contract` utility function, which simplifies the integration of Brownie contracts with the EVM exporter system. This utility automatically converts all `ContractCall` methods on a Brownie `Contract` object into `ContractCallMetric` objects. Dependencies include `brownie` and `evm_contract_exporter`. ```python from brownie import Contract, network from evm_contract_exporter import wrap_contract network.connect('mainnet') ``` -------------------------------- ### Manage Contract Time Series Data with Python Source: https://context7.com/bobthebuidler/evm_contract_exporter/llms.txt Utilizes the `GenericContractTimeSeriesKeyValueStore` for persisting exported metrics to PostgreSQL or SQLite. This class offers bulk insert, caching, and automatic entity management for contracts and tokens, simplifying data storage for blockchain metrics. ```python import asyncio from datetime import datetime from decimal import Decimal from brownie import chain, network from evm_contract_exporter.datastore import GenericContractTimeSeriesKeyValueStore network.connect('mainnet') async def use_datastore(): # Get or create datastore for current chain datastore = GenericContractTimeSeriesKeyValueStore.get_for_chain(chain.id) # Check if data exists address = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48" timestamp = datetime(2024, 1, 1) exists = await datastore.data_exists(address, "total_supply", timestamp) # Push data (typically done automatically by exporters) # datastore.push(address, "total_supply", timestamp, Decimal("24000000000")) asyncio.run(use_datastore()) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.