Skip to content

Commit

Permalink
Merge pull request #98 from The-Poolz/fee-issue
Browse files Browse the repository at this point in the history
made PayFee public
  • Loading branch information
ashwinarora authored Apr 19, 2024
2 parents 304e86a + 816a620 commit a36ba42
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 100 deletions.
63 changes: 63 additions & 0 deletions contracts/Fee/FeeBaseHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../ERC20Helper.sol";
import "./WhiteListHelper.sol";

abstract contract FeeBaseHelper is ERC20Helper, WhiteListHelper {
event TransferInETH(uint Amount, address From);
event NewFeeAmount(uint NewFeeAmount, uint OldFeeAmount);
event NewFeeToken(address NewFeeToken, address OldFeeToken);

error NotEnoughFeeProvided();
error FeeAmountIsZero();
error TransferFailed();

uint public FeeAmount;
address public FeeToken;

mapping(address => uint) public FeeReserve;

function TakeFee() internal virtual firewallProtected returns(uint feeToPay){
feeToPay = FeeAmount;
if(feeToPay == 0) return 0;
uint credits = getCredits(msg.sender);
if(credits > 0) {
_whiteListRegister(msg.sender, credits < feeToPay ? credits : feeToPay);
if(credits < feeToPay) {
feeToPay -= credits;
} else {
return 0;
}
}
_TakeFee(feeToPay);
}

function _TakeFee(uint _fee) private {
address _feeToken = FeeToken; // cache storage reads
if (_feeToken == address(0)) {
if (msg.value < _fee) revert NotEnoughFeeProvided();
emit TransferInETH(msg.value, msg.sender);
} else {
TransferInToken(_feeToken, msg.sender, _fee);
}
FeeReserve[_feeToken] += _fee;
}

function setFee(address _token, uint _amount) external firewallProtected onlyOwnerOrGov {
FeeToken = _token;
FeeAmount = _amount;
}

function WithdrawFee(address _token, address _to) external firewallProtected onlyOwnerOrGov {
if (FeeReserve[_token] == 0) revert FeeAmountIsZero();
uint feeAmount = FeeReserve[_token];
FeeReserve[_token] = 0;
if (_token == address(0)) {
(bool success, ) = _to.call{value: feeAmount}("");
if (!success) revert TransferFailed();
} else {
TransferToken(_token, _to, feeAmount);
}
}
}
39 changes: 39 additions & 0 deletions contracts/Fee/WhiteListHelper.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../interfaces/IWhiteList.sol";
import "../GovManager.sol";

abstract contract WhiteListHelper is GovManager {
error WhiteListNotSet();

uint public WhiteListId;
address public WhiteListAddress;

modifier WhiteListSet {
if(WhiteListAddress == address(0) || WhiteListId == 0) revert WhiteListNotSet();
_;
}

function getCredits(address _user) public view returns(uint) {
if(WhiteListAddress == address(0) || WhiteListId == 0) return 0;
return IWhiteList(WhiteListAddress).Check(_user, WhiteListId);
}

function setupNewWhitelist(address _whiteListAddress) external firewallProtected onlyOwnerOrGov {
WhiteListAddress = _whiteListAddress;
WhiteListId = IWhiteList(_whiteListAddress).CreateManualWhiteList(type(uint256).max, address(this));
}

function addUsers(address[] calldata _users, uint256[] calldata _credits) external firewallProtected onlyOwnerOrGov WhiteListSet {
IWhiteList(WhiteListAddress).AddAddress(WhiteListId, _users, _credits);
}

function removeUsers(address[] calldata _users) external firewallProtected onlyOwnerOrGov WhiteListSet {
IWhiteList(WhiteListAddress).RemoveAddress(WhiteListId, _users);
}

function _whiteListRegister(address _user, uint _credits) internal {
IWhiteList(WhiteListAddress).Register(_user, WhiteListId, _credits);
}
}
48 changes: 0 additions & 48 deletions contracts/FeeBaseHelper.sol

This file was deleted.

2 changes: 2 additions & 0 deletions contracts/interfaces/IWhiteList.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,6 @@ interface IWhiteList {
function LastRoundRegister(address _Subject,uint256 _Id) external;
function CreateManualWhiteList(uint256 _ChangeUntil, address _Contract) external payable returns(uint256 Id);
function ChangeCreator(uint256 _Id, address _NewCreator) external;
function AddAddress(uint256 _Id, address[] calldata _Users, uint256[] calldata _Amount) external;
function RemoveAddress(uint256 _Id, address[] calldata _Users) external;
}
31 changes: 4 additions & 27 deletions contracts/mocks/FeeHelper.sol
Original file line number Diff line number Diff line change
@@ -1,34 +1,11 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../GovManager.sol";
import "../FeeBaseHelper.sol";
import "../Fee/FeeBaseHelper.sol";

// example to use FeeBaseHelper
contract FeeHelper is GovManager, ERC20Helper {
FeeBaseHelper public BaseFee;
contract FeeHelper is FeeBaseHelper {

constructor() {
BaseFee = new FeeBaseHelper();
}

function PayFee() public payable {
if (BaseFee.FeeToken() != address(0)) {
TransferInToken(BaseFee.FeeToken(), msg.sender, BaseFee.Fee());
IERC20(BaseFee.FeeToken()).approve(address(BaseFee), BaseFee.Fee());
}
BaseFee.PayFee{value: msg.value}(BaseFee.Fee());
}

function WithdrawFee(address payable _to) public onlyOwnerOrGov {
BaseFee.WithdrawFee(BaseFee.FeeToken(), _to);
}

function SetFee(uint256 _amount) public onlyOwnerOrGov {
BaseFee.SetFeeAmount(_amount);
}

function SetToken(address _token) public onlyOwnerOrGov {
BaseFee.SetFeeToken(_token);
function MethodWithFee() public payable returns(uint){
return TakeFee();
}
}
41 changes: 41 additions & 0 deletions contracts/mocks/WhiteListMock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "../interfaces/IWhiteList.sol";

contract WhiteListMock is IWhiteList {
uint public WhiteListId;

mapping (address => uint) public AddressToCredits;

function CreateManualWhiteList(uint, address) external payable returns(uint) {
return ++WhiteListId;
}

function Check(address _user, uint _whiteListId) external view returns(uint) {
if(_whiteListId != WhiteListId) return 0;
return AddressToCredits[_user];
}

function Register(address _user, uint _whiteListId, uint _amount) external {
if(_whiteListId != WhiteListId) return;
AddressToCredits[_user] = AddressToCredits[_user] - _amount;
}

function AddAddress(uint256 , address[] calldata _Users, uint256[] calldata _Amount) external{
for(uint i = 0; i < _Users.length; i++){
AddressToCredits[_Users[i]] = _Amount[i];
}
}

function RemoveAddress(uint256 , address[] calldata _Users) external{
for(uint i = 0; i < _Users.length; i++){
delete AddressToCredits[_Users[i]];
}
}

// to avoid the error: Contract "WhiteListMock" should be marked as abstract
function LastRoundRegister(address _Subject,uint256 _Id) external {}
function ChangeCreator(uint256 _Id, address _NewCreator) external {}

}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@poolzfinance/poolz-helper-v2",
"version": "2.3.5",
"version": "2.4.2",
"description": "A single source of truth of helper contracts used by Poolz.",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
Loading

0 comments on commit a36ba42

Please sign in to comment.