├── SimpleStorage.sol └── StorageFactory.sol /SimpleStorage.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity ^0.6.0; 4 | 5 | contract SimpleStorage { 6 | 7 | // this will get initialized to 0! 8 | uint256 favoriteNumber; 9 | bool favoriteBool; 10 | 11 | struct People { 12 | uint256 favoriteNumber; 13 | string name; 14 | } 15 | 16 | People[] public people; 17 | mapping(string => uint256) public nameToFavoriteNumber; 18 | 19 | function store(uint256 _favoriteNumber) public { 20 | favoriteNumber = _favoriteNumber; 21 | } 22 | 23 | function retrieve() public view returns(uint256) { 24 | return favoriteNumber; 25 | } 26 | 27 | function addPerson(string memory _name, uint256 _favoriteNumber) public{ 28 | people.push(People(_favoriteNumber, _name)); 29 | nameToFavoriteNumber[_name] = _favoriteNumber; 30 | } 31 | 32 | } 33 | -------------------------------------------------------------------------------- /StorageFactory.sol: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: MIT 2 | 3 | pragma solidity ^0.6.0; 4 | 5 | import "./SimpleStorage.sol"; 6 | 7 | contract StorageFactory is SimpleStorage { 8 | 9 | SimpleStorage[] public simpleStorageArray; 10 | 11 | function createSimpleStorageContract() public { 12 | SimpleStorage simpleStorage = new SimpleStorage(); 13 | simpleStorageArray.push(simpleStorage); 14 | } 15 | 16 | function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { 17 | // Address 18 | // ABI 19 | //this line has an explicit cast to the address type and initializes a new SimpleStorage object from the address 20 | SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber); 21 | 22 | //this line simply gets the SimpleStorage object at the index _simpleStorageIndex in the array simpleStorageArray 23 | //simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber); 24 | } 25 | 26 | function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) { 27 | //this line has an explicit cast to the address type and initializes a new SimpleStorage object from the address 28 | return SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).retrieve(); 29 | 30 | //this line simply gets the SimpleStorage object at the index _simpleStorageIndex in the array simpleStorageArray 31 | //return simpleStorageArray[_simpleStorageIndex].retrieve(); 32 | } 33 | } 34 | --------------------------------------------------------------------------------