TransWikia.com

Is it a good practice to store a lot of keys in a string array in smart contracts?

Ethereum Asked by silverwrath on December 25, 2021

I am working on a project where blockchain will be used on a supply chain. One can create and transfer assets to another in the same.

Whenever one is creating an asset or transferring any asset, that action is being stored in the smart contract’s string array.

And the strings quite big, for eg. “Token Tkn56fg2 is created by vendor with ID 4123” or “Tokens Tkn5rts2, Tkn67htf were transferred from vendor 5612 to vendor 4523” and so on…

All these actions are stored so that every action happened till date can be seen by anyone. In the dashboard section of my frontend, I’m taking this entire string array, and displaying these actions as individual actions taken place till date.

My question is, is this a good practice to store the actions in a string array? Obviously this seems like it is not. But since I’m required to store all the actions till date, and also don’t want anyone to make changes to these, I’m avoiding storing these in any other databases.
Pardon me if my question did not make sense. Also any suggestions to a different approach would be appreciated. Thank you.

3 Answers

If the string you want to store represents the occurrence of some event (which is what it looks like in your question) it would probably make sense to emit them as events and read them from the logs rather than save them in the state of the contract. For starters its way cheaper storage mechanism, and if you the event its essentially free storage. (Although I have bene hearing about some potential future changes which may trucncate some of the logs. Not sure of the status of that.);

Answered by GGizmos on December 25, 2021

Do you really need those strings in the smart contract? Is there a functionality that use those strings?

Storage and string manipulation are expensive in a smart contract. If you want to track performed actions there are better and cheaper options, like events, or using a mapping with a fixed array size like bytes32.

Answered by Ismael on December 25, 2021

Why do you want to save the transactions by yourself? it's not secure and they can be tampered. Transactions are automatically get saved (and validated) into a block in the blockchain, which means it's tamper proof.

if you want to get all transactions in your smart contract a tool like Etherscan can help you look at an address, and transactions from that address via your web browser. If you want to do something programmatically, you will need to use a Web3 library like Web3.js or Web3.py.

For more information visit web3.js documentation for transaction related examples

Answered by Masoud jt on December 25, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP