forked from solana-labs/solana-program-library
-
Notifications
You must be signed in to change notification settings - Fork 67
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
donate to reserve function #194
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f6c6ecb
donate to reserve function
nope-finance 8620038
fixes
0xripleys c9f2bdf
fixes2
nope-finance e8e619a
fix CI
0xripleys 333fe17
check state
0xripleys 6f5a427
fmt
0xripleys 973787d
pr feedback
nope-finance d79c48a
fix CI
0xripleys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -196,6 +196,10 @@ pub fn process_instruction( | |
msg!("Instruction: Mark Obligation As Closable"); | ||
process_set_obligation_closeability_status(program_id, closeable, accounts) | ||
} | ||
LendingInstruction::DonateToReserve { liquidity_amount } => { | ||
msg!("Instruction: Donate To Reserve"); | ||
process_donate_to_reserve(program_id, liquidity_amount, accounts) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -3187,6 +3191,65 @@ pub fn process_set_obligation_closeability_status( | |
Ok(()) | ||
} | ||
|
||
/// process donate to reserve | ||
pub fn process_donate_to_reserve( | ||
program_id: &Pubkey, | ||
liquidity_amount: u64, | ||
accounts: &[AccountInfo], | ||
) -> ProgramResult { | ||
let account_info_iter = &mut accounts.iter(); | ||
let source_liquidity_info = next_account_info(account_info_iter)?; | ||
let destination_liquidity_info = next_account_info(account_info_iter)?; | ||
let reserve_info = next_account_info(account_info_iter)?; | ||
let lending_market_info = next_account_info(account_info_iter)?; | ||
let user_transfer_authority_info = next_account_info(account_info_iter)?; | ||
let token_program_id = next_account_info(account_info_iter)?; | ||
let clock = &Clock::get()?; | ||
|
||
let lending_market = LendingMarket::unpack(&lending_market_info.data.borrow())?; | ||
if lending_market_info.owner != program_id { | ||
msg!("Lending market provided is not owned by the lending program"); | ||
return Err(LendingError::InvalidAccountOwner.into()); | ||
} | ||
if &lending_market.token_program_id != token_program_id.key { | ||
msg!("Lending market token program does not match the token program provided"); | ||
return Err(LendingError::InvalidTokenProgram.into()); | ||
} | ||
|
||
if reserve_info.owner != program_id { | ||
msg!("Lending market provided is not owned by the lending program"); | ||
return Err(LendingError::InvalidAccountOwner.into()); | ||
} | ||
|
||
let mut reserve = Box::new(Reserve::unpack(&reserve_info.data.borrow())?); | ||
if &reserve.lending_market != lending_market_info.key { | ||
msg!("Reserve lending market does not match the lending market provided"); | ||
return Err(LendingError::InvalidAccountInput.into()); | ||
} | ||
|
||
if &reserve.liquidity.supply_pubkey != destination_liquidity_info.key { | ||
msg!("Reserve liquidity supply does not match the reserve liquidity supply provided"); | ||
return Err(LendingError::InvalidAccountInput.into()); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typically we also check this, but i don't think it's strictly necessary:
|
||
_refresh_reserve_interest(program_id, reserve_info, clock)?; | ||
|
||
reserve.liquidity.donate(liquidity_amount)?; | ||
spl_token_transfer(TokenTransferParams { | ||
source: source_liquidity_info.clone(), | ||
destination: destination_liquidity_info.clone(), | ||
amount: liquidity_amount, | ||
authority: user_transfer_authority_info.clone(), | ||
authority_signer_seeds: &[], | ||
token_program: token_program_id.clone(), | ||
})?; | ||
|
||
reserve.last_update.mark_stale(); | ||
Reserve::pack(*reserve, &mut reserve_info.data.borrow_mut())?; | ||
|
||
Ok(()) | ||
} | ||
|
||
fn assert_uninitialized<T: Pack + IsInitialized>( | ||
account_info: &AccountInfo, | ||
) -> Result<T, ProgramError> { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
#![cfg(feature = "test-bpf")] | ||
use crate::solend_program_test::custom_scenario; | ||
|
||
use crate::solend_program_test::User; | ||
|
||
use crate::solend_program_test::BalanceChecker; | ||
|
||
use crate::solend_program_test::PriceArgs; | ||
use crate::solend_program_test::ReserveArgs; | ||
use crate::solend_program_test::TokenBalanceChange; | ||
|
||
mod helpers; | ||
|
||
use helpers::*; | ||
use solana_program_test::*; | ||
use solend_sdk::state::Reserve; | ||
|
||
use std::collections::HashSet; | ||
|
||
#[tokio::test] | ||
async fn test_donate_to_reserve() { | ||
let (mut test, lending_market, reserves, _obligations, _users, _) = custom_scenario( | ||
&[ReserveArgs { | ||
mint: usdc_mint::id(), | ||
config: test_reserve_config(), | ||
liquidity_amount: 100_000 * FRACTIONAL_TO_USDC, | ||
price: PriceArgs { | ||
price: 10, | ||
conf: 0, | ||
expo: -1, | ||
ema_price: 10, | ||
ema_conf: 1, | ||
}, | ||
}], | ||
&[], | ||
) | ||
.await; | ||
|
||
let whale = User::new_with_balances( | ||
&mut test, | ||
&[(&usdc_mint::id(), 100_000 * FRACTIONAL_TO_USDC)], | ||
) | ||
.await; | ||
|
||
let balance_checker = BalanceChecker::start(&mut test, &[&whale, &reserves[0]]).await; | ||
|
||
lending_market | ||
.donate_to_reserve( | ||
&mut test, | ||
&reserves[0], | ||
&whale, | ||
100_000 * FRACTIONAL_TO_USDC, | ||
) | ||
.await | ||
.unwrap(); | ||
|
||
let reserve_post = test.load_account::<Reserve>(reserves[0].pubkey).await; | ||
|
||
assert_eq!( | ||
reserve_post.account.liquidity.available_amount, | ||
200_000 * FRACTIONAL_TO_USDC | ||
); | ||
|
||
let (balance_changes, _) = balance_checker.find_balance_changes(&mut test).await; | ||
let expected_balance_changes = HashSet::from([ | ||
TokenBalanceChange { | ||
token_account: whale.get_account(&usdc_mint::id()).unwrap(), | ||
mint: usdc_mint::id(), | ||
diff: -(100_000 * FRACTIONAL_TO_USDC as i128), | ||
}, | ||
TokenBalanceChange { | ||
token_account: reserves[0].account.liquidity.supply_pubkey, | ||
mint: usdc_mint::id(), | ||
diff: 100_000 * FRACTIONAL_TO_USDC as i128, | ||
}, | ||
]); | ||
|
||
assert_eq!(balance_changes, expected_balance_changes); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
missing reserve whitelist