Skip to content

Commit

Permalink
[add proposal type] governance general purpose (#116)
Browse files Browse the repository at this point in the history
* feat : add execute

* fix : upgrade issue

* test : execute

* feat : append gap

* fix : gap size

* fix : [execute fail] return value

* fix : typo

* fix : typo

* feat : test code

* feat : gov abigen
  • Loading branch information
felix-shin-wt authored Aug 2, 2024
1 parent 1edab31 commit 537371d
Show file tree
Hide file tree
Showing 11 changed files with 867 additions and 38 deletions.
62 changes: 59 additions & 3 deletions wemix/bind/gen_ballotStorage_abi.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions wemix/bind/gen_envStorage_abi.go

Large diffs are not rendered by default.

373 changes: 353 additions & 20 deletions wemix/bind/gen_gov_abi.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions wemix/bind/gen_ncpExit_abi.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion wemix/bind/gen_registry_abi.go

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

2 changes: 1 addition & 1 deletion wemix/bind/gen_staking_abi.go

Large diffs are not rendered by default.

75 changes: 75 additions & 0 deletions wemix/governance-contract/contracts/GovImp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -509,6 +509,14 @@ contract GovImp is AGov, ReentrancyGuardUpgradeable, BallotEnums, EnvConstants,
changeGov(ballotIdx);
} else if (ballotType == uint256(BallotTypes.EnvValChange)) {
applyEnv(ballotIdx);
} else if (ballotType == uint256(BallotTypes.Execute)) {
_execute(ballotIdx);
}
} else {
if (ballotType == uint256(BallotTypes.Execute)) {
IBallotStorage _ballotStorage = IBallotStorage(getBallotStorageAddress());
(, uint256 _value, ) = _ballotStorage.getBallotExecute(ballotIdx);
_returnValueToCreator(_ballotStorage, ballotIdx, _value);
}
}
finalizeBallot(ballotIdx, ballotState);
Expand Down Expand Up @@ -1077,4 +1085,71 @@ contract GovImp is AGov, ReentrancyGuardUpgradeable, BallotEnums, EnvConstants,

return 0;
}

// Critical

function upgradeTo(address) external override {
revert("Invalid access");
}

function upgradeToAndCall(address, bytes memory) external payable override {
revert("Invalid access");
}

// Genernal Purpose

event Executed(bool indexed success, address indexed to, uint256 value, bytes calldatas, bytes returnData);
event FailReturnValue(uint256 indexed ballotIdx, address indexed creator, uint256 value, bytes result);

function addProposalToExecute(
address _target,
bytes memory _calldata,
bytes memory _memo,
uint256 _duration
) external payable onlyGovMem checkTimePeriod checkLockedAmount {
require(_target != ZERO, "target cannot be zero");

address _creator = msg.sender;
if (msg.value != 0) {
(bool _ok, ) = _creator.call{ value: 0 }("");
require(_ok, "creator is not payable");
}

uint256 _ballotIdx = ballotLength + 1;

IBallotStorage(getBallotStorageAddress()).createBallotForExecute(
_ballotIdx, // ballot id
uint256(BallotTypes.Execute), // ballot type
_duration,
_creator, // creator
_target,
msg.value,
_calldata
);
updateBallotMemo(_ballotIdx, _memo);
ballotLength = _ballotIdx;
}

function _execute(uint256 _ballotIdx) private {
fromValidBallot(_ballotIdx, uint256(BallotTypes.Execute));
IBallotStorage _ballotStorage = IBallotStorage(getBallotStorageAddress());

(address _target, uint256 _value, bytes memory _calldata) = _ballotStorage.getBallotExecute(_ballotIdx);
(bool _success, bytes memory _returnData) = _target.call{ value: _value }(_calldata);

modifiedBlock = block.number;
emit Executed(_success, _target, _value, _calldata, _returnData);

if (!_success) _returnValueToCreator(_ballotStorage, _ballotIdx, _value);
}

function _returnValueToCreator(IBallotStorage _ballotStorage, uint256 _ballotIDx, uint256 _value) private {
if (_value == 0) return;

(, , , address _creator, , , , , , , ) = _ballotStorage.getBallotBasic(_ballotIDx);
(bool _ok, bytes memory _returnData) = _creator.call{ value: _value }("");
if (!_ok) {
emit FailReturnValue(_ballotIDx, _creator, _value, _returnData);
}
}
}
3 changes: 2 additions & 1 deletion wemix/governance-contract/contracts/abstract/BallotEnums.sol
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ contract BallotEnums {
MemberRemoval, // old Member Address
MemberChange, // Old Member Address, New Member Address, new Node id, New Node ip, new Node port
GovernanceChange, // new Governace Impl Address
EnvValChange // Env variable name, type , value
EnvValChange, // Env variable name, type , value
Execute // Genernal Purpose
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,8 @@ interface IBallotStorage {
function getBallotAddress(uint256) external view returns (address);
function getBallotVariable(uint256) external view returns (bytes32, uint256, bytes memory);
function getBallotForExit(uint256) external view returns (uint256, uint256);

// Genernal Purpose
function createBallotForExecute(uint256, uint256, uint256, address, address, uint256, bytes memory) external;
function getBallotExecute(uint256) external view returns (address, uint256, bytes memory);
}
38 changes: 38 additions & 0 deletions wemix/governance-contract/contracts/storage/BallotStorageImp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -567,4 +567,42 @@ contract BallotStorageImp is GovChecker, BallotEnums, IBallotStorage, UUPSUpgrad
}

function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}

// Genernal Purpose

struct BallotExecute {
address target;
uint256 value;
bytes data;
}
mapping(uint => BallotExecute) private __ballotExecuteMap;

function createBallotForExecute(
uint256 _id,
uint256 _ballotType,
uint256 _duration,
address _creator,
address _target,
uint256 _value,
bytes memory _calldata
) external override onlyGov notDisabled {
require(_ballotType == uint256(BallotTypes.Execute), "Invalid Ballot Type");
require(_target != address(0), "Invalid target address");
// ballot basic
_createBallot(_id, _ballotType, _duration, _creator);
// ballot executeMap
__ballotExecuteMap[_id] = BallotExecute({ target: _target, value: _value, data: _calldata });
}

function getBallotExecute(uint256 _id) external view override returns (address, uint256, bytes memory) {
BallotExecute memory _ballot = __ballotExecuteMap[_id];
return (_ballot.target, _ballot.value, _ballot.data);
}

/**
* @dev This empty reserved space is put in place to allow future versions to add new
* variables without shifting down storage in the inheritance chain.
* See https://docs.openzeppelin.com/contracts/4.x/upgradeable#storage_gaps
*/
uint256[40] private __gap;
}
Loading

0 comments on commit 537371d

Please sign in to comment.