Skip to content

jmcore.bond_calc

jmcore.bond_calc

Fidelity bond value calculations.

Attributes

DEFAULT_BOND_VALUE_EXPONENT = 1.3 module-attribute

DEFAULT_INTEREST_RATE = 0.015 module-attribute

Functions

calculate_timelocked_fidelity_bond_value(utxo_value: int, confirmation_time: int, locktime: int, current_time: int | None = None, interest_rate: float = DEFAULT_INTEREST_RATE, exponent: float = DEFAULT_BOND_VALUE_EXPONENT) -> int

Calculate fidelity bond value using the timelocked bond formula.

Args: utxo_value: UTXO value in satoshis confirmation_time: UTXO confirmation timestamp (Unix seconds) locktime: Bond locktime (Unix seconds) current_time: Current time (Unix seconds), defaults to now interest_rate: Annual interest rate (default 0.015 = 1.5%) exponent: Bond value exponent (default 1.3)

Returns: Bond value as integer

Source code in jmcore/src/jmcore/bond_calc.py
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
def calculate_timelocked_fidelity_bond_value(
    utxo_value: int,
    confirmation_time: int,
    locktime: int,
    current_time: int | None = None,
    interest_rate: float = DEFAULT_INTEREST_RATE,
    exponent: float = DEFAULT_BOND_VALUE_EXPONENT,
) -> int:
    """
    Calculate fidelity bond value using the timelocked bond formula.

    Args:
        utxo_value: UTXO value in satoshis
        confirmation_time: UTXO confirmation timestamp (Unix seconds)
        locktime: Bond locktime (Unix seconds)
        current_time: Current time (Unix seconds), defaults to now
        interest_rate: Annual interest rate (default 0.015 = 1.5%)
        exponent: Bond value exponent (default 1.3)

    Returns:
        Bond value as integer
    """
    if current_time is None:
        current_time = int(datetime.now(UTC).timestamp())

    year = 60 * 60 * 24 * 365.2425

    r = interest_rate
    time_to_maturity = (locktime - confirmation_time) / year
    locktime_years = locktime / year
    current_years = current_time / year

    a = max(
        0,
        min(1, math.exp(r * time_to_maturity) - 1)
        - min(1, math.exp(r * max(0, current_years - locktime_years)) - 1),
    )

    return int(pow(utxo_value * a, exponent))