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?
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?
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
@sp.module
def main():
class C1(sp.Contract):
def __init__(self):
self.data.x = sp.int(1)
@sp.entrypoint
def set_x(self, x):
self.data.x = x
class C2(sp.Contract):
@sp.entrypoint
def set_x_c1(self, x, c1):
dest = sp.contract(sp.int, c1, entrypoint="set_x")
sp.transfer(x, sp.tez(0), dest.unwrap_some())
class Factory(sp.Contract):
def __init__(self):
self.data.c1 = None
self.data.c2 = None
@sp.entrypoint
def create_c1(self):
self.data.c1 = sp.Some(
sp.create_contract(C1, None, sp.mutez(0), sp.record(x=42))
)
@sp.entrypoint
def create_c2(self):
self.data.c2 = sp.Some(
sp.create_contract(C2, None, sp.mutez(0), ())
)
@sp.entrypoint
def set_x(self, x):
param = sp.record(x=x, c1=self.data.c1.unwrap_some())
contract = sp.contract(
sp.record(x=sp.int, c1=sp.address),
self.data.c2.unwrap_some(),
entrypoint="set_x_c1"
).unwrap_some()
sp.transfer(param, sp.tez(0), contract)
if "templates" not in __name__:
@sp.add_test(name="MyContract")
def test():
sc = sp.test_scenario(main)
factory = main.Factory()
sc += factory
factory.create_c1()
factory.create_c2()
factory.set_x(1337)
My question was more about testing C1 entrypoint that, for example, ‘claims tokens’ or tickets for that matter, from C2.
Logic here would be -
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
Like:
c.data.contracts[1]
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 c.data.c1 / c.data.c2 if considering your example above?