Skip to content

Commit f67d823

Browse files
committed
fix decimals issue with price feed
remove console logs
1 parent b50e243 commit f67d823

File tree

2 files changed

+9
-7
lines changed

2 files changed

+9
-7
lines changed

src/FixedStakingRewards.sol

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import "openzeppelin-contracts/contracts/access/Ownable.sol";
99
// Inheritance
1010
import "./interfaces/IStakingRewards.sol";
1111
import "./interfaces/IChainlinkAggregator.sol";
12-
import {console} from "forge-std/console.sol";
1312

1413
/* ========== CUSTOM ERRORS ========== */
1514

@@ -27,6 +26,7 @@ contract FixedStakingRewards is IStakingRewards, ERC20, ReentrancyGuard, Ownable
2726
IERC20 immutable public rewardsToken;
2827
IERC20 immutable public stakingToken;
2928
IChainlinkAggregator immutable public rewardsTokenRateAggregator;
29+
uint256 public immutable rewardsTokenRateDecimals;
3030
uint256 public targetRewardApy = 0;
3131
uint256 public rewardRate = 0;
3232
uint256 public lastUpdateTime;
@@ -48,6 +48,7 @@ contract FixedStakingRewards is IStakingRewards, ERC20, ReentrancyGuard, Ownable
4848
rewardsToken = IERC20(_rewardsToken);
4949
stakingToken = IERC20(_stakingToken);
5050
rewardsTokenRateAggregator = IChainlinkAggregator(_rewardsTokenRateAggregator);
51+
rewardsTokenRateDecimals = rewardsTokenRateAggregator.decimals();
5152
rewardsAvailableDate = block.timestamp + 86400 * 365;
5253
}
5354

@@ -156,7 +157,7 @@ contract FixedStakingRewards is IStakingRewards, ERC20, ReentrancyGuard, Ownable
156157
/* ========== INTERNAL FUNCTIONS ========== */
157158
function _rebalance() internal {
158159
(, int256 currentRewardTokenRate, , , ) = rewardsTokenRateAggregator.latestRoundData();
159-
rewardRate = targetRewardApy * 1e18 / uint256(currentRewardTokenRate) / 365 days;
160+
rewardRate = targetRewardApy * 1e18 / (uint256(currentRewardTokenRate) * 10 ** (18 - rewardsTokenRateDecimals)) / 365 days;
160161
}
161162

162163
/* ========== MODIFIERS ========== */

test/FixedStakingRewards.t.sol

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: UNLICENSED
22
pragma solidity ^0.8.13;
33

4-
import {Test, console} from "forge-std/Test.sol";
4+
import {Test} from "forge-std/Test.sol";
55
import {FixedStakingRewards} from "../src/FixedStakingRewards.sol";
66
import {IERC20} from "@openzeppelin/contracts/token/ERC20/IERC20.sol";
77
import {ERC20, IERC20Errors} from "@openzeppelin/contracts/token/ERC20/ERC20.sol";
@@ -185,7 +185,8 @@ contract FixedStakingRewardsTest is Test {
185185
mockAggregator = new MockChainlinkAggregator();
186186

187187
// assuming a 50c reward token rate
188-
mockAggregator.setLatestAnswer(1e18 / 2, block.timestamp);
188+
mockAggregator.setDecimals(8);
189+
mockAggregator.setLatestAnswer(1e8 / 2, block.timestamp);
189190

190191
// Deploy staking contract
191192
stakingRewards = new FixedStakingRewards(
@@ -711,7 +712,7 @@ contract FixedStakingRewardsTest is Test {
711712
assertEq(stakingRewards.rewardRate(), expectedRewardRate);
712713

713714
// Change aggregator rate to 0.25 (token price doubled)
714-
mockAggregator.setLatestAnswer(1e18 / 4, block.timestamp);
715+
mockAggregator.setLatestAnswer(1e8 / 4, block.timestamp);
715716

716717
// Call rebalance
717718
stakingRewards.rebalance();
@@ -741,7 +742,7 @@ contract FixedStakingRewardsTest is Test {
741742
assertGt(earnedBefore, 0);
742743

743744
// Change the aggregator rate and rebalance
744-
mockAggregator.setLatestAnswer(1e18, block.timestamp); // Rate changes from 0.5 to 1.0
745+
mockAggregator.setLatestAnswer(1e8, block.timestamp); // Rate changes from 0.5 to 1.0
745746
stakingRewards.rebalance();
746747

747748
// Rewards should be preserved due to updateReward modifier
@@ -754,7 +755,7 @@ contract FixedStakingRewardsTest is Test {
754755
stakingRewards.setRewardYieldForYear(0);
755756

756757
// Change aggregator rate
757-
mockAggregator.setLatestAnswer(1e18, block.timestamp);
758+
mockAggregator.setLatestAnswer(1e8, block.timestamp);
758759

759760
// Rebalance should result in 0 reward rate
760761
stakingRewards.rebalance();

0 commit comments

Comments
 (0)