CreateGroup: Allow multiple groups per admin

This commit is contained in:
Christian Kamm 2022-05-27 09:23:17 +02:00
parent 478b794034
commit b1fcb4a7e6
4 changed files with 22 additions and 10 deletions

View File

@ -4,10 +4,11 @@ use crate::error::*;
use crate::state::*;
#[derive(Accounts)]
#[instruction(group_num: u32)]
pub struct CreateGroup<'info> {
#[account(
init,
seeds = [b"Group".as_ref(), admin.key().as_ref()],
seeds = [b"Group".as_ref(), admin.key().as_ref(), &group_num.to_le_bytes()],
bump,
payer = payer,
space = 8 + std::mem::size_of::<Group>(),
@ -22,9 +23,10 @@ pub struct CreateGroup<'info> {
pub system_program: Program<'info, System>,
}
pub fn create_group(ctx: Context<CreateGroup>) -> Result<()> {
pub fn create_group(ctx: Context<CreateGroup>, group_num: u32) -> Result<()> {
let mut group = ctx.accounts.group.load_init()?;
group.admin = ctx.accounts.admin.key();
group.bump = *ctx.bumps.get("group").ok_or(MangoError::SomeError)?;
group.group_num = group_num;
Ok(())
}

View File

@ -25,8 +25,8 @@ pub mod mango_v4 {
use super::*;
pub fn create_group(ctx: Context<CreateGroup>) -> Result<()> {
instructions::create_group(ctx)
pub fn create_group(ctx: Context<CreateGroup>, group_num: u32) -> Result<()> {
instructions::create_group(ctx, group_num)
}
#[allow(clippy::too_many_arguments)]

View File

@ -5,7 +5,6 @@ use std::mem::size_of;
// TODO: Assuming we allow up to 65536 different tokens
pub type TokenIndex = u16;
// TODO: Should we call this `Group` instead of `Group`? And `Account` instead of `MangoAccount`?
#[account(zero_copy)]
pub struct Group {
// Relying on Anchor's discriminator be sufficient for our versioning needs?
@ -13,15 +12,22 @@ pub struct Group {
pub admin: Pubkey,
pub bump: u8,
pub reserved: [u8; 7],
pub padding: [u8; 3],
pub group_num: u32,
pub reserved: [u8; 8],
}
const_assert_eq!(size_of::<Group>(), 40);
const_assert_eq!(size_of::<Group>(), 48);
const_assert_eq!(size_of::<Group>() % 8, 0);
#[macro_export]
macro_rules! group_seeds {
( $group:expr ) => {
&[b"Group".as_ref(), $group.admin.as_ref(), &[$group.bump]]
&[
b"Group".as_ref(),
$group.admin.as_ref(),
&$group.group_num.to_le_bytes(),
&[$group.bump],
]
};
}

View File

@ -694,10 +694,14 @@ impl<'keypair> ClientInstruction for CreateGroupInstruction<'keypair> {
_account_loader: impl ClientAccountLoader + 'async_trait,
) -> (Self::Accounts, instruction::Instruction) {
let program_id = mango_v4::id();
let instruction = Self::Instruction {};
let instruction = Self::Instruction { group_num: 0 };
let group = Pubkey::find_program_address(
&[b"Group".as_ref(), self.admin.pubkey().as_ref()],
&[
b"Group".as_ref(),
self.admin.pubkey().as_ref(),
&instruction.group_num.to_le_bytes(),
],
&program_id,
)
.0;