latest_custom_metric_by_contract
Schema: lib
Description
This function retrieves the most recent value of a custom metric for all contracts matching the provided filter.
This function is best used when you want a single custom metric (e.g. supplyRate
, price
, etc.) for a list
of contracts.
A few variants of this function exist:
latest_custom_metric_by_contract
: Get the latest single metric for a list of contractslatest_custom_metrics_by_contract
Similar tolatest_custom_metric_by_contract
, but get the latest metric for a list of metrics for a list of contracts (e.g. getborrowRate
andsupplyRate
in one call for a set of contracts)
Usage
Signature
Single metric:
lib.latest_custom_metric_by_contract(contracts text[], metric_name text)
Single metric with with constrained-date lookup; this version is optimized when you know there will be at least one custom metric at or later than the start_time:
lib.latest_custom_metric_by_contract(contracts text[], metric_name text, start_time timestamptz)
Multiple metrics:
lib.latest_custom_metrics_by_contract(contracts text[], metric_names text[])
Parameters
Name | Type | Description |
---|---|---|
contracts | text[] | An array of contract names find metrics for. |
metric_name | text | The name of the custom metric to retrieve the latest value for. |
Results
The function returns a table with the following columns:
Column | Type | Description |
---|---|---|
contract | text | The contract address. |
metric_name | text | The name of the custom metric. |
val | numeric | The latest value of the custom metric for the contract. |
updated_time | timestamptz | The timestamp of when the custom metric was last updated. |
Examples
Retrieve the latest supply rate for two contracts
select * from lib.latest_custom_metric_by_contract(
contracts => array['0x2da7e489b935f0ed7c5191358a917086eaa1f735', '0xf3d4c27cb8ffe8fe3fccddea80f0b6c71a1118ed'],
metric_name => 'supplyRate'
);
Retrieve the latest LP=>Underlying exchange rate for all contracts owned deployed by company XYZ
with lending_pools as (
select
lpm.account as lending_pool_contract,
lpm.symbol,
lpm.decimals,
lpm.underlying_token_decimals
from lib_lending.lending_pool_metadata_for_company('XYZ') as lpm
),
lending_pool_contracts as (
select array_agg(lp.lending_pool_contract) as contracts
from lending_pools lp
)
select *
from lib.latest_custom_metric_by_contract(
(select contracts from lending_pool_contracts),
'exchangeRate'
);