-
Notifications
You must be signed in to change notification settings - Fork 0
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
fix: enable/disable earning in HubPortal #20
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,8 @@ | |
pragma solidity 0.8.26; | ||
|
||
import { IERC20 } from "../lib/common/src/interfaces/IERC20.sol"; | ||
import { UIntMath } from "../lib/common/src/libs/UIntMath.sol"; | ||
import { IndexingMath } from "../lib/common/src/libs/IndexingMath.sol"; | ||
import { TransceiverStructs } from "../lib/example-native-token-transfers/evm/src/libraries/TransceiverStructs.sol"; | ||
|
||
import { IMTokenLike } from "./interfaces/IMTokenLike.sol"; | ||
|
@@ -25,14 +27,11 @@ contract HubPortal is IHubPortal, Portal { | |
|
||
/* ============ Variables ============ */ | ||
|
||
/// @dev Registrar key holding value of whether the earners list can be ignored or not. | ||
bytes32 internal constant _EARNERS_LIST_IGNORED = "earners_list_ignored"; | ||
/// @dev The Hub Portal's index when earning was most recently disabled | ||
uint128 private _disablePortalIndex; | ||
|
||
/// @dev Registrar key of earners list. | ||
bytes32 internal constant _EARNERS_LIST = "earners"; | ||
|
||
/// @dev Array of indices at which earning was enabled or disabled. | ||
uint128[] internal _enableDisableEarningIndices; | ||
/// @dev The M token's index when earning was most recently enabled | ||
uint128 private _enableMTokenIndex; | ||
|
||
/* ============ Constructor ============ */ | ||
|
||
|
@@ -48,6 +47,15 @@ contract HubPortal is IHubPortal, Portal { | |
uint16 chainId_ | ||
) Portal(mToken_, registrar_, Mode.LOCKING, chainId_) {} | ||
|
||
function _initialize() internal override { | ||
super._initialize(); | ||
|
||
// set _disablePortalIndex to the default value on first deployment | ||
if (_disablePortalIndex == 0) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. why this check is required? isn't it 0 by default on initialize? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I want to set |
||
_disablePortalIndex = IndexingMath.EXP_SCALED_ONE; | ||
} | ||
} | ||
|
||
/* ============ Interactive Functions ============ */ | ||
|
||
/// @inheritdoc IHubPortal | ||
|
@@ -91,34 +99,27 @@ contract HubPortal is IHubPortal, Portal { | |
|
||
/// @inheritdoc IHubPortal | ||
function enableEarning() external { | ||
if (!_isApprovedEarner()) revert NotApprovedEarner(); | ||
if (_isEarningEnabled()) revert EarningIsEnabled(); | ||
|
||
// NOTE: This is a temporary measure to prevent re-enabling earning after it has been disabled. | ||
// This line will be removed in the future. | ||
if (_enableDisableEarningIndices.length != 0) revert EarningCannotBeReenabled(); | ||
uint128 mTokenIndex_ = _currentMTokenIndex(); | ||
_enableMTokenIndex = mTokenIndex_; | ||
|
||
IMTokenLike mToken_ = IMTokenLike(mToken()); | ||
uint128 currentMIndex_ = mToken_.currentIndex(); | ||
_enableDisableEarningIndices.push(currentMIndex_); | ||
IMTokenLike(mToken()).startEarning(); | ||
|
||
mToken_.startEarning(); | ||
|
||
emit EarningEnabled(currentMIndex_); | ||
emit EarningEnabled(mTokenIndex_); | ||
} | ||
|
||
/// @inheritdoc IHubPortal | ||
function disableEarning() external { | ||
if (_isApprovedEarner()) revert IsApprovedEarner(); | ||
toninorair marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (!_isEarningEnabled()) revert EarningIsDisabled(); | ||
|
||
IMTokenLike mToken_ = IMTokenLike(mToken()); | ||
uint128 currentMIndex_ = mToken_.currentIndex(); | ||
_enableDisableEarningIndices.push(currentMIndex_); | ||
uint128 portalIndex_ = _currentIndex(); | ||
_disablePortalIndex = portalIndex_; | ||
_enableMTokenIndex = 0; | ||
PierrickGT marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
mToken_.stopEarning(); | ||
IMTokenLike(mToken()).stopEarning(address(this)); | ||
|
||
emit EarningDisabled(currentMIndex_); | ||
emit EarningDisabled(portalIndex_); | ||
} | ||
|
||
/* ============ Internal Interactive Functions ============ */ | ||
|
@@ -168,33 +169,23 @@ contract HubPortal is IHubPortal, Portal { | |
return TransceiverStructs.nttManagerMessageDigest(chainId, message_); | ||
} | ||
|
||
/* ============ Internal View/Pure Functions ============ */ | ||
/* ============ Internal/Private View Functions ============ */ | ||
|
||
/// @dev Returns the current M token index used by the Hub Portal. | ||
/// @dev Returns the current Hub Portal index | ||
function _currentIndex() internal view override returns (uint128) { | ||
if (_isEarningEnabled()) { | ||
return IMTokenLike(mToken()).currentIndex(); | ||
} | ||
|
||
// If earning has been enabled in the past, return the latest recorded index when it was disabled. | ||
// Otherwise, return the starting index. | ||
return | ||
_enableDisableEarningIndices.length != 0 | ||
? _enableDisableEarningIndices[_enableDisableEarningIndices.length - 1] | ||
: 0; | ||
_isEarningEnabled() | ||
? UIntMath.bound128((uint256(_disablePortalIndex) * _currentMTokenIndex()) / _enableMTokenIndex) | ||
: _disablePortalIndex; | ||
} | ||
|
||
/// @dev Returns whether the Hub Portal is a TTG-approved earner or not. | ||
function _isApprovedEarner() internal view returns (bool) { | ||
IRegistrarLike registrar_ = IRegistrarLike(registrar); | ||
|
||
return | ||
registrar_.get(_EARNERS_LIST_IGNORED) != bytes32(0) || | ||
registrar_.listContains(_EARNERS_LIST, address(this)); | ||
/// @dev Returns the current M Token index | ||
function _currentMTokenIndex() private view returns (uint128) { | ||
return IMTokenLike(mToken()).currentIndex(); | ||
} | ||
|
||
/// @dev Returns whether earning was enabled for HubPortal or not. | ||
function _isEarningEnabled() internal view returns (bool) { | ||
toninorair marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return IMTokenLike(mToken()).isEarning(address(this)); | ||
function _isEarningEnabled() private view returns (bool) { | ||
return _enableMTokenIndex != 0; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why names
_disablePortalIndex
and_enableMTokenIndex
are not symmetrical? why private and not internal?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
_disablePortalIndex
is the last index of the Portal when earning was disabled, it's "virtual" index._enableMTokenIndex
is the index of M Token when earning was enabled, the "actual" index.Private
because we're not planning to inherit fromHubPortal