0

I'm trying to provide better error messages. I've tried several things already that don't work such as regular python fstrings, regular string concatenation. I'm tried to using the sp.string function now, but it still doesn't work.

Example:

x_i = (sp.to_int(params.x + i))
y_j = (sp.to_int(params.y + j))
err_msg = "contains pixel(" + sp.string(x_i) + ") that has been bought already"
sp.failwith(err_msg)

Error message when running it:

Error: Error in Scenario
Literal format error: (string(toInt(add(attr(params 48)x 57)(iter i 55)58)58))

2 Answers 2

1

It is not possible to cast an int to a string in Michelson.

I recommend you to have static error messages, such as:

class Errors:
  SOME_ERROR = "SOME_ERROR"

# some contract
class Contract(sp.Contract):
  ...

  x_i = (sp.to_int(params.x + I))
  y_j = (sp.to_int(params.y + j))
  sp.failwith(Errors.SOME_ERROR)

SmartPy also gives you the option to define the exception verbosity.

4
  • 2
    Failing is possible on a non constant as well such as: sp.failwith(("contains pixel", x_i, "that has been bought already")). It should also provide all the necessary data (but it will take a lot of place).
    – FFF
    Apr 6, 2021 at 20:19
  • 1
    Also, sp.string is for string literals, aka, constants.
    – FFF
    Apr 6, 2021 at 20:20
  • @FFF passing in that tuple worked! (Feel free to write that as a separate answer and I can mark it as accepted) What do you mean it will take a lot of place?
    – JJJ
    Apr 6, 2021 at 20:37
  • 2
    Strings are major culprits for storage taken by smart contracts. You may not care for a small contract but it’s important for bigger ones when you do this repeatedly (this is the reason why SmartPy compiles location error as the line number only).
    – FFF
    Apr 7, 2021 at 9:29
1
def string_of_nat(params):
    c   = sp.map({x : str(x) for x in range(0, 10)})
    x   = sp.local('x', params)
    res = sp.local('res', [])
    sp.if x.value == 0:
        res.value.push('0')
    sp.while 0 < x.value:
        res.value.push(c[x.value % 10])
        x.value //= 10
    return sp.concat(res.value)

ref: https://smartpy.io/releases/20210118-6f466eee56038df8fda8b2f77819b83398346296/ide?template=stringManipulations.py

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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