Skip to content

Commit

Permalink
chore: add getter function for
Browse files Browse the repository at this point in the history
  • Loading branch information
0xrusowsky committed Sep 27, 2023
1 parent 0da529d commit 76262a5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/CoolerFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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_)];
}
}
11 changes: 11 additions & 0 deletions src/test/CoolerFactory.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ contract CoolerFactoryTest is Test {
}

// -- CoolerFactory Functions -------------------------------------------------

function test_generateCooler() public {

vm.startPrank(alice);
Expand Down Expand Up @@ -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)));
}
}

0 comments on commit 76262a5

Please sign in to comment.