0

Lets say i want to store some data about each address ,through the mapping. My first idea is: store in struct this way:

struct user {
   address userAddress;
   uint256 someUint:
   string someString;
}

mapping (address => user[]) public addressToStruct;

function storeData (uint256 _someUnit, string _someString) external {
   addressToStruct[msg.sender].push(user{address(msg.sender), _someUnit, someString});
}

But now i have a new idea to doing this but i don't know which one is gas efficient and if one of them is more secure , and idea is that store them as byte:

mapping (address => bytes[]) public addressToBytes;

function storeData(uint256 _someUint, string _someString) external {
   bytes data = abi.encodePacked(_someUint,_someString,address(msg.sender));
   addressToBytes[msg.sender].push(data);
}

1 Answer 1

0

just a hint:

Think twice, maybe you can store the data outside of blockchain using events.

(Also I can't find the information to prove my statement,but as i know doing abi.encodePacked costs like 800 gas)

1
  • I don't like to store them off chain , just calling : "abi.encodePacked" costs 800?? it means store as a struct is the gas efficient way?
    – Alireza
    Commented Feb 4, 2023 at 16:53

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.