use solana_program::instruction::AccountMeta;
use solana_program::pubkey::Pubkey;
pub struct KaminoRemainingAccountsParams {
pub deposit_reserves: Vec<Pubkey>,
pub borrow_reserves: Vec<Pubkey>,
}
pub fn get_kamino_remaining_accounts(
params: &KaminoRemainingAccountsParams,
) -> Vec<AccountMeta> {
let mut accounts = Vec::with_capacity(
params.deposit_reserves.len().saturating_add(params.borrow_reserves.len()),
);
for reserve in ¶ms.deposit_reserves {
accounts.push(AccountMeta::new(*reserve, false));
}
for reserve in ¶ms.borrow_reserves {
accounts.push(AccountMeta::new(*reserve, false));
}
accounts
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use solana_pubkey::pubkey;
#[test]
fn test_deposit_and_borrow_reserves() {
let deposit_a = pubkey!("d4A2prbA2whesmvHaL88BH6Ewn5N4bTSjm4GiKy2eSi");
let deposit_b = pubkey!("7u3HeHxYDLhnCoErrtycNokbQYbWGzLs6JSDqGAv5PfF");
let borrow_a = pubkey!("Gnt27xtC473ZT2Mw5u8wZ68Z3gULkSTb5DuxJy7eJotD");
let accounts = get_kamino_remaining_accounts(&KaminoRemainingAccountsParams {
deposit_reserves: vec![deposit_a, deposit_b],
borrow_reserves: vec![borrow_a],
});
assert_eq!(accounts.len(), 3);
assert_eq!(accounts[0].pubkey, deposit_a);
assert!(accounts[0].is_writable);
assert_eq!(accounts[1].pubkey, deposit_b);
assert!(accounts[1].is_writable);
assert_eq!(accounts[2].pubkey, borrow_a);
assert!(accounts[2].is_writable);
for acc in &accounts {
assert!(!acc.is_signer);
}
}
#[test]
fn test_empty_params() {
let accounts = get_kamino_remaining_accounts(&KaminoRemainingAccountsParams {
deposit_reserves: vec![],
borrow_reserves: vec![],
});
assert!(accounts.is_empty());
}
#[test]
fn test_deposits_only() {
let deposit = pubkey!("d4A2prbA2whesmvHaL88BH6Ewn5N4bTSjm4GiKy2eSi");
let accounts = get_kamino_remaining_accounts(&KaminoRemainingAccountsParams {
deposit_reserves: vec![deposit],
borrow_reserves: vec![],
});
assert_eq!(accounts.len(), 1);
assert_eq!(accounts[0].pubkey, deposit);
assert!(accounts[0].is_writable);
}
#[test]
fn test_borrows_only() {
let borrow = pubkey!("Gnt27xtC473ZT2Mw5u8wZ68Z3gULkSTb5DuxJy7eJotD");
let accounts = get_kamino_remaining_accounts(&KaminoRemainingAccountsParams {
deposit_reserves: vec![],
borrow_reserves: vec![borrow],
});
assert_eq!(accounts.len(), 1);
assert_eq!(accounts[0].pubkey, borrow);
assert!(accounts[0].is_writable);
}
}