Last active
January 28, 2023 18:23
-
-
Save az0mb13/a1ff0a4a0a9421dd7aca3d4dfef95563 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
... | |
contract SimpleGovernance { | |
... | |
struct GovernanceAction { | |
address receiver; | |
bytes data; | |
uint256 weiAmount; | |
uint256 proposedAt; | |
uint256 executedAt; | |
} | |
... | |
uint256 private ACTION_DELAY_IN_SECONDS = 2 days; | |
... | |
function queueAction(address receiver, bytes calldata data, uint256 weiAmount) external returns (uint256) { | |
require(_hasEnoughVotes(msg.sender), "Not enough votes to propose an action"); | |
require(receiver != address(this), "Cannot queue actions that affect Governance"); | |
uint256 actionId = actionCounter; | |
GovernanceAction storage actionToQueue = actions[actionId]; | |
actionToQueue.receiver = receiver; | |
actionToQueue.weiAmount = weiAmount; | |
actionToQueue.data = data; | |
actionToQueue.proposedAt = block.timestamp; | |
actionCounter++; | |
emit ActionQueued(actionId, msg.sender); | |
return actionId; | |
} | |
function executeAction(uint256 actionId) external payable { | |
require(_canBeExecuted(actionId), "Cannot execute this action"); | |
GovernanceAction storage actionToExecute = actions[actionId]; | |
actionToExecute.executedAt = block.timestamp; | |
actionToExecute.receiver.functionCallWithValue( | |
actionToExecute.data, | |
actionToExecute.weiAmount | |
); | |
emit ActionExecuted(actionId, msg.sender); | |
} | |
function getActionDelay() public view returns (uint256) { | |
return ACTION_DELAY_IN_SECONDS; | |
} | |
/** | |
* @dev an action can only be executed if: | |
* 1) it's never been executed before and | |
* 2) enough time has passed since it was first proposed | |
*/ | |
function _canBeExecuted(uint256 actionId) private view returns (bool) { | |
GovernanceAction memory actionToExecute = actions[actionId]; | |
return ( | |
actionToExecute.executedAt == 0 && | |
(block.timestamp - actionToExecute.proposedAt >= ACTION_DELAY_IN_SECONDS) | |
); | |
} | |
function _hasEnoughVotes(address account) private view returns (bool) { | |
uint256 balance = governanceToken.getBalanceAtLastSnapshot(account); | |
uint256 halfTotalSupply = governanceToken.getTotalSupplyAtLastSnapshot() / 2; | |
return balance > halfTotalSupply; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment