Skip to content

Commit

Permalink
🚧 moar contract and test updates
Browse files Browse the repository at this point in the history
  • Loading branch information
audsssy committed Oct 10, 2023
1 parent a860299 commit dd14267
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 8 deletions.
8 changes: 4 additions & 4 deletions src/KaliBerger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -322,10 +322,10 @@ contract KaliBerger is Storage {
onlyOwner(token, tokenId)
{
uint256 deposit = this.getDeposit(token, tokenId);
uint256 diff = (amount - deposit > 0) ? type(uint256).max : deposit - amount;
uint256 diff = (deposit >= amount) ? deposit - amount : type(uint256).max;

if (diff != type(uint256).max) {
(bool success,) = msg.sender.call{value: diff}("");
(bool success,) = msg.sender.call{value: amount}("");
if (!success) revert TransferFailed();

_forecloseIfNecessary(token, tokenId, diff, amount);
Expand Down Expand Up @@ -589,8 +589,8 @@ contract KaliBerger is Storage {
// }
// }

function _forecloseIfNecessary(address token, uint256 tokenId, uint256 deposit, uint256 amount) internal {
if (deposit == 0 && amount == 0) {
function _forecloseIfNecessary(address token, uint256 tokenId, uint256 diff, uint256 amount) internal {
if (diff == 0 && amount == 0) {
IERC721(token).safeTransferFrom(IERC721(token).ownerOf(tokenId), address(this), tokenId);
deleteDeposit(token, tokenId);
deletePrice(token, tokenId);
Expand Down
36 changes: 32 additions & 4 deletions test/KaliBerger.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -289,22 +289,50 @@ contract KaliBergerTest is Test {
vm.warp(4500);

uint256 _deposit = kaliBerger.getDeposit(address(token_1), 1);
// emit log_uint(_deposit);
uint256 patronage = kaliBerger.patronageToCollect(address(token_1), 1);
// emit log_uint(patronage);

// Bob exits a portion of deposit.
vm.prank(bob);
kaliBerger.exit(address(token_1), 1, 0.3 ether);

// Validate deposit amount
assertEq(
kaliBerger.getDeposit(address(token_1), 1),
_deposit - 0.3 ether - kaliBerger.patronageToCollect(address(token_1), 1)
);
assertEq(kaliBerger.getDeposit(address(token_1), 1), _deposit - 0.3 ether - patronage);
validatePatronageToCollect(token_1, 1);
} // timestamp: 4500

/// @notice Bob ragequits by removing all of his deposit, triggering foreclosure.
function testBuy_ragequit() public payable {
testBuy_addDeposit();

vm.warp(5000);

uint256 _deposit = kaliBerger.getDeposit(address(token_1), 1);
// emit log_uint(_deposit);
uint256 patronage = kaliBerger.patronageToCollect(address(token_1), 1);
// emit log_uint(patronage);

// Bob withdraws all of deposit.
vm.prank(bob);
kaliBerger.exit(address(token_1), 1, _deposit - patronage);

// Validate
assertEq(kaliBerger.getDeposit(address(token_1), 1), 0);
assertEq(token_1.balanceOf(address(kaliBerger)), 1);
validatePatronageToCollect(token_1, 1);
}

/// @notice Bob withdraws too much and triggers InvalidExit() error.
function testBuy_invalidExit() public payable {
testBuy_addDeposit();

vm.warp(5000);

// InvalidExit()
vm.expectRevert(KaliBerger.InvalidExit.selector);
vm.prank(bob);
kaliBerger.exit(address(token_1), 1, 1 ether);
}

/// @notice Charlie buys token_1, tokenId #1 and announces new price for sale.
Expand Down

0 comments on commit dd14267

Please sign in to comment.