I am using sp.if in an internal function, then I call it in an entry point, but whatever I enter in sp.if, it always is read as "True". Why? And how to fix it? My code :

def is_something(self, contract):
    sp.if 1 == 2:
        return "Yes"
        return "No"

def updateSomething(self, params):
    sp.set_type(params, sp.TRecord(_id = sp.TNat, percentage = sp.TNat))

When the entry point is called, sp.traces writes "Yes" instead of no. Why?

1 Answer 1


sp.if gets translated to Michelson IF's, and cannot be used together with meta-programming e.g. ruturn statements since meta-programming gets applied before compilation.

Meta-programming (Applied before compilation, useful for creating contracts with multiple configurations)

if <condition>:
   do stuff...
   do something else...

Applied at runtime

some_var = sp.local("some_var", 0)
sp.if <condition>:
   some_var.value += 1
   some_var.value = 0

Example on how to do it: https://smartpy.io/ide?cid=QmUWSVV6wnkDUF25thYSBPTJqWaATHeDELoQWwgh3ap6DV&k=17f3f11bcd5974c8d408

Feel free to join SmartPy Telegram.

  • Extended the initial answer with more descriptive examples. Commented May 19, 2021 at 13:41

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.