From 8594030a7ca2354ef08f9af9ea2833b25a87ebb2 Mon Sep 17 00:00:00 2001 From: 0xripleys <105607696+0xripleys@users.noreply.github.com> Date: Sun, 1 Jan 2023 22:00:32 -0500 Subject: [PATCH] Create a crate, relax solana dependencies in crate (#118) --- Cargo.lock | 30 +++++++++++--- Cargo.toml | 1 + token-lending/cli/Cargo.toml | 2 +- token-lending/cli/src/lending_state.rs | 4 +- token-lending/cli/src/main.rs | 8 ++-- token-lending/program/Cargo.toml | 9 ++-- token-lending/program/src/lib.rs | 6 +-- token-lending/program/src/processor.rs | 24 +---------- token-lending/program/tests/helpers/mod.rs | 2 +- token-lending/sdk/Cargo.toml | 41 +++++++++++++++++++ token-lending/{program => sdk}/src/error.rs | 11 +++++ .../{program => sdk}/src/instruction.rs | 0 token-lending/sdk/src/lib.rs | 39 ++++++++++++++++++ .../{program => sdk}/src/math/common.rs | 0 .../{program => sdk}/src/math/decimal.rs | 0 .../{program => sdk}/src/math/mod.rs | 0 .../{program => sdk}/src/math/rate.rs | 0 token-lending/{program => sdk}/src/oracles.rs | 0 .../{program => sdk}/src/state/last_update.rs | 0 .../src/state/lending_market.rs | 0 .../{program => sdk}/src/state/mod.rs | 0 .../{program => sdk}/src/state/obligation.rs | 0 .../{program => sdk}/src/state/reserve.rs | 0 23 files changed, 131 insertions(+), 46 deletions(-) create mode 100644 token-lending/sdk/Cargo.toml rename token-lending/{program => sdk}/src/error.rs (95%) rename token-lending/{program => sdk}/src/instruction.rs (100%) create mode 100644 token-lending/sdk/src/lib.rs rename token-lending/{program => sdk}/src/math/common.rs (100%) rename token-lending/{program => sdk}/src/math/decimal.rs (100%) rename token-lending/{program => sdk}/src/math/mod.rs (100%) rename token-lending/{program => sdk}/src/math/rate.rs (100%) rename token-lending/{program => sdk}/src/oracles.rs (100%) rename token-lending/{program => sdk}/src/state/last_update.rs (100%) rename token-lending/{program => sdk}/src/state/lending_market.rs (100%) rename token-lending/{program => sdk}/src/state/mod.rs (100%) rename token-lending/{program => sdk}/src/state/obligation.rs (100%) rename token-lending/{program => sdk}/src/state/reserve.rs (100%) diff --git a/Cargo.lock b/Cargo.lock index 9fcd9fc0f71..ce88bde98dc 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3411,13 +3411,10 @@ dependencies = [ name = "solend-program" version = "0.1.0" dependencies = [ - "arrayref", "assert_matches", "base64 0.13.0", "bytemuck", "log", - "num-derive", - "num-traits", "proptest", "pyth-sdk-solana", "serde", @@ -3425,12 +3422,12 @@ dependencies = [ "solana-program", "solana-program-test", "solana-sdk", + "solend-sdk", "spl-token", "static_assertions", "switchboard-program", "switchboard-v2", "thiserror", - "uint", ] [[package]] @@ -3444,11 +3441,34 @@ dependencies = [ "solana-logger", "solana-program", "solana-sdk", - "solend-program", + "solend-sdk", "spl-associated-token-account", "spl-token", ] +[[package]] +name = "solend-sdk" +version = "0.1.0" +dependencies = [ + "arrayref", + "assert_matches", + "base64 0.13.0", + "bytemuck", + "log", + "num-derive", + "num-traits", + "proptest", + "pyth-sdk-solana", + "serde", + "serde_yaml", + "solana-program", + "solana-sdk", + "spl-token", + "static_assertions", + "thiserror", + "uint", +] + [[package]] name = "spin" version = "0.5.2" diff --git a/Cargo.toml b/Cargo.toml index 091493833da..5916528a22d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -2,6 +2,7 @@ members = [ "token-lending/cli", "token-lending/program", + "token-lending/sdk", ] [profile.dev] diff --git a/token-lending/cli/Cargo.toml b/token-lending/cli/Cargo.toml index 881fd51c858..88b0e30f4d8 100644 --- a/token-lending/cli/Cargo.toml +++ b/token-lending/cli/Cargo.toml @@ -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" diff --git a/token-lending/cli/src/lending_state.rs b/token-lending/cli/src/lending_state.rs index 2ab8b5dae9d..5ed1e23e9e6 100644 --- a/token-lending/cli/src/lending_state.rs +++ b/token-lending/cli/src/lending_state.rs @@ -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; diff --git a/token-lending/cli/src/main.rs b/token-lending/cli/src/main.rs index 9d006431649..93fce733b3d 100644 --- a/token-lending/cli/src/main.rs +++ b/token-lending/cli/src/main.rs @@ -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, @@ -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, @@ -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!()) @@ -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!( diff --git a/token-lending/program/Cargo.toml b/token-lending/program/Cargo.toml index 95e86d3052b..b54b9dcf6e1 100644 --- a/token-lending/program/Cargo.toml +++ b/token-lending/program/Cargo.toml @@ -12,21 +12,17 @@ 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" @@ -34,6 +30,7 @@ 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"] diff --git a/token-lending/program/src/lib.rs b/token-lending/program/src/lib.rs index c74299fdf28..fc727ffe647 100644 --- a/token-lending/program/src/lib.rs +++ b/token-lending/program/src/lib.rs @@ -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; diff --git a/token-lending/program/src/processor.rs b/token-lending/program/src/processor.rs index 64ce17d074d..eb55b13a67d 100644 --- a/token-lending/program/src/processor.rs +++ b/token-lending/program/src/processor.rs @@ -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}, @@ -32,6 +30,7 @@ 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::{ @@ -39,16 +38,6 @@ use switchboard_program::{ }; 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, @@ -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(&self) - where - E: 'static + std::error::Error + DecodeError + PrintProgramError + FromPrimitive, - { - msg!(&self.to_string()); - } -} diff --git a/token-lending/program/tests/helpers/mod.rs b/token-lending/program/tests/helpers/mod.rs index 3b3124fe171..17236e0719b 100644 --- a/token-lending/program/tests/helpers/mod.rs +++ b/token-lending/program/tests/helpers/mod.rs @@ -23,7 +23,6 @@ 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, @@ -31,6 +30,7 @@ use solend_program::{ ReserveLiquidity, INITIAL_COLLATERAL_RATIO, PROGRAM_VERSION, }, }; +use solend_sdk::switchboard_v2_mainnet; use spl_token::{ instruction::approve, state::{Account as Token, AccountState, Mint}, diff --git a/token-lending/sdk/Cargo.toml b/token-lending/sdk/Cargo.toml new file mode 100644 index 00000000000..14adbe21c8b --- /dev/null +++ b/token-lending/sdk/Cargo.toml @@ -0,0 +1,41 @@ +[package] +name = "solend-sdk" +version = "0.1.0" +description = "Solend Sdk" +authors = ["Solend Maintainers "] +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 diff --git a/token-lending/program/src/error.rs b/token-lending/sdk/src/error.rs similarity index 95% rename from token-lending/program/src/error.rs rename to token-lending/sdk/src/error.rs index 5adaa438396..1e15ed63e65 100644 --- a/token-lending/program/src/error.rs +++ b/token-lending/sdk/src/error.rs @@ -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. @@ -203,3 +205,12 @@ impl DecodeError for LendingError { "Lending Error" } } + +impl PrintProgramError for LendingError { + fn print(&self) + where + E: 'static + std::error::Error + DecodeError + PrintProgramError + FromPrimitive, + { + msg!(&self.to_string()); + } +} diff --git a/token-lending/program/src/instruction.rs b/token-lending/sdk/src/instruction.rs similarity index 100% rename from token-lending/program/src/instruction.rs rename to token-lending/sdk/src/instruction.rs diff --git a/token-lending/sdk/src/lib.rs b/token-lending/sdk/src/lib.rs new file mode 100644 index 00000000000..8fc5cd4c978 --- /dev/null +++ b/token-lending/sdk/src/lib.rs @@ -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"); +} diff --git a/token-lending/program/src/math/common.rs b/token-lending/sdk/src/math/common.rs similarity index 100% rename from token-lending/program/src/math/common.rs rename to token-lending/sdk/src/math/common.rs diff --git a/token-lending/program/src/math/decimal.rs b/token-lending/sdk/src/math/decimal.rs similarity index 100% rename from token-lending/program/src/math/decimal.rs rename to token-lending/sdk/src/math/decimal.rs diff --git a/token-lending/program/src/math/mod.rs b/token-lending/sdk/src/math/mod.rs similarity index 100% rename from token-lending/program/src/math/mod.rs rename to token-lending/sdk/src/math/mod.rs diff --git a/token-lending/program/src/math/rate.rs b/token-lending/sdk/src/math/rate.rs similarity index 100% rename from token-lending/program/src/math/rate.rs rename to token-lending/sdk/src/math/rate.rs diff --git a/token-lending/program/src/oracles.rs b/token-lending/sdk/src/oracles.rs similarity index 100% rename from token-lending/program/src/oracles.rs rename to token-lending/sdk/src/oracles.rs diff --git a/token-lending/program/src/state/last_update.rs b/token-lending/sdk/src/state/last_update.rs similarity index 100% rename from token-lending/program/src/state/last_update.rs rename to token-lending/sdk/src/state/last_update.rs diff --git a/token-lending/program/src/state/lending_market.rs b/token-lending/sdk/src/state/lending_market.rs similarity index 100% rename from token-lending/program/src/state/lending_market.rs rename to token-lending/sdk/src/state/lending_market.rs diff --git a/token-lending/program/src/state/mod.rs b/token-lending/sdk/src/state/mod.rs similarity index 100% rename from token-lending/program/src/state/mod.rs rename to token-lending/sdk/src/state/mod.rs diff --git a/token-lending/program/src/state/obligation.rs b/token-lending/sdk/src/state/obligation.rs similarity index 100% rename from token-lending/program/src/state/obligation.rs rename to token-lending/sdk/src/state/obligation.rs diff --git a/token-lending/program/src/state/reserve.rs b/token-lending/sdk/src/state/reserve.rs similarity index 100% rename from token-lending/program/src/state/reserve.rs rename to token-lending/sdk/src/state/reserve.rs