Skip to content

Commit

Permalink
💯 coverage (almost)
Browse files Browse the repository at this point in the history
  • Loading branch information
audsssy committed Nov 4, 2023
1 parent f7707c4 commit 443525e
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 49 deletions.
77 changes: 38 additions & 39 deletions src/KaliBerger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -412,12 +412,11 @@ contract KaliBerger is Storage {
}

function setPatron(address token, uint256 tokenId, address patron) internal {
incrementPatronId(token, tokenId);
_setAddress(keccak256(abi.encode(token, tokenId, this.getPatronCount(token, tokenId))), patron);
_setAddress(keccak256(abi.encode(token, tokenId, ".patrons.", incrementPatronId(token, tokenId))), patron);
}

function setPatronStatus(address token, uint256 tokenId, address patron, bool status) internal {
_setBool(keccak256(abi.encode(token, tokenId, patron, ".isPatron")), status);
_setBool(keccak256(abi.encode(token, tokenId, ".isPatron.", patron)), status);
}

/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -490,13 +489,13 @@ contract KaliBerger is Storage {
}

function getPatronCount(address token, uint256 tokenId) external view returns (uint256) {
return this.getUint(keccak256(abi.encode(token, tokenId, ".patronCount")));
return this.getUint(keccak256(abi.encode(token, tokenId, ".patrons.count")));
}

function getPatronId(address token, uint256 tokenId, address patron) external view returns (uint256) {
uint256 count = this.getPatronCount(token, tokenId);

for (uint256 i = 0; i < count;) {
for (uint256 i = 0; i <= count;) {
if (patron == this.getPatron(token, tokenId, i)) return i;
unchecked {
++i;
Expand All @@ -507,11 +506,11 @@ contract KaliBerger is Storage {
}

function isPatron(address token, uint256 tokenId, address patron) external view returns (bool) {
return this.getBool(keccak256(abi.encode(token, tokenId, patron, ".isPatron")));
return this.getBool(keccak256(abi.encode(token, tokenId, ".isPatron.", patron)));
}

function getPatron(address token, uint256 tokenId, uint256 patronId) external view returns (address) {
return this.getAddress(keccak256(abi.encode(token, tokenId, patronId)));
return this.getAddress(keccak256(abi.encode(token, tokenId, ".patrons.", patronId)));
}

function getPatronContribution(address token, uint256 tokenId, address patron) external view returns (uint256) {
Expand Down Expand Up @@ -550,8 +549,8 @@ contract KaliBerger is Storage {
addUint(keccak256(abi.encodePacked("bergerTimes.count")), 1);
}

function incrementPatronId(address token, uint256 tokenId) internal {
addUint(keccak256(abi.encode(token, tokenId, ".patronCount")), 1);
function incrementPatronId(address token, uint256 tokenId) internal returns (uint256) {
return addUint(keccak256(abi.encode(token, tokenId, ".patrons.count")), 1);
}

/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -589,38 +588,38 @@ contract KaliBerger is Storage {
/// -----------------------------------------------------------------------

// credit: simondlr https://github.com/simondlr/thisartworkisalwaysonsale/blob/master/packages/hardhat/contracts/v1/ArtStewardV2.sol
function isForeclosed(address token, uint256 tokenId) external view returns (bool, uint256) {
// returns whether it is in foreclosed state or not
// depending on whether deposit covers patronage due
// useful helper function when price should be zero, but contract doesn't reflect it yet.
uint256 toCollect = this.patronageToCollect(token, tokenId);
uint256 _deposit = this.getDeposit(token, tokenId);
if (toCollect >= _deposit) {
return (true, 0);
} else {
return (false, _deposit - toCollect);
}
}
// function isForeclosed(address token, uint256 tokenId) external view returns (bool, uint256) {
// // returns whether it is in foreclosed state or not
// // depending on whether deposit covers patronage due
// // useful helper function when price should be zero, but contract doesn't reflect it yet.
// uint256 toCollect = this.patronageToCollect(token, tokenId);
// uint256 _deposit = this.getDeposit(token, tokenId);
// if (toCollect >= _deposit) {
// return (true, 0);
// } else {
// return (false, _deposit - toCollect);
// }
// }

// credit: simondlr https://github.com/simondlr/thisartworkisalwaysonsale/blob/master/packages/hardhat/contracts/v1/ArtStewardV2.sol
function foreclosureTime(address token, uint256 tokenId) external view returns (uint256) {
uint256 pps = this.getPrice(token, tokenId) / 365 days * (this.getTax(token, tokenId) / 100);
(, uint256 daw) = this.isForeclosed(token, tokenId);
if (daw > 0) {
return block.timestamp + daw / pps;
} else if (pps > 0) {
// it is still active, but in foreclosure state
// it is block.timestamp or was in the pas
// not active and actively foreclosed (price is zero)
uint256 timeLastCollected = this.getTimeLastCollected(token, tokenId);
return timeLastCollected
+ (block.timestamp - timeLastCollected) * this.getDeposit(token, tokenId)
/ this.patronageToCollect(token, tokenId);
} else {
// not active and actively foreclosed (price is zero)
return this.getTimeLastCollected(token, tokenId); // it has been foreclosed or in foreclosure.
}
}
// function foreclosureTime(address token, uint256 tokenId) external view returns (uint256) {
// uint256 pps = this.getPrice(token, tokenId) / 365 days * (this.getTax(token, tokenId) / 100);
// (, uint256 daw) = this.isForeclosed(token, tokenId);
// if (daw > 0) {
// return block.timestamp + daw / pps;
// } else if (pps > 0) {
// // it is still active, but in foreclosure state
// // it is block.timestamp or was in the pas
// // not active and actively foreclosed (price is zero)
// uint256 timeLastCollected = this.getTimeLastCollected(token, tokenId);
// return timeLastCollected
// + (block.timestamp - timeLastCollected) * this.getDeposit(token, tokenId)
// / this.patronageToCollect(token, tokenId);
// } else {
// // not active and actively foreclosed (price is zero)
// return this.getTimeLastCollected(token, tokenId); // it has been foreclosed or in foreclosure.
// }
// }

/// @notice Internal function to collect patronage.
/// @param token ERC721 token address.
Expand Down
48 changes: 38 additions & 10 deletions test/KaliBerger.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ contract KaliBergerTest is Test {
escrow(alfred, token_1, 1);
escrow(bob, token_2, 1);
escrow(charlie, token_3, 1);
} // 300
}

/// @notice DAO approves all tokens for purchase and adds custom detail.
function testApprove() public payable {
Expand Down Expand Up @@ -227,8 +227,6 @@ contract KaliBergerTest is Test {
/// Buy Test - Single Token
/// -----------------------------------------------------------------------

// TODO: Tests for getTimeHeld(), getPatronId(), isPatron()

/// @notice Bob buys token_1, tokenId #1 and declares a new price for sale
function testSingleBuy() public payable {
// Escrow & approve
Expand Down Expand Up @@ -350,7 +348,7 @@ contract KaliBergerTest is Test {
mintImpactDaoTokens(KaliDAO(impactDao), alfred, alfred, 100 ether);

// Balance.
emit log_uint(block.timestamp);
// emit log_uint(block.timestamp);
balanceDao(block.timestamp + 20000, address(token_1), 1, alfred);
}

Expand All @@ -364,10 +362,40 @@ contract KaliBergerTest is Test {
mintImpactDaoTokens(KaliDAO(impactDao), alfred, bob, 100 ether);

// Balance.
emit log_uint(block.timestamp);
// emit log_uint(block.timestamp);
balanceDao(block.timestamp + 20000, address(token_1), 1, alfred);
}

/// -----------------------------------------------------------------------
/// Getter Test
/// -----------------------------------------------------------------------

function testGetTimeHeld() public payable {
testSingleBuy_thirdBuy();
vm.warp(block.timestamp + 10000);

assertEq(kaliBerger.getTimeHeld(bob), 2000);
assertEq(kaliBerger.getTimeHeld(charlie), 2000);
}

function testGetPatronId() public payable {
testSingleBuy_thirdBuy();

assertEq(kaliBerger.getPatronId(address(token_1), 1, bob), 1);
assertEq(kaliBerger.getPatronId(address(token_1), 1, charlie), 2);
assertEq(kaliBerger.getPatronId(address(token_1), 1, earn), 3);
assertEq(kaliBerger.getPatronId(address(token_1), 1, darius), 0);
}

function testIsPatron() public payable {
testSingleBuy_thirdBuy();

assertEq(kaliBerger.isPatron(address(token_1), 1, bob), true);
assertEq(kaliBerger.isPatron(address(token_1), 1, charlie), true);
assertEq(kaliBerger.isPatron(address(token_1), 1, darius), false);
assertEq(kaliBerger.isPatron(address(token_1), 1, earn), true);
}

/// -----------------------------------------------------------------------
/// Buy Test - Multiple Tokens
/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -560,11 +588,11 @@ contract KaliBergerTest is Test {
if (creator == alfred) assertEq(creator_balance, bob_balance + charlie_balance + earn_balance + darius_balance);
if (creator == bob) assertEq(creator_balance, alfred_balance + charlie_balance + earn_balance + darius_balance);
if (creator == charlie) assertEq(creator_balance, alfred_balance + bob_balance + earn_balance + darius_balance);
emit log_uint(alfred_balance);
emit log_uint(bob_balance);
emit log_uint(charlie_balance);
emit log_uint(darius_balance);
emit log_uint(earn_balance);
// emit log_uint(alfred_balance);
// emit log_uint(bob_balance);
// emit log_uint(charlie_balance);
// emit log_uint(darius_balance);
// emit log_uint(earn_balance);
}

/// @notice Buy ERC721.
Expand Down

0 comments on commit 443525e

Please sign in to comment.