1

Regarding the fact using String in contract is expensive, is there any alternative type in Solidity ?

The input value (Payload) I want to set is as follows :

{"SQN":4,"Node_ID":"00deadface0102030405060708090a0b","Energy":"5.00","Power":"1.80","Sign":"54c5645616a8567d605bd990df0456913420fb630860316cf29bf57e19ef3102933ac948e672a4e3dfa8b7e20cc9eb44520aa8d8dc69143a5bc718a17a1d3f2dfc0084f172dbda6dd38e411e75100136d95d46e3d563c3a0aed062c40c6d17a5038ef0ad681622245bd2332008b1edca693b3418df1f5f86f09a6585d4af7da0d2ab44f5ce1c7ec8eed51696697b2674a881868be7163eda7c6562d02bdf069fcd66d483e8b22c5c976d27c04ffe38e62800eec17f1786b2f2161fcf44b91ba85a4486edb48a5c5a2f58dc1fa3969917eadabd7af753947de4a7f03034de09adb3b1cacb77e21444eee2d90944037606f84d0aa99f4794e8df8a2d0fbfff55c7"}
  • use events instead storing as string or bytes – Badr Bellaj May 1 '18 at 19:41
  • @BadrBellaj Bellaj Thank you. Would you explain me a bit more how to declare such this event ? ex. event MyEvent(uint256 SQN, ....); I'm wrong ? – Questioner May 1 '18 at 19:50
2

you can use events to store your data as it's a cheaper storage alternative.

event payload(uint256 indexed SQN, ....);

you can use an indexed variable to use it as an index to retrieve your input. in the front-end you can use get to retrieve the events or watch to catch fired ones.

  • Thank you, but how about for "Sign" field since it contains both digit and letter ? I cannot define it as uint256 Sign, Isn't it ? – Questioner May 1 '18 at 20:42
  • no it's a string, the difference here is you're using a string in an event not as a state – Badr Bellaj May 1 '18 at 20:44
  • but as I mentioned in my question i want to avoid using string since it's expensive in contract. – Questioner May 1 '18 at 20:45
  • it's expensive if used as a variable not as a parameter for event. read this post to know more media.consensys.net/… otherwise you can store data off-chain in ipfs or other systems – Badr Bellaj May 1 '18 at 20:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.