Are there any restrictions on the length of the value that can be stored as type string in Solidity? I am trying to store a string value in the smart contract member field. The length comes to around 220 chars. If I reduce the string value to around 140 chars I am able to store it. Any more and it does not work.
Asked
Active
Viewed 8,640 times
8
sravtej
- 93
- 1
- 5
1 Answers
8
the problem is not about the string size but about the provided gas, the storage costs more gas as you store more data. so when you increase the string size increase the gas limit provided in your transaction. using the following example :
contract store{
string public storage_;
function store_it(string s){
storage_=s;
}
}
I've provided a string with 220 character it costs me 774675 gas.
Badr Bellaj
- 18,780
- 4
- 58
- 75
-
1BTW, the documentation defines string as arbitrary-length, which suggests that the gas cost is the only limiting factor here. – Sergei Tikhomirov Aug 03 '17 at 12:17
-
1the storage cost is discussed in a related post https://ethereum.stackexchange.com/questions/872/what-is-the-cost-to-store-1kb-10kb-100kb-worth-of-data-into-the-ethereum-block – Badr Bellaj Aug 03 '17 at 12:32
-
@SergeiTikhomirov is there a way to limit the number of characters in the string? – T. Thomas Aug 31 '20 at 22:58
-
If you limit to 255 characters, how expensive is it. As of this moment 774675 gas is around $0.00000000123888350025 – Lokiare Feb 08 '21 at 00:04
-
@Lokiare where can I find information like how much 1 gas costs for example (except google. I tried) – S. Goody Aug 05 '21 at 20:53
-
1@S. Goody its listed on the Ethereum site. – Lokiare Aug 13 '21 at 17:44