[SOLVED] Factory Pattern, Address Storage instead of whole Interface

Hey,

I have a few questions regarding Sophia.

  1. Is it possible to create and deploy new contracts inside a function of another contract (e.g. create and deploy a Car contract in CarFactory contract function create_car(…))?

  2. Is it possible to store only the address of a contract and when I want to call functions of it to cast it to its interface (e.g. ierc20: address, call transfer function IERC20(ierc20).transfer(…))?

Thanks

3 Likes

Hi @rwazqay,

  1. At the moment, it is not possible for one contract do deploy another one so you’ll have to work around that.

  2. As far as my knowledge goes you cannot cast remote contract to address and vice versa, I was playing with this exact thing few days ago but haven’t found a way to make it work. The thing is, when you have something of remote contract type (interface for another contract) you cannot do anything with this object other than calling the functions defined there, pass it as parameter to another function, or persist it’s value in state. Someone should correct me here if I’m wrong.

1 Like

Hey @rwazqay,

  1. as @filip said, I think this is not possible right now
  2. what you can do is let the one contract have a mapping e.g. map(address, Remotecontract), where you can add a remote contract like this
  stateful entrypoint add_remotecontract(remotecontract : Remotecontract) =
    put(state{ remotecontracts[Remotecontract.get_address()] = remotecontract})

Your remote contract will have to implement entrypoint get_address() = Contract.address.
Sadly this is not optimal as address type is ak_ so only references the account of that contract which can be misleading, but this allows you to call contracts via proxy by address.

3 Likes

Maybe you can have a look at the WORK IN PROGRESS governance contracts, where we have a registry of polls as separate contracts that follow a similar pattern as I described. https://github.com/aeternity/aepp-governance/tree/master/governance-contracts/contracts

2 Likes

To do that, if it was possible, the first contract would need to have to know the code of the second contract. So why not include the functionality in the first contract then?

1 Like