From b8f9449a6e16a73b3b8927257622bb508fcabb4c Mon Sep 17 00:00:00 2001 From: r4bbit <445106+0x-r4bbit@users.noreply.github.com> Date: Wed, 25 Sep 2024 09:50:06 +0200 Subject: [PATCH] chore: make linter and formatter happy --- .gas-report | 43 ++++++++++++++++++++++++++++++++++++ .gas-snapshot | 2 ++ script/RewardsStreamer.s.sol | 4 +--- src/RewardsStreamer.sol | 24 ++++++++++---------- src/RewardsStreamerMP.sol | 24 ++++++++++---------- 5 files changed, 70 insertions(+), 27 deletions(-) create mode 100644 .gas-report create mode 100644 .gas-snapshot diff --git a/.gas-report b/.gas-report new file mode 100644 index 0000000..8b65511 --- /dev/null +++ b/.gas-report @@ -0,0 +1,43 @@ +| src/RewardsStreamer.sol:RewardsStreamer contract | | | | | | +|--------------------------------------------------|-----------------|--------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 690902 | 3154 | | | | | +| Function Name | min | avg | median | max | # calls | +| accountedRewards | 351 | 601 | 351 | 2351 | 8 | +| getUserInfo | 793 | 793 | 793 | 793 | 12 | +| rewardIndex | 350 | 600 | 350 | 2350 | 8 | +| stake | 85235 | 100736 | 105135 | 111838 | 3 | +| totalStaked | 351 | 351 | 351 | 351 | 8 | +| unstake | 110100 | 110106 | 110106 | 110112 | 2 | +| updateRewardIndex | 23374 | 45581 | 39585 | 73785 | 3 | + + +| src/RewardsStreamerMP.sol:RewardsStreamerMP contract | | | | | | +|------------------------------------------------------|-----------------|--------|--------|--------|---------| +| Deployment Cost | Deployment Size | | | | | +| 1096353 | 4939 | | | | | +| Function Name | min | avg | median | max | # calls | +| accountedRewards | 373 | 595 | 373 | 2373 | 9 | +| getUserInfo | 1553 | 1553 | 1553 | 1553 | 16 | +| potentialMP | 330 | 330 | 330 | 330 | 9 | +| rewardIndex | 373 | 595 | 373 | 2373 | 9 | +| stake | 167821 | 194716 | 187721 | 228608 | 3 | +| totalMP | 352 | 352 | 352 | 352 | 9 | +| totalStaked | 330 | 330 | 330 | 330 | 9 | +| unstake | 133268 | 133274 | 133274 | 133280 | 2 | +| updateGlobalState | 30008 | 52396 | 49622 | 80335 | 4 | + + +| test/mocks/MockToken.sol:MockToken contract | | | | | | +|---------------------------------------------|-----------------|-------|--------|-------|---------| +| Deployment Cost | Deployment Size | | | | | +| 639406 | 3369 | | | | | +| Function Name | min | avg | median | max | # calls | +| approve | 46346 | 46346 | 46346 | 46346 | 10 | +| balanceOf | 561 | 1143 | 561 | 2561 | 79 | +| mint | 51284 | 58124 | 51284 | 68384 | 10 | +| transfer | 34390 | 42940 | 42940 | 51490 | 4 | + + + + diff --git a/.gas-snapshot b/.gas-snapshot new file mode 100644 index 0000000..8bf8ac8 --- /dev/null +++ b/.gas-snapshot @@ -0,0 +1,2 @@ +RewardsStreamerMPTest:testStake() (gas: 1377536) +RewardsStreamerTest:testStake() (gas: 869874) \ No newline at end of file diff --git a/script/RewardsStreamer.s.sol b/script/RewardsStreamer.s.sol index e216301..4656952 100644 --- a/script/RewardsStreamer.s.sol +++ b/script/RewardsStreamer.s.sol @@ -1,14 +1,12 @@ // SPDX-License-Identifier: MIT pragma solidity ^0.8.26; -import { Script, console } from "forge-std/Script.sol"; +import { Script } from "forge-std/Script.sol"; import { RewardsStreamer } from "../src/RewardsStreamer.sol"; contract RewardsStreamerScript is Script { RewardsStreamer public rewardsStreamer; - function setUp() public { } - function run() public { vm.startBroadcast(); diff --git a/src/RewardsStreamer.sol b/src/RewardsStreamer.sol index d7302f9..19e3585 100644 --- a/src/RewardsStreamer.sol +++ b/src/RewardsStreamer.sol @@ -9,8 +9,8 @@ contract RewardsStreamer is ReentrancyGuard { error StakingManager__TransferFailed(); error StakingManager__InsufficientBalance(); - IERC20 public immutable stakingToken; - IERC20 public immutable rewardToken; + IERC20 public immutable STAKING_TOKEN; + IERC20 public immutable REWARD_TOKEN; uint256 public constant SCALE_FACTOR = 1e18; @@ -23,11 +23,11 @@ contract RewardsStreamer is ReentrancyGuard { uint256 userRewardIndex; } - mapping(address => UserInfo) public users; + mapping(address account => UserInfo data) public users; constructor(address _stakingToken, address _rewardToken) { - stakingToken = IERC20(_stakingToken); - rewardToken = IERC20(_rewardToken); + STAKING_TOKEN = IERC20(_stakingToken); + REWARD_TOKEN = IERC20(_rewardToken); } function stake(uint256 amount) external nonReentrant { @@ -43,7 +43,7 @@ contract RewardsStreamer is ReentrancyGuard { distributeRewards(msg.sender, userRewards); } - bool success = stakingToken.transferFrom(msg.sender, address(this), amount); + bool success = STAKING_TOKEN.transferFrom(msg.sender, address(this), amount); if (!success) { revert StakingManager__TransferFailed(); } @@ -69,7 +69,7 @@ contract RewardsStreamer is ReentrancyGuard { user.stakedBalance -= amount; totalStaked -= amount; - bool success = stakingToken.transfer(msg.sender, amount); + bool success = STAKING_TOKEN.transfer(msg.sender, amount); if (!success) { revert StakingManager__TransferFailed(); } @@ -82,7 +82,7 @@ contract RewardsStreamer is ReentrancyGuard { return; } - uint256 rewardBalance = rewardToken.balanceOf(address(this)); + uint256 rewardBalance = REWARD_TOKEN.balanceOf(address(this)); uint256 newRewards = rewardBalance > accountedRewards ? rewardBalance - accountedRewards : 0; if (newRewards > 0) { @@ -106,18 +106,18 @@ contract RewardsStreamer is ReentrancyGuard { // send the rewards and updates accountedRewards function distributeRewards(address to, uint256 amount) internal { - uint256 rewardBalance = rewardToken.balanceOf(address(this)); + uint256 rewardBalance = REWARD_TOKEN.balanceOf(address(this)); // If amount is higher than the contract's balance (for rounding error), transfer the balance. if (amount > rewardBalance) { amount = rewardBalance; } - bool success = rewardToken.transfer(to, amount); + accountedRewards -= amount; + + bool success = REWARD_TOKEN.transfer(to, amount); if (!success) { revert StakingManager__TransferFailed(); } - - accountedRewards -= amount; } function getUserInfo(address userAddress) public view returns (UserInfo memory) { diff --git a/src/RewardsStreamerMP.sol b/src/RewardsStreamerMP.sol index 9064f07..0d7ee1c 100644 --- a/src/RewardsStreamerMP.sol +++ b/src/RewardsStreamerMP.sol @@ -13,8 +13,8 @@ contract RewardsStreamerMP is ReentrancyGuard { error StakingManager__CannotRestakeWithLockedFunds(); error StakingManager__TokensAreLocked(); - IERC20 public immutable stakingToken; - IERC20 public immutable rewardToken; + IERC20 public immutable STAKING_TOKEN; + IERC20 public immutable REWARD_TOKEN; uint256 public constant SCALE_FACTOR = 1e18; uint256 public constant MP_RATE_PER_YEAR = 1e18; @@ -39,11 +39,11 @@ contract RewardsStreamerMP is ReentrancyGuard { uint256 lockUntil; } - mapping(address => UserInfo) public users; + mapping(address account => UserInfo data) public users; constructor(address _stakingToken, address _rewardToken) { - stakingToken = IERC20(_stakingToken); - rewardToken = IERC20(_rewardToken); + STAKING_TOKEN = IERC20(_stakingToken); + REWARD_TOKEN = IERC20(_rewardToken); lastMPUpdatedTime = block.timestamp; } @@ -69,7 +69,7 @@ contract RewardsStreamerMP is ReentrancyGuard { distributeRewards(msg.sender, userRewards); } - bool success = stakingToken.transferFrom(msg.sender, address(this), amount); + bool success = STAKING_TOKEN.transferFrom(msg.sender, address(this), amount); if (!success) { revert StakingManager__TransferFailed(); } @@ -131,7 +131,7 @@ contract RewardsStreamerMP is ReentrancyGuard { totalMP -= mpToReduce; potentialMP -= potentialMPToReduce; - bool success = stakingToken.transfer(msg.sender, amount); + bool success = STAKING_TOKEN.transfer(msg.sender, amount); if (!success) { revert StakingManager__TransferFailed(); } @@ -184,7 +184,7 @@ contract RewardsStreamerMP is ReentrancyGuard { return; } - uint256 rewardBalance = rewardToken.balanceOf(address(this)); + uint256 rewardBalance = REWARD_TOKEN.balanceOf(address(this)); uint256 newRewards = rewardBalance > accountedRewards ? rewardBalance - accountedRewards : 0; if (newRewards > 0) { @@ -226,18 +226,18 @@ contract RewardsStreamerMP is ReentrancyGuard { } function distributeRewards(address to, uint256 amount) internal { - uint256 rewardBalance = rewardToken.balanceOf(address(this)); + uint256 rewardBalance = REWARD_TOKEN.balanceOf(address(this)); // If amount is higher than the contract's balance (for rounding error), transfer the balance. if (amount > rewardBalance) { amount = rewardBalance; } - bool success = rewardToken.transfer(to, amount); + accountedRewards -= amount; + + bool success = REWARD_TOKEN.transfer(to, amount); if (!success) { revert StakingManager__TransferFailed(); } - - accountedRewards -= amount; } function getStakedBalance(address userAddress) external view returns (uint256) {