It is available in the alpha of SmartPy 18.1. Here is an example:
import smartpy as sp
@sp.module
def main():
class C(sp.Contract):
def __init__(self):
self.data.is_negative = False
@sp.entrypoint
def check_negative(self, x):
self.data.is_negative = sp.to_int(x) < 0
@sp.add_test(name = "test")
def test():
scenario = sp.test_scenario(main)
c = main.C()
scenario += c
c.check_negative(sp.bytes("0x09"))
scenario.verify(c.data.is_negative == False)
c.check_negative(sp.bytes("0xf6"))
scenario.verify(c.data.is_negative == True)
c.check_negative(sp.bytes("0x00f6"))
scenario.verify(c.data.is_negative == False)
Here is a workaround for the legacy syntax (notice that it doesn't work in the test scenario but you can originate the contract, you may use meta-programmation to mock it in scenario if you need):
to_int = sp.unpack(sp.constant(
"expru2UfuHkkPH6UHCZyYuLU6BdmNhuksJkTbtAB1C1vVXiFQ9R1Ed",
t = sp.TBytes), sp.TLambda(sp.TBytes, sp.TInt)).open_some()
Example:
import smartpy as sp
class Test(sp.Contract):
def __init__(self, in_scenario):
self.init(is_negative=False)
self.in_scenario = in_scenario
@sp.entrypoint
def check_negative(self, x):
to_int = sp.unpack(sp.constant(
"expru2UfuHkkPH6UHCZyYuLU6BdmNhuksJkTbtAB1C1vVXiFQ9R1Ed",
t = sp.TBytes), sp.TLambda(sp.TBytes, sp.TInt)).open_some()
self.data.is_negative = to_int(x) < 0
if "templates" not in __name__:
@sp.add_test(name = "Test")
def test():
c1 = Test(True)
scenario = sp.test_scenario()
scenario += c1