Skip to content

Commit

Permalink
whitelist and fee experiment (#99)
Browse files Browse the repository at this point in the history
* whitelist and fee experiment

* refactor
  • Loading branch information
ashwinarora authored Mar 25, 2024
1 parent 472977f commit 900352c
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 18 deletions.
22 changes: 5 additions & 17 deletions contracts/FeeBaseHelper.sol → contracts/Fee/FeeBaseHelper.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

abstract contract FeeBaseHelper is ERC20Helper, GovManager {
abstract contract FeeBaseHelper is ERC20Helper, WhiteListHelper {
event TransferInETH(uint Amount, address From);
event NewFeeAmount(uint NewFeeAmount, uint OldFeeAmount);
event NewFeeToken(address NewFeeToken, address OldFeeToken);
Expand All @@ -15,17 +14,16 @@ abstract contract FeeBaseHelper is ERC20Helper, GovManager {
error TransferFailed();

uint public FeeAmount;
uint public WhiteListId;
address public FeeToken;
address public WhiteListAddress;

mapping(address => uint) public FeeReserve;

function TakeFee() internal virtual firewallProtected {
uint feeToPay = FeeAmount;
if(feeToPay == 0) return;
uint credits = getCredits(msg.sender);
if(credits > 0) {
IWhiteList(WhiteListAddress).Register(msg.sender, WhiteListId, credits < feeToPay ? credits : feeToPay);
_whiteListRegister(msg.sender, credits < feeToPay ? credits : feeToPay);
if(credits < feeToPay) {
feeToPay -= credits;
} else {
Expand All @@ -46,21 +44,11 @@ abstract contract FeeBaseHelper is ERC20Helper, GovManager {
FeeReserve[_feeToken] += _fee;
}

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

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

function setWhiteList(address _whiteListAddr, uint _id) external firewallProtected onlyOwnerOrGov {
WhiteListAddress = _whiteListAddr;
WhiteListId = _id;
}

function WithdrawFee(address _token, address _to) external firewallProtected onlyOwnerOrGov {
if (FeeReserve[_token] == 0) revert FeeAmountIsZero();
if (_token == address(0)) {
Expand Down
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);
}
}
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;
}
2 changes: 1 addition & 1 deletion contracts/mocks/FeeHelper.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

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

contract FeeHelper is FeeBaseHelper {

Expand Down

0 comments on commit 900352c

Please sign in to comment.