1

I have one contract which creates and stores another contract, so in the creator contract I have something like:

children=sp.map
            (
                tkey=sp.TNat,
                tvalue=sp.TAddress
            ),

Which is basically a map where the address of all the created children are stored. Now the problem comes when I want to get a children and interact with it (read data/execute a function). I've tried using the sp.contract() function but I can't seem to get it working. Any example code snippets or advice?

Thanks in advance.

1

AFAIK it's not possible to read the data of another contract (but this is probably changing when the Hangzhou protocol is adopted).

The following is an example of calling an entry point on another contract, but note that some of my comments about what the purpose of "open_some()" is are wrong, and I'm being much more explicit about types than you might need to be (no need to make every params a sp.TRecord but it's at least consistent).

https://gitlab.com/SmartPy/smartpy/-/blob/mr-5/python/templates/InterContractCallsExample.py

It's not available on smartpy.io yet, hopefully it will be soon, for now you can just copy/paste from there and experiment.

0

You are missing a few parameters: documentation

Also, you may want to use sp.big_map instead of sp.map if you expect to have many child contracts.

In the snippet below, inside entry_point ep I build a typed contract that will be used to call an entry_point named some_entrypoint on a child contract, where the input parameter is of type sp.TUnit.

import smartpy as sp

class MyContract(sp.Contract):
    def __init__(self, **kargs):
        self.init(
            children = sp.map(
                {
                    0: sp.record(
                        balance = 10,
                        address = sp.address("KT1Tezooo1zzSmartPyzzSTATiCzzzwwBFA1")
                    )
                },
                tkey = sp.TNat,
                tvalue = sp.TRecord(
                        balance = sp.TNat,
                        address = sp.TAddress
                    )
            )
        )

    @sp.entry_point
    def ep(self):
        child = self.data.children.get(0, message = "User does not exist")
        contract = sp.contract(
            sp.TUnit,
            child.address,
            entry_point = "some_entrypoint"
        ).open_some("invalid_contract")
        sp.transfer(sp.unit, sp.tez(0), contract)

@sp.add_test(name = "Minimal")
def test():
    scenario = sp.test_scenario()
    c1 = MyContract(x = 12)
    scenario += c1
    c1.ep()
2
  • Hi, thanks for the answer. Imagine i wanted to access a children data variable, like children.data.title, how would I do that?
    – TzDev
    Nov 8 '21 at 11:39
  • Updated the example to show how you can do it. Nov 8 '21 at 13:16

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.