Skip to content

Commit

Permalink
Created new fixture contracts for testing
Browse files Browse the repository at this point in the history
  • Loading branch information
KyrylR committed Dec 19, 2023
1 parent b00b584 commit c0ef81d
Show file tree
Hide file tree
Showing 29 changed files with 731 additions and 469 deletions.
63 changes: 63 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"build": "tsc --build .",
"prepare-tests": "npm run compile --workspaces",
"test": "mocha --recursive 'test/**/*.ts' --exit && npm run lint-fix",
"prepare-clean": "npm run clean --workspaces",
"clean-tests": "npm run clean --workspaces",
"lint-fix": "prettier --write \"./**/*.ts\" && eslint \"src/**/*.{js,ts}\" --cache --fix",
"publish-to-npm": "npm run build && npm run lint-fix && npm publish ./ --access public"
},
Expand All @@ -59,6 +59,7 @@
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "^3.0.4",
"@nomiclabs/hardhat-truffle5": "^2.0.0",
"@openzeppelin/contracts": "^5.0.1",
"@typechain/ethers-v6": "^0.5.0",
"@typechain/hardhat": "^9.1.0",
"@typechain/truffle-v5": "^8.0.6",
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {EIP712} from "@openzeppelin/contracts/utils/cryptography/EIP712.sol";
import {ERC20Votes, ERC20} from "@openzeppelin/contracts/token/ERC20/extensions/ERC20Votes.sol";

import {VotingPowerLib} from "./libs/VotingPowerLib.sol";
import {TimestampClockLib} from "./libs/TimestampClockLib.sol";

/**
* @title GovToken
*
* @notice This is an ERC20 token that includes vote delegation.
* It is intentionally designed with added complexity for testing purposes.
*/
contract GovToken is ERC20Votes, Ownable {
using VotingPowerLib for uint256;

constructor(string memory name_, string memory symbol_) ERC20(name_, symbol_) EIP712(name_, version()) Ownable(msg.sender) {
_mint(msg.sender, 1000000);
}

function decimals() public pure override returns (uint8) {
return 3;
}

function version() public pure returns (string memory) {
return "1";
}

function mint(address to_, uint256 amount_) external onlyOwner {
_mint(to_, amount_);
}

function burn(address from_, uint256 amount_) external onlyOwner {
_burn(from_, amount_);
}

function getUserVotingPower(address account_) public view returns (uint256) {
return getVotes(account_).calculateUserVotingPower(decimals());
}

function getPastUserVotingPower(address account_, uint256 timestamp_) public view returns (uint256) {
return getPastVotes(account_, timestamp_).calculateUserVotingPower(decimals());
}

function getVotingPowerThreshold() public view returns (uint256) {
return _getTotalSupply().calculateVotingPowerThreshold(decimals());
}

function getPastVotingPowerThreshold(uint256 timestamp_) public view returns (uint256) {
return getPastTotalSupply(timestamp_).calculateVotingPowerThreshold(decimals());
}

function clock() public view override returns (uint48) {
return TimestampClockLib.clock();
}

function CLOCK_MODE() public view override returns (string memory) {
return TimestampClockLib.CLOCK_MODE();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

import {Time} from "@openzeppelin/contracts/utils/types/Time.sol";
import {Votes} from "@openzeppelin/contracts/governance/utils/Votes.sol";

library TimestampClockLib {
using Time for *;

function clock() public view returns (uint48) {
return Time.timestamp();
}

function CLOCK_MODE() public view returns (string memory) {
if (clock() != Time.timestamp()) {
revert Votes.ERC6372InconsistentClock();
}
return "mode=timestamp";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

import "@openzeppelin/contracts/utils/math/Math.sol";

library VotingPowerLib {
using Math for *;

function calculateUserVotingPower(uint256 tokenAmount_, uint8 decimals_) external pure returns (uint256) {
uint256 oneToken_ = 10 ** decimals_;

if (tokenAmount_ <= oneToken_) {
return 0;
}

return (tokenAmount_ / oneToken_).log2();
}

function calculateVotingPowerThreshold(uint256 totalSupply_, uint8 decimals_) external pure returns (uint256) {
uint256 oneToken_ = 10 ** decimals_;

if (totalSupply_ <= oneToken_) {
return 0;
}

return (totalSupply_ / oneToken_).log10();
}
}

This file was deleted.

Loading

0 comments on commit c0ef81d

Please sign in to comment.