1

I was previously told not to use String in ink! contracts.

Right now I'm using Vec<u8> to represent strings, but I'm still feeling a bit unsure if I'm on the right path, mainly because:

  • they're unbounded, which can lead to undefined behavior
  • whenever I have Vec<u8> on some message interface, polkadot-js allows me to upload a file, which feels very wrong

which type is best suited to represent strings in a contract?

1 Answer 1

4

If you really need a String in your contract then you should use a String type. That is the correct way to do it.

However, using strings in contracts can be considered an anti pattern. Strings are for human consumption and anything user facing should be placed in UI code.

they're unbounded, which can lead to undefined behavior

They are not "more" unbounded as a String. Please note that this can not lead to undefined behavior in a contract. At some point you will hit a limit (memory allotted to your contract, gas limit) and this is completely deterministic.

2
  • the strings I need to deal with are coming into the contract as inputs from the user Commented Feb 24, 2022 at 18:42
  • As I said: If you are sure that you want to handle text in your contract then String is the way to go. Maybe accept this answer and ask a new question specifically about whether text handling is the right thing to do in your case. Commented Mar 24, 2022 at 9:24

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.