Skip to content

Commit

Permalink
first version
Browse files Browse the repository at this point in the history
  • Loading branch information
spengrah committed May 3, 2023
1 parent f81d747 commit a80bbf9
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 56 deletions.
4 changes: 3 additions & 1 deletion foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ auto_detect_solc = false
solc = "0.8.18"
remappings = [
"ds-test/=lib/forge-std/lib/ds-test/src/",
"forge-std/=lib/forge-std/src/"
"forge-std/=lib/forge-std/src/",
"council/=lib/council/contracts/",
"hats-protocol/=lib/hats-protocol/src/"
]

[profile.ci]
Expand Down
20 changes: 20 additions & 0 deletions gasreport.ansi
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
Compiling 3 files with 0.8.18
Solc 0.8.18 finished in 705.01ms
Compiler run successful

Running 5 tests for test/HatsHighCouncilVotingVault.t.sol:IsVotingRepHat
[PASS] testFuzz_matchesPattern_true(uint16) (runs: 5000, μ: 33584, ~: 33584)
[PASS] testFuzz_tooChildy_false(uint160) (runs: 5000, μ: 33626, ~: 33626)
[PASS] testFuzz_wrongLevel1Hat_false(uint16) (runs: 5000, μ: 33649, ~: 33649)
[PASS] testFuzz_wrongLevel3Hat_false(uint16) (runs: 5000, μ: 33671, ~: 33671)
[PASS] testFuzz_wrongTopHat_false(uint32) (runs: 5000, μ: 33604, ~: 33604)
Test result: ok. 5 passed; 0 failed; finished in 212.68ms
| src/HatsHighCouncilVotingVault.sol:HatsHighCouncilVotingVault contract | | | | | |
|------------------------------------------------------------------------|-----------------|-----|--------|-----|---------|
| Deployment Cost | Deployment Size | | | | |
| 168051 | 1015 | | | | |
| Function Name | min | avg | median | max | # calls |
| isVotingRepHat | 254 | 254 | 254 | 254 | 5 |



12 changes: 8 additions & 4 deletions script/Counter.s.sol → script/HatsHighCouncilVotingVault.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@
pragma solidity ^0.8.18;

import { Script, console2 } from "forge-std/Script.sol";
import { Counter } from "../src/Counter.sol";
import { HatsHighCouncilVotingVault } from "../src/HatsHighCouncilVotingVault.sol";
import { IHats } from "hats-protocol/Interfaces/IHats.sol";

contract Deploy is Script {
Counter public counter;
HatsHighCouncilVotingVault public hhcvv;
bytes32 public SALT = keccak256("lets add some salt to this meal");
IHats public constant hats = IHats(0x9D2dfd6066d5935267291718E8AA16C8Ab729E9d); // v1.hatsprotocol.eth
// uint256 public membershipDomain = 0x00000001_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
// // hat 1.1

// default values
bool private verbose = true;
Expand All @@ -21,12 +25,12 @@ contract Deploy is Script {
address deployer = vm.rememberKey(privKey);
vm.startBroadcast(deployer);

counter = new Counter{ salt: SALT}();
hhcvv = new HatsHighCouncilVotingVault{ salt: SALT}(hats);

vm.stopBroadcast();

if (verbose) {
console2.log("Counter:", address(counter));
console2.log("Counter:", address(hhcvv));
}
}
}
Expand Down
16 changes: 0 additions & 16 deletions src/Counter.sol

This file was deleted.

44 changes: 44 additions & 0 deletions src/HatsHighCouncilVotingVault.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import { console2 } from "forge-std/Test.sol"; // remove before deploy
import { IHats } from "hats-protocol/Interfaces/IHats.sol";
import { IVotingVault } from "council/interfaces/IVotingVault.sol";

contract HatsHighCouncilVotingVault is IVotingVault {
/// @dev The pattern of a member DAO voting rep hat
uint256 internal constant PATTERN = 0x00000001_0001_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000;
/// @dev The mask for a member DAO voting rep hat
uint256 internal constant MASK = 0xFFFFFFFF_FFFF_0000_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF_FFFF;
/// @notice The Hats Protocol contract
IHats public immutable HATS;

constructor(IHats hats) {
HATS = hats;
}

/// @inheritdoc IVotingVault
function queryVotePower(address user, uint256 /* blockNumber */, bytes calldata extraData)// forgefmt: disable-line
external
view
override
returns (uint256)
{
/// @dev `extraData` is the abi-encoded id of `user`'s Member DAO Voting Rep hat
uint256 votingHat = abi.decode(extraData, (uint256));

if (isVotingRepHat(votingHat)) {
// hat balances are either 0 or 1
return HATS.balanceOf(user, votingHat);
} else {
return 0;
}
}

/// @notice Checks if `hatId` is a member DAO voting rep hat
/// @param hatId The id of the hat to check
/// @return True if `hatId` is a member DAO voting rep hat
function isVotingRepHat(uint256 hatId) public pure returns (bool) {
return (hatId & MASK) == PATTERN;
}
}
35 changes: 0 additions & 35 deletions test/Counter.t.sol

This file was deleted.

64 changes: 64 additions & 0 deletions test/HatsHighCouncilVotingVault.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.18;

import { Test, console2 } from "forge-std/Test.sol";
import { HatsHighCouncilVotingVault } from "../src/HatsHighCouncilVotingVault.sol";
import { Deploy } from "../script/HatsHighCouncilVotingVault.s.sol";

contract HHCVVTest is Deploy, Test {
// variables inhereted from Deploy script
// HatsHighCouncilVotingVault public hhcvv;
// IHats public hats;
// uint256 public membershipDomain;

uint256 public fork;
uint256 public BLOCK_NUMBER;

function setUp() public virtual {
// create and activate a fork, at BLOCK_NUMBER
// fork = vm.createSelectFork(vm.rpcUrl("mainnet"), BLOCK_NUMBER);

// deploy via the script
Deploy.prepare(false);
Deploy.run();
}
}

contract IsVotingRepHat is HHCVVTest {
uint256 testValue;

function testFuzz_matchesPattern_true(uint16 id) public {
vm.assume(id > 0);
testValue = 0x00000001_0001_0000_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 + (uint256(id) << 192);
console2.log(testValue);
assertTrue(hhcvv.isVotingRepHat(testValue));
}

function testFuzz_tooChildy_false(uint160 id) public {
vm.assume(id > 0);
testValue = 0x00000001_0001_0001_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 + uint256(id);
console2.log(testValue);
assertFalse(hhcvv.isVotingRepHat(testValue));
}

function testFuzz_wrongTopHat_false(uint32 id) public {
vm.assume(id > 1);
testValue = 0x00000000_0001_0001_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 + (uint256(id) << 224);
console2.log(testValue);
assertFalse(hhcvv.isVotingRepHat(testValue));
}

function testFuzz_wrongLevel1Hat_false(uint16 id) public {
vm.assume(id > 1);
testValue = 0x00000001_0000_0001_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 + (uint256(id) << 208);
console2.log(testValue);
assertFalse(hhcvv.isVotingRepHat(testValue));
}

function testFuzz_wrongLevel3Hat_false(uint16 id) public {
vm.assume(id > 1);
testValue = 0x00000001_0001_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000 + (uint256(id) << 176);
console2.log(testValue);
assertFalse(hhcvv.isVotingRepHat(testValue));
}
}

0 comments on commit a80bbf9

Please sign in to comment.