Skip to content

Commit

Permalink
🚧 update contracts and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
audsssy committed Oct 9, 2023
1 parent 61cf68d commit f164bb7
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 38 deletions.
82 changes: 48 additions & 34 deletions src/KaliBerger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -154,25 +154,29 @@ contract KaliBerger is Storage {
/// @param creator Creator of ERC721.
/// @param patron Patron of ERC721.
function summonDao(address token, uint256 tokenId, address creator, address patron) private returns (address) {
address[] memory extensions = new address[](1);
extensions[0] = address(this);
// address[] memory extensions;
bytes[] memory extensionsData = new bytes[](1);
extensionsData[0] = "0x0";

// Provide creator and patron to summon DAO.
address[] memory voters = new address[](2);
voters[0] = creator;
voters[1] = patron;

// Provide respective token amount.
uint256[] memory tokens = new uint256[](2);
tokens[0] = this.getPatronContribution(token, tokenId, patron);
tokens[1] = tokens[0];

// Provide KaliBerger as extension.
address[] memory extensions = new address[](1);
extensions[0] = address(this);
bytes[] memory extensionsData = new bytes[](1);
extensionsData[0] = "0x0";

// Provide KaliDAO governance settings
uint32[16] memory govSettings;
govSettings = [uint32(300), 0, 20, 52, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1];

// Summon a KaliDAO
uint256 count = this.getBergerCount();
address payable dao = payable(
address payable impactDao = payable(
KaliDAOfactory(this.getKaliDaoFactory()).deployKaliDAO(
string.concat("BergerTime #", LibString.toString(count)),
string.concat("BT #", LibString.toString(count)),
Expand All @@ -186,9 +190,12 @@ contract KaliBerger is Storage {
)
);

this.setImpactDao(token, tokenId, dao);
addBergerCount();
return dao;
// Store dao address for future.
this.setImpactDao(token, tokenId, impactDao);

// Increment number of impactDAOs.
incrementBergerCount();
return impactDao;
}

/// @notice Update DAO balance when ERC721 is purchased.
Expand All @@ -197,14 +204,14 @@ contract KaliBerger is Storage {
/// @param patron Patron of ERC721.
function updateBalances(address token, uint256 tokenId, address patron) internal {
// Get DAO address to manage revenue from Harberger Tax
address dao = this.getImpactDao(token, tokenId);
address impactDao = this.getImpactDao(token, tokenId);

if (dao == address(0)) {
if (impactDao == address(0)) {
// Summon DAO with 50/50 ownership between creator and patron(s).
this.setImpactDao(token, tokenId, summonDao(token, tokenId, this.getCreator(token, tokenId), patron));
} else {
// Update DAO balance.
_balance(token, tokenId, dao);
_balance(token, tokenId, impactDao);
}
}

Expand Down Expand Up @@ -302,7 +309,7 @@ contract KaliBerger is Storage {
/// @param tokenId ERC721 tokenId.
/// @param deposit Amount to deposit to pay for tax.
function addDeposit(address token, uint256 tokenId, uint256 deposit) external payable onlyPatron(token, tokenId) {
this.addUint(keccak256(abi.encode(token, tokenId, ".deposit")), deposit);
addUint(keccak256(abi.encode(token, tokenId, ".deposit")), deposit);
}

/// @notice Withdraw from deposit.
Expand Down Expand Up @@ -359,10 +366,6 @@ contract KaliBerger is Storage {
this.setUint(keccak256(abi.encode(token, tokenId, ".price")), price);
}

function _addDeposit(address token, uint256 tokenId, uint256 deposit) internal {
this.addUint(keccak256(abi.encode(token, tokenId, ".deposit")), deposit);
}

function setTimeLastCollected(address token, uint256 tokenId, uint256 timestamp) internal {
this.setUint(keccak256(abi.encode(token, tokenId, ".timeLastCollected")), timestamp);
}
Expand Down Expand Up @@ -486,44 +489,52 @@ contract KaliBerger is Storage {
/// Add Logic
/// -----------------------------------------------------------------------

function addBergerCount() internal {
this.addUint(keccak256(abi.encodePacked("bergerTimes.count")), 1);
function _addDeposit(address token, uint256 tokenId, uint256 amount) internal {
addUint(keccak256(abi.encode(token, tokenId, ".deposit")), amount);
}

function subDeposit(address token, uint256 tokenId, uint256 amount) internal {
subUint(keccak256(abi.encode(token, tokenId, ".deposit")), amount);
}

function addUnclaimed(address user, uint256 amount) internal {
this.addUint(keccak256(abi.encode(user, ".unclaimed")), amount);
addUint(keccak256(abi.encode(user, ".unclaimed")), amount);
}

function addTimeHeld(address user, uint256 time) internal {
this.addUint(keccak256(abi.encode(user, ".timeHeld")), time);
addUint(keccak256(abi.encode(user, ".timeHeld")), time);
}

function addTotalCollected(address token, uint256 tokenId, uint256 collected) internal {
this.addUint(keccak256(abi.encode(token, tokenId, ".totalCollected")), collected);
addUint(keccak256(abi.encode(token, tokenId, ".totalCollected")), collected);
}

function incrementPatronId(address token, uint256 tokenId) internal {
this.addUint(keccak256(abi.encode(token, tokenId, ".patronCount")), 1);
function addPatronContribution(address token, uint256 tokenId, address patron, uint256 amount) internal {
addUint(keccak256(abi.encode(token, tokenId, patron, ".contribution")), amount);
}

function addPatronContribution(address token, uint256 tokenId, address patron, uint256 amount) internal {
this.addUint(keccak256(abi.encode(token, tokenId, patron, ".contribution")), amount);
function incrementBergerCount() internal {
addUint(keccak256(abi.encodePacked("bergerTimes.count")), 1);
}

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

/// -----------------------------------------------------------------------
/// Delete Logic
/// -----------------------------------------------------------------------

function deletePrice(address token, uint256 tokenId) internal {
return this.deleteUint(keccak256(abi.encode(token, tokenId, ".price")));
return deleteUint(keccak256(abi.encode(token, tokenId, ".price")));
}

function deleteDeposit(address token, uint256 tokenId) internal {
return this.deleteUint(keccak256(abi.encode(token, tokenId, ".deposit")));
return deleteUint(keccak256(abi.encode(token, tokenId, ".deposit")));
}

function deleteUnclaimed(address user) internal {
this.deleteUint(keccak256(abi.encode(user, ".unclaimed")));
deleteUint(keccak256(abi.encode(user, ".unclaimed")));
}

/// -----------------------------------------------------------------------
Expand Down Expand Up @@ -598,7 +609,7 @@ contract KaliBerger is Storage {
// TLC + (time_elapsed)*deposit/toCollect
setTimeLastCollected(token, tokenId, (block.timestamp - timeLastCollected) * deposit / toCollect);

// Add to unclaimed pool for corresponding dao to claim at later time.
// Add to unclaimed pool for corresponding impactDao to claim at later time.
addUnclaimed(this.getImpactDao(token, tokenId), deposit);

// Take deposit.
Expand All @@ -607,15 +618,18 @@ contract KaliBerger is Storage {
// Normal collection.
setTimeLastCollected(token, tokenId, block.timestamp);

// Add to unclaimed pool for corresponding dao to claim at later time.
if (toCollect != 0) addUnclaimed(this.getImpactDao(token, tokenId), toCollect);
// Add to unclaimed pool for corresponding impactDao to claim at later time.
if (toCollect != 0) {
addUnclaimed(this.getImpactDao(token, tokenId), toCollect);
subDeposit(token, tokenId, toCollect);
}
}

// Add to total amount collected.
addTotalCollected(token, tokenId, toCollect);

// Add to amount collected by patron.
addPatronContribution(token, tokenId, msg.sender, toCollect);
addPatronContribution(token, tokenId, this.getOwner(token, tokenId), toCollect);

// Foreclose if necessary.
_forecloseIfNecessary(token, tokenId, deposit - toCollect);
Expand Down
19 changes: 15 additions & 4 deletions test/KaliBerger.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "forge-std/Test.sol";
import "forge-std/console2.sol";

import {IERC721} from "lib/forge-std/src/interfaces/IERC721.sol";
import {IERC20} from "lib/forge-std/src/interfaces/IERC20.sol";
import {MockERC721} from "lib/solbase/test/utils/mocks/MockERC721.sol";
import {KaliDAOfactory, KaliDAO} from "src/kalidao/KaliDAOfactory.sol";

Expand Down Expand Up @@ -154,6 +155,7 @@ contract KaliBergerTest is Test {

// Validate
assertEq(address(kaliBerger).balance, 0.1 ether);
assertEq(address(bob).balance, 9.9 ether);
} // 700

/// @notice Calculate patronage to collect after Buy
Expand All @@ -166,6 +168,7 @@ contract KaliBergerTest is Test {
* (block.timestamp - kaliBerger.getTimeLastCollected(address(erc721), 1))
* kaliBerger.getTax(address(erc721), 1) / 365 days / 100;
assertEq(kaliBerger.patronageToCollect(address(erc721), 1), amount);
emit log_uint(amount);
}

/// @notice Secondary sale of ERC721
Expand All @@ -176,17 +179,25 @@ contract KaliBergerTest is Test {

// Deal Charlie ether
vm.deal(charlie, 10 ether);
// emit log_uint(address(bob).balance);
emit log_uint(address(bob).balance);

// Charlie buys
vm.prank(charlie);
kaliBerger.buy{value: 1.1 ether}(address(erc721), 1, 1.5 ether, 1 ether);
emit log_uint(address(bob).balance);
vm.warp(1000);

// Validate
// TODO: Add more validation checks, including balance at impactDao
emit log_uint(address(kaliBerger).balance);
emit log_uint(address(bob).balance);
emit log_uint(kaliBerger.getPatronContribution(address(erc721), 1, bob));
emit log_uint(kaliBerger.getPatronContribution(address(erc721), 1, charlie));

uint256 alice_balance = IERC20(kaliBerger.getImpactDao(address(erc721), 1)).balanceOf(alice);
uint256 bob_balance = IERC20(kaliBerger.getImpactDao(address(erc721), 1)).balanceOf(bob);
uint256 charlie_balance = IERC20(kaliBerger.getImpactDao(address(erc721), 1)).balanceOf(charlie);
emit log_uint(alice_balance);
emit log_uint(bob_balance);
emit log_uint(charlie_balance);
assertEq(alice_balance, bob_balance + charlie_balance);
} // 900

function testReceiveETH() public payable {
Expand Down

0 comments on commit f164bb7

Please sign in to comment.