Skip to main content

lp_to_underlying_amount

Schema: lib_lending

Description

Converts a raw fixed-point lending LP token amount to its corresponding underlying token amount. The underlying amount returned is decimaled. 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_underlying_amount(raw_lp_val numeric, lp_to_underlying_exchange_rate numeric, underlying_token_decimals integer)

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

Results

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

Examples

Convert 100.0 LP tokens to underlying tokens

In this example, we convert 100.0 LP tokens with an exchange rate of 2.5 LP tokens per underlying token. The result should be 40.0 (100.0 / 2.5).

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
),
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
from reader_friendly_conversion_properties
)
select
lib_lending.lp_to_underlying_amount(
raw_lp_tokens::numeric,
derived_exchange_rate::numeric,
underlying_token_decimals::int
)
from derived_values;