Updating a value of a Struct which is mapped with Addresses

Hello,

Lets say there are about 10,000 addresses mapped through Map<&Addr, User> = Map::new("users") and User is a struct variable with 4 Uint128 field in it, whats the best way to update all these 10,000 addresses? I mean, for example, let’s say that one of those Uint128 field in balance and I want to update this field for all the 10,000 addresses.

Thank you.

Regards,
Aries

1 Like

Many thoughts about storage: (hope it helps)

  1. If you are updating the values for “all” of your keys then I would consider creating a different storage approach where you “index” (set the key to be unique and meaningful) to the value or even a single key to all of the values you tend to update at once. For example, you could store a Vec as an Item and just read and write it once to/from storage. Where UserBalance has the Addr and Balance defined. Or, you could create a Map<Addr, Balance> and still loop over them all in storage—but then you have maps for each “property” balance and the other three.

  2. I personally like for loops, just go for it and loop through all 10000 addresses and update them. And, see what happens with fees on testnet.

  3. I create a autoincrement counter to represent the primary key of my most important structure(s). And then use that to store vectors of them, keyed or “indexed” so I can at least get all their ids at one time and then loop over them “hydrate/instantiate” them from their primary storage and update them. But this depends on have a good data design (Entity Relationships) for on-chain storage.

  4. I also use concatenated strings as key when I have a multipart key, because the typed key concatenation and IndexedMap have been difficult for me to comprehend—and I prefer this approach. It seems to work well. But, I’m also open to other approaches.

Here’s examples of storage for the project I’m working on…

1 Like

Hey,

@petegordon

Thank you for the detailed explanation.

Here is the code which I am trying to test.

let User_Addresses: StdResult<Vec<_>> = USERS.range(deps.storage, None, None, Order::Ascending).collect();

Now, in the above code, I assume all the data is collected into the variable User_Addresses. How do I loop through the User_Addresses and get Addr value?

Also, I am trying the following code:

for keys in USERS.keys(deps.storage, None, None, Order::Ascending).into_iter()
{
}

Here too, I am unable to access the addresses. Here is the Storage code.

pub struct User {
    pub balance: Uint128,
}

pub const USERS: Map<&Addr, User> = Map::new("user");

Now, I need to update the balances of all the users using the above mapping structure.

I know you cannot execute 10,000 account updates at one shot. But, wanted to know how to code this and get it working.

Regards,
Aries