Calling contracts created with create_contract

The following question has been asked on Telegram:

How can I test the interactions of the contracts that are deployed with create_contract? How can I store the address of the deployed contract in a variable?

Just for the context.

  • I have a factory contract (FC) that has 2 entrypoints (EPs) - one that deploys C1 and another that deploys C2.

Now, within the same def test():

  • I first want to use the two create_contract EPs in the factory contract.
    That works. No problem.

  • But now I want to test if one of the deployed contract (C1) can call an entrypoint of the other deployed contract (C2).

  • In my code I can use the sp.transfer that takes in the address of the destination. How can I get the destination address ie the address that online IDE assigned for C1 and C2 when executing the testing code?

sp.create_contract returns the created contract’s address.

Here is an example that does what you expect.

import smartpy as sp

def main():
    class C1(sp.Contract):
        def __init__(self):

        def set_x(self, x):
   = x

    class C2(sp.Contract):
        def set_x_c1(self, x, c1):
            dest = sp.contract(, c1, entrypoint="set_x")
            sp.transfer(x, sp.tez(0), dest.unwrap_some())

    class Factory(sp.Contract):
        def __init__(self):
   = None
   = None

        def create_c1(self):
   = sp.Some(
                sp.create_contract(C1, None, sp.mutez(0), sp.record(x=42))

        def create_c2(self):
   = sp.Some(
                sp.create_contract(C2, None, sp.mutez(0), ())

        def set_x(self, x):
            param = sp.record(x=x,
            contract = sp.contract(
                sp.record(, c1=sp.address),
            sp.transfer(param, sp.tez(0), contract)

if "templates" not in __name__:
    def test():
        sc = sp.test_scenario(main)
        factory = main.Factory()
        sc += factory

My question was more about testing C1 entrypoint that, for example, ‘claims tokens’ or tickets for that matter, from C2.

Logic here would be -

  • C2 creates a ticket, and stores in the contract storage.
  • C1 has an entrypoint claim_ticket that calls C2 EP claim_ownership.
  • the claim_ownership in C2 implements some logic as needed and eventually sends the ticket back to C1
  • C1 receives the ‘ticket’ and stores it in it’s storage.

For the current example, we can ignore tickets, and we can work with a record for example.

But the idea is that I want to test this interaction in the online IDE.
For that, I need the contract addresses that are generated during the testing simulation so that I can call the entrypoints of C1 or C2

Factory contracts only function is to deploy. The rest of the logic is between the contracts that are deployed. And that interaction is something I want to test…

Or, since I do store the deployed contract address in the factory contracts storage, I can just access the storage in FC storage


and that gets me the element from contracts list with index 1 (assuming the deployed contract address was stored in a list)? What is the correct way to access different elements in a list stored in storage? Basic python syntax seems not to work here (my_list[index]) as smartpy interprets that as trying to access a map.

or / if considering your example above?