0
0
Blockchain / Solidityprogramming~10 mins

Factory pattern in Blockchain / Solidity - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a new contract instance using the factory.

Blockchain / Solidity
contract MyFactory {
    function create() public returns (address) {
        [1] newContract = new SimpleContract();
        return address(newContract);
    }
}
Drag options to blanks, or click blank then click option'
ASimpleContract
BFactory
CContract
DInstance
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong contract name that does not exist.
Trying to return the contract type instead of its address.
2fill in blank
medium

Complete the code to store the address of the created contract in the factory.

Blockchain / Solidity
contract MyFactory {
    address[] public contracts;

    function create() public {
        SimpleContract newContract = new SimpleContract();
        contracts.[1](address(newContract));
    }
}
Drag options to blanks, or click blank then click option'
Ainsert
Bpush
Cremove
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes elements instead of adding.
Trying to assign directly without using array methods.
3fill in blank
hard

Fix the error in the factory function to correctly emit an event after contract creation.

Blockchain / Solidity
contract MyFactory {
    event ContractCreated(address contractAddress);

    function create() public {
        SimpleContract newContract = new SimpleContract();
        emit [1](address(newContract));
    }
}
Drag options to blanks, or click blank then click option'
AContractCreatedEvent
BCreatedContract
CContractCreated
DNewContract
Attempts:
3 left
💡 Hint
Common Mistakes
Using a wrong event name that does not exist.
Misspelling the event name or changing its case.
4fill in blank
hard

Fill both blanks to define a factory that creates different contract types based on input.

Blockchain / Solidity
contract Factory {
    function create(uint8 typeId) public returns (address) {
        if (typeId == 1) {
            return address(new ContractA());
        } else if (typeId == 2) {
            return address(new [1]());
        } else {
            revert("Invalid type");
        }
    }
}

contract ContractA {}
contract [2] {}
Drag options to blanks, or click blank then click option'
AContractB
BContractC
CContractA
DFactory
Attempts:
3 left
💡 Hint
Common Mistakes
Using different names for the contract creation and definition.
Trying to create a contract that is not defined.
5fill in blank
hard

Fill all three blanks to implement a factory that creates contracts with an owner and stores them.

Blockchain / Solidity
contract Owned {
    address public owner;
    constructor(address _owner) {
        owner = _owner;
    }
}

contract Factory {
    address[] public contracts;

    function create(address _owner) public returns (address) {
        Owned newContract = new Owned([1]);
        contracts.[2](address(newContract));
        emit Created([3]);
        return address(newContract);
    }

    event Created(address contractAddress);
}
Drag options to blanks, or click blank then click option'
A_owner
Bpush
Caddress(newContract)
Downer
Attempts:
3 left
💡 Hint
Common Mistakes
Passing wrong argument to the constructor.
Using wrong array method instead of push.
Emitting wrong value in the event.