0

I play this https://beta.solpg.io/62d67703f6273245aca4f5fe example days ago. Just added more logs inside initialize() instruction. After invoke initialize() multiple times with different 'new_account' parameter input, then click 'FetchAll', but, it display all the NewAccount, which is to my surprise. enter image description here

I suppose that only latest data were stored and old ones be covered or overwrited.Because 'new_account' is just a normal account, not PDA account, and should not alloc new space when accept different 'new_account' address.

So my question is: why doesn't it just store one data copy? How does it work to store all history data?

Another question is, which API does playground used to read out all these data?

1 Answer 1

2

Notice how in that result each account has a different publicKey? What's happening here is that every time you run your instruction with a different address passed as newAccount it creates a new account at the address you give it. If you try to use the same address twice (or one of the existing ones), you'll get an error. That's because your instruction is using #[account(init, ...) so it expects to initialise that account when it runs.

It's not storing history, you're creating lots of accounts and it's returning all of them.

PDAs aren't special in this regard, any account that your program initialises will be returned in these results.

In terms of the API used, it's an Anchor API. Anchor generates a program client based on the structure of your program. In your case it'd be program.account.newAccount.all() that would return that data.

4
  • Your explanation make sense. Thanks. Commented Jan 15 at 2:50
  • Suppose I add two more account: newAccount2, newAccount3, they all have same annotations and attribute, only name different. And initialize newAccount with pubkey1,pubkey2,pubkey3, initialize newAccount2 with pubkey2,pubkey3, pubkey4, initialize newAccount3 with pubkey3, pubkey4,pubkey5, then, I wonder how Anchor tell these apart when fetch out? Does anchor add a special field to account data, for example 'catalog_name' as field name, and store value newAccount1 or newAccount2 or newAccount3 in this special field? Commented Jan 15 at 3:08
  • You might want to ask another question, comments don't really work for Q+A! But if you have new_account2: Account<'info, NewAccount> that will be a NewAccount. If you have pub struct NewAccount2 { with the #[account] annotation somewhere, then you do new_account2: Account<'info, NewAccount2 then that'll show up as NewAccount2. Remember you can only initialise to a public key once though (unless it's cleared + closed), so there's never overlap there.
    – Callum M
    Commented Jan 15 at 9:08
  • yeah, I will create a new post on this question to get thing more clear. Thanks Commented Jan 16 at 2:27

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.