Skip to content

Commit

Permalink
feat: gear staking tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Van0k committed Mar 27, 2023
1 parent c5ae0e8 commit acf067c
Show file tree
Hide file tree
Showing 5 changed files with 429 additions and 139 deletions.
9 changes: 9 additions & 0 deletions contracts/interfaces/IGearStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ interface IGearStakingEvents {

/// @dev Emits when the user claims a mature withdrawal from staked GEAR
event GearWithdrawalClaimed(address indexed user, address to, uint256 amount);

/// @dev Emits when the configurator adds or removes a voting contract
event VotingContractStatusUpdated(address indexed votingContract, VotingContractStatus status);
}

interface IGearStaking is IGearStakingEvents, IGearStakingExceptions, IVersion {
Expand Down Expand Up @@ -93,6 +96,12 @@ interface IGearStaking is IGearStakingEvents, IGearStakingExceptions, IVersion {
/// @dev The amount available to the user for voting or withdrawal
function availableBalance(address user) external view returns (uint256);

/// @dev Returns the amounts withdrawable now and over the next 4 epochs
function getWithdrawableAmounts(address user)
external
view
returns (uint256 withdrawableNow, uint256[4] memory withdrawableInEpochs);

/// @dev Mapping of address to their status as allowed voting contract
function allowedVotingContract(address c) external view returns (VotingContractStatus);
}
46 changes: 46 additions & 0 deletions contracts/support/GearStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,50 @@ contract GearStaking is ACLNonReentrantTrait, IGearStaking {
return uint256(voteLockData[user].available);
}

/// @dev Returns the amounts withdrawable now and over the next 4 epochs
function getWithdrawableAmounts(address user)
external
view
returns (uint256 withdrawableNow, uint256[4] memory withdrawableInEpochs)
{
uint16 epochNow = getCurrentEpoch();

WithdrawalData memory wd = withdrawalData[user];

if (epochNow > wd.epochLU) {
if (
wd.withdrawalsPerEpoch[0] + wd.withdrawalsPerEpoch[1] + wd.withdrawalsPerEpoch[2]
+ wd.withdrawalsPerEpoch[3] > 0
) {
uint16 epochDiff = epochNow - wd.epochLU;

for (uint256 i = 0; i < 4;) {
if (i < epochDiff) {
withdrawableNow += wd.withdrawalsPerEpoch[i];
}

if (epochDiff < 4 && i < 4 - epochDiff) {
withdrawableInEpochs[i] = wd.withdrawalsPerEpoch[i + epochDiff];
} else {
withdrawableInEpochs[i] = 0;
}

unchecked {
++i;
}
}
}
} else {
for (uint256 i = 0; i < 4;) {
withdrawableInEpochs[i] = uint256(wd.withdrawalsPerEpoch[i]);

unchecked {
++i;
}
}
}
}

/// @dev Deposits an amount of GEAR into staked GEAR. Optionally, performs a sequence of vote changes according to
/// the passed `votes` array
/// @param amount Amount of GEAR to deposit into staked GEAR
Expand Down Expand Up @@ -227,5 +271,7 @@ contract GearStaking is ACLNonReentrantTrait, IGearStaking {
/// * UNVOTE_ONLY - can only unvote
function setVotingContractStatus(address votingContract, VotingContractStatus status) external configuratorOnly {
allowedVotingContract[votingContract] = status;

emit VotingContractStatusUpdated(votingContract, status);
}
}
6 changes: 6 additions & 0 deletions contracts/test/mocks/core/AddressProviderACLMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ contract AddressProviderACLMock {

address public owner;

address public getGearToken;

constructor() {
getACL = address(this);
getPriceOracle = address(this);
Expand All @@ -32,5 +34,9 @@ contract AddressProviderACLMock {
priceFeeds[token] = feed;
}

function setGearToken(address gearToken) external {
getGearToken = gearToken;
}

receive() external payable {}
}
Loading

0 comments on commit acf067c

Please sign in to comment.