name and owner state variables.Jump into concepts and practice - no test required
name and owner state variables.Asset with two public state variables: string public name and address public owner. Add a constructor that takes string memory _name and address _owner as parameters and sets the state variables accordingly.Think of the asset contract as a simple container that stores a name and an owner address. The constructor sets these values when the contract is created.
AssetFactory. Inside it, create a public dynamic array of addresses called assets to store the addresses of created asset contracts.The factory contract will keep track of all asset contracts it creates by storing their addresses in an array.
AssetFactory contract, add a public function called createAsset that takes a string memory _name parameter. This function should deploy a new Asset contract with _name and msg.sender as the owner. Then, add the new asset's address to the assets array.Use the new keyword to deploy a new contract inside another contract. Store the new contract's address in the array.
getAssetsCount in the AssetFactory contract that returns the number of created asset contracts. Then, write a simple test by calling createAsset twice with names "Gold" and "Silver", and print the result of getAssetsCount().The length of the assets array tells how many asset contracts have been created. Solidity contracts do not print, so you return values to check.
What is the main purpose of the Factory pattern in blockchain development?
Which of the following is the correct Solidity syntax to deploy a new contract inside a factory contract?
function create() public returns (address) {
address newContract = new ?();
return newContract;
}new ContractName().new ?() expects the contract name without 'new' repeated.Consider this Solidity factory contract snippet:
contract Simple {
uint public value;
constructor(uint _value) {
value = _value;
}
}
contract Factory {
Simple[] public simples;
function createSimple(uint _val) public {
Simple s = new Simple(_val);
simples.push(s);
}
function getValue(uint index) public view returns (uint) {
return simples[index].value();
}
}What will getValue(0) return after calling createSimple(42) once?
createSimple(42) creates a new Simple contract with value = 42 and stores it in simples array.getValue(0) returnsvalue of the first Simple contract, which is 42.Identify the error in this factory contract code snippet:
contract Product {
uint public id;
constructor(uint _id) {
id = _id;
}
}
contract ProductFactory {
Product[] public products;
function createProduct(uint _id) public {
Product p = Product(_id);
products.push(p);
}
}new keyword.Product p = Product(_id); misses new, it should be Product p = new Product(_id);.new keyword when creating Product -> Option BYou want to build a factory contract that creates multiple token contracts with different initial supplies and keeps track of them. Which approach best applies the factory pattern to save gas and organize your project?