TransWikia.com

Initiate a field in the storage of a contract with the source address of origination

Tezos Asked by Daly on August 20, 2021

I have the following smart contract where I pass admin address in the init function of the smart contract: (you can run it on smartPy online editor)

class SC(sp.Contract):
    def __init__(self, admin):
        self.init(administrator = admin, value = 0)


    @sp.entry_point
    def increment(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.value =+ 1

if "templates" not in __name__:
    @sp.add_test(name = "SC")
    def test():

        scenario = sp.test_scenario()
        scenario.h1("SC")

        admin = sp.address("tz1-admin-address-1234")
        alice = sp.address("tz1-alice-address-1234")

        c1 = SC(admin)

        scenario += c1
        scenario.h2("Admin increments value")
        scenario += c1.increment().run(sender = admin)
        scenario.h2("Alice tries to increment value")
        scenario += c1.increment().run(sender = alice, valid = False)

        scenario.verify(c1.data.value == 1)

Can I initiate the administrator field in storage by doing something like:

class SC(sp.Contract):
    # admin is no longer passed here
    def __init__(self): 
        # Difference here
        self.init(administrator = sp.source, value = 0)


    @sp.entry_point
    def increment(self, params):
        sp.verify(sp.sender == self.data.administrator)
        self.data.value =+ 1

if "templates" not in __name__:
    @sp.add_test(name = "SC")
    def test():

        scenario = sp.test_scenario()
        scenario.h1("SC")

        admin = sp.address("tz1-admin-address-1234")
        alice = sp.address("tz1-alice-address-1234")

        # Difference here
        c1 = SC().run(source = admin) # or c1 = SC()

        scenario += c1
        scenario.h2("Admin increments value")
        scenario += c1.increment().run(sender = admin)
        scenario.h2("Alice tries to increment value")
        scenario += c1.increment().run(sender = alice, valid = False)

        scenario.verify(c1.data.value == 1)

What I want is initiate the administrator without passing his address in the init when deploying but by setting the deployer (source of the origination) as administrator.

Thank you in advance.

One Answer

You cannot do that.

  1. sp.source is only available in a contract code, not storage. SmartPy is telling you that there is a 'Missing environment' when you compute the storage.

  2. Your __init__ function should take an argument as is shown in the quoted template.

Answered by FFF on August 20, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP