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

feat(Governance): new module based on TimeLock #196

Merged
merged 15 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 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
94 changes: 94 additions & 0 deletions contracts/governance/InterchainGovernance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import { AxelarExecutable } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/executable/AxelarExecutable.sol';
import { TimeLock } from '@axelar-network/axelar-gmp-sdk-solidity/contracts/utils/TimeLock.sol';
import { IInterchainGovernance } from '../interfaces/IInterchainGovernance.sol';

contract InterchainGovernance is AxelarExecutable, TimeLock, IInterchainGovernance {
enum GovernanceCommand {
ScheduleTimeLockProposal,
CancelTimeLockProposal
}

bytes32 public immutable governanceChainHash;
bytes32 public immutable governanceAddressHash;

// solhint-disable-next-line no-empty-blocks
re1ro marked this conversation as resolved.
Show resolved Hide resolved
constructor(
address gatewayAddress,
string memory governanceChain_,
string memory governanceAddress_,
uint256 minimumTimeDelay
) AxelarExecutable(gatewayAddress) TimeLock(minimumTimeDelay) {
re1ro marked this conversation as resolved.
Show resolved Hide resolved
governanceChainHash = keccak256(bytes(governanceChain_));
governanceAddressHash = keccak256(bytes(governanceAddress_));
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
}

function executeProposal(address target, bytes calldata callData) external payable {
bytes32 proposalHash = keccak256(abi.encodePacked(target, callData, msg.value));

_executeTimeLock(proposalHash);

(bool success, ) = target.call{ value: msg.value }(callData);

if (!success) {
revert ExecutionFailed();
}

emit ProposalExecuted(proposalHash);
}
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

function _execute(
string calldata sourceChain,
string calldata sourceAddress,
bytes calldata payload
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
) internal override {
if (keccak256(bytes(sourceChain)) != governanceChainHash || keccak256(bytes(sourceAddress)) != governanceAddressHash)
revert NotGovernance();

(uint256 command, address target, bytes memory callData, uint256 nativeValue, uint256 eta) = abi.decode(
payload,
(uint256, address, bytes, uint256, uint256)
);

if (target == address(0)) revert InvalidTarget();
if (callData.length == 0) revert InvalidCallData();
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

_processCommand(command, target, callData, nativeValue, eta);
}

function _processCommand(
uint256 commandId,
address target,
bytes memory callData,
uint256 nativeValue,
uint256 eta
) internal virtual {
GovernanceCommand command = GovernanceCommand(commandId);
bytes32 proposalHash = keccak256(abi.encodePacked(target, callData, nativeValue));

if (command == GovernanceCommand.ScheduleTimeLockProposal) {
eta = _scheduleTimeLock(proposalHash, eta);

emit ProposalScheduled(proposalHash, target, callData, eta);
} else if (command == GovernanceCommand.CancelTimeLockProposal) {
_cancelTimeLock(proposalHash);

emit ProposalCancelled(proposalHash);
} else {
revert InvalidCommand();
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
}
}

function _executeWithToken(
string calldata, /* sourceChain */
string calldata, /* sourceAddress */
bytes calldata, /* payload */
string calldata, /* tokenSymbol */
uint256 /* amount */
) internal pure override {
revert TokenNotSupported();
}
}
20 changes: 20 additions & 0 deletions contracts/interfaces/IInterchainGovernance.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// SPDX-License-Identifier: MIT

pragma solidity 0.8.9;
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

import '@axelar-network/axelar-gmp-sdk-solidity/contracts/interfaces/IAxelarExecutable.sol';

interface IInterchainGovernance is IAxelarExecutable {
error NotGovernance();
error InvalidCommand();
error InvalidTarget();
error InvalidCallData();
error ExecutionFailed();
error TokenNotSupported();

event ProposalScheduled(bytes32 indexed proposalHash, address indexed targetContract, bytes callData, uint256 eta);
event ProposalCancelled(bytes32 indexed proposalHash);
event ProposalExecuted(bytes32 indexed proposalHash);
milapsheth marked this conversation as resolved.
Show resolved Hide resolved

function executeProposal(address targetContract, bytes calldata callData) external payable;
milapsheth marked this conversation as resolved.
Show resolved Hide resolved
}