-1

I have the contract

use anchor_lang::prelude::*;

declare_id!("kek");

#[constant]
pub const MEDIA_TAG: &[u8] = b"MEDIA_STATE";

#[account]
#[derive(Default)]
pub struct MediaState {
    pub authority: Pubkey,
    pub name: String,
}

#[program]
pub mod publisher {
    use super::*;

    pub fn create_media(ctx: Context<CreateMedia>, name: String) -> Result<()> {
        msg!("Media name: {}", name);

        let media_state = &mut ctx.accounts.media_state;

        media_state.authority = ctx.accounts.authority.key();
        media_state.name = name;

        msg!("Media created");
        Ok(())
    }
}

#[derive(Accounts)]
#[instruction()]
pub struct CreateMedia<'info> {
    #[account(mut)]
    pub authority: Signer<'info>,

    #[account(
        init,
        seeds = [MEDIA_TAG, authority.key().as_ref()],
        bump,
        payer = authority,
        space = 8 + std::mem::size_of::<MediaState>(),
    )]
    pub media_state: Box<Account<'info, MediaState>>,

    pub system_program: Program<'info, System>,
}

and I`d like to get the MediaState/CreateMedia discriminator in my javascript code

  const publisher = new web3.PublicKey(
    "kek"
  );
  const connection = new web3.Connection(web3.clusterApiUrl("devnet"));

  const accounts = await connection.getProgramAccounts(publisher, {
    filters: [{ memcmp: { offset: 0, bytes: ?????????? } }],
  });

I don`t want use anchor in my js code bc i need only the getProgramAccounts

Thanks

filters: [{ memcmp: { offset: 0, bytes: "FtoqZ3bt1he" } }] - it works. But I don`t know how I can get "FtoqZ3bt1he". I stole it from SolanaPlayground (network devtools)

keker
  • 29
  • 3

1 Answers1

0

const name = 'MediaState';
const discriminator = Buffer.from(sha256.digest("account:" + name)).slice(
  0,
  8
);

const publisher = new web3.PublicKey(
  "kek"
);
const connection = new web3.Connection(web3.clusterApiUrl("devnet"));

const accounts = await connection.getProgramAccounts(publisher, {
  filters: [{ memcmp: { offset: 0, bytes: base58.encode(discriminator) } }],
});
keker
  • 29
  • 3
  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 09 '23 at 04:14