Skip to content

Commit

Permalink
chore: make linter and formatter happy
Browse files Browse the repository at this point in the history
  • Loading branch information
0x-r4bbit committed Sep 25, 2024
1 parent 4ef7562 commit b8f9449
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 27 deletions.
43 changes: 43 additions & 0 deletions .gas-report
Original file line number Diff line number Diff line change
@@ -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 |




2 changes: 2 additions & 0 deletions .gas-snapshot
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
RewardsStreamerMPTest:testStake() (gas: 1377536)
RewardsStreamerTest:testStake() (gas: 869874)
4 changes: 1 addition & 3 deletions script/RewardsStreamer.s.sol
Original file line number Diff line number Diff line change
@@ -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();

Expand Down
24 changes: 12 additions & 12 deletions src/RewardsStreamer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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();
}
Expand All @@ -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();
}
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down
24 changes: 12 additions & 12 deletions src/RewardsStreamerMP.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
}

Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit b8f9449

Please sign in to comment.