0

There is an interface called IiZiSwapFactory (source), which declares a method named swapX2YModule() (source).

IiZiSwapFactory.sol

interface IiZiSwapFactory {

    /// @notice emit when successfuly create a new pool (calling iZiSwapFactory#newPool)
    /// @param tokenX address of erc-20 tokenX
    /// @param tokenY address of erc-20 tokenY
    /// @param fee fee amount of swap (3000 means 0.3%)
    /// @param pointDelta minimum number of distance between initialized or limitorder points
    /// @param pool address of swap pool
    event NewPool(
        address indexed tokenX,
        address indexed tokenY,
        uint24 indexed fee,
        uint24 pointDelta,
        address pool
    );

    /// @notice module to support swap from tokenX to tokenY
    /// @return swapX2YModule address
    function swapX2YModule() external returns (address);
    ... omitted ...

There is a concrete contract called iZiSwapPool (source) and within its constructor, the interface IiZiSwapFactory is instantiated (source.

iZiSwapFactory.sol

contract iZiSwapPool is IiZiSwapPool {

    using Liquidity for mapping(bytes32 =>Liquidity.Data);
    ... omitted ...
    constructor() {
        swapModuleX2Y = IiZiSwapFactory(msg.sender).swapX2YModule();
    ... omitted ...

Questions

  1. How does it work in general?

  2. Who is msg.sender? Is it another contract or an Account?

  3. How is it possible that an interface is being instantiated?

  4. swapModuleX2Y = IiZiSwapFactory(msg.sender).swapX2YModule(); does this line mean the following?

    A. msg.sender is a contract.

    B. msg.sender fulfils IiZiSwapFactory interface

    C. Contract iZiSwapPool is IiZiSwapPool means iZiSwapPool fulfils IiZiSwapFactory interface

    D. Could it be that msg.sender is also iZiSwapPool?

1 Answer 1

2

Q1 is more general so I will answer others first.

  1. msg.sender is the one who initiates the contract call. In the context of this constructor, it means the one who calls contract creation. Note that there may be multiple subcalls in a transaction, msg.sender means the one who directly calls the contract and may or may not be the one who submit the transaction to blockchain (tx.origin). Take a look at this scenario where user A submit a transaction to call a function in contract B, that function then invoke another function in contract C:
  • User A -> Contract B (msg.sender = A, tx.origin = A) -> Contract C (msg.sender = B, tx.origin = A)..
  1. swapModuleX2Y = IiZiSwapFactory(msg.sender).swapX2YModule(); means that the variable swapModuleX2Y will be the result of the call to the contract IiZiSwapFactory with address being msg.sender. This will revert if msg.sender does not support the function call swapX2YModule as defined in the interface.
  2. A and B are correct, others are dependent on the code of these interfaces. Solidity will check if the method calls actually return the same types as they defined in the Interface. You can craft multiple interfaces that are compatible to one another.

Now question 1. This factory pattern is used to create contracts that conform to certain interface with different initial values. The most probable scenario is from a user A with ETH wallet : User A -> Factory contract (IiZiSwapFactory) B newPool() -> Create Pool C

The place where C is created is here When C is created, its constructor will get parameters from the contract B.

The steps to deploy using this pattern is that you deploy the factory first and call its newPool() method to create the pool contracts.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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