Skip to main content

lp_to_usd_amount

Schema: lib_lending

Description

Converts a raw lending LP token amount to its corresponding USD amount. A "lending LP token" is a token that represents a user's balance in a lending pool, such as cTokens in Compound-forks.

Usage

Signature

lib_lending.lp_to_usd_amount(raw_lp_val numeric, lp_to_underlying_exchange_rate numeric, underlying_token_decimals integer, underlying_usd_price numeric)

Parameters

NameTypeDescription
raw_lp_valnumericThe raw lending LP token amount to be converted
lp_to_underlying_exchange_ratenumericThe exchange rate between the LP token and the underlying token. This can typically be pulled from lens contracts or directly from a lending token on Compound-forks. For an example, see the exchangeRateCurrent function on this lending pool contract from Hover
underlying_token_decimalsintegerThe number of decimals the underlying token uses
underlying_usd_pricenumericThe USD price of the underlying token. This is specified as the amount of USD per 1 underlying token (e.g. 3.50 means 1 underlying token is worth $3.50)

Results

The function returns the numeric result of converting the raw lending LP token amount to its USD equivalent amount.

Examples

Convert 100.0 LP tokens to USD

In this example, we convert 100.0 LP tokens:

  • The exchange rate is 2.5 LP tokens per underlying token.
  • The price of 1 underlying token is $3.50
  • The result should be 140.0 (100.0 / 2.5 * 3.50).

While the steps above seem relatively simple, this example tries to unhide how some of the raw values used by Compound-forked lending pools are derived, which is where some of the more complicated on-chain values come from. There are two CTEs used below:

  1. reader_friendly_conversion_properties: This CTE sets up the more human-friendly values for conceptualizing the conversion
  2. derived_values: This CTE shows how a Compound-based smart contract derives and stores on-chain values representing the simpler conceptual values
with reader_friendly_conversion_properties as (
select
-- Use 100.0 LP tokens (decimaled) for this example
100.0 as decimaled_lp_tokens,
-- How many LP tokens per 1 underlying token? E.g. 2.5 LP tokens per 1 underlying
2.5 as lp_tokens_per_underlying_token,
-- How many decimals used by the LP token
8 as lp_token_decimals,
-- How many decimals used by the underlying token
6 as underlying_token_decimals,
-- 1 underlying token is worth $3.50
3.50 as underlying_price
),
derived_values as (
select
-- Raw LP tokens
decimaled_lp_tokens * power(10, lp_token_decimals) as raw_lp_tokens,
-- Compound forks use an exponentiated inversion of the conversion rate.
-- Inverted: "how many underlying per 1 LP as opposed to "How many LP per 1 underlying"
-- Exponentiated: Raise the value to 10 ** (18 + underlying_token_decimals - lp_token_decimals)
-- to ensure fixed-point notation
1.0 / lp_tokens_per_underlying_token * power(10, 18 + underlying_token_decimals - lp_token_decimals) as derived_exchange_rate,
underlying_token_decimals,
underlying_price
from reader_friendly_conversion_properties
)
select
lib_lending.lp_to_usd_amount(
raw_lp_tokens::numeric,
derived_exchange_rate::numeric,
underlying_token_decimals::int,
underlying_price::numeric
)
from derived_values;