Skip to content
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

[Liquidity Mining] Obligation packing & unpacking (5) #204

Merged
merged 41 commits into from
Mar 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
49a6c03
Creating data structures for LM
vanitymnm Feb 25, 2025
aa13771
Adding admin IXs for LM reward management
vanitymnm Feb 25, 2025
0abe52c
Document accounts for admin IXs
vanitymnm Feb 25, 2025
4e4694b
Checking that token vault is empty and belogs to the token program
vanitymnm Feb 25, 2025
9d0c84c
Note on running out of IDs
vanitymnm Feb 26, 2025
eff2b19
Removing libssl1.1 dependency as it can no longer be installed on CI
vanitymnm Feb 26, 2025
df22c76
Merge branch 'ci/libssl-fix' into feat/lm-state-intro
vanitymnm Feb 26, 2025
6a51385
Improving clarity of PositionKind unpacking
vanitymnm Feb 26, 2025
fd22173
Describing what LM is
vanitymnm Feb 26, 2025
8ee16b0
Improving documentation and wording
vanitymnm Feb 26, 2025
d184804
Merge branch 'feat/lm-state-intro' into feat/lm-admin-ix-accounts
vanitymnm Feb 26, 2025
7a4b404
Removing duplicate code
vanitymnm Feb 26, 2025
9637228
Adding math for reward accrual
vanitymnm Feb 28, 2025
ef72f92
Improving comments wording
vanitymnm Feb 28, 2025
7833ca2
Fixing error names
vanitymnm Feb 28, 2025
2a28819
Merge branch 'feat/lm-admin-ix-accounts' into feat/lm-reward-accruing
vanitymnm Mar 5, 2025
d8901cc
Merging stash
vanitymnm Mar 5, 2025
b9808d7
Packing and unpacking reward managers
vanitymnm Mar 5, 2025
341a6a9
Adding packing & unpacking tests
vanitymnm Mar 7, 2025
965ca13
Adding an ix to migrate reserve
vanitymnm Mar 7, 2025
34570ed
Realloc succesful
vanitymnm Mar 12, 2025
3faec66
Running test script via anchor tests
vanitymnm Mar 14, 2025
a1bf811
Version bump
vanitymnm Mar 14, 2025
6217267
Merge branch 'feat/liquidity-mining' into feat/lm-state-intro
vanitymnm Mar 14, 2025
95343e6
Merge branch 'feat/lm-state-intro' into feat/lm-admin-ix-accounts
vanitymnm Mar 14, 2025
a4d235b
Merge branch 'feat/liquidity-mining' into feat/lm-admin-ix-accounts
vanitymnm Mar 14, 2025
dc24e8e
Merge branch 'feat/lm-admin-ix-accounts' into feat/lm-reward-accruing
vanitymnm Mar 14, 2025
5c0a70e
Merge branch 'feat/liquidity-mining' into feat/lm-reward-accruing
vanitymnm Mar 14, 2025
52982e4
Merge branch 'feat/lm-reward-accruing' into feat/lm-reserve-migration
vanitymnm Mar 14, 2025
0bf9913
Using latest cache action
vanitymnm Mar 14, 2025
7e19f5c
Packs and unpacks user managers
vanitymnm Mar 16, 2025
1486fd9
Removing Pack trait from Obligation
vanitymnm Mar 17, 2025
8163fc8
Adding account discriminator
vanitymnm Mar 17, 2025
67e0908
Fix compile errors in tests
vanitymnm Mar 17, 2025
39efb8a
Merge branch 'feat/liquidity-mining' into feat/lm-reserve-migration
vanitymnm Mar 17, 2025
c6a2efd
Merge branch 'feat/lm-reserve-migration' into feat/lm-obligation-pack…
vanitymnm Mar 17, 2025
d9968a6
Making the BPF tests pass
vanitymnm Mar 17, 2025
54d0e1d
Removing version
vanitymnm Mar 21, 2025
268a0c1
Skipping packing/unpacking for vacant slots
vanitymnm Mar 21, 2025
d7d072b
Extracting CU budgets to consts
vanitymnm Mar 21, 2025
8671efe
Removing some stale TODOs
vanitymnm Mar 21, 2025
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ hfuzz_workspace
**/*.so
**/.DS_Store
test-ledger
cargo-test-*.profraw
1 change: 0 additions & 1 deletion token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -781,7 +781,6 @@ fn main() {
.required(true)
.help("Reserve address"),
)

)
.subcommand(
SubCommand::with_name("update-reserve")
Expand Down
2 changes: 1 addition & 1 deletion token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -983,7 +983,7 @@ fn process_init_obligation(program_id: &Pubkey, accounts: &[AccountInfo]) -> Pro
let token_program_id = next_account_info(account_info_iter)?;

assert_rent_exempt(rent, obligation_info)?;
let mut obligation = assert_uninitialized::<Obligation>(obligation_info)?;
let mut obligation = Obligation::unpack_uninitialized(&obligation_info.data.borrow())?;
if obligation_info.owner != program_id {
msg!("Obligation provided is not owned by the lending program");
return Err(LendingError::InvalidAccountOwner.into());
Expand Down
20 changes: 14 additions & 6 deletions token-lending/program/src/processor/liquidity_mining.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
//! implementation of the same feature.
//!
//! There are three admin-only ixs:
//! - [add_pool_reward]
//! - [cancel_pool_reward]
//! - [close_pool_reward]
//! - [add_pool_reward] (TODO: add bpf tests)
//! - [cancel_pool_reward] (TODO: add bpf tests)
//! - [close_pool_reward] (TODO: add bpf tests)
//!
//! [suilend-lm]: https://github.com/solendprotocol/suilend/blob/dc53150416f352053ac3acbb320ee143409c4a5d/contracts/suilend/sources/liquidity_mining.move#L2

Expand All @@ -33,6 +33,7 @@ use solana_program::{
system_instruction,
sysvar::Sysvar,
};
use solend_sdk::state::discriminator::AccountDiscriminator;
use solend_sdk::{
error::LendingError,
state::{LendingMarket, PositionKind, Reserve},
Expand Down Expand Up @@ -282,9 +283,16 @@ pub(crate) fn upgrade_reserve(program_id: &Pubkey, accounts: &[AccountInfo]) ->
// 3.
//

// sanity checks pack and unpack reserves is ok
let reserve = Reserve::unpack(&accounts.reserve_info.data.borrow())?;
Reserve::pack(reserve, &mut accounts.reserve_info.data.borrow_mut())?;
// we upgrade discriminator as we've checked that the account is indeed
// a reserve account in [UpgradeReserveAccounts::from_unchecked_iter]
let mut data = accounts.reserve_info.data.borrow_mut();
data[0] = AccountDiscriminator::Reserve as u8;
// Now the reserve can unpack fine and doesn't have to worry about
// migrations.
// Instead it returns an error on an invalid discriminator.
// This way a reserve cannot be mistaken for an obligation.
let reserve = Reserve::unpack(&data)?;
Reserve::pack(reserve, &mut data)?;

Ok(())
}
Expand Down
8 changes: 4 additions & 4 deletions token-lending/program/tests/attributed_borrows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use solana_program::native_token::LAMPORTS_PER_SOL;

use solend_sdk::math::Decimal;

use solend_program::state::{Obligation, ReserveConfig};
use solend_program::state::ReserveConfig;

use solend_sdk::state::ReserveFees;
mod helpers;
Expand Down Expand Up @@ -398,7 +398,7 @@ async fn test_calculations() {
.await
.unwrap();

let obligation_post = test.load_account::<Obligation>(obligations[0].pubkey).await;
let obligation_post = test.load_obligation(obligations[0].pubkey).await;

// obligation 0 after borrowing 10 usd
// usdc.borrow_attribution = 80 / 100 * 30 = 24
Expand Down Expand Up @@ -688,7 +688,7 @@ async fn test_withdraw() {
Decimal::from_percent(250)
);

let obligation_post = test.load_account::<Obligation>(obligations[0].pubkey).await;
let obligation_post = test.load_obligation(obligations[0].pubkey).await;
assert_eq!(
obligation_post.account.deposits[0].attributed_borrow_value,
Decimal::from(7500u64)
Expand Down Expand Up @@ -733,7 +733,7 @@ async fn test_withdraw() {
Decimal::zero()
);

let obligation_post = test.load_account::<Obligation>(obligations[0].pubkey).await;
let obligation_post = test.load_obligation(obligations[0].pubkey).await;
assert_eq!(
obligation_post.account.deposits[0].attributed_borrow_value,
Decimal::from(10u64)
Expand Down
6 changes: 3 additions & 3 deletions token-lending/program/tests/borrow_obligation_liquidity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ async fn setup(
.unwrap();

// populate deposit value correctly.
let obligation = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation = test.load_obligation(obligation.pubkey).await;
lending_market
.refresh_obligation(&mut test, &obligation)
.await
Expand All @@ -86,7 +86,7 @@ async fn setup(
let lending_market = test.load_account(lending_market.pubkey).await;
let usdc_reserve = test.load_account(usdc_reserve.pubkey).await;
let wsol_reserve = test.load_account(wsol_reserve.pubkey).await;
let obligation = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation = test.load_obligation(obligation.pubkey).await;

let host_fee_receiver = User::new_with_balances(&mut test, &[(&wsol_mint::id(), 0)]).await;
(
Expand Down Expand Up @@ -244,7 +244,7 @@ async fn test_success() {
wsol_reserve_post, expected_wsol_reserve_post
);

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;
assert_eq!(
obligation_post.account,
Obligation {
Expand Down
8 changes: 4 additions & 4 deletions token-lending/program/tests/borrow_weight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ async fn test_refresh_obligation() {
.await
.unwrap();

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;

// obligation has borrowed 10 sol and sol = $10 but since borrow weight == 2, the
// borrowed_value is 200 instead of 100.
Expand Down Expand Up @@ -134,7 +134,7 @@ async fn test_borrow() {
.await
.unwrap();

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;
// - usdc ltv is 0.5,
// - sol borrow weight is 2
// max you can borrow is 100 * 0.5 / 2 = 2.5 SOL
Expand Down Expand Up @@ -176,7 +176,7 @@ async fn test_borrow() {

test.advance_clock_by_slots(1).await;

let obligation = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation = test.load_obligation(obligation.pubkey).await;

// max withdraw
{
Expand Down Expand Up @@ -302,7 +302,7 @@ async fn test_liquidation() {
.await
.unwrap();

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;
// - usdc ltv is 0.5,
// - sol borrow weight is 1
// max you can borrow is 100 * 0.5 = 5 SOL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ async fn test_success() {
let usdc_reserve_post = test.load_account(usdc_reserve.pubkey).await;
assert_eq!(usdc_reserve, usdc_reserve_post);

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;
assert_eq!(
obligation_post.account,
Obligation {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ async fn test_success() {
}
);

let obligation_post = test.load_account::<Obligation>(obligation.pubkey).await;
let obligation_post = test.load_obligation(obligation.pubkey).await;
assert_eq!(
obligation_post.account,
Obligation {
Expand Down
3 changes: 2 additions & 1 deletion token-lending/program/tests/forgive_debt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async fn test_forgive_debt_success_easy() {
.await
.unwrap();

let obligation_post = test.load_account::<Obligation>(obligations[0].pubkey).await;
let obligation_post = test.load_obligation(obligations[0].pubkey).await;
assert_eq!(
obligation_post.account,
Obligation {
Expand All @@ -182,6 +182,7 @@ async fn test_forgive_debt_success_easy() {
allowed_borrow_value: Decimal::zero(),
unhealthy_borrow_value: Decimal::zero(),
super_unhealthy_borrow_value: Decimal::zero(),
user_reward_managers: obligation_post.account.user_reward_managers.clone(),
..obligations[0].account
}
);
Expand Down
22 changes: 11 additions & 11 deletions token-lending/program/tests/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,25 +117,25 @@ pub mod bonk_mint {
}

pub trait AddPacked {
fn add_packed(&mut self, pubkey: Pubkey, amount: u64, data: &[u8], owner: &Pubkey);

fn add_packable_account<T: Pack>(
&mut self,
pubkey: Pubkey,
amount: u64,
data: &T,
unpacked: &T,
owner: &Pubkey,
);
) {
let mut data = vec![0; T::get_packed_len()];
unpacked.pack_into_slice(&mut data);
self.add_packed(pubkey, amount, &data, owner);
}
}

impl AddPacked for ProgramTest {
fn add_packable_account<T: Pack>(
&mut self,
pubkey: Pubkey,
amount: u64,
data: &T,
owner: &Pubkey,
) {
let mut account = Account::new(amount, T::get_packed_len(), owner);
data.pack_into_slice(&mut account.data);
fn add_packed(&mut self, pubkey: Pubkey, amount: u64, data: &[u8], owner: &Pubkey) {
let mut account = Account::new(amount, data.len(), owner);
account.data.copy_from_slice(data);
self.add_account(pubkey, account);
}
}
Expand Down
Loading
Loading