Skip to content

Commit

Permalink
Create a crate, relax solana dependencies in crate (solana-labs#118)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xripleys authored Jan 2, 2023
1 parent 92d38cf commit 8594030
Show file tree
Hide file tree
Showing 23 changed files with 131 additions and 46 deletions.
30 changes: 25 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
"token-lending/cli",
"token-lending/program",
"token-lending/sdk",
]

[profile.dev]
Expand Down
2 changes: 1 addition & 1 deletion token-lending/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ solana-client = "=1.9.18"
solana-logger = "=1.9.18"
solana-sdk = "=1.9.18"
solana-program = "=1.9.18"
solend-program = { path="../program", features = [ "no-entrypoint" ] }
solend-sdk = { path="../sdk" }
spl-token = { version = "3.2.0", features=["no-entrypoint"] }
spl-associated-token-account = "1.0.3"

Expand Down
4 changes: 2 additions & 2 deletions token-lending/cli/src/lending_state.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use solana_program::instruction::Instruction;
use solend_program::instruction::{
use solend_sdk::instruction::{
refresh_obligation, refresh_reserve, withdraw_obligation_collateral,
};
use solend_program::state::{Obligation, Reserve};
use solend_sdk::state::{Obligation, Reserve};

use solana_client::rpc_client::RpcClient;
use solana_program::program_pack::Pack;
Expand Down
8 changes: 4 additions & 4 deletions token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use lending_state::SolendState;
use solana_client::rpc_config::RpcSendTransactionConfig;
use solana_sdk::{commitment_config::CommitmentLevel, compute_budget::ComputeBudgetInstruction};
use solend_program::{
use solend_sdk::{
instruction::{
liquidate_obligation_and_redeem_reserve_collateral, redeem_reserve_collateral,
refresh_obligation, refresh_reserve,
Expand Down Expand Up @@ -32,7 +32,7 @@ use {
system_instruction,
transaction::Transaction,
},
solend_program::{
solend_sdk::{
self,
instruction::{init_lending_market, init_reserve, update_reserve_config},
math::WAD,
Expand Down Expand Up @@ -109,7 +109,7 @@ const SWITCHBOARD_PROGRAM_ID_DEV: &str = "7azgmy1pFXHikv36q1zZASvFq5vFa39TT9NweV
fn main() {
solana_logger::setup_with_default("solana=info");

let default_lending_program_id: &str = &solend_program::id().to_string();
let default_lending_program_id: &str = &solend_sdk::solend_mainnet::id().to_string();

let matches = App::new(crate_name!())
.about(crate_description!())
Expand Down Expand Up @@ -1642,7 +1642,7 @@ fn command_update_reserve(
reserve.config.protocol_take_rate = reserve_config.protocol_take_rate.unwrap();
}

let mut new_pyth_product_pubkey = solend_program::NULL_PUBKEY;
let mut new_pyth_product_pubkey = solend_sdk::NULL_PUBKEY;
if pyth_price_pubkey.is_some() {
no_change = false;
println!(
Expand Down
9 changes: 3 additions & 6 deletions token-lending/program/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,25 @@ no-entrypoint = []
test-bpf = []

[dependencies]
arrayref = "0.3.6"
bytemuck = "1.5.1"
num-derive = "0.3"
num-traits = "0.2"
pyth-sdk-solana = "0.7.0"
solana-program = "=1.9.18"
spl-token = { version = "3.2.0", features=["no-entrypoint"] }
solend-sdk = { path = "../sdk" }
static_assertions = "1.1.0"
switchboard-program = "0.2.0"
switchboard-v2 = "0.1.3"
thiserror = "1.0"
uint = "=0.9.1"

[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.9.18"
solana-sdk = "=1.9.18"
serde = "1.0"
serde_yaml = "0.8"
thiserror = "1.0"

[lib]
crate-type = ["cdylib", "lib"]
Expand Down
6 changes: 1 addition & 5 deletions token-lending/program/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,8 @@
//! A lending program for the Solana blockchain.
pub mod entrypoint;
pub mod error;
pub mod instruction;
pub mod math;
pub mod oracles;
pub mod processor;
pub mod state;
pub use solend_sdk::{error, instruction, math, oracles, state};

// Export current sdk types for downstream users building with a different sdk version
pub use solana_program;
Expand Down
24 changes: 2 additions & 22 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,14 @@ use crate::{
ReserveCollateral, ReserveConfig, ReserveLiquidity,
},
};
use num_traits::FromPrimitive;
use pyth_sdk_solana::{self, state::ProductAccount};
use solana_program::{
account_info::{next_account_info, AccountInfo},
decode_error::DecodeError,
entrypoint::ProgramResult,
instruction::{get_stack_height, Instruction, TRANSACTION_LEVEL_STACK_HEIGHT},
msg,
program::{invoke, invoke_signed},
program_error::{PrintProgramError, ProgramError},
program_error::ProgramError,
program_pack::{IsInitialized, Pack},
pubkey::Pubkey,
sysvar::instructions::{load_current_index_checked, load_instruction_at_checked},
Expand All @@ -32,23 +30,14 @@ use solana_program::{
Sysvar,
},
};
use solend_sdk::{switchboard_v2_devnet, switchboard_v2_mainnet};
use spl_token::state::Mint;
use std::{cmp::min, result::Result};
use switchboard_program::{
get_aggregator, get_aggregator_result, AggregatorState, RoundResult, SwitchboardAccountType,
};
use switchboard_v2::AggregatorAccountData;

/// 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");
}

/// Processes an instruction
pub fn process_instruction(
program_id: &Pubkey,
Expand Down Expand Up @@ -2948,12 +2937,3 @@ struct TokenBurnParams<'a: 'b, 'b> {
authority_signer_seeds: &'b [&'b [u8]],
token_program: AccountInfo<'a>,
}

impl PrintProgramError for LendingError {
fn print<E>(&self)
where
E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
{
msg!(&self.to_string());
}
}
2 changes: 1 addition & 1 deletion token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,14 @@ use solend_program::{
withdraw_obligation_collateral_and_redeem_reserve_collateral,
},
math::{Decimal, Rate, TryAdd, TryMul},
processor::switchboard_v2_mainnet,
state::{
InitLendingMarketParams, InitObligationParams, InitReserveParams, LendingMarket,
NewReserveCollateralParams, NewReserveLiquidityParams, Obligation, ObligationCollateral,
ObligationLiquidity, Reserve, ReserveCollateral, ReserveConfig, ReserveFees,
ReserveLiquidity, INITIAL_COLLATERAL_RATIO, PROGRAM_VERSION,
},
};
use solend_sdk::switchboard_v2_mainnet;
use spl_token::{
instruction::approve,
state::{Account as Token, AccountState, Mint},
Expand Down
41 changes: 41 additions & 0 deletions token-lending/sdk/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[package]
name = "solend-sdk"
version = "0.1.0"
description = "Solend Sdk"
authors = ["Solend Maintainers <maintainers@solend.fi>"]
repository = "https://github.com/solendprotocol/solana-program-library"
license = "Apache-2.0"
edition = "2018"

[dependencies]
arrayref = "0.3.6"
bytemuck = "1.5.1"
num-derive = "0.3"
num-traits = "0.2"
pyth-sdk-solana = "0.7.0"
solana-program = ">=1.9, < 1.15"
spl-token = { version = "3.2.0", features=["no-entrypoint"] }
static_assertions = "1.1.0"
thiserror = "1.0"
uint = "=0.9.1"

[dev-dependencies]
assert_matches = "1.5.0"
base64 = "0.13"
log = "0.4.14"
proptest = "1.0"
solana-sdk = ">=1.9, < 1.15"
serde = "1.0"
serde_yaml = "0.8"

[lib]
crate-type = ["cdylib", "lib"]

[profile.release]
lto = "fat"
codegen-units = 1

[profile.release.build-override]
opt-level = 3
incremental = false
codegen-units = 1
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
//! Error types
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use solana_program::{decode_error::DecodeError, program_error::ProgramError};
use solana_program::{msg, program_error::PrintProgramError};
use thiserror::Error;

/// Errors that may be returned by the TokenLending program.
Expand Down Expand Up @@ -203,3 +205,12 @@ impl<T> DecodeError<T> for LendingError {
"Lending Error"
}
}

impl PrintProgramError for LendingError {
fn print<E>(&self)
where
E: 'static + std::error::Error + DecodeError<E> + PrintProgramError + FromPrimitive,
{
msg!(&self.to_string());
}
}
File renamed without changes.
39 changes: 39 additions & 0 deletions token-lending/sdk/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
#![deny(missing_docs)]

//! A lending program for the Solana blockchain.
pub mod error;
pub mod instruction;
pub mod math;
pub mod oracles;
pub mod state;

// Export current sdk types for downstream users building with a different sdk version
pub use solana_program;

/// mainnet program id
pub mod solend_mainnet {
solana_program::declare_id!("So1endDq2YkqhipRh3WViPa8hdiSpxWy6z3Z6tMCpAo");
}

/// devnet program id
pub mod solend_devnet {
solana_program::declare_id!("So1endDq2YkqhipRh3WViPa8hdiSpxWy6z3Z6tMCpAo");
}

/// Canonical null pubkey. Prints out as "nu11111111111111111111111111111111111111111"
pub const NULL_PUBKEY: solana_program::pubkey::Pubkey =
solana_program::pubkey::Pubkey::new_from_array([
11, 193, 238, 216, 208, 116, 241, 195, 55, 212, 76, 22, 75, 202, 40, 216, 76, 206, 27, 169,
138, 64, 177, 28, 19, 90, 156, 0, 0, 0, 0, 0,
]);

/// 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");
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 8594030

Please sign in to comment.