Skip to content

Commit

Permalink
use _isVestingContract instead of isVesting in WeightedStaking
Browse files Browse the repository at this point in the history
  • Loading branch information
jameshowlett977 committed Jun 7, 2021
1 parent 6a83007 commit fdce225
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 68 deletions.
67 changes: 0 additions & 67 deletions contracts/governance/Staking/Staking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -26,32 +26,6 @@ contract Staking is IStaking, WeightedStaking, ApprovalReceiver {
/// @notice Constant used for computing the vesting dates.
uint256 constant FOUR_WEEKS = 4 weeks;

/**
* @dev Throws if called by any account other than the owner or admin.
*/
modifier onlyAuthorized() {
require(isOwner() || admins[msg.sender], "unauthorized");
_;
}

/**
* @notice Add account to ACL.
* @param _admin The addresses of the account to grant permissions.
* */
function addAdmin(address _admin) public onlyOwner {
admins[_admin] = true;
emit AdminAdded(_admin);
}

/**
* @notice Remove account from ACL.
* @param _admin The addresses of the account to revoke permissions.
* */
function removeAdmin(address _admin) public onlyOwner {
admins[_admin] = false;
emit AdminRemoved(_admin);
}

/**
* @notice Stake the given amount for the given duration of time.
* @param amount The number of tokens to stake.
Expand Down Expand Up @@ -310,47 +284,6 @@ contract Staking is IStaking, WeightedStaking, ApprovalReceiver {
emit VestingTokensWithdrawn(vesting, receiver);
}

/**
* @notice Add vesting contract's code hash to a map of code hashes.
* @param vesting The address of Vesting contract.
* @dev We need it to use _isVestingContract() function instead of isContract()
*/
function addContractCodeHash(address vesting) public onlyAuthorized {
bytes32 codeHash = _getCodeHash(vesting);
vestingCodeHashes[codeHash] = true;
emit ContractCodeHashAdded(codeHash);
}

/**
* @notice Add vesting contract's code hash to a map of code hashes.
* @param vesting The address of Vesting contract.
* @dev We need it to use _isVestingContract() function instead of isContract()
*/
function removeContractCodeHash(address vesting) public onlyAuthorized {
bytes32 codeHash = _getCodeHash(vesting);
vestingCodeHashes[codeHash] = false;
emit ContractCodeHashRemoved(codeHash);
}

/**
* @notice Return flag whether message sender is a registered vesting contract.
*/
function _isVestingContract() internal view returns (bool) {
bytes32 codeHash = _getCodeHash(msg.sender);
return vestingCodeHashes[codeHash];
}

/**
* @notice Return hash of contract code
*/
function _getCodeHash(address _contract) internal view returns (bytes32) {
bytes32 codeHash;
assembly {
codeHash := extcodehash(_contract)
}
return codeHash;
}

/**
* @notice Send user' staked tokens to a receiver taking into account punishments.
* Sovryn encourages long-term commitment and thinking. When/if you unstake before
Expand Down
71 changes: 70 additions & 1 deletion contracts/governance/Staking/WeightedStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@ import "../../openzeppelin/Address.sol";
* */
contract WeightedStaking is Checkpoints {
using Address for address payable;

/**
* @dev Throws if called by any account other than the owner or admin.
*/
modifier onlyAuthorized() {
require(isOwner() || admins[msg.sender], "unauthorized");
_;
}

/************* TOTAL VOTING POWER COMPUTATION ************************/

/**
Expand Down Expand Up @@ -272,7 +281,7 @@ contract WeightedStaking is Checkpoints {
uint96 priorStake = _getPriorUserStakeByDate(account, date, blockNumber);
// @dev we need to modify function in order to workaround issue with Vesting.withdrawTokens:
// return 1 instead of 0 if message sender is a contract.
if (priorStake == 0 && msg.sender.isContract()) {
if (priorStake == 0 && _isVestingContract()) {
priorStake = 1;
}
return priorStake;
Expand Down Expand Up @@ -376,4 +385,64 @@ contract WeightedStaking is Checkpoints {
}
return date;
}

/**
* @notice Add account to ACL.
* @param _admin The addresses of the account to grant permissions.
* */
function addAdmin(address _admin) public onlyOwner {
admins[_admin] = true;
emit AdminAdded(_admin);
}

/**
* @notice Remove account from ACL.
* @param _admin The addresses of the account to revoke permissions.
* */
function removeAdmin(address _admin) public onlyOwner {
admins[_admin] = false;
emit AdminRemoved(_admin);
}

/**
* @notice Add vesting contract's code hash to a map of code hashes.
* @param vesting The address of Vesting contract.
* @dev We need it to use _isVestingContract() function instead of isContract()
*/
function addContractCodeHash(address vesting) public onlyAuthorized {
bytes32 codeHash = _getCodeHash(vesting);
vestingCodeHashes[codeHash] = true;
emit ContractCodeHashAdded(codeHash);
}

/**
* @notice Add vesting contract's code hash to a map of code hashes.
* @param vesting The address of Vesting contract.
* @dev We need it to use _isVestingContract() function instead of isContract()
*/
function removeContractCodeHash(address vesting) public onlyAuthorized {
bytes32 codeHash = _getCodeHash(vesting);
vestingCodeHashes[codeHash] = false;
emit ContractCodeHashRemoved(codeHash);
}

/**
* @notice Return flag whether message sender is a registered vesting contract.
*/
function _isVestingContract() internal view returns (bool) {
bytes32 codeHash = _getCodeHash(msg.sender);
return vestingCodeHashes[codeHash];
}

/**
* @notice Return hash of contract code
*/
function _getCodeHash(address _contract) internal view returns (bytes32) {
bytes32 codeHash;
assembly {
codeHash := extcodehash(_contract)
}
return codeHash;
}

}

0 comments on commit fdce225

Please sign in to comment.