0

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"
    sp.else:
        return "No"

@sp.entry_point
def updateSomething(self, params):
    sp.set_type(params, sp.TRecord(_id = sp.TNat, percentage = sp.TNat))
    sp.trace(self.is_something(sp.sender))

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

1 Answer 1

3

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...
else:
   do something else...

Applied at runtime

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

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

Feel free to join SmartPy Telegram.

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

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.