|
| 1 | +//! Adds a pool reward to a reserve. |
| 2 | +//! |
| 3 | +//! The signer must be the owner of the lending market, and there must be a free slot in the reserve. |
| 4 | +
|
| 5 | +use std::{borrow::Borrow, str::FromStr}; |
| 6 | + |
| 7 | +use solana_program::program_pack::Pack; |
| 8 | +use solana_sdk::{pubkey::Pubkey, signature::Keypair, signer::Signer, system_instruction}; |
| 9 | +use solend_sdk::{ |
| 10 | + instruction::{add_pool_reward, find_reward_vault_authority}, |
| 11 | + state::{LendingMarket, PoolRewardEntry, PositionKind, Reserve}, |
| 12 | +}; |
| 13 | + |
| 14 | +use crate::{send_transaction, CommandResult, Config}; |
| 15 | + |
| 16 | +pub(crate) fn command( |
| 17 | + config: &mut Config, |
| 18 | + reserve_pubkey: Pubkey, |
| 19 | + position_kind: PositionKind, |
| 20 | + source_reward_token_account_pubkey: Pubkey, |
| 21 | + start_time_secs: u64, |
| 22 | + duration_secs: u32, |
| 23 | + token_amount: u64, |
| 24 | +) -> CommandResult { |
| 25 | + let reserve_info = config.rpc_client.get_account(&reserve_pubkey)?; |
| 26 | + let reserve = Reserve::unpack_from_slice(reserve_info.data.borrow())?; |
| 27 | + let lending_market_info = config.rpc_client.get_account(&reserve.lending_market)?; |
| 28 | + let lending_market = LendingMarket::unpack(lending_market_info.data.borrow())?; |
| 29 | + |
| 30 | + if config.fee_payer.pubkey() != lending_market.owner { |
| 31 | + return Err(format!( |
| 32 | + "The fee payer must be the owner of the lending market '{}'", |
| 33 | + reserve.lending_market |
| 34 | + ) |
| 35 | + .into()); |
| 36 | + } |
| 37 | + |
| 38 | + let has_free_slot = reserve |
| 39 | + .pool_reward_manager(position_kind) |
| 40 | + .pool_rewards |
| 41 | + .iter() |
| 42 | + .any(|pr| matches!(pr, PoolRewardEntry::Vacant { .. })); |
| 43 | + |
| 44 | + if !has_free_slot { |
| 45 | + return Err(format!( |
| 46 | + "There are no vacant slots to add the pool reward. Please crank it first" |
| 47 | + ) |
| 48 | + .into()); |
| 49 | + } |
| 50 | + |
| 51 | + let Some(source_reward_token_account) = config |
| 52 | + .rpc_client |
| 53 | + .get_token_account(&source_reward_token_account_pubkey)? |
| 54 | + else { |
| 55 | + return Err(format!( |
| 56 | + "Failed to fetch source token account '{}'", |
| 57 | + source_reward_token_account_pubkey |
| 58 | + ))?; |
| 59 | + }; |
| 60 | + |
| 61 | + let reward_mint = Pubkey::from_str(&source_reward_token_account.mint)?; |
| 62 | + |
| 63 | + let reward_vault_keypair = Keypair::new(); |
| 64 | + |
| 65 | + let create_account_ix = system_instruction::create_account( |
| 66 | + &config.fee_payer.pubkey(), |
| 67 | + &reward_vault_keypair.pubkey(), |
| 68 | + config |
| 69 | + .rpc_client |
| 70 | + .get_minimum_balance_for_rent_exemption(spl_token::state::Account::LEN)?, |
| 71 | + spl_token::state::Account::LEN as _, |
| 72 | + &spl_token::id(), |
| 73 | + ); |
| 74 | + |
| 75 | + let (reward_vault_authority, reward_authority_bump) = find_reward_vault_authority( |
| 76 | + &config.lending_program_id, |
| 77 | + &reserve.lending_market, |
| 78 | + &reward_vault_keypair.pubkey(), |
| 79 | + ); |
| 80 | + |
| 81 | + let add_reward_ix = add_pool_reward( |
| 82 | + config.lending_program_id, |
| 83 | + reward_authority_bump, |
| 84 | + position_kind, |
| 85 | + start_time_secs, |
| 86 | + start_time_secs + duration_secs as u64, |
| 87 | + token_amount, |
| 88 | + reserve_pubkey, |
| 89 | + reward_mint, |
| 90 | + source_reward_token_account_pubkey, |
| 91 | + reward_vault_authority, |
| 92 | + reward_vault_keypair.pubkey(), |
| 93 | + reserve.lending_market, |
| 94 | + lending_market.owner, |
| 95 | + ); |
| 96 | + |
| 97 | + let recent_blockhash = config.rpc_client.get_latest_blockhash()?; |
| 98 | + |
| 99 | + let message = solana_sdk::message::Message::new_with_blockhash( |
| 100 | + &[create_account_ix, add_reward_ix], |
| 101 | + Some(&config.fee_payer.pubkey()), |
| 102 | + &recent_blockhash, |
| 103 | + ); |
| 104 | + |
| 105 | + let transaction = solana_sdk::transaction::Transaction::new( |
| 106 | + &vec![config.fee_payer.as_ref()], |
| 107 | + message, |
| 108 | + recent_blockhash, |
| 109 | + ); |
| 110 | + |
| 111 | + send_transaction(config, transaction)?; |
| 112 | + |
| 113 | + Ok(()) |
| 114 | +} |
0 commit comments