Skip to content

Commit

Permalink
fixed: set solc version
Browse files Browse the repository at this point in the history
  • Loading branch information
Iam0TI committed Oct 9, 2024
1 parent db49c8e commit f61bb8b
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 71 deletions.
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
src = "src"
out = "out"
libs = ["lib"]
solc_version = "0.8.27"

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options

Expand Down
26 changes: 13 additions & 13 deletions script/Counter.s.sol
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
// // SPDX-License-Identifier: UNLICENSED
// pragma solidity ^0.8.13;

import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
// import {Script, console} from "forge-std/Script.sol";
// import {Counter} from "../src/Counter.sol";

contract CounterScript is Script {
Counter public counter;
// contract CounterScript is Script {
// Counter public counter;

function setUp() public {}
// function setUp() public {}

function run() public {
vm.startBroadcast();
// function run() public {
// vm.startBroadcast();

counter = new Counter();
// counter = new Counter();

vm.stopBroadcast();
}
}
// vm.stopBroadcast();
// }
// }
13 changes: 13 additions & 0 deletions src/Factory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;


import "./TwarStaking.sol";

contract TwarStakingFactory {
// constructor() {

// }
mapping (address tokenA => mapping (address tokenB => address pool)) public pair;

}
51 changes: 28 additions & 23 deletions src/TwarStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ contract TwarStaking {
uint256 public rewardFinishTime;
// Minimum of block.timestamp and rewardFinishTime
uint256 public lastRewardUpdatedTIme;
// Reward to be paid per second
// Reward to be paid per second
uint256 public rewardRate;
// Amount of Staking tokens staked
uint256 public totalAmountStaked;
Expand All @@ -40,13 +40,16 @@ contract TwarStaking {
rewardToken = IERC20(_rewardToken);
}

modifier updateReward(address _account) {

rewardPerTokenStored = TwarStakingLib.rewardPerToken(totalAmountStaked,rewardPerTokenStored,rewardRate,lastTimeForReward(),lastRewardUpdatedTIme);
modifier updateReward(address _account) {
rewardPerTokenStored = TwarStakingLib.rewardPerToken(
totalAmountStaked, rewardPerTokenStored, rewardRate, lastTimeForReward(), lastRewardUpdatedTIme
);
lastRewardUpdatedTIme = lastTimeForReward();

if (_account != address(0)) {
rewards[_account] = TwarStakingLib.earned(usersBalance[_account],rewardPerTokenStored,userRewardPerTokenPaid[_account],rewards[_account]);
rewards[_account] = TwarStakingLib.earned(
usersBalance[_account], rewardPerTokenStored, userRewardPerTokenPaid[_account], rewards[_account]
);
userRewardPerTokenPaid[_account] = rewardPerTokenStored;
}

Expand All @@ -68,7 +71,7 @@ contract TwarStaking {
totalAmountStaked += _amount;
}

function withdraw(uint256 _amount) public updateReward(msg.sender){
function withdraw(uint256 _amount) public updateReward(msg.sender) {
require(_amount > 0, TwarStakingError.Withdraw_ZeroAmount());

usersBalance[msg.sender] -= _amount;
Expand All @@ -77,8 +80,8 @@ contract TwarStaking {
stakingToken.transfer(msg.sender, _amount);
}

function withdrawRewards() public updateReward(msg.sender) returns(uint256 reward) {
reward = rewards[msg.sender];
function withdrawRewards() public updateReward(msg.sender) returns (uint256 reward) {
reward = rewards[msg.sender];

require(reward > 0, TwarStakingError.Reward_ZeroAmount());

Expand All @@ -89,27 +92,27 @@ contract TwarStaking {
}

function exit() public {
uint256 reward = withdrawRewards();
uint256 reward = withdrawRewards();
withdraw(usersBalance[msg.sender]);
emit TwarStakingEvent.ExitPool(msg.sender,usersBalance[msg.sender],reward);
emit TwarStakingEvent.ExitPool(msg.sender, usersBalance[msg.sender], reward);
}

function updateRewardRate(uint256 _poolReward) external onlyOwner updateReward(address(0)) {
function updateRewardRate(uint256 _poolReward) external onlyOwner updateReward(address(0)) {
require(rewardDuraton > 0, TwarStakingError.DurationisZero());
rewardToken.transferFrom(msg.sender, address(this), _poolReward);
// for case when rewardFinishTime is 0 or as ended
if (block.timestamp > rewardFinishTime) {
rewardRate = _poolReward / rewardDuraton;
if(rewardFinishTime==0) emit TwarStakingEvent.StakingStarted(msg.sender,_poolReward,rewardDuraton);
if (rewardFinishTime == 0) emit TwarStakingEvent.StakingStarted(msg.sender, _poolReward, rewardDuraton);
}
// if a staking period is till active
else {
uint256 remainingPeriod = rewardFinishTime - block.timestamp;
uint256 remainingReward = rewardRate * remainingPeriod ;
uint256 remainingPeriod = rewardFinishTime - block.timestamp;
uint256 remainingReward = rewardRate * remainingPeriod;

rewardRate = (remainingReward + _poolReward) / rewardDuraton;

emit TwarStakingEvent.StakingRewardIncreased(msg.sender,rewardRate,remainingPeriod);
emit TwarStakingEvent.StakingRewardIncreased(msg.sender, rewardRate, remainingPeriod);
}

//Ensure the provided reward amount is not more than the balance in the contract.
Expand All @@ -119,7 +122,6 @@ contract TwarStaking {

rewardFinishTime = rewardDuraton + block.timestamp;
lastRewardUpdatedTIme = block.timestamp;

}

function setRewardDuraton(uint256 _duration) external onlyOwner {
Expand All @@ -130,11 +132,15 @@ contract TwarStaking {
rewardDuraton = _duration;
}

function earned (address _account) external view returns (uint256 earnings){
uint256 rewardPerToken = TwarStakingLib.rewardPerToken(totalAmountStaked,rewardPerTokenStored,rewardRate,lastTimeForReward(),lastRewardUpdatedTIme);

earnings = TwarStakingLib.earned(usersBalance[_account],rewardPerToken,userRewardPerTokenPaid[_account],rewards[_account]);
}
function earned(address _account) external view returns (uint256 earnings) {
uint256 rewardPerToken = TwarStakingLib.rewardPerToken(
totalAmountStaked, rewardPerTokenStored, rewardRate, lastTimeForReward(), lastRewardUpdatedTIme
);

earnings = TwarStakingLib.earned(
usersBalance[_account], rewardPerToken, userRewardPerTokenPaid[_account], rewards[_account]
);
}
// function earned(address _account) public view returns (uint256) {

// return ((usersBalance[_account] * (rewardPerToken() - userRewardPerTokenPaid[_account])) / 1e18)
Expand All @@ -150,9 +156,8 @@ function earned (address _account) external view returns (uint256 earnings){
// rewardPerTokenStored + (rewardRate * (lastTimeForReward()- lastRewardUpdatedTIme) * 1e18)/totalAmountStaked;
// }
//}


function lastTimeForReward() public view returns (uint256) {
function lastTimeForReward() public view returns (uint256) {
return _min(rewardFinishTime, block.timestamp);
}

Expand Down
28 changes: 12 additions & 16 deletions src/utils.sol
Original file line number Diff line number Diff line change
@@ -1,40 +1,36 @@


// SPDX-License-Identifier: MIT
pragma solidity 0.8.27;

library TwarStakingLib {

// The minimum function to return the smaller of two values
function _min(uint256 x, uint256 y) internal pure returns (uint256) {
return x <= y ? x : y;
}

// The earned function for calculating rewards
function earned(
uint256 userBalance,
uint256 rewardPerTokenStored,
uint256 userRewardPerTokenPaid,

uint256 rewards
) internal pure returns (uint256) {
function earned(uint256 userBalance, uint256 rewardPerTokenStored, uint256 userRewardPerTokenPaid, uint256 rewards)
internal
pure
returns (uint256)
{
return ((userBalance * (rewardPerTokenStored - userRewardPerTokenPaid)) / 1e18) + rewards;
}

// rewardpertoken is 0 when totalAmountStaked is 0 and when totalAmountStaked is not 0
// rewardpertoken is 0 when totalAmountStaked is 0 and when totalAmountStaked is not 0
// then rewardpertoken = RJ = RJ0 + R/T (J-J0) where J is in seconds
// The rewardPerToken function to calculate the reward rate per token
function rewardPerToken(
uint256 totalAmountStaked,
uint256 rewardPerTokenStored,
uint256 rewardRate,
uint256 lastTimeForReward,
uint256 totalAmountStaked,
uint256 rewardPerTokenStored,
uint256 rewardRate,
uint256 lastTimeForReward,
uint256 lastRewardUpdatedTIme
) internal pure returns (uint256) {
if (totalAmountStaked == 0) {
return rewardPerTokenStored;
} else {
return rewardPerTokenStored + (rewardRate * (lastTimeForReward - lastRewardUpdatedTIme) * 1e18) / totalAmountStaked;
return rewardPerTokenStored
+ (rewardRate * (lastTimeForReward - lastRewardUpdatedTIme) * 1e18) / totalAmountStaked;
}
}
}
Expand Down
38 changes: 19 additions & 19 deletions test/Counter.t.sol
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
// // SPDX-License-Identifier: UNLICENSED
// pragma solidity ^0.8.13;

import {Test, console} from "forge-std/Test.sol";
import {Counter} from "../src/Counter.sol";
// import {Test, console} from "forge-std/Test.sol";
// import {Counter} from "../src/Counter.sol";

contract CounterTest is Test {
Counter public counter;
// contract CounterTest is Test {
// Counter public counter;

function setUp() public {
counter = new Counter();
counter.setNumber(0);
}
// function setUp() public {
// counter = new Counter();
// counter.setNumber(0);
// }

function test_Increment() public {
counter.increment();
assertEq(counter.number(), 1);
}
// function test_Increment() public {
// counter.increment();
// assertEq(counter.number(), 1);
// }

function testFuzz_SetNumber(uint256 x) public {
counter.setNumber(x);
assertEq(counter.number(), x);
}
}
// function testFuzz_SetNumber(uint256 x) public {
// counter.setNumber(x);
// assertEq(counter.number(), x);
// }
// }

0 comments on commit f61bb8b

Please sign in to comment.