prices-helper

Crates.ioprices-helper
lib.rsprices-helper
version0.1.0
created_at2025-12-11 17:31:56.716706+00
updated_at2025-12-11 17:31:56.716706+00
descriptionA Rust library for calculating TWAP and cross-price calculations for Soroban smart contracts. This library provides utilities for working with SEP-40 oracle price data.
homepage
repositoryhttps://github.com/reflector-network/prices-helper
max_upload_size
id1980162
size58,192
(hawthorne-abendsen)

documentation

README

Prices Helper

A Rust library for calculating TWAP and cross-price calculations for Soroban smart contracts. This library provides utilities for working with SEP-40 oracle price data.

Features

  • TWAP Calculation: Calculate time-weighted average prices from price data vectors
  • Cross-Price Computation: Calculate cross-prices between two price feeds
  • Cross-Price TWAP: Compute TWAP values for derived cross-prices

Installation

Add this to your Cargo.toml:

[dependencies]
prices-helper = "0.1.0"

Usage

Basic TWAP Calculation

use prices_helper::twap;
use sep_40_oracle::{PriceData, Asset};
use soroban_sdk::{Env, Vec};

pub fn calculate_twap(env: &Env, reflector_contract_id: &str, asset: Asset) -> Option<i128> {
    let reflector_contract = PriceOracleClient::new(&env, &reflector_contract_id);

    // get the prices from the SEP-40 oracle
    let prices = reflector_contract.prices(&asset).unwrap()

    let result = twap(&prices);

    result
}

Cross-Price Calculation

Calculate the price of asset A in terms of asset B:

use prices_helper::twap;
use sep_40_oracle::{PriceData, Asset};
use soroban_sdk::{Env, Vec};

pub fn calculate_cross_price(
    env: &Env,
    reflector_contract_id: &str,
    asset_a: Asset,
    asset_b: Asset
) -> Option<i128> {
    let reflector_contract = PriceOracleClient::new(&env, &reflector_contract_id);

    // get the prices from the SEP-40 oracle
    let price_a = reflector_contract.lastprice(&asset_a).unwrap();
    let price_b = reflector_contract.lastprice(&asset_b).unwrap();

    // get the decimals
    let decimals = reflector_contract.get_decimals();

    let result = x_price(&price_a, &price_b, decimals); 

    result
}

Cross-Price Vector

Generate a vector of cross-prices from two price feeds with timestamp alignment:

use prices_helper::x_prices;
use sep_40_oracle::PriceData;
use soroban_sdk::{Env, Vec};

pub fn calculate_cross_prices(
    env: &Env,
    reflector_contract_id: &str,
    asset_a: Asset,
    asset_b: Asset
) -> Option<Vec<PriceData>> {
    let reflector_contract = PriceOracleClient::new(&env, &reflector_contract_id);

    // get the prices from the SEP-40 oracle
    let prices_a = reflector_contract.prices(&asset_a, 20).unwrap();
    let prices_b = reflector_contract.prices(&asset_b, 20).unwrap();

    // get the decimals
    let decimals = reflector_contract.get_decimals();

    let cross_prices = x_prices(&prices_a, &prices_b, decimals);

    cross_prices
}

Cross-Price TWAP

Calculate TWAP for derived cross-prices between two assets:

use prices_helper::x_twap;
use sep_40_oracle::PriceData;
use soroban_sdk::{Env, Vec};

pub fn calculate_cross_price_twap(
    env: &Env,
    reflector_contract_id: &str,
    asset_a: Asset,
    asset_b: Asset
) -> Option<i128> {
    let reflector_contract = PriceOracleClient::new(&env, &reflector_contract_id);

    // get the prices from the SEP-40 oracle
    let prices_a = reflector_contract.prices(&asset_a, 20).unwrap();
    let prices_b = reflector_contract.prices(&asset_b, 20).unwrap();

    // get the decimals
    let decimals = reflector_contract.get_decimals();

    let cross_price_twap = x_twap(&prices_a, &prices_b, decimals);

    cross_price_twap
}

Testing

Run the tests:

cargo test

License

MIT

Commit count: 0

cargo fmt