Created
February 15, 2022 12:39
-
-
Save sorXCode/d2f2028826849f2ceed9745222126b98 to your computer and use it in GitHub Desktop.
15-02-2022
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract SimpleStorage { | |
// this will get initialized as 0 | |
uint256 favoriteNumber; | |
bool favoriteBool; | |
struct Person { | |
uint256 favoriteNumber; | |
string name; | |
} | |
Person[] people; | |
mapping(string => uint256) public nameToFavoriteNumber; | |
function store(uint256 _favoriteNumber) public { | |
favoriteNumber = _favoriteNumber; | |
} | |
// view, pure | |
function retrieveFavoriteNumber() public view returns(uint256) { | |
return favoriteNumber; | |
} | |
function addToPeople(string memory _name, uint _favoriteNumber) public { | |
Person memory _person = Person({name: _name, favoriteNumber: _favoriteNumber}); | |
people.push(_person); | |
nameToFavoriteNumber[_name] = _favoriteNumber; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "./SimpleStorage.sol"; | |
contract StorageFactory { | |
// create an array of SimpleStorage | |
SimpleStorage[] simpleStorages; | |
address[] factoryDeployed; | |
address[] predeployedSimpleStoragesLocations; | |
function createSimpleStorage() public { | |
SimpleStorage simpleStorage = new SimpleStorage(); | |
simpleStorages.push(simpleStorage); | |
factoryDeployed.push(address(simpleStorage)); | |
} | |
function addToPredeployed(address[] memory _contractAddresses) public { | |
for (uint256 i; i<_contractAddresses.length; i++){ | |
predeployedSimpleStoragesLocations.push(_contractAddresses[i]); | |
} | |
} | |
function getTotalSimpleStorages() public view returns (uint) { | |
return simpleStorages.length; | |
} | |
function getLocations() public view returns (address[] memory){ | |
return factoryDeployed; | |
} | |
function sfStore(uint256 _index, uint256 _favoriteNumber) public { | |
// Address | |
// ABI | |
simpleStorages[_index].store(_favoriteNumber); | |
/* same as | |
SimpleStorage _simpleStorage = simpleStorages[_index]; | |
_simpleStorage.store(_favoriteNumber); | |
*/ | |
} | |
function sfGet(uint256 _index) public view returns(uint256){ | |
return simpleStorages[_index].retrieveFavoriteNumber(); | |
} | |
function sfGetALlFav() public view returns(uint256[] memory, uint256[] memory) { | |
// mapping(string => uint256[]) memory _favorites; | |
uint256[] memory _factoryDeployedFavorites = new uint256[](factoryDeployed.length); | |
// loop factoryDeployed | |
for (uint256 _index=0; _index < factoryDeployed.length; _index++){ | |
uint256 _fav = simpleStorages[_index].retrieveFavoriteNumber(); | |
_factoryDeployedFavorites[_index] = _fav; | |
} | |
uint256[] memory _predeployedFavorites = new uint256[](predeployedSimpleStoragesLocations.length); | |
// loop predeployedSimpleStoragesLocations | |
for (uint256 _index=0; _index < predeployedSimpleStoragesLocations.length; _index++){ | |
address _contractAddress = predeployedSimpleStoragesLocations[_index]; | |
SimpleStorage _simpleStorage = SimpleStorage(_contractAddress); | |
uint256 _fav = _simpleStorage.retrieveFavoriteNumber(); | |
_predeployedFavorites[_index] = _fav; | |
} | |
return (_factoryDeployedFavorites, _predeployedFavorites); | |
} | |
} | |
// 0 => 0x02F024B95859661614f5e20Cdff0b47e48415e7f => 7 | |
// 1 => 0x403607E85201AEE3887869cCa2861B4d1d7FC575 => 5 | |
// 2 => 0xd71AcF2b3DB4378AA5aa65D06972BAE16940Bd8d => 8 | |
// probably the same SimpleStorage contracts | |
// 0x618e104D81669c9ab0082a4e934abD5c3e9c2473 | |
// 0xbf2506980854b6676bc03Fff61eA37e16F590cA5 | |
// 0x775B9feFf325f057ddf796dc582f0ca3c2D9aAA2 | |
// 0xAE76c561daeDD9f06122ED9a16C39B32E35acA69 | |
// 0x84F1eA9fb2bFd2082De925245846D547cD8Bb00E | |
// 0x869C831dFBBFd8a23bb7e78B64E459a8bcE22D40 | |
// 0xF88EDA0af349C50A469F7F43Fb15cF8831F7a081 | |
// 0xc6b336BE77D9A30776f044375A96224303d07F03 | |
// 0xBebf4f6A18e74329C4c69673A284AFa30333643a |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment