Consider this Solidity factory contract that creates simple storage contracts. What will be the value of storedData in the newly created contract after deployment?
pragma solidity ^0.8.0; contract SimpleStorage { uint public storedData; constructor(uint initVal) { storedData = initVal; } } contract StorageFactory { SimpleStorage[] public storages; function createStorage(uint _initVal) public { SimpleStorage s = new SimpleStorage(_initVal); storages.push(s); } function getLastStoredData() public view returns (uint) { return storages[storages.length - 1].storedData(); } }
Look at how the SimpleStorage contract is created and initialized.
The factory creates a new SimpleStorage contract passing _initVal to its constructor. This sets storedData to that value.
Which of the following is the main reason to use a factory pattern in blockchain smart contracts?
Think about how factories help manage many contracts.
Factory contracts allow deploying many instances of a contract, each with its own state, making it easier to manage and create multiple contracts dynamically.
What error will this Solidity factory contract produce when calling createStorage?
pragma solidity ^0.8.0;
contract SimpleStorage {
uint public storedData;
constructor(uint initVal) {
storedData = initVal;
}
}
contract StorageFactory {
SimpleStorage[] public storages;
function createStorage(uint _initVal) public {
SimpleStorage s = SimpleStorage(_initVal);
storages.push(s);
}
}Check how the new contract instance is created.
The line SimpleStorage s = SimpleStorage(_initVal); tries to call the constructor like a function, which is not allowed. The correct way is to use new SimpleStorage(_initVal);.
Given the contract Child with a constructor that takes a uint, which option correctly creates a new Child instance inside a factory contract?
pragma solidity ^0.8.0; contract Child { uint public value; constructor(uint _value) { value = _value; } } contract Factory { Child[] public children; function createChild(uint _val) public { // Which line is correct here? } }
Remember the syntax for creating new contract instances in Solidity.
The correct syntax to create a new contract instance is new ContractName(constructorArgs). Option A uses this correctly.
Given the following factory contract, how many Child contracts exist after these calls?
Calls made in order:
createChild(10)createChild(20)createChild(30)deleteChild(1)
pragma solidity ^0.8.0; contract Child { uint public value; constructor(uint _value) { value = _value; } } contract Factory { Child[] public children; function createChild(uint _val) public { Child c = new Child(_val); children.push(c); } function deleteChild(uint index) public { require(index < children.length, "Index out of range"); delete children[index]; } function getChildrenCount() public view returns (uint) { return children.length; } }
Deleting an element from an array in Solidity does not reduce its length.
The delete keyword resets the element at the index to its default value but does not remove it from the array or reduce the array length. So, the array length remains 3.