fix clippy warnings

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-03-23 09:33:51 +01:00
parent 3443ab0d70
commit f135ca4d9b
19 changed files with 48 additions and 38 deletions

View File

@ -5,7 +5,7 @@ use solana_program::pubkey::{Pubkey, PUBKEY_BYTES};
use std::str::FromStr;
pub fn id() -> Pubkey {
Pubkey::from_str(&"AddressLookupTab1e1111111111111111111111111").unwrap()
Pubkey::from_str("AddressLookupTab1e1111111111111111111111111").unwrap()
}
/// The maximum number of addresses that a lookup table can hold
@ -17,15 +17,12 @@ pub const LOOKUP_TABLE_META_SIZE: usize = 56;
pub const LOOKUP_TABLE_MAX_ACCOUNT_SIZE: usize =
LOOKUP_TABLE_META_SIZE + LOOKUP_TABLE_MAX_ADDRESSES * PUBKEY_BYTES;
pub fn addresses<'a>(table: &[u8]) -> &[Pubkey] {
pub fn addresses(table: &[u8]) -> &[Pubkey] {
bytemuck::try_cast_slice(&table[LOOKUP_TABLE_META_SIZE..]).unwrap()
}
pub fn contains(table: &[u8], pubkey: &Pubkey) -> bool {
addresses(table)
.iter()
.find(|&addr| addr == pubkey)
.is_some()
addresses(table).iter().any(|&addr| addr == *pubkey)
}
pub fn extend<'info>(

View File

@ -70,7 +70,7 @@ pub fn deposit(ctx: Context<Deposit>, amount: u64) -> Result<()> {
// TODO: This will be used to disable is_bankrupt or being_liquidated
// when health recovers sufficiently
//
let health = compute_health(&account, &ctx.remaining_accounts)?;
let health = compute_health(&account, ctx.remaining_accounts)?;
msg!("health: {}", health);
//

View File

@ -125,7 +125,7 @@ pub fn margin_trade<'key, 'accounts, 'remaining, 'info>(
Ok(())
}
fn get_pre_cpi_amounts(ctx: &Context<MarginTrade>, cpi_ais: &Vec<AccountInfo>) -> Vec<u64> {
fn get_pre_cpi_amounts(ctx: &Context<MarginTrade>, cpi_ais: &[AccountInfo]) -> Vec<u64> {
let mut amounts = vec![];
for token_account in cpi_ais
.iter()
@ -168,16 +168,16 @@ fn adjust_for_post_cpi_amounts(
let bank_loader = AccountLoader::<'_, Bank>::try_from(bank_ai)?;
let mut bank = bank_loader.load_mut()?;
let mut position = account
let position = account
.token_account_map
.get_mut_or_create(bank.token_index)?
.0;
// user has either withdrawn or deposited
if *pre_cpi_amount > vault.amount {
bank.withdraw(&mut position, pre_cpi_amount - vault.amount)?;
bank.withdraw(position, pre_cpi_amount - vault.amount)?;
} else {
bank.deposit(&mut position, vault.amount - pre_cpi_amount)?;
bank.deposit(position, vault.amount - pre_cpi_amount)?;
}
}
}

View File

@ -48,7 +48,7 @@ pub fn place_perp_order(
let mut perp_market = ctx.accounts.perp_market.load_mut()?;
let bids = &ctx.accounts.bids.to_account_info();
let asks = &ctx.accounts.asks.to_account_info();
let mut book = Book::load_checked(&bids, &asks, &perp_market)?;
let mut book = Book::load_checked(bids, asks, &perp_market)?;
// let oracle_price = oracle_price(&ctx.accounts.oracle.to_account_info())?;

View File

@ -103,8 +103,7 @@ pub fn register_token(
let alt_previous_size =
address_lookup_table::addresses(&ctx.accounts.address_lookup_table.try_borrow_data()?)
.iter()
.count();
.len();
let mut mint_info = ctx.accounts.mint_info.load_init()?;
*mint_info = MintInfo {
mint: ctx.accounts.mint.key(),

View File

@ -33,12 +33,12 @@ impl BorshDeserialize for CancelOrderInstructionData {
.try_into()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::UnexpectedEof, e))?;
*buf = &buf[20..];
Ok(Self::unpack(data).ok_or_else(|| {
Self::unpack(data).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
error!(MangoError::SomeError),
)
})?)
})
}
}
@ -112,7 +112,7 @@ pub fn serum3_cancel_order(
//
// Cancel
//
cpi_cancel_order(&ctx.accounts, order)?;
cpi_cancel_order(ctx.accounts, order)?;
Ok(())
}

View File

@ -47,7 +47,7 @@ pub struct Serum3CreateOpenOrders<'info> {
}
pub fn serum3_create_open_orders(ctx: Context<Serum3CreateOpenOrders>) -> Result<()> {
cpi_init_open_orders(&ctx.accounts)?;
cpi_init_open_orders(ctx.accounts)?;
let serum_market = ctx.accounts.serum_market.load()?;
let mut account = ctx.accounts.account.load_mut()?;

View File

@ -101,7 +101,7 @@ pub fn serum3_liq_force_cancel_orders(
// TODO: do the correct health / being_liquidated check
{
let account = ctx.accounts.account.load()?;
let health = compute_health(&account, &ctx.remaining_accounts)?;
let health = compute_health(&account, ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health < 0, MangoError::SomeError);
}
@ -115,8 +115,8 @@ pub fn serum3_liq_force_cancel_orders(
//
// Cancel all and settle
//
cpi_cancel_all_orders(&ctx.accounts, limit)?;
cpi_settle_funds(&ctx.accounts)?;
cpi_cancel_all_orders(ctx.accounts, limit)?;
cpi_settle_funds(ctx.accounts)?;
//
// After-settle tracking

View File

@ -69,12 +69,12 @@ impl BorshDeserialize for NewOrderInstructionData {
.try_into()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::UnexpectedEof, e))?;
*buf = &buf[46..];
Ok(Self::unpack(data).ok_or_else(|| {
Self::unpack(data).ok_or_else(|| {
std::io::Error::new(
std::io::ErrorKind::InvalidInput,
error!(MangoError::SomeError),
)
})?)
})
}
}
@ -203,8 +203,8 @@ pub fn serum3_place_order(
// Apply the order to serum. Also immediately settle, in case the order
// matched against an existing other order.
//
cpi_place_order(&ctx.accounts, order)?;
cpi_settle_funds(&ctx.accounts)?;
cpi_place_order(ctx.accounts, order)?;
cpi_settle_funds(ctx.accounts)?;
//
// After-order tracking
@ -234,7 +234,7 @@ pub fn serum3_place_order(
// Health check
//
let account = ctx.accounts.account.load()?;
let health = compute_health(&account, &ctx.remaining_accounts)?;
let health = compute_health(&account, ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);

View File

@ -107,7 +107,7 @@ pub fn serum3_settle_funds(ctx: Context<Serum3SettleFunds>) -> Result<()> {
//
// Settle
//
cpi_settle_funds(&ctx.accounts)?;
cpi_settle_funds(ctx.accounts)?;
//
// After-order tracking

View File

@ -99,7 +99,7 @@ pub fn withdraw(ctx: Context<Withdraw>, amount: u64, allow_borrow: bool) -> Resu
//
// Health check
//
let health = compute_health(&account, &ctx.remaining_accounts)?;
let health = compute_health(&account, ctx.remaining_accounts)?;
msg!("health: {}", health);
require!(health >= 0, MangoError::SomeError);

View File

@ -143,6 +143,7 @@ pub mod mango_v4 {
)
}
#[allow(clippy::too_many_arguments)]
pub fn place_perp_order(
ctx: Context<PlacePerpOrder>,
price: i64,

View File

@ -32,9 +32,9 @@ pub fn remove_slop_mut<T: bytemuck::Pod>(bytes: &mut [u8]) -> &mut [T] {
bytemuck::cast_slice_mut(&mut bytes[..new_len])
}
fn strip_data_header_mut<'a, H: bytemuck::Pod, D: bytemuck::Pod>(
orig_data: RefMut<'a, [u8]>,
) -> Result<(RefMut<'a, H>, RefMut<'a, [D]>)> {
fn strip_data_header_mut<H: bytemuck::Pod, D: bytemuck::Pod>(
orig_data: RefMut<[u8]>,
) -> Result<(RefMut<H>, RefMut<[D]>)> {
Ok(RefMut::map_split(orig_data, |data| {
let (header_bytes, inner_bytes) = data.split_at_mut(size_of::<H>());
let header = bytemuck::try_from_bytes_mut(header_bytes).unwrap();
@ -72,7 +72,7 @@ pub fn load_bids_mut<'a>(
bids: &'a AccountInfo,
) -> Result<RefMut<'a, serum_dex::critbit::Slab>> {
require!(
&bids.key.to_aligned_bytes() == &identity(sm.bids),
bids.key.to_aligned_bytes() == identity(sm.bids),
MangoError::SomeError
);
let orig_data = strip_dex_padding_mut(bids)?;
@ -91,7 +91,7 @@ pub fn load_asks_mut<'a>(
asks: &'a AccountInfo,
) -> Result<RefMut<'a, serum_dex::critbit::Slab>> {
require!(
&asks.key.to_aligned_bytes() == &identity(sm.asks),
asks.key.to_aligned_bytes() == identity(sm.asks),
MangoError::SomeError
);
let orig_data = strip_dex_padding_mut(asks)?;

View File

@ -122,7 +122,7 @@ fn compute_health_detail(
// converts the token value to the basis token value for health computations
// TODO: health basis token == USDC?
let native = position.native(&bank);
let native = position.native(bank);
token_info.balance = cm!(token_info.balance + native);
}

View File

@ -61,6 +61,12 @@ pub struct TokenAccountMap {
pub values: [TokenAccount; MAX_INDEXED_POSITIONS],
}
impl Default for TokenAccountMap {
fn default() -> Self {
Self::new()
}
}
impl TokenAccountMap {
pub fn new() -> Self {
Self {
@ -163,6 +169,12 @@ pub struct Serum3AccountMap {
pub values: [Serum3Account; MAX_SERUM_OPEN_ORDERS],
}
impl Default for Serum3AccountMap {
fn default() -> Self {
Self::new()
}
}
impl Serum3AccountMap {
pub fn new() -> Self {
Self {

View File

@ -34,7 +34,7 @@ pub fn oracle_price(acc_info: &AccountInfo) -> Result<I80F48> {
Ok(match oracle_type {
OracleType::Stub => acc_info.load::<StubOracle>()?.price,
OracleType::Pyth => {
let price_struct = pyth_client::load_price(&data).unwrap();
let price_struct = pyth_client::load_price(data).unwrap();
I80F48::from_num(price_struct.agg.price)
}
})

View File

@ -333,6 +333,7 @@ impl<'a> Book<'a> {
// todo: can new_bid and new_ask be elegantly folded into one method?
#[inline(never)]
#[allow(clippy::too_many_arguments)]
pub fn new_bid(
&mut self,
// program_id: &Pubkey,

View File

@ -74,9 +74,7 @@ impl<'a> Iterator for BookSideIter<'a> {
fn next(&mut self) -> Option<Self::Item> {
// if next leaf is None just return it
if self.next_leaf.is_none() {
return None;
}
self.next_leaf?;
// start popping from stack and get the other child
let current_leaf = self.next_leaf;

View File

@ -108,7 +108,9 @@ pub struct LeafNode {
fn key_to_price(key: i128) -> i64 {
(key >> 64) as i64
}
impl LeafNode {
#[allow(clippy::too_many_arguments)]
pub fn new(
version: u8,
owner_slot: u8,