Skip to content

Instantly share code, notes, and snippets.

@alexon1234
Created April 20, 2021 14:25
Show Gist options
  • Save alexon1234/5e8a5ba7633cbae321d71aafcbde550b to your computer and use it in GitHub Desktop.
Save alexon1234/5e8a5ba7633cbae321d71aafcbde550b to your computer and use it in GitHub Desktop.
Optimize storage in PoolFactory & LoanFactory contracts

In both PoolFactory.sol and LoanFactory.sol we found the following properties.

contract PoolFactory is Pausable {
    ...

    uint256 public poolsCreated; // Incrementor for number of Pools created

    mapping(uint256 => address) public pools; // Map to keep `Pool` contract corresponds to its index.
    mapping(address => bool) public isPool; // Used to check if a `Pool` was instantiated from this factory.
    ...
contract LoanFactory is Pausable {
    ...

    uint256 public loansCreated; // Incrementor for number of loan vaults created.

    mapping(uint256 => address) public loans; // Loans address mapping
    mapping(address => bool) public isLoan; // Used to check if a Loan was instantiated from this contract

I feel both contracts could remove the mapping(uint256 => address) field without impacting the contracts.

The only explanation I can find to having this mapping mapping(uint256 => address) is because they want to be able to iterate over mapping to check the loans. But in this case we could use an array instead of a mapping and we could remove the incrementor.

contract LoanFactory is Pausable {
    ...

    Loan[] public loans;
    mapping(address => bool) public isLoan; // Used to check if a Loan was instantiated from this contract
    
    function loansCreated() public view returns (uint256) {
        return loans.length;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment