How to Read an Account

To read accounts, readers are used - these are a set of abstractions that ensure the account being read is in the correct state, has the correct version, the right size, etc. To utilize a reader, you need an instance of a configured DvlClient.

For example, reading the root account can be done in the following way:

let root_account = client.get_account::<RootAccount>(()).await?;

Handling Errors and Optimizing RPC Calls

Note that if the operation fails, you will receive an error that can be printed and must be handled according to the logic of your program. You can save the public key of an account and subsequently use it to read by the public key to reduce the number of RPC calls and increase response speed:

let instruments_account_pubkey = &root_account.instruments_address;

let instruments_account = client.get_account_by_public_key::<InstrumentsAccount>(instruments_account_pubkey).await?;

Reading Parameters

To read certain accounts, no parameters are required (as in the example with the root account), so an empty object must be passed to the read function. However, some accounts, such as the worker_account, require an index to be passed for reading (since there are many such accounts, the index serves as an additional element in locating a specific account):

let worker_7 = client.get_account::<WorkerAccount>(DvlIndexParam { id: 7 }).await?;

When attempting to read a specific account, it is necessary to check which type of parameters are required for its retrieval. Possible options include:

  • () - No parameters are required.
  • DvlIndexParam - The index of the account is required.
  • DvlClientParam - Required for reading the Payoff log from a client account.
  • DvlClientParams - Parameters needed for reading a client account.