Skip to content

merge staking fixes #5

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .npmignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@ node_modules/
.env.example
.github/
hardhat.config.ts
tsconfig.json
tsconfig.json
contracts/TestERC20.sol
contracts/WSYS.sol
contracts/Distributor.sol
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ TimeLock: `0x2C4ce5DcE61b22d9eed75136CD5C8bbd788A243B`

GovernorAlpha: `0x633Bdeb5D4b5f93933833A692e230a7d48fC2d77`

PegasysStaking: `0x83dd2F2bFd808b4618BF1d6c7d09714e66EE3014`
PegasysStaking: `0x1e6dc4CB2F98817A0E3D850Bba7aEfa3CFcdE55F`

### Syscoin Tanenbaum Testnet:

Expand Down
25 changes: 14 additions & 11 deletions contracts/earn/FeeCollector.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ contract FeeCollector is Ownable {
EnumerableSet.AddressSet private _isAuth;

modifier onlyAuth() {
require(_isAuth.contains(_msgSender()), "MoneyMaker: FORBIDDEN");
require(_isAuth.contains(_msgSender()), "FeeCollector: FORBIDDEN");
_;
}

Expand Down Expand Up @@ -98,15 +98,18 @@ contract FeeCollector is Ownable {
function addAuth(address _auth) external onlyOwner {
require(
_isAuth.add(_auth),
"MoneyMaker: Address is already authorized"
"FeeCollector: Address is already authorized"
);
emit AddAuthorizedAddress(_auth);
}

/// @notice Remove a user of authorized addresses
/// @param _auth The address to remove
function removeAuth(address _auth) external onlyOwner {
require(_isAuth.remove(_auth), "MoneyMaker: Address is not authorized");
require(
_isAuth.remove(_auth),
"FeeCollector: Address is not authorized"
);
emit RemoveAuthorizedAddress(_auth);
}

Expand All @@ -130,7 +133,7 @@ contract FeeCollector is Ownable {
// Checks
require(
token != tokenTo && token != wsys && token != bridge,
"MoneyMaker: Invalid bridge"
"FeeCollector: Invalid bridge"
);

// Effects
Expand Down Expand Up @@ -185,7 +188,7 @@ contract FeeCollector is Ownable {
// C6: It's not a fool proof solution, but it prevents flash loans, so here it's ok to use tx.origin
modifier onlyEOA() {
// Try to make flash-loan exploit harder to do by only allowing externally owned addresses.
require(_msgSender() == tx.origin, "MoneyMaker: must use EOA");
require(_msgSender() == tx.origin, "FeeCollector: must use EOA");
_;
}

Expand All @@ -201,7 +204,7 @@ contract FeeCollector is Ownable {
) external onlyEOA onlyAuth {
require(
slippage < 5_000,
"MoneyMaker: slippage needs to be lower than 50%"
"FeeCollector: slippage needs to be lower than 50%"
);
_convert(token0, token1, slippage);
}
Expand All @@ -219,11 +222,11 @@ contract FeeCollector is Ownable {
// TODO: This can be optimized a fair bit, but this is safer and simpler for now
require(
slippage < 5_000,
"MoneyMaker: slippage needs to be lower than 50%"
"FeeCollector: slippage needs to be lower than 50%"
);
require(
token0.length == token1.length,
"MoneyMaker: arrays length don't match"
"FeeCollector: arrays length don't match"
);

uint256 len = token0.length;
Expand Down Expand Up @@ -251,7 +254,7 @@ contract FeeCollector is Ownable {
amount1 = 0;
} else {
IPegasysPair pair = IPegasysPair(factory.getPair(token0, token1));
require(address(pair) != address(0), "MoneyMaker: Invalid pair");
require(address(pair) != address(0), "FeeCollector: Invalid pair");

IERC20(address(pair)).safeTransfer(
address(pair),
Expand Down Expand Up @@ -384,7 +387,7 @@ contract FeeCollector is Ownable {
// Checks
// X1 - X5: OK
IPegasysPair pair = IPegasysPair(factory.getPair(fromToken, toToken));
require(address(pair) != address(0), "MoneyMaker: Cannot convert");
require(address(pair) != address(0), "FeeCollector: Cannot convert");

(uint256 reserve0, uint256 reserve1, ) = pair.getReserves();
(uint256 reserveInput, uint256 reserveOutput) = fromToken ==
Expand Down Expand Up @@ -413,7 +416,7 @@ contract FeeCollector is Ownable {
reserveOutput,
reserveInput
) >= amountInput.mul(rest).mul(rest).div(100_000_000),
"MoneyMaker: Slippage caught"
"FeeCollector: Slippage caught"
);
}

Expand Down
67 changes: 65 additions & 2 deletions contracts/earn/PegasysStaking.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import "openzeppelin-contracts-legacy/math/SafeMath.sol";
import "openzeppelin-contracts-legacy/token/ERC20/SafeERC20.sol";
import "openzeppelin-contracts-legacy/access/Ownable.sol";
import "openzeppelin-contracts-legacy/token/ERC20/IERC20.sol";
import "../pegasys-core/interfaces/IPegasysERC20.sol";

/**
* @title Pegasys Staking
Expand Down Expand Up @@ -179,6 +180,68 @@ contract PegasysStaking is Ownable {
emit Deposit(_msgSender(), _amountMinusFee, _fee);
}

/**
* @notice Deposit PSYS for reward token allocation with permit
* @param _amount The amount of PSYS to deposit
*/
function depositWithPermit(
uint256 _amount,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external {
UserInfo storage user = userInfo[_msgSender()];

uint256 _fee = _amount.mul(depositFeePercent).div(
DEPOSIT_FEE_PERCENT_PRECISION
);
uint256 _amountMinusFee = _amount.sub(_fee);

uint256 _previousAmount = user.amount;
uint256 _newAmount = user.amount.add(_amountMinusFee);
user.amount = _newAmount;

uint256 _len = rewardTokens.length;
for (uint256 i; i < _len; i++) {
IERC20 _token = rewardTokens[i];
updateReward(_token);

uint256 _previousRewardDebt = user.rewardDebt[_token];
user.rewardDebt[_token] = _newAmount
.mul(accRewardPerShare[_token])
.div(ACC_REWARD_PER_SHARE_PRECISION);

if (_previousAmount != 0) {
uint256 _pending = _previousAmount
.mul(accRewardPerShare[_token])
.div(ACC_REWARD_PER_SHARE_PRECISION)
.sub(_previousRewardDebt);
if (_pending != 0) {
safeTokenTransfer(_token, _msgSender(), _pending);
emit ClaimReward(_msgSender(), address(_token), _pending);
}
}
}

// permit
IPegasysERC20(address(psys)).permit(
msg.sender,
address(this),
_amount,
deadline,
v,
r,
s
);

internalPsysBalance = internalPsysBalance.add(_amountMinusFee);
psys.safeTransferFrom(_msgSender(), address(this), _amount);
psys.safeTransfer(feeReceiver, _fee);

emit Deposit(_msgSender(), _amountMinusFee, _fee);
}

/**
* @notice Get user info
* @param _user The address of the user
Expand Down Expand Up @@ -265,8 +328,8 @@ contract PegasysStaking is Ownable {
uint256 _depositFeePercent
) external onlyOwner {
require(
_depositFeePercent <= 5e17,
"PegasysStaking: deposit fee can't be greater than 50%"
_depositFeePercent <= 3e16,
"PegasysStaking: deposit fee can't be greater than 3%"
);
uint256 oldFee = depositFeePercent;
depositFeePercent = _depositFeePercent;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@pollum-io/pegasys-protocol",
"version": "0.0.16",
"version": "0.0.17",
"description": "Contracts for the Pegasys Dex.",
"main": "index.js",
"keywords": [
Expand Down