Wondering if anyone can explain this. I am following along in the "Solidity, Blockchain, and Smart Contract Course - Beginner to Expert Python Tutorial" by freeCodeCamp.org.
In lesson two we create a contract factory in which we store an array of contracts and then create a function to retrieve the contract by index and call a function on it.
Here is how he did it: SimpleStorage(address(simpleStorages[_simpleStorageIndex])).store(_simpleStorageNumber)
I do not understand the SimpleStorage(address(...)) part. I understand indexing into the array and getting the storage, but I tested it and this works the same: simpleStorages[_simpleStorageIndex].store(_simpleStorageNumber)
What is this address function? I assume it gets the address of the contract instance. Then why do we pass it to a constructor(?) of SimpleStorage? Why do all this when calling store on the instance itself without going through addresses works.
Thanks!!
EDIT: Entire contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
import "./SimpleStorage.sol";
contract StorageFactory is SimpleStorage { // is SimpleStorgae = inheritance
SimpleStorage[] public simpleStorages;
function createSimpleStorageContract() public {
SimpleStorage simpleStorage = new SimpleStorage();
simpleStorages.push(simpleStorage);
}
function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public {
// Anytime you interact with a contract you need two things:
// Address
// ABI - Application Binary Interface
return simpleStorages[_simpleStorageIndex].store(_simpleStorageNumber);
//return SimpleStorage(address(simpleStorages[_simpleStorageIndex])).store(_simpleStorageNumber);
}
function sfGet(uint256 _simpleStorageIndex) public view returns(uint256) {
return SimpleStorage(address(simpleStorages[_simpleStorageIndex])).retrieve();
}
}