Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hogehoge #4

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .solhint.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"extends": "solhint:recommended",
"rules": {
"no-empty-blocks": "off",
"compiler-version": ["error", "0.8.7"],
"compiler-version": ["error", "0.8.9"],
"func-visibility": ["warn", { "ignoreConstructors": true }]
}
}
2 changes: 1 addition & 1 deletion contracts/Admin.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.7;
pragma solidity =0.8.9;

import {ProxyAdmin} from "@openzeppelin/contracts/proxy/transparent/ProxyAdmin.sol";

Expand Down
16 changes: 0 additions & 16 deletions contracts/Example.sol

This file was deleted.

16 changes: 16 additions & 0 deletions contracts/IPropertyGovernance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

interface IPropertyGovernance {
struct TokenAllocate {
address member;
uint256 percentage;
}
event SetTokenAllocate(TokenAllocate[] tokenAllocateInfo);

function setAllocateInfo(TokenAllocate[] memory _share) external;

function finish() external returns (bool);

function rescue(address _token) external returns (bool);
}
13 changes: 13 additions & 0 deletions contracts/IPropertyGovernanceFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

interface IPropertyGovernanceFactory {
event Created(
address indexed property,
address author,
address governance,
uint256 tokenAmount
);

function create(address _property) external returns (address);
}
152 changes: 152 additions & 0 deletions contracts/PropertyGovernance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "@devprotocol/protocol-v2/contracts/interface/IProperty.sol";
import "@devprotocol/protocol-v2/contracts/interface/IWithdraw.sol";
import "./UsingAddressRegistry.sol";
import "./IPropertyGovernance.sol";

contract PropertyGovernance is UsingAddressRegistry, IPropertyGovernance {
address public property;
address public factory;
bool public isFinished = false;
uint256 public alreadyAllocateReward;
mapping(address => uint256) public percentage;
mapping(address => uint256) public rewardMap;
EnumerableSet.AddressSet private members;

using EnumerableSet for EnumerableSet.AddressSet;

constructor(address _addressRegistry, address _property)
UsingAddressRegistry(_addressRegistry)
{
property = _property;
factory = msg.sender;
address author = IProperty(property).author();
members.add(author);
percentage[author] = 100;
}

modifier onlyPropertyAuthor() {
require(IProperty(property).author() == msg.sender, "illegal access");
_;
}

modifier notFinished() {
require(isFinished == false, "already finished");
_;
}

function setAllocateInfo(TokenAllocate[] memory _allocate)
external
onlyPropertyAuthor
notFinished
{
validateTokenAllocate(_allocate);
addReward();
for (uint256 i = 0; i < _allocate.length; i++) {
address member = _allocate[i].member;
if (!members.contains(member)) {
members.add(member);
}
percentage[member] = _allocate[i].percentage;
}
for (uint256 i = 0; i < members.length(); i++) {
address member = members.at(i);
if (!isAllocateMember(_allocate, member)) {
percentage[member] = 0;
}
}
emit SetTokenAllocate(_allocate);
}

function allocate() public onlyPropertyAuthor notFinished returns (bool) {
addReward();
IWithdraw(withdrawAddress()).withdraw(property);
IERC20 dev = IERC20(devAddress());
uint256 transferedReward = 0;
for (uint256 i = 0; i < members.length(); i++) {
address member = members.at(i);
uint256 reward = rewardMap[member];
if (reward == 0) {
continue;
}
rewardMap[member] = 0;
require(dev.transfer(member, reward), "failed to transfer");
transferedReward += reward;
}
return true;
}

function finish() external onlyPropertyAuthor notFinished returns (bool) {
require(allocate(), "failed to allocate");
require(sendToken(property), "failed to send property token");
// just in case
require(sendToken(devAddress()), "failed to send dev token");
isFinished = true;
return true;
}

function rescue(address _token) external onlyPropertyAuthor returns (bool) {
require(isFinished == true, "illegal access");
return sendToken(_token);
}

function addReward() private {
(uint256 currentReward, , , ) = IWithdraw(withdrawAddress())
.calculateRewardAmount(property, address(this));
uint256 reward = currentReward - alreadyAllocateReward;
for (uint256 i = 0; i < members.length(); i++) {
address member = members.at(i);
uint256 individualReward = (reward * percentage[member]) / 100;
rewardMap[member] += individualReward;
}
alreadyAllocateReward = currentReward;
}

function isAllocateMember(TokenAllocate[] memory _allocate, address _member)
private
pure
returns (bool)
{
for (uint256 i = 0; i < _allocate.length; i++) {
address member = _allocate[i].member;
if (member == _member) {
return true;
}
}
return false;
}

function validateTokenAllocate(TokenAllocate[] memory _allocate)
private
pure
{
uint256 sum = 0;
address[] memory alocateMembers = new address[](_allocate.length);
for (uint256 i = 0; i < _allocate.length; i++) {
alocateMembers[i] = _allocate[i].member;
sum += _allocate[i].percentage;
}
require(sum == 100, "total percentage is not 100");
for (uint256 i = 0; i < _allocate.length; i++) {
for (uint256 k = i; k < _allocate.length; k++) {
require(
alocateMembers[k] == alocateMembers[i],
"total percentage is not 100"
);
}
}
}

function sendToken(address _token) private returns (bool) {
IERC20 token = IERC20(_token);
uint256 balance = token.balanceOf(address(this));
if (balance == 0) {
return true;
}
return token.transfer(IProperty(property).author(), balance);
}
}
44 changes: 44 additions & 0 deletions contracts/PropertyGovernanceFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@devprotocol/protocol-v2/contracts/interface/IProperty.sol";
import "@devprotocol/protocol-v2/contracts/interface/IPropertyFactory.sol";
import "./IPropertyGovernanceFactory.sol";
import "./PropertyGovernance.sol";
import "./UsingAddressRegistryUpgradable.sol";

contract PropertyGovernanceFactory is
UsingAddressRegistryUpgradable,
IPropertyGovernanceFactory
{
mapping(address => address) public governanceMap;

function initialize(address _addressRegistry) external initializer {
__UsingAddressRegistryUpgradable_init(_addressRegistry);
}

function create(address _property) external returns (address) {
require(
IPropertyFactory(propertyFactoryAddress()).isProperty(_property),
"not property address"
);
IProperty property = IProperty(_property);
require(property.author() == msg.sender, "illegal access");
PropertyGovernance governance = new PropertyGovernance(
addressRegistry,
_property
);
IERC20 erc20Token = IERC20(_property);
uint256 balance = erc20Token.balanceOf(msg.sender);
erc20Token.transferFrom(msg.sender, address(governance), balance);
emit Created(
_property,
property.author(),
address(governance),
balance
);
governanceMap[_property] = address(governance);
return address(governance);
}
}
2 changes: 1 addition & 1 deletion contracts/UpgradeableProxy.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.7;
pragma solidity =0.8.9;

import {TransparentUpgradeableProxy} from "@openzeppelin/contracts/proxy/transparent/TransparentUpgradeableProxy.sol";

Expand Down
20 changes: 20 additions & 0 deletions contracts/UsingAddressRegistry.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

import {IAddressRegistry} from "@devprotocol/protocol-v2/contracts/interface/IAddressRegistry.sol";

contract UsingAddressRegistry {
address public addressRegistry;

constructor(address _addressRegistry) {
addressRegistry = _addressRegistry;
}

function withdrawAddress() internal view returns (address) {
return IAddressRegistry(addressRegistry).registries("Withdraw");
}

function devAddress() internal view returns (address) {
return IAddressRegistry(addressRegistry).registries("Dev");
}
}
22 changes: 22 additions & 0 deletions contracts/UsingAddressRegistryUpgradable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: MPL-2.0
pragma solidity =0.8.9;

import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {IAddressRegistry} from "@devprotocol/protocol-v2/contracts/interface/IAddressRegistry.sol";
import {IWithdraw} from "@devprotocol/protocol-v2/contracts/interface/IWithdraw.sol";

contract UsingAddressRegistryUpgradable is Initializable {
address public addressRegistry;

// solhint-disable-next-line func-name-mixedcase
function __UsingAddressRegistryUpgradable_init(address _addressRegistry)
public
initializer
{
addressRegistry = _addressRegistry;
}

function propertyFactoryAddress() internal view returns (address) {
return IAddressRegistry(addressRegistry).registries("PropertyFactory");
}
}
2 changes: 1 addition & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@ import '@nomiclabs/hardhat-ethers'
import '@nomiclabs/hardhat-waffle'

module.exports = {
solidity: '0.8.7',
solidity: '0.8.9',
}
13 changes: 7 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@
"devDependencies": {
"@nomiclabs/hardhat-ethers": "2.0.2",
"@nomiclabs/hardhat-waffle": "2.0.1",
"@openzeppelin/contracts": "4.3.2",
"@typechain/ethers-v5": "^8.0.0",
"@typechain/hardhat": "^2.3.0",
"@types/chai": "4.2.22",
"@types/dotenv": "8.2.0",
"@types/mocha": "9.0.0",
Expand All @@ -44,13 +45,13 @@
"prettier": "2.4.1",
"prettier-plugin-solidity": "1.0.0-beta.18",
"ts-node": "10.4.0",
"typescript": "4.4.4"
"typescript": "4.4.4",
"solhint": "^3.3.2",
"typechain": "^6.0.0"
},
"dependencies": {
"@devprotocol/protocol-v2": "^0.5.0",
"@openzeppelin/contracts-upgradeable": "^4.3.2",
"@typechain/ethers-v5": "^8.0.0",
"@typechain/hardhat": "^2.3.0",
"solhint": "^3.3.2",
"typechain": "^6.0.0"
"@openzeppelin/contracts": "4.3.2"
}
}
42 changes: 21 additions & 21 deletions scripts/deploy-example.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import { ethers } from 'hardhat'
import { deployAdmin, deployProxy } from './utils'
// Import { ethers } from 'hardhat'
// import { deployAdmin, deployProxy } from './utils'

async function main() {
const Example = await ethers.getContractFactory('Example')
const example = await Example.deploy()
// async function main() {
// const Example = await ethers.getContractFactory('Example')
// const example = await Example.deploy()

const admin = await deployAdmin()
// const admin = await deployAdmin()

const upgradeableProxy = await deployProxy(
example.address,
admin.address,
ethers.utils.arrayify('0x')
)
// const upgradeableProxy = await deployProxy(
// example.address,
// admin.address,
// ethers.utils.arrayify('0x')
// )

console.log('Example address:', example.address)
console.log('Admin address:', admin.address)
console.log('UpgradeableProxy address:', upgradeableProxy.address)
}
// console.log('Example address:', example.address)
// console.log('Admin address:', admin.address)
// console.log('UpgradeableProxy address:', upgradeableProxy.address)
// }

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error)
process.exit(1)
})
// main()
// .then(() => process.exit(0))
// .catch((error) => {
// console.error(error)
// process.exit(1)
// })
Loading