Price Oracle
From Scope GitHub - hubbleprotocol/scope: 🎯 Scope is a Solana price oracle aggregator - a price oracle aggregator
pub enum OracleType {
Pyth = 0,
SwitchboardV1 = 1,
SwitchboardV2 = 2,
/// Deprecated (formerly YiToken)
// Do not remove - breaks the typescript idl codegen
DeprecatedPlaceholder = 3,
/// Solend tokens
CToken = 4,
/// SPL Stake Pool token (like scnSol)
SplStake = 5,
/// KTokens from Kamino
KToken = 6,
/// Pyth Exponentially-Weighted Moving Average
PythEMA = 7,
/// MSOL Stake Pool token
MsolStake = 8,
}
Solend - CToken
Solend 基于 solana-program-library/token-lending 开发。
CToken state account 实现了 `Pack` (并不是用的 anchor) https://github.com/solendprotocol/solana-program-library/blob/86746738e3c8289db6c18d2a0f2f6b421c74a2f1/token-lending/sdk/src/state/reserve.rs#L1128
获取CToken价格时,应首先手动刷新利率累积
reserve.accrue_interest(clock.slot).is_ok()
pyth
sdk: GitHub - pyth-network/pyth-sdk-rs: Pyth SDK in Rust
load price info account: pyth_sdk_solana::state::load_price_account 会检查 data 的 长度>= 0xCF0 bytes / magic / version / type.
返回 price struct:
Price {
price: self.price,
conf: self.conf,
expo: self.expo,
}
conf https://docs.pyth.network/documentation/solana-price-feeds/best-practices#confidence-intervals 置信区间,他不是百分比,是标准差的值大小。正常情况下应低于价格的1%
Pyth represents these possibly-different prices by giving its users a probability distribution over price instead of just a single price. Pyth models the price according to a Laplace distribution centered on the Pyth aggregate price with a standard deviation equal to the confidence interval (the scale parameter b of the Laplace distribution is equal to the standard deviation divided by the square root of 2). The Laplace distribution contains ~95% of the probability mass within ~2.12 standard deviations (~3 times the scale parameter). If markets are behaving normally, then the confidence interval will be tight -- typically much less than 1% of the price -- and the Laplace distribution will be highly peaked. However, at unusual times, the confidence interval can widen out dramatically.
pyth-ema
https://docs.pyth.network/documentation/how-pyth-works/ema-price-aggregation
for 指数加权平均价格 exponentially-weighted moving average (EMA) price ,其实就是 TWAP 的一种算法变体。因为 solana 的网络特性,pyth 会高频地推送price,这就导致传统的 TWAP 算法在数据采样上会浪费大量资源在时间窗口早期的数据上。 pyth 采用 EMA 算法使临近的数据在 TWAP 采样中有更高的权重。