Skip to content

Commit

Permalink
Merge pull request #18 from GenerationSoftware/gen-291-c4-issue-383-q…
Browse files Browse the repository at this point in the history
…a-report

Gas optimizations
  • Loading branch information
trmid authored Sep 11, 2023
2 parents 7b62fb8 + e119aa6 commit fe34039
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 56 deletions.
7 changes: 4 additions & 3 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
{
"printWidth": 100,
"tabWidth": 2,
"plugins": ["prettier-plugin-solidity"],
"overrides": [
{
"files": "*.sol",
"options": {
"bracketSpacing": true
"bracketSpacing": true,
"printWidth": 100,
"tabWidth": 2
}
}
]
Expand Down
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@
},
"devDependencies": {
"husky": "8.0.3",
"lint-staged": "13.1.0",
"prettier": "2.8.3",
"prettier-plugin-solidity": "1.1.1",
"solhint": "3.3.8",
"lint-staged": "14.0.1",
"prettier": "3.0.3",
"prettier-plugin-solidity": "1.1.3",
"solhint": "3.6.2",
"solhint-plugin-prettier": "0.0.5"
},
"files": [
Expand Down
66 changes: 25 additions & 41 deletions src/Claimer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,11 @@ error PrizePoolZeroAddress();
/// @author PoolTogether Inc. Team
/// @notice This contract uses a variable rate gradual dutch auction to inventivize prize claims on behalf of others
contract Claimer is Multicall {

/// @notice Emitted when a prize has already been claimed
/// @param winner The winner of the prize
/// @param tier The prize tier
/// @param prizeIndex The prize index
event AlreadyClaimed(
address winner,
uint8 tier,
uint32 prizeIndex
);
event AlreadyClaimed(address winner, uint8 tier, uint32 prizeIndex);

/// @notice Emitted when a claim reverts
/// @param vault The vault for which the claim failed
Expand Down Expand Up @@ -120,20 +115,15 @@ contract Claimer is Multicall {
revert ClaimArraySizeMismatch(_winners.length, _prizeIndices.length);
}

uint96 feePerClaim = SafeCast.toUint96(_computeFeePerClaimForBatch(_tier, _winners, _prizeIndices));
uint96 feePerClaim = SafeCast.toUint96(
_computeFeePerClaimForBatch(_tier, _winners, _prizeIndices)
);

if (feePerClaim < _minVrgdaFeePerClaim) {
revert VrgdaClaimFeeBelowMin(_minVrgdaFeePerClaim, feePerClaim);
}

return feePerClaim * _claim(
_vault,
_tier,
_winners,
_prizeIndices,
_feeRecipient,
feePerClaim
);
return feePerClaim * _claim(_vault, _tier, _winners, _prizeIndices, _feeRecipient, feePerClaim);
}

/// @notice Computes the fee per claim given a batch of winners and prize indices
Expand All @@ -152,11 +142,7 @@ contract Claimer is Multicall {
claimCount += _prizeIndices[i].length;
}

return _computeFeePerClaim(
_computeMaxFee(_tier),
claimCount,
prizePool.claimCount()
);
return _computeFeePerClaim(_computeMaxFee(_tier), claimCount, prizePool.claimCount());
}

/// @notice Claims prizes for a batch of winners and prize indices
Expand All @@ -176,16 +162,15 @@ contract Claimer is Multicall {
uint96 _feePerClaim
) internal returns (uint256) {
uint256 actualClaimCount;
uint256 prizeIndicesLength;

// `_winners.length` is not cached cause via-ir would need to be used
for (uint256 w = 0; w < _winners.length; w++) {
uint256 prizeIndicesLength = _prizeIndices[w].length;
prizeIndicesLength = _prizeIndices[w].length;
for (uint256 p = 0; p < prizeIndicesLength; p++) {
try _vault.claimPrize(
_winners[w],
_tier,
_prizeIndices[w][p],
_feePerClaim,
_feeRecipient
) returns (uint256 prizeSize) {
try
_vault.claimPrize(_winners[w], _tier, _prizeIndices[w][p], _feePerClaim, _feeRecipient)
returns (uint256 prizeSize) {
if (0 != prizeSize) {
actualClaimCount++;
} else {
Expand All @@ -196,6 +181,7 @@ contract Claimer is Multicall {
}
}
}

return actualClaimCount;
}

Expand All @@ -205,11 +191,7 @@ contract Claimer is Multicall {
/// @return The total fees for those claims
function computeTotalFees(uint8 _tier, uint256 _claimCount) external view returns (uint256) {
return
_computeFeePerClaim(
_computeMaxFee(_tier),
_claimCount,
prizePool.claimCount()
) * _claimCount;
_computeFeePerClaim(_computeMaxFee(_tier), _claimCount, prizePool.claimCount()) * _claimCount;
}

/// @notice Computes the total fees for the given number of claims if a number of claims have already been made.
Expand All @@ -222,19 +204,18 @@ contract Claimer is Multicall {
uint256 _claimCount,
uint256 _claimedCount
) external view returns (uint256) {
return
_computeFeePerClaim(
_computeMaxFee(_tier),
_claimCount,
_claimedCount
) * _claimCount;
return _computeFeePerClaim(_computeMaxFee(_tier), _claimCount, _claimedCount) * _claimCount;
}

/// @notice Computes the fees for several claims.
/// @param _maxFee the maximum fee that can be charged
/// @param _claimCount the number of claims to check
/// @return The fees for the claims
function computeFeePerClaim(uint256 _maxFee, uint256 _claimCount) external view returns (uint256) {
function computeFeePerClaim(uint256 _maxFee, uint256 _claimCount)
external
view
returns (uint256)
{
return _computeFeePerClaim(_maxFee, _claimCount, prizePool.claimCount());
}

Expand Down Expand Up @@ -283,7 +264,10 @@ contract Claimer is Multicall {
/// @param _tier The tier to compute the max fee for
/// @return The maximum fee that will be charged for a prize claim for the given tier
function _computeMaxFee(uint8 _tier) internal view returns (uint256) {
return UD60x18.unwrap(maxFeePortionOfPrize.intoUD60x18().mul(UD60x18.wrap(prizePool.getTierPrizeSize(_tier))));
return
UD60x18.unwrap(
maxFeePortionOfPrize.intoUD60x18().mul(UD60x18.wrap(prizePool.getTierPrizeSize(_tier)))
);
}

/// @notice Computes the fee for the next claim.
Expand Down
26 changes: 18 additions & 8 deletions src/libraries/LinearVRGDALib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ library LinearVRGDALib {
/// @param _count The total number of claims
/// @param _durationSeconds The duration over which claiming should occur
/// @return The target number of claims per second
function getPerTimeUnit(
uint256 _count,
uint256 _durationSeconds
) internal pure returns (SD59x18) {
function getPerTimeUnit(uint256 _count, uint256 _durationSeconds)
internal
pure
returns (SD59x18)
{
return convert(int256(_count)).div(convert(int256(_durationSeconds)));
}

Expand Down Expand Up @@ -105,9 +106,18 @@ library LinearVRGDALib {
uint256 _maxFee,
uint256 _time
) internal pure returns (UD2x18) {
int256 div = wadDiv(SafeCast.toInt256(_maxFee), SafeCast.toInt256(_minFee));
int256 ln = wadLn(div);
int256 maxDiv = wadDiv(ln, SafeCast.toInt256(_time));
return ud2x18(SafeCast.toUint64(uint256(wadExp(maxDiv / 1e18))));
return
ud2x18(
SafeCast.toUint64(
uint256(
wadExp(
wadDiv(
wadLn(wadDiv(SafeCast.toInt256(_maxFee), SafeCast.toInt256(_minFee))),
SafeCast.toInt256(_time)
) / 1e18
)
)
)
);
}
}

0 comments on commit fe34039

Please sign in to comment.