Hi. What will be the best option to store user transaction data in terms of cost saving? I have two options: mapping and array.
With mapping it will looks something like this:

mapping(address => mapping(uint256 => UserTransaction)) data;

struct UserTransaction {
address user;
address token;
uint256 amount;
bytes32 title;
uint256 status;
}

With an array it will looks something like this:

mapping(address => UserTransaction) data;
struct UserTransaction {
Transaction[] transactions;
}

struct Transaction {
address user;
address token;
uint256 amount;
bytes32 title;
uint256 status;
}

I know that the first option will cost me more if I will iterate through the mapping to get all user's data because I'll need to pay for each method call via my RPC provider.
The second option will cost less from RPC provider perspective because I will do just one method call to get data.
But what about gas cost? If the user will have thousands of items in the array, will adding the new items increase gas costs over time?
Am I right or wrong in my estimations and what else did I miss?

Mar 8, 2023, 4:47 PM

© 2024 Draquery.com All rights reserved.