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

resize lending market #173

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
40 changes: 38 additions & 2 deletions token-lending/cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use solend_sdk::instruction::resize_reserve;
use solend_sdk::{
instruction::{
liquidate_obligation_and_redeem_reserve_collateral, redeem_reserve_collateral,
refresh_obligation, refresh_reserve,
refresh_obligation, refresh_reserve, resize_lending_market,
},
state::Obligation,
state::ReserveType,
Expand Down Expand Up @@ -218,6 +218,19 @@ fn main() {
.help("reserve pubkey"),
)
)
.subcommand(
SubCommand::with_name("resize-lending-market")
.about("Resize lending market")
.arg(
Arg::with_name("lending_market")
.long("lending-market")
.validator(is_pubkey)
.value_name("PUBKEY")
.takes_value(true)
.required(true)
.help("lending market pubkey"),
)
)
.subcommand(
SubCommand::with_name("view-market")
.about("View market")
Expand Down Expand Up @@ -1041,6 +1054,12 @@ fn main() {

Ok(())
}
("resize-lending-market", Some(arg_matches)) => {
let lending_market = pubkey_of(arg_matches, "lending_market").unwrap();
command_resize_lending_market(&config, &lending_market).unwrap();

Ok(())
}
("view-market", Some(arg_matches)) => {
let market = pubkey_of(arg_matches, "market").unwrap();
let data = config.rpc_client.get_account_data(&market).unwrap();
Expand Down Expand Up @@ -1319,7 +1338,7 @@ fn main() {
lending_market_owner_keypair,
)
}
_ => unreachable!(),
(s, _) => unreachable!("{}", s),
}
.map_err(|err| {
eprintln!("{}", err);
Expand Down Expand Up @@ -1412,6 +1431,23 @@ fn command_resize_reserve(config: &Config, reserve: &Pubkey) -> CommandResult {
Ok(())
}

fn command_resize_lending_market(config: &Config, lending_market: &Pubkey) -> CommandResult {
let ix = resize_lending_market(
config.lending_program_id,
*lending_market,
config.fee_payer.pubkey(),
);
let tx = Transaction::new_signed_with_payer(
&[ix],
Some(&config.fee_payer.pubkey()),
&vec![config.fee_payer.as_ref()],
config.rpc_client.get_latest_blockhash().unwrap(),
);

send_transaction(config, tx).unwrap();
Ok(())
}

#[allow(clippy::too_many_arguments)]
fn command_redeem_collateral(
config: &Config,
Expand Down
72 changes: 72 additions & 0 deletions token-lending/program/src/processor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,10 @@ pub fn process_instruction(
msg!("Instruction: Mark Obligation As Closable");
process_set_obligation_closeability_status(program_id, closeable, accounts)
}
LendingInstruction::ResizeLendingMarket => {
msg!("Instruction: Resize Lending Market");
process_resize_lending_market(program_id, accounts)
}
}
}

Expand Down Expand Up @@ -3139,6 +3143,74 @@ pub fn process_resize_reserve(_program_id: &Pubkey, accounts: &[AccountInfo]) ->
return Err(LendingError::InvalidAccountInput.into());
}

if let Err(e) = Reserve::unpack(&new_data) {
msg!("failed to unpack new data: {:?}", e);
return Err(LendingError::InvalidAccountInput.into());
}

Ok(())
}

/// resize a lending market
pub fn process_resize_lending_market(
_program_id: &Pubkey,
accounts: &[AccountInfo],
) -> ProgramResult {
let account_info_iter = &mut accounts.iter();
let lending_market_info = next_account_info(account_info_iter)?;
let signer_info = next_account_info(account_info_iter)?;
let system_program_info = next_account_info(account_info_iter)?;

if *signer_info.key != resizer::id() || !signer_info.is_signer {
msg!("Resizer pubkey must be a signer");
return Err(LendingError::InvalidSigner.into());
}

let data: Vec<u8> = lending_market_info.data.clone().borrow().to_vec();

if data.len() != 290 {
msg!("initial data doesn't match expected length!");
return Err(LendingError::InvalidAccountInput.into());
}

let new_size = LendingMarket::LEN;
let rent = Rent::get()?;
let new_minimum_balance = rent.minimum_balance(new_size);

let lamports_diff = new_minimum_balance.saturating_sub(lending_market_info.lamports());
invoke(
&transfer(signer_info.key, lending_market_info.key, lamports_diff),
&[
signer_info.clone(),
lending_market_info.clone(),
system_program_info.clone(),
],
)?;

// idk who signer should be lol
lending_market_info
.realloc(LendingMarket::LEN, true)
.map_err(|e| {
msg!("realloc failed: {:?}", e);
e
})?;

let new_data: Vec<u8> = lending_market_info.data.clone().borrow().to_vec();
if new_data.len() != 1290 {
msg!("new data doesn't match expected length!");
return Err(LendingError::InvalidAccountInput.into());
}

if data[..] != new_data[0..290] {
msg!("new data's first 290 bytes don't match old data!");
return Err(LendingError::InvalidAccountInput.into());
}

if let Err(e) = LendingMarket::unpack(&new_data) {
msg!("failed to unpack new data: {:?}", e);
return Err(LendingError::InvalidAccountInput.into());
}

Ok(())
}

Expand Down
39 changes: 39 additions & 0 deletions token-lending/sdk/src/instruction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,15 @@ pub enum LendingInstruction {
/// Obligation is closable
closeable: bool,
},

// 25
/// ResizeLendingMarket
///
/// Accounts expected by this instruction:
/// 0. `[]` LendingMarket account.
/// 1. `[signer]` fee payer.
/// 2. '[]' System Program
ResizeLendingMarket,
}

impl LendingInstruction {
Expand Down Expand Up @@ -753,6 +762,7 @@ impl LendingInstruction {

Self::SetObligationCloseabilityStatus { closeable }
}
25 => Self::ResizeLendingMarket,
_ => {
msg!("Instruction cannot be unpacked {:?} {:?}", tag, rest);
return Err(LendingError::InstructionUnpackError.into());
Expand Down Expand Up @@ -1015,6 +1025,9 @@ impl LendingInstruction {
buf.push(24);
buf.extend_from_slice(&(closeable as u8).to_le_bytes());
}
Self::ResizeLendingMarket => {
buf.push(25);
}
}
buf
}
Expand Down Expand Up @@ -1800,6 +1813,23 @@ pub fn set_obligation_closeability_status(
}
}

/// Creates a `ResizeLendingMarket` instruction
pub fn resize_lending_market(
program_id: Pubkey,
lending_market_pubkey: Pubkey,
signer: Pubkey,
) -> Instruction {
Instruction {
program_id,
accounts: vec![
AccountMeta::new(lending_market_pubkey, false),
AccountMeta::new_readonly(signer, true),
AccountMeta::new_readonly(system_program::id(), false),
],
data: LendingInstruction::ResizeLendingMarket.pack(),
}
}

#[cfg(test)]
mod test {
use super::*;
Expand Down Expand Up @@ -2119,6 +2149,15 @@ mod test {
let unpacked = LendingInstruction::unpack(&packed).unwrap();
assert_eq!(instruction, unpacked);
}

// resize lending market
{
let instruction = LendingInstruction::ResizeLendingMarket {};

let packed = instruction.pack();
let unpacked = LendingInstruction::unpack(&packed).unwrap();
assert_eq!(instruction, unpacked);
}
}
}
}
6 changes: 3 additions & 3 deletions token-lending/sdk/src/state/lending_market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ impl IsInitialized for LendingMarket {
}
}

const LENDING_MARKET_LEN: usize = 290; // 1 + 1 + 32 + 32 + 32 + 32 + 32 + 56 + 32 + 40
const LENDING_MARKET_LEN: usize = 1290; // 1 + 1 + 32 + 32 + 32 + 32 + 32 + 56 + 32 + 40
impl Pack for LendingMarket {
const LEN: usize = LENDING_MARKET_LEN;

Expand Down Expand Up @@ -111,7 +111,7 @@ impl Pack for LendingMarket {
RATE_LIMITER_LEN,
PUBKEY_BYTES,
PUBKEY_BYTES,
8
1008
];

*version = self.version.to_le_bytes();
Expand Down Expand Up @@ -161,7 +161,7 @@ impl Pack for LendingMarket {
RATE_LIMITER_LEN,
PUBKEY_BYTES,
PUBKEY_BYTES,
8
1008
];

let version = u8::from_le_bytes(*version);
Expand Down
Loading