2

Hi i have a UserAccount which is used to hold the data of the user and is created with a instruction called sign_up , and also i have a setup_game instruction and in there I want to check that is there a user registered with the given Pubkey. In TS tests to do that i use :

const account = await program.account.userAccount.fetch(
            secondUser.publicKey
        ); 

So this gives the user_account of that Pubkey . And my Rust code so far :

pub fn setup_game(ctx: Context<SetupGame>, player_two: Pubkey) -> Result<()> {
    ctx.accounts
        .game
        .start([ctx.accounts.player_one.key(), player_two])
}
2
  • Based on the code provided, could you clarify what you're asking? you want to check that account belongs to a registered user?
    – C.OG
    Commented Aug 14, 2022 at 10:43
  • Actually i want to check if any user with Pubkey has the UserAccount or not (registered or not ) . Commented Aug 14, 2022 at 11:02

1 Answer 1

1

On solana, for you to read/write to an account, it needs to be passed as an account to the instruction.

So if you want to read the account that belongs to player_two, you'll need to pass it as an account to the SetupGame instruction.

So you'll have access to the account info of ctx.accounts.player_two if its initialized

6
  • Thanks, but do you see the code const account = await program.account.userAccount.fetch( secondUser.publicKey ); i want the rust code of it . Actually by registered i mean the UserAccount of Pubkey . Commented Aug 14, 2022 at 11:47
  • 1
    Oh, I think I understand your question now. Sure will modify
    – C.OG
    Commented Aug 14, 2022 at 11:48
  • Could you edit your question and add in the rust code you have for setup_game
    – C.OG
    Commented Aug 14, 2022 at 11:49
  • Of course, I edited it . However I don't know why you asked . Commented Aug 14, 2022 at 11:56
  • It helps me to answer your question.
    – C.OG
    Commented Aug 14, 2022 at 12:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.