I have a question regarding Smart Contract Factories.
I have a simple Bank, Bank factory contract:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "hardhat/console.sol";
// Template used to initialize state variables
contract Bank {
uint256 public bank_funds;
address public owner;
address public deployer;
constructor(address _owner, uint256 _funds) {
bank_funds = _funds;
owner = _owner;
deployer = msg.sender;
}
}
contract BankFactory {
// instantiate Bank contract
Bank bank;
//keep track of created Bank addresses in array
Bank[] public list_of_banks;
// function arguments are passed to the constructor of the new created contract
function createBank(address _owner, uint256 _funds) external {
bank = new Bank(_owner, _funds);
list_of_banks.push(bank);
}
}
What i want to achieve is to list all items from the list_of_banks array and not only created bank addresses. Is there any way to push _funds here list_of_banks.push(bank);
as well ?
Thank You for all the answers.
You can use a cycle and call the bank_funds
getter on each bank in the array
Hi,
Could you please show in a code ?
Thanks
Your code and requirement are flawed in several levels.
First of all, this doesn't instantiate anything:
// instantiate Bank contract
Bank bank;
Second, this seems like a waste of gas:
bank = new Bank(_owner, _funds);
list_of_banks.push(bank);
In short, the Bank bank
state variable seems completely redundant.
It only stores the last bank created, which is also available at the end of the bank list.
Finally, your requirement to store the input _funds
as part of your list of banks (which I assume you want in order to avoid an external call to each Bank
instance on the list) - doesn't make sense to begin with.
Isn't the value of bank_funds
(in each instance of the Bank
contract) supposed to change over time?
I assume that it is, because this contract would be pretty useless otherwise.
And if my assumption is correct, then storing _funds
in your list of banks seems pretty useless by itself.
Thank You for reply, but i believe new Bank
is creating new contract called Bank, and what i wan't to achieve is to push to the array not only newly created smart contract address, but _funds as well. The code is copy paste from Factory pattern example.