0

In order to create history and statistics, I'm struggling to figure out the best way to implement a lookup system. I'm building a game, and right now, my init for the entrant account looks like this:

    #[account(
        init, 
        payer = player,
        seeds = [b"entrant", player.key().as_ref(), round.key().as_ref()],
        bump,
        space = 8 + Entrant::MAXIMUM_SIZE)
    ]
    pub entrant: Account<'info, Entrant>,

Since a player can enter more than one round, I'm using both the player key and the round key as the seeds.

However, given the player, I want to be able to somehow lookup all the rounds the player has ever played. I don't want to keep in in one account, as this game can have a lot of rounds and I don't want to run out space.

What would be the best practice in dealing with a situation like this?

2 Answers 2

1

I think you should use:

seeds = [b"entrant", game.key().as_ref(), player.key().as_ref(), round.key().as_ref()]

And pass the relevant round to the program.

The client can search via getProgramAccounts for the player/game combo.

5
  • Thanks! I think I was a little unclear. There is only one game, as a one game = one round, so there is no "game" account.
    – web3man
    Commented Jul 27, 2022 at 2:53
  • The issue is, that you need to pass the program all accounts for the transaction. So an on-chain lookup doesn't help much. Since you can just supply a pointer to it an use that. Unless you store a blob of data on-chain which is expensive and limited.
    – Ohad Dahan
    Commented Jul 27, 2022 at 5:54
  • Hmm. That's tough. How would you handle a game that has more players than can fit in an account. Let's say 2000 people participate in a game
    – web3man
    Commented Jul 27, 2022 at 10:57
  • Do you need all 2000 players info in each txn?
    – Ohad Dahan
    Commented Jul 27, 2022 at 14:51
  • 1
    I was able to figure out a different structure for this in the contract where I just check to see individually if the user won each round, and can use the getProgramAccounts on the client. Thank you for the help!
    – web3man
    Commented Jul 27, 2022 at 16:06
0

You have two main options:

  • iterate through all the possible combinations of seeds for a user. If you know that the current max round is 2000, you can try all possible 2000 entrant addresses and fetch the account there. For huge numbers, this will be tricky.
  • store the player address in the entrant account, and then do a get_program_accounts with the correct offsets for the entrant account type and the user's address

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.