| Crates.io | prices-helper |
| lib.rs | prices-helper |
| version | 0.1.0 |
| created_at | 2025-12-11 17:31:56.716706+00 |
| updated_at | 2025-12-11 17:31:56.716706+00 |
| description | 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. |
| homepage | |
| repository | https://github.com/reflector-network/prices-helper |
| max_upload_size | |
| id | 1980162 |
| size | 58,192 |
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.
Add this to your Cargo.toml:
[dependencies]
prices-helper = "0.1.0"
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
}
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
}
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
}
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
}
Run the tests:
cargo test
MIT