0

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.

0

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.

| improve this answer | |
  • I see, Is there an other way or it's impossible to initiate a value with the address of the originator of the smart contract without passing his address in the init? – Daly Feb 11 at 10:32
  • It's impossible unless you change it by calling an entry point (and adding your contract again in the scenario but that changes the contracts). – FFF Feb 11 at 12:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.