tornado-contracts/contracts/Governance/v1/Delegation.sol

54 lines
1.9 KiB
Solidity
Raw Permalink Normal View History

2024-03-29 21:52:45 +00:00
// SPDX-License-Identifier: MIT
pragma solidity ^0.6.0;
2025-01-05 09:29:24 +00:00
import { Core } from './Core.sol';
2024-03-29 21:52:45 +00:00
abstract contract Delegation is Core {
/// @notice Delegatee records
mapping(address => address) public delegatedTo;
event Delegated(address indexed account, address indexed to);
event Undelegated(address indexed account, address indexed from);
function delegate(address to) external {
address previous = delegatedTo[msg.sender];
require(
to != msg.sender && to != address(this) && to != address(0) && to != previous,
2025-01-05 09:29:24 +00:00
'Governance: invalid delegatee'
2024-03-29 21:52:45 +00:00
);
if (previous != address(0)) {
emit Undelegated(msg.sender, previous);
}
delegatedTo[msg.sender] = to;
emit Delegated(msg.sender, to);
}
function undelegate() external {
address previous = delegatedTo[msg.sender];
2025-01-05 09:29:24 +00:00
require(previous != address(0), 'Governance: tokens are already undelegated');
2024-03-29 21:52:45 +00:00
delegatedTo[msg.sender] = address(0);
emit Undelegated(msg.sender, previous);
}
2025-01-05 09:29:24 +00:00
function proposeByDelegate(address from, address target, string memory description) external returns (uint256) {
require(delegatedTo[from] == msg.sender, 'Governance: not authorized');
2024-03-29 21:52:45 +00:00
return _propose(from, target, description);
}
2025-01-05 09:29:24 +00:00
function _propose(address proposer, address target, string memory description) internal virtual returns (uint256);
2024-03-29 21:52:45 +00:00
function castDelegatedVote(address[] memory from, uint256 proposalId, bool support) external virtual {
for (uint256 i = 0; i < from.length; i++) {
2025-01-05 09:29:24 +00:00
require(delegatedTo[from[i]] == msg.sender, 'Governance: not authorized');
2024-03-29 21:52:45 +00:00
_castVote(from[i], proposalId, support);
}
if (lockedBalance[msg.sender] > 0) {
_castVote(msg.sender, proposalId, support);
}
}
function _castVote(address voter, uint256 proposalId, bool support) internal virtual;
}