From b8be2d01d10a4a24fb774864f139956227d659ac Mon Sep 17 00:00:00 2001 From: 0xrusowsky <90208954+0xRusowsky@users.noreply.github.com> Date: Wed, 27 Sep 2023 15:39:06 +0200 Subject: [PATCH] chore: add getter function for `coolerFor`(#67) --- src/CoolerFactory.sol | 10 ++++++++-- src/test/CoolerFactory.t.sol | 11 +++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/CoolerFactory.sol b/src/CoolerFactory.sol index cba9657..d52634d 100644 --- a/src/CoolerFactory.sol +++ b/src/CoolerFactory.sol @@ -42,8 +42,7 @@ contract CoolerFactory { mapping(address => bool) public created; /// @notice Mapping to prevent duplicate coolers. - mapping(address => mapping(ERC20 => mapping(ERC20 => address))) - private coolerFor; + mapping(address => mapping(ERC20 => mapping(ERC20 => address))) private coolerFor; /// @notice Mapping to query Coolers for Collateral-Debt pair. mapping(ERC20 => mapping(ERC20 => address[])) public coolersFor; @@ -120,4 +119,11 @@ function generateCooler(ERC20 collateral_, ERC20 debt_) external returns (addres function logDefaultLoan(uint256 loanID_, uint256 collateral_) external onlyFromFactory { emit DefaultLoan(msg.sender, loanID_, collateral_); } + + // --- AUX FUNCTIONS --------------------------------------------- + + /// @notice Getter function to get an existing cooler for a given user <> collateral <> debt combination. + function getCoolerFor(address user_, address collateral_, address debt_) public view returns (address) { + return coolerFor[user_][ERC20(collateral_)][ERC20(debt_)]; + } } \ No newline at end of file diff --git a/src/test/CoolerFactory.t.sol b/src/test/CoolerFactory.t.sol index 1f989bd..d3c9186 100644 --- a/src/test/CoolerFactory.t.sol +++ b/src/test/CoolerFactory.t.sol @@ -55,6 +55,7 @@ contract CoolerFactoryTest is Test { } // -- CoolerFactory Functions ------------------------------------------------- + function test_generateCooler() public { vm.startPrank(alice); @@ -141,4 +142,14 @@ contract CoolerFactoryTest is Test { vm.expectRevert(CoolerFactory.NotFromFactory.selector); coolerFactory.logRequestLoan(id); } + + + function test_getCoolerFor() public { + // Unexistent loans return address(0). + assertEq(address(0), coolerFactory.getCoolerFor(alice, address(collateral), address(debt))); + + vm.startPrank(alice); + address coolerAlice = coolerFactory.generateCooler(collateral, debt); + assertEq(coolerAlice, coolerFactory.getCoolerFor(alice, address(collateral), address(debt))); + } } \ No newline at end of file