I am trying to do some maths operations inside an on-chain view and am getting an unexpected result.
The reproduction contract tries to perform the following steps:
- Define an internal contract property of type nat (e.g. 40)
- Define a new local variable A of type nat inside the view (e.g. 1000)
- Multiply the internal nat value by 10 and store its result in another local variable B (will be 400)
- Subtract variable B from variable A (result is 600) and store result in another local variable C
- Divide variable C by 2 storing it in variable D and return the value (should return 300)
import smartpy as sp
class MathsTest(sp.Contract):
def __init__(self, **kargs):
self.init(
internal_number=sp.nat(40),
**kargs
)
@sp.onchain_view()
def nat_test(self):
maxi = sp.local('maxi', sp.nat(1000))
multi = sp.local('multi', self.data.internal_number * sp.nat(10))
sub = sp.local('sub', maxi.value - multi.value)
div = sp.local('div', sub.value // sp.nat(2))
sp.result(div.value)
@sp.add_test(name = "MathsTest")
def test():
scenario = sp.test_scenario()
scenario.h1("Maths")
c1 = MathsTest()
scenario += c1
scenario.verify(c1.nat_test() == sp.nat(300))
Instead of passing the test throws the following:
Error: Type Error
Type sp.TInt / sp.TNat mismatch
Type Error (maxi.value : sp.TNat), (multi.value : sp.TNat) cannot be subtracted in (maxi.value - multi.value : sp.TNat)
(__main__, line 14)
Why does it say there is an int
mismatch when everything is declared as nat
?