mango-v4/programs/mango-v4/src/instructions/account_edit.rs

58 lines
1.3 KiB
Rust

use anchor_lang::prelude::*;
use crate::error::MangoError;
use crate::state::*;
use crate::util::fill_from_str;
#[derive(Accounts)]
pub struct AccountEdit<'info> {
pub group: AccountLoader<'info, Group>,
#[account(
mut,
has_one = group,
has_one = owner
)]
pub account: AccountLoader<'info, MangoAccountFixed>,
pub owner: Signer<'info>,
}
pub fn account_edit(
ctx: Context<AccountEdit>,
name_opt: Option<String>,
// note: can also be used to unset by using the default pubkey here as a param
delegate_opt: Option<Pubkey>,
) -> Result<()> {
require!(
name_opt.is_some() || delegate_opt.is_some(),
MangoError::SomeError
);
let mut account = ctx.accounts.account.load_full_mut()?;
// note: unchanged fields are inline, and match exact definition in create_account
// please maintain, and don't remove, makes it easy to reason about which support modification by owner
if let Some(name) = name_opt {
account.fixed.name = fill_from_str(&name)?;
}
// unchanged -
// owner
// account_num
// bump
if let Some(delegate) = delegate_opt {
account.fixed.delegate = delegate;
}
// unchanged -
// tokens
// serum3
// perps
// being_liquidated
// is_bankrupt
Ok(())
}