-
Notifications
You must be signed in to change notification settings - Fork 71
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
Merge WithdrawObligationCollateral and RedeemReserveCollateral #14
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,13 +104,23 @@ pub fn process_instruction( | |
process_flash_loan(program_id, amount, accounts) | ||
} | ||
LendingInstruction::DepositReserveLiquidityAndObligationCollateral { liquidity_amount } => { | ||
msg!("Instruction Deposit Reserve Liquidity and Obligation Collateral"); | ||
msg!("Instruction: Deposit Reserve Liquidity and Obligation Collateral"); | ||
process_deposit_reserve_liquidity_and_obligation_collateral( | ||
program_id, | ||
liquidity_amount, | ||
accounts, | ||
) | ||
} | ||
LendingInstruction::WithdrawObligationCollateralAndRedeemReserveCollateral { | ||
collateral_amount, | ||
} => { | ||
msg!("Instruction: Withdraw Obligation Collateral and Redeem Reserve Collateral "); | ||
process_withdraw_obligation_collateral_and_redeem_reserve_liquidity( | ||
program_id, | ||
collateral_amount, | ||
accounts, | ||
) | ||
} | ||
} | ||
} | ||
|
||
|
@@ -626,7 +636,37 @@ fn process_redeem_reserve_collateral( | |
let user_transfer_authority_info = next_account_info(account_info_iter)?; | ||
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?; | ||
let token_program_id = next_account_info(account_info_iter)?; | ||
_redeem_reserve_collateral( | ||
program_id, | ||
collateral_amount, | ||
source_collateral_info, | ||
destination_liquidity_info, | ||
reserve_info, | ||
reserve_collateral_mint_info, | ||
reserve_liquidity_supply_info, | ||
lending_market_info, | ||
lending_market_authority_info, | ||
user_transfer_authority_info, | ||
clock, | ||
token_program_id, | ||
) | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn _redeem_reserve_collateral<'a>( | ||
program_id: &Pubkey, | ||
collateral_amount: u64, | ||
source_collateral_info: &AccountInfo<'a>, | ||
destination_liquidity_info: &AccountInfo<'a>, | ||
reserve_info: &AccountInfo<'a>, | ||
reserve_collateral_mint_info: &AccountInfo<'a>, | ||
reserve_liquidity_supply_info: &AccountInfo<'a>, | ||
lending_market_info: &AccountInfo<'a>, | ||
lending_market_authority_info: &AccountInfo<'a>, | ||
user_transfer_authority_info: &AccountInfo<'a>, | ||
clock: &Clock, | ||
token_program_id: &AccountInfo<'a>, | ||
) -> ProgramResult { | ||
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"); | ||
|
@@ -1099,7 +1139,36 @@ fn process_withdraw_obligation_collateral( | |
let obligation_owner_info = next_account_info(account_info_iter)?; | ||
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?; | ||
let token_program_id = next_account_info(account_info_iter)?; | ||
_withdraw_obligation_collateral( | ||
program_id, | ||
collateral_amount, | ||
source_collateral_info, | ||
destination_collateral_info, | ||
withdraw_reserve_info, | ||
obligation_info, | ||
lending_market_info, | ||
lending_market_authority_info, | ||
obligation_owner_info, | ||
clock, | ||
token_program_id, | ||
)?; | ||
Ok(()) | ||
} | ||
|
||
#[allow(clippy::too_many_arguments)] | ||
fn _withdraw_obligation_collateral<'a>( | ||
program_id: &Pubkey, | ||
collateral_amount: u64, | ||
source_collateral_info: &AccountInfo<'a>, | ||
destination_collateral_info: &AccountInfo<'a>, | ||
withdraw_reserve_info: &AccountInfo<'a>, | ||
obligation_info: &AccountInfo<'a>, | ||
lending_market_info: &AccountInfo<'a>, | ||
lending_market_authority_info: &AccountInfo<'a>, | ||
obligation_owner_info: &AccountInfo<'a>, | ||
clock: &Clock, | ||
token_program_id: &AccountInfo<'a>, | ||
) -> Result<u64, ProgramError> { | ||
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"); | ||
|
@@ -1228,7 +1297,7 @@ fn process_withdraw_obligation_collateral( | |
token_program: token_program_id.clone(), | ||
})?; | ||
|
||
Ok(()) | ||
Ok(withdraw_amount) | ||
} | ||
|
||
#[inline(never)] // avoid stack frame limit | ||
|
@@ -1874,6 +1943,57 @@ fn process_flash_loan( | |
Ok(()) | ||
} | ||
|
||
#[inline(never)] // avoid stack frame limit | ||
fn process_withdraw_obligation_collateral_and_redeem_reserve_liquidity( | ||
program_id: &Pubkey, | ||
collateral_amount: u64, | ||
accounts: &[AccountInfo], | ||
) -> ProgramResult { | ||
let account_info_iter = &mut accounts.iter(); | ||
let reserve_collateral_info = next_account_info(account_info_iter)?; | ||
let user_collateral_info = next_account_info(account_info_iter)?; | ||
let reserve_info = next_account_info(account_info_iter)?; | ||
let obligation_info = next_account_info(account_info_iter)?; | ||
let lending_market_info = next_account_info(account_info_iter)?; | ||
let lending_market_authority_info = next_account_info(account_info_iter)?; | ||
let user_liquidity_info = next_account_info(account_info_iter)?; | ||
let reserve_collateral_mint_info = next_account_info(account_info_iter)?; | ||
let reserve_liquidity_supply_info = next_account_info(account_info_iter)?; | ||
let obligation_owner_info = next_account_info(account_info_iter)?; | ||
let user_transfer_authority_info = next_account_info(account_info_iter)?; | ||
let clock = &Clock::from_account_info(next_account_info(account_info_iter)?)?; | ||
let token_program_id = next_account_info(account_info_iter)?; | ||
|
||
let liquidity_amount = _withdraw_obligation_collateral( | ||
program_id, | ||
collateral_amount, | ||
reserve_collateral_info, | ||
user_collateral_info, | ||
reserve_info, | ||
obligation_info, | ||
lending_market_info, | ||
lending_market_authority_info, | ||
obligation_owner_info, | ||
clock, | ||
token_program_id, | ||
)?; | ||
|
||
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. So we don't have to refresh anything here because _withdraw_obligation_collateral requires the reserve to be refreshed, and does not mark it as stale (only reads from it as far as I can tell) 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. oh hm maybe my flow chart is wrong then 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. Its not strictly wrong, since it does require a refreshed reserve. 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. i guess it makes sense since removing the collateral doesn't change anything about the deposited liquidity so no interest calculations need to happen 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. 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. Thats just the obligation thats getting marked stale right? 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. ye 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. Did the frontend PR, works like a charm! https://github.com/solendprotocol/web/pull/114 |
||
_redeem_reserve_collateral( | ||
program_id, | ||
liquidity_amount, | ||
user_collateral_info, | ||
user_liquidity_info, | ||
reserve_info, | ||
reserve_collateral_mint_info, | ||
reserve_liquidity_supply_info, | ||
lending_market_info, | ||
lending_market_authority_info, | ||
user_transfer_authority_info, | ||
clock, | ||
token_program_id, | ||
) | ||
} | ||
|
||
fn assert_rent_exempt(rent: &Rent, account_info: &AccountInfo) -> ProgramResult { | ||
if !rent.is_exempt(account_info.lamports(), account_info.data_len()) { | ||
msg!( | ||
|
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.
Should call it
destination_liquidity_info
to be consistent with how_redeem_reserve_collateral
refers to it.Also for whatever reason, a user may not want to withdraw to themselves. We see this with proxy wallets and gassless txs in Ethereum. Basically the naive way to implement in Solidity is to withdraw to the caller (
msg.sender
), but for gasless txs the user signs a message that the smart contract verifies, and a relayer submits the tx (so they're themsg.sender
). The result is an improved UX where the user signs something but doesn't have to spend the gas, and maybe doesn't even need a web3 wallet like Metamask at all.Anyway just some Ethereum history.