The following question has been asked on Telegram:
can I create a contract in new syntax and
call the onchain view of that contract
from the contract that is being written on old syntax ?
The following question has been asked on Telegram:
can I create a contract in new syntax and
call the onchain view of that contract
from the contract that is being written on old syntax ?
Yes, here is an example that shows all the configuration: new syntax calling legacy, legacy calling new syntax and even legacy calling legacy and new syntax calling new syntax:
import smartpy as sp
class OldSyntax(sp.Contract):
def __init__(self):
self.init(res=sp.none)
@sp.entry_point
def call_view(self, addr):
self.data.res = sp.view("the_view", addr, sp.unit, t = sp.TString)
@sp.onchain_view()
def the_view(self):
sp.result("legacy")
@sp.module
def main():
class NewSyntax(sp.Contract):
def __init__(self):
self.data.res = None
@sp.entrypoint
def call_view(self, addr):
self.data.res = sp.view("the_view", addr, (), sp.string)
@sp.onchain_view()
def the_view(self):
return "new syntax"
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
sc = sp.test_scenario(main)
c1 = OldSyntax()
sc += c1
c2 = main.NewSyntax()
sc += c2
sc.h2("Legacy calling own view")
c1.call_view(c1.address)
sc.verify(c1.data.res == sp.some("legacy"))
sc.h2("Legacy calling new syntax view")
c1.call_view(c2.address)
sc.verify(c1.data.res == sp.some("new syntax"))
sc.h2("New syntax calling own view")
c2.call_view(c2.address)
sc.verify(c2.data.res == sp.some("new syntax"))
sc.h2("New syntax calling legacy view")
c2.call_view(c1.address)
sc.verify(c2.data.res == sp.some("legacy"))