Skip to content

Pull oracle support for pyth, switchboard #187

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 31 commits into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
c27c1d8
upgrade
0xripleys Jun 7, 2024
6a30199
add receiver sdk dependency
0xripleys Jun 7, 2024
a6703de
impementing pyth pull oracle
nope-finance Jun 14, 2024
119cfed
rustfmt
nope-finance Jun 14, 2024
c678941
clippy
nope-finance Jun 14, 2024
17cde76
added some account state
nope-finance Jun 24, 2024
c4c40a2
test example
0xripleys Jun 25, 2024
6f24450
test doesn't break for stupid reasons
0xripleys Jun 25, 2024
01213ff
fix tests
nope-finance Jul 3, 2024
748eda2
making get price function more generic
nope-finance Jul 3, 2024
b4e2872
feat: switchboard on-demand, init deps
mgild Jun 20, 2024
1cb1d1e
add body for parsing switchboard feeds
mgild Jun 20, 2024
b4109da
add body for parsing switchboard feeds
mgild Jun 20, 2024
4859319
update to sdk 0.1.12
mgild Jul 3, 2024
0330d2f
fixing up sb on demand
nope-finance Jul 3, 2024
d630929
Merge branch 'mgild-add_sb_on_demand_shit' into add_pythnet_shit
nope-finance Jul 3, 2024
97aa9f8
adding range check for switchboard
nope-finance Jul 3, 2024
b7c4fc0
pull oracle tests
0xripleys Jul 3, 2024
6e46900
switchboard test failing
0xripleys Jul 4, 2024
89cedae
fixing tests
0xripleys Jul 5, 2024
78b44b8
fmt
0xripleys Jul 5, 2024
ca8e365
update solana version
0xripleys Jul 5, 2024
949c00f
rust version
0xripleys Jul 5, 2024
e0cb002
fixing other warnings
0xripleys Jul 5, 2024
7468a62
create oracle crate
0xripleys Jul 5, 2024
cf78a99
minor cleanup
0xripleys Jul 5, 2024
be326fb
forgot to add fixtures
0xripleys Jul 5, 2024
a7ec4fa
clip
0xripleys Jul 5, 2024
1056f96
oops
0xripleys Jul 5, 2024
eed4f01
remove oracle ids from sdk
0xripleys Jul 5, 2024
e3634f5
fmt
0xripleys Jul 8, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,322 changes: 2,539 additions & 783 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@ members = [
"token-lending/program",
"token-lending/sdk",
"token-lending/brick"
]
, "token-lending/oracles"]

[profile.dev]
split-debuginfo = "unpacked"

[profile.release]
overflow-checks = true
2 changes: 1 addition & 1 deletion ci/rust-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
if [[ -n $RUST_STABLE_VERSION ]]; then
stable_version="$RUST_STABLE_VERSION"
else
stable_version=1.65.0
stable_version=1.76.0
fi

if [[ -n $RUST_NIGHTLY_VERSION ]]; then
Expand Down
2 changes: 1 addition & 1 deletion ci/solana-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
if [[ -n $SOLANA_VERSION ]]; then
solana_version="$SOLANA_VERSION"
else
solana_version=v1.14.10
solana_version=v1.16.20
fi

export solana_version="$solana_version"
Expand Down
10 changes: 5 additions & 5 deletions token-lending/brick/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,21 @@ no-entrypoint = []
test-bpf = []

[dependencies]
solana-program = "=1.14.10"
solana-program = "=1.16.20"

[dev-dependencies]
assert_matches = "1.5.0"
bytemuck = "1.5.1"
base64 = "0.13"
log = "0.4.14"
proptest = "1.0"
solana-program-test = "=1.14.10"
solana-sdk = "=1.14.10"
serde = "=1.0.140"
solana-program-test = "=1.16.20"
solana-sdk = "=1.16.20"
serde = "1.0.140"
serde_yaml = "0.8"
thiserror = "1.0"
bincode = "1.3.3"
borsh = "0.9.3"
borsh = "0.10.3"

[lib]
crate-type = ["cdylib", "lib"]
Expand Down
27 changes: 27 additions & 0 deletions token-lending/oracles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "oracles"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
pyth-sdk-solana = "0.8.0"
pyth-solana-receiver-sdk = "0.3.0"
solana-program = ">=1.9"
anchor-lang = "0.28.0"
solend-sdk = { path = "../sdk" }
switchboard-on-demand = "0.1.12"
switchboard-program = "0.2.0"
switchboard-v2 = "0.1.3"

[dev-dependencies]
bytemuck = "1.5.1"
assert_matches = "1.5.0"
base64 = "0.13"
log = "0.4.14"
proptest = "1.0"
solana-sdk = ">=1.9"
serde = "1.0.140"
serde_yaml = "0.8"
rand = "0.8.5"
Binary file not shown.
Binary file not shown.
106 changes: 106 additions & 0 deletions token-lending/oracles/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
pub mod pyth;
pub mod switchboard;

use crate::pyth::get_pyth_price_unchecked;
use crate::pyth::get_pyth_pull_price;
use crate::pyth::get_pyth_pull_price_unchecked;
use crate::switchboard::get_switchboard_price;
use crate::switchboard::get_switchboard_price_on_demand;
use crate::switchboard::get_switchboard_price_v2;
use solana_program::{
account_info::AccountInfo, msg, program_error::ProgramError, sysvar::clock::Clock,
};
use solend_sdk::error::LendingError;
use solend_sdk::math::Decimal;

pub enum OracleType {
Pyth,
Switchboard,
PythPull,
SbOnDemand,
}

pub fn get_oracle_type(oracle_info: &AccountInfo) -> Result<OracleType, ProgramError> {
if *oracle_info.owner == pyth_mainnet::id() {
return Ok(OracleType::Pyth);
} else if *oracle_info.owner == pyth_pull_mainnet::id() {
return Ok(OracleType::PythPull);
} else if *oracle_info.owner == switchboard_v2_mainnet::id() {
return Ok(OracleType::Switchboard);
} else if *oracle_info.owner == switchboard_on_demand_mainnet::id() {
return Ok(OracleType::SbOnDemand);
}

msg!(
"Could not find oracle type for {:?} with owner {:?}",
oracle_info.key,
oracle_info.owner
);
Err(LendingError::InvalidOracleConfig.into())
}

pub fn get_single_price(
oracle_account_info: &AccountInfo,
clock: &Clock,
) -> Result<(Decimal, Option<Decimal>), ProgramError> {
match get_oracle_type(oracle_account_info)? {
OracleType::Pyth => {
let price = pyth::get_pyth_price(oracle_account_info, clock)?;
Ok((price.0, Some(price.1)))
}
OracleType::PythPull => {
let price = get_pyth_pull_price(oracle_account_info, clock)?;
Ok((price.0, Some(price.1)))
}
OracleType::Switchboard => {
let price = get_switchboard_price(oracle_account_info, clock)?;
Ok((price, None))
}
OracleType::SbOnDemand => {
let price = get_switchboard_price(oracle_account_info, clock)?;
Ok((price, None))
}
}
}

pub fn get_single_price_unchecked(
oracle_account_info: &AccountInfo,
clock: &Clock,
) -> Result<Decimal, ProgramError> {
match get_oracle_type(oracle_account_info)? {
OracleType::Pyth => get_pyth_price_unchecked(oracle_account_info),
OracleType::PythPull => get_pyth_pull_price_unchecked(oracle_account_info),
OracleType::Switchboard => get_switchboard_price_v2(oracle_account_info, clock, false),
OracleType::SbOnDemand => get_switchboard_price_on_demand(oracle_account_info, clock, true),
}
}

/// Mainnet program id for Switchboard v2.
pub mod switchboard_v2_mainnet {
solana_program::declare_id!("SW1TCH7qEPTdLsDHRgPuMQjbQxKdH2aBStViMFnt64f");
}

/// Devnet program id for Switchboard v2.
pub mod switchboard_v2_devnet {
solana_program::declare_id!("2TfB33aLaneQb5TNVwyDz3jSZXS6jdW2ARw1Dgf84XCG");
}

/// Mainnet program id for Switchboard On-Demand Oracle.
pub mod switchboard_on_demand_mainnet {
solana_program::declare_id!("SBondMDrcV3K4kxZR1HNVT7osZxAHVHgYXL5Ze1oMUv");
}

/// Devnet program id for Switchboard On-Demand Oracle.
pub mod switchboard_on_demand_devnet {
solana_program::declare_id!("SBondMDrcV3K4kxZR1HNVT7osZxAHVHgYXL5Ze1oMUv");
}

/// Mainnet program id for pyth
pub mod pyth_mainnet {
solana_program::declare_id!("FsJ3A3u2vn5cTVofAjvy6y5kwABJAqYWpe4975bi2epH");
}

/// Mainnet program id for pyth
pub mod pyth_pull_mainnet {
solana_program::declare_id!("rec5EKMGg6MxZYaMdyBfgwp4d5rB9T1VQH5pJv5LtFJ");
}
Loading
Loading