Skip to content

Commit

Permalink
v0.4.0 (#135)
Browse files Browse the repository at this point in the history
* Limit gas to 3500 (#127) 
* Implement function emergency exit for Validators (#126) 
* [ValidatorSet] Replace error messages by custom erorrs (#129) 
* [RoninGovernanceAdmin] Expose function & store bridge operator set (#132) 
* [ValidatorSet] Add details for `StakingRewardDistributed` event (#133) 
* Impl forwarder contract (#128) 
* Support voting without signatures on the Ronin network (#130)
* [Staking] Rename for clarity between pools and active pools (#134)
  • Loading branch information
nxqbao authored Jan 3, 2023
2 parents d722d7b + a55231a commit 80f9852
Show file tree
Hide file tree
Showing 95 changed files with 2,995 additions and 642 deletions.
20 changes: 18 additions & 2 deletions contracts/extensions/RONTransferHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
pragma solidity ^0.8.9;

abstract contract RONTransferHelper {
/// @dev Error of recipient not accepting RON when transfer RON.
error ErrRecipientRevert();
/// @dev Error of sender has insufficient balance.
error ErrInsufficientBalance();

/**
* @dev See `_sendRON`.
* Reverts if the recipient does not receive RON.
*/
function _transferRON(address payable _recipient, uint256 _amount) internal {
require(_sendRON(_recipient, _amount), "RONTransfer: unable to transfer value, recipient may have reverted");
if (!_sendRON(_recipient, _amount)) revert ErrRecipientRevert();
}

/**
Expand All @@ -20,7 +25,7 @@ abstract contract RONTransferHelper {
*
*/
function _sendRON(address payable _recipient, uint256 _amount) internal returns (bool _success) {
require(address(this).balance >= _amount, "RONTransfer: insufficient balance");
if (address(this).balance < _amount) revert ErrInsufficientBalance();
return _unsafeSendRON(_recipient, _amount);
}

Expand All @@ -37,4 +42,15 @@ abstract contract RONTransferHelper {
function _unsafeSendRON(address payable _recipient, uint256 _amount) internal returns (bool _success) {
(_success, ) = _recipient.call{ value: _amount }("");
}

/**
* @dev Same purpose with {_unsafeSendRON(address,uin256)} but containing gas limit stipend forwarded in the call.
*/
function _unsafeSendRON(
address payable _recipient,
uint256 _amount,
uint256 _gas
) internal returns (bool _success) {
(_success, ) = _recipient.call{ value: _amount, gas: _gas }("");
}
}
4 changes: 2 additions & 2 deletions contracts/extensions/collections/HasBridgeContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract HasBridgeContract is IHasBridgeContract, HasProxyAdmin {
IBridge internal _bridgeContract;

modifier onlyBridgeContract() {
require(bridgeContract() == msg.sender, "HasBridgeContract: method caller must be bridge contract");
if (bridgeContract() != msg.sender) revert ErrCallerMustBeBridgeContract();
_;
}

Expand All @@ -24,7 +24,7 @@ contract HasBridgeContract is IHasBridgeContract, HasProxyAdmin {
* @inheritdoc IHasBridgeContract
*/
function setBridgeContract(address _addr) external virtual override onlyAdmin {
require(_addr.code.length > 0, "HasBridgeContract: set to non-contract");
if (_addr.code.length <= 0) revert ErrZeroCodeContract();
_setBridgeContract(_addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasBridgeTrackingContract is IHasBridgeTrackingContract, HasProxyAdmin
IBridgeTracking internal _bridgeTrackingContract;

modifier onlyBridgeTrackingContract() {
require(
bridgeTrackingContract() == msg.sender,
"HasBridgeTrackingContract: method caller must be bridge tracking contract"
);
if (bridgeTrackingContract() != msg.sender) revert ErrCallerMustBeBridgeTrackingContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasBridgeTrackingContract is IHasBridgeTrackingContract, HasProxyAdmin
* @inheritdoc IHasBridgeTrackingContract
*/
function setBridgeTrackingContract(address _addr) external virtual override onlyAdmin {
require(_addr.code.length > 0, "HasBridgeTrackingContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setBridgeTrackingContract(_addr);
}

Expand Down
7 changes: 2 additions & 5 deletions contracts/extensions/collections/HasMaintenanceContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasMaintenanceContract is IHasMaintenanceContract, HasProxyAdmin {
IMaintenance internal _maintenanceContract;

modifier onlyMaintenanceContract() {
require(
maintenanceContract() == msg.sender,
"HasMaintenanceContract: method caller must be scheduled maintenance contract"
);
if (maintenanceContract() != msg.sender) revert ErrCallerMustBeMaintenanceContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasMaintenanceContract is IHasMaintenanceContract, HasProxyAdmin {
* @inheritdoc IHasMaintenanceContract
*/
function setMaintenanceContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasMaintenanceContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setMaintenanceContract(_addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasRoninGovernanceAdminContract is IHasRoninGovernanceAdminContract, Ha
IRoninGovernanceAdmin internal _roninGovernanceAdminContract;

modifier onlyRoninGovernanceAdminContract() {
require(
roninGovernanceAdminContract() == msg.sender,
"HasRoninGovernanceAdminContract: method caller must be ronin governance admin contract"
);
if (roninGovernanceAdminContract() != msg.sender) revert ErrCallerMustBeGovernanceAdminContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasRoninGovernanceAdminContract is IHasRoninGovernanceAdminContract, Ha
* @inheritdoc IHasRoninGovernanceAdminContract
*/
function setRoninGovernanceAdminContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasRoninGovernanceAdminContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setRoninGovernanceAdminContract(_addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasRoninTrustedOrganizationContract is IHasRoninTrustedOrganizationCont
IRoninTrustedOrganization internal _roninTrustedOrganizationContract;

modifier onlyRoninTrustedOrganizationContract() {
require(
roninTrustedOrganizationContract() == msg.sender,
"HasRoninTrustedOrganizationContract: method caller must be ronin trusted organization contract"
);
if (roninTrustedOrganizationContract() != msg.sender) revert ErrCallerMustBeRoninTrustedOrgContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasRoninTrustedOrganizationContract is IHasRoninTrustedOrganizationCont
* @inheritdoc IHasRoninTrustedOrganizationContract
*/
function setRoninTrustedOrganizationContract(address _addr) external virtual override onlyAdmin {
require(_addr.code.length > 0, "HasRoninTrustedOrganizationContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setRoninTrustedOrganizationContract(_addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasSlashIndicatorContract is IHasSlashIndicatorContract, HasProxyAdmin
ISlashIndicator internal _slashIndicatorContract;

modifier onlySlashIndicatorContract() {
require(
slashIndicatorContract() == msg.sender,
"HasSlashIndicatorContract: method caller must be slash indicator contract"
);
if (slashIndicatorContract() != msg.sender) revert ErrCallerMustBeSlashIndicatorContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasSlashIndicatorContract is IHasSlashIndicatorContract, HasProxyAdmin
* @inheritdoc IHasSlashIndicatorContract
*/
function setSlashIndicatorContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasSlashIndicatorContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setSlashIndicatorContract(_addr);
}

Expand Down
4 changes: 2 additions & 2 deletions contracts/extensions/collections/HasStakingContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract HasStakingContract is IHasStakingContract, HasProxyAdmin {
IStaking internal _stakingContract;

modifier onlyStakingContract() {
require(stakingContract() == msg.sender, "HasStakingContract: method caller must be staking contract");
if (stakingContract() != msg.sender) revert ErrCallerMustBeStakingContract();
_;
}

Expand All @@ -24,7 +24,7 @@ contract HasStakingContract is IHasStakingContract, HasProxyAdmin {
* @inheritdoc IHasStakingContract
*/
function setStakingContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasStakingContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setStakingContract(_addr);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ contract HasStakingVestingContract is IHasStakingVestingContract, HasProxyAdmin
IStakingVesting internal _stakingVestingContract;

modifier onlyStakingVestingContract() {
require(
stakingVestingContract() == msg.sender,
"HasStakingVestingContract: method caller must be staking vesting contract"
);
if (stakingVestingContract() != msg.sender) revert ErrCallerMustBeStakingVestingContract();
_;
}

Expand All @@ -27,7 +24,7 @@ contract HasStakingVestingContract is IHasStakingVestingContract, HasProxyAdmin
* @inheritdoc IHasStakingVestingContract
*/
function setStakingVestingContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasStakingVestingContract: set to non-contract");
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setStakingVestingContract(_addr);
}

Expand Down
6 changes: 3 additions & 3 deletions contracts/extensions/collections/HasValidatorContract.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ contract HasValidatorContract is IHasValidatorContract, HasProxyAdmin {
IRoninValidatorSet internal _validatorContract;

modifier onlyValidatorContract() {
require(validatorContract() == msg.sender, "HasValidatorContract: method caller must be validator contract");
if (validatorContract() != msg.sender) revert ErrCallerMustBeValidatorContract();
_;
}

Expand All @@ -23,8 +23,8 @@ contract HasValidatorContract is IHasValidatorContract, HasProxyAdmin {
/**
* @inheritdoc IHasValidatorContract
*/
function setValidatorContract(address _addr) external override onlyAdmin {
require(_addr.code.length > 0, "HasValidatorContract: set to non-contract");
function setValidatorContract(address _addr) external virtual override onlyAdmin {
if (_addr.code.length == 0) revert ErrZeroCodeContract();
_setValidatorContract(_addr);
}

Expand Down
144 changes: 144 additions & 0 deletions contracts/extensions/forwarder/Forwarder.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;

import "./ForwarderLogic.sol";
import "./ForwarderRole.sol";

contract Forwarder is ForwarderLogic, ForwarderRole {
/**
* @dev Initializes the forwarder with an initial target address and a contract admin.
*/
constructor(address __target, address __admin) payable {
_changeTargetTo(__target);
_changeAdminTo(__admin);
}

modifier onlyModerator() {
require(_isModerator(msg.sender), "Forwarder: unauthorized call");
_;
}

modifier adminExecutesOrModeratorForwards() {
if (_isAdmin(msg.sender)) {
_;
} else {
require(_isModerator(msg.sender), "Forwarder: unauthorized call");
_fallback();
}
}

/**
* @dev Forwards the call to the target (the `msg.value` is sent along in the call).
*
* Requirements:
* - Only moderator can invoke fallback method.
*/
fallback() external payable override onlyModerator {
_fallback();
}

/**
* @dev Receives RON transfer from all addresses.
*/
receive() external payable override {}

/**
* @dev Returns the current admin.
*
* NOTE: Only the admin can call this function. See {ForwarderStorage-_getAdmin}.
*
* TIP: To get this value clients can read directly from the storage slot shown below using the
* https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0xa8c82e6b38a127695961bbff56774712a221ab251224d4167eab01e23fcee6ca`
*/
function admin() external adminExecutesOrModeratorForwards returns (address admin_) {
admin_ = _getAdmin();
}

/**
* @dev Changes the admin of the forwarder.
*
* Emits an {AdminChanged} event.
*
* NOTE: Only the admin can call this function. See {ForwarderStorage-_changeAdminTo}.
*/
function changeAdminTo(address newAdmin) external virtual adminExecutesOrModeratorForwards {
_changeAdminTo(newAdmin);
}

/**
* @dev Returns the current target.
*
* NOTE: Only the admin can call this function. See {ForwarderStorage-_getTarget}.
*
* TIP: To get this value clients can read directly from the storage slot shown below using the
* https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0xcbec2a70e8f0a52aeb8f96e02517dc497e58d9a6fa86ab4056563f1e6baf3d3e`
*/
function moderator() external adminExecutesOrModeratorForwards returns (address moderator_) {
moderator_ = _getModerator();
}

/**
* @dev Changes the moderator of the forwarder.
*
* Emits an {ModeratorChanged} event.
*
* NOTE: Only the moderator can call this function. See {ForwarderStorage-_changeModeratorTo}.
*/
function changeModeratorTo(address newModerator) external virtual adminExecutesOrModeratorForwards {
_changeModeratorTo(newModerator);
}

/**
* @dev Returns the current target.
*
* NOTE: Only the moderator can call this function. See {ForwarderStorage-_getTarget}.
*
* TIP: To get this value clients can read directly from the storage slot shown below using the
* https://eth.wiki/json-rpc/API#eth_getstorageat[`eth_getStorageAt`] RPC call.
* `0x58221d865d4bfcbfe437720ee0c958ac3269c4e9c775f643bf474ed980d61168`
*/
function target() external adminExecutesOrModeratorForwards returns (address target_) {
target_ = _target();
}

/**
* @dev Changes the target of the forwarder.
*
* Emits an {TargetChanged} event.
*
* NOTE: Only the admin can call this function. See {ForwarderStorage-_changeTargetTo}.
*/
function changeTargetTo(address newTarget) external virtual adminExecutesOrModeratorForwards {
_changeTargetTo(newTarget);
}

/**
* @dev Forwards the encoded call specified by `_data` to the target. The forwarder attachs `_val` value
* from the forwarder contract and sends along with the call.
*
* Requirements:
* - Only moderator can call this method.
*/
function functionCall(bytes memory _data, uint256 _val) external payable onlyModerator {
_functionCall(_data, _val);
}

/**
* @dev Calls a function from the current forwarder to the target as specified by `_data`, which should be an encoded
* function call, with the value `_val`.
*/
function _functionCall(bytes memory _data, uint256 _val) internal {
require(_val <= address(this).balance, "Forwarder: invalid forwarding value");
_call(_target(), _data, _val);
}

/**
* @dev Returns the current target address.
*/
function _target() internal view virtual override returns (address target_) {
return _getTarget();
}
}
Loading

0 comments on commit 80f9852

Please sign in to comment.