Skip to content

Commit

Permalink
prettiered
Browse files Browse the repository at this point in the history
  • Loading branch information
Turupawn committed Feb 5, 2023
1 parent 4535a10 commit 33e8f52
Show file tree
Hide file tree
Showing 26 changed files with 905 additions and 420 deletions.
50 changes: 26 additions & 24 deletions contracts/ERC20/BalancerV2FeeToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import "./interfaces/BalancerInterfaces.sol";
/// @author Filosofía Codigo
/// @notice You can use this contract launch your own token or to study the Balancer ecosystem.
/// @dev Based on top OpenZeppelin contracts but changed balances from private to internal for flexibility
abstract contract BalancerV2FeeToken is ERC20
{
abstract contract BalancerV2FeeToken is ERC20 {
/// @notice List of address that won't pay transaction fees
mapping(address => bool) public isTaxless;
/// @notice Address that will recieve fees taken from each transaction
Expand All @@ -34,14 +33,17 @@ abstract contract BalancerV2FeeToken is ERC20
/// @param sellFeePercentage Percent of tokens that will be sent to the feeReciever when token is sold on Balancer
/// @param p2pFeePercentage Percent of tokens that will be sent to the feeReciever when token is transfered outside of Balancer
/// @param feeReceiver_ Address that will recieve the fees taken every transaction
constructor(string memory name, string memory symbol,
constructor(
string memory name,
string memory symbol,
uint totalSupply_,
uint buyFeePercentage, uint sellFeePercentage, uint p2pFeePercentage,
address feeReceiver_)
ERC20(name, symbol, totalSupply_)
{
uint buyFeePercentage,
uint sellFeePercentage,
uint p2pFeePercentage,
address feeReceiver_
) ERC20(name, symbol, totalSupply_) {
feeReceiver = feeReceiver_;

isTaxless[msg.sender] = true;
isTaxless[address(this)] = true;
isTaxless[feeReceiver] = true;
Expand All @@ -50,7 +52,7 @@ abstract contract BalancerV2FeeToken is ERC20
fees.push(buyFeePercentage);
fees.push(sellFeePercentage);
fees.push(p2pFeePercentage);

isFeeActive = true;
}

Expand All @@ -65,44 +67,41 @@ abstract contract BalancerV2FeeToken is ERC20
bool sell = to == balancerVault;
bool p2p = from != balancerVault && to != balancerVault;
uint feeIndex = 0;
if(p2p)
feeIndex = 2;
else if(sell)
feeIndex = 1;
feesCollected = (amount * fees[feeIndex]) / (10**(feeDecimals + 2));
if (p2p) feeIndex = 2;
else if (sell) feeIndex = 1;
feesCollected =
(amount * fees[feeIndex]) /
(10 ** (feeDecimals + 2));
}

amount -= feesCollected;
_balances[from] -= feesCollected;
_balances[feeReceiver] += feesCollected;

emit Transfer(from, feeReceiver, amount);

super._transfer(from, to, amount);
}

/// @notice Set excemptions for transaction fee payments
/// @param account Address that tax configuration will be affected
/// @param isTaxless_ If set to true the account will not pay transaction fees
/// @custom:internal This function is internal, can be overrided.
function _setTaxless(address account, bool isTaxless_) internal
{
function _setTaxless(address account, bool isTaxless_) internal {
isTaxless[account] = isTaxless_;
}

/// @notice Changes the address that will recieve fees
/// @param feeReceiver_ If set to true the account will not pay transaction fees
/// @custom:internal This function is internal, can be overrided.
function _setFeeReceiver(address feeReceiver_) internal
{
function _setFeeReceiver(address feeReceiver_) internal {
feeReceiver = feeReceiver_;
}

/// @notice Changes the address that will recieve fees
/// @param isFeeActive_ If set to true all transaction fees will not be charged
/// @custom:internal This function is internal, can be overrided.
function _setFeeActive(bool isFeeActive_) internal
{
function _setFeeActive(bool isFeeActive_) internal {
isFeeActive = isFeeActive_;
}

Expand All @@ -111,10 +110,13 @@ abstract contract BalancerV2FeeToken is ERC20
/// @param sellFeePercentage New sell percentage fee
/// @param p2pFeePercentage New peer to peer percentage fee
/// @custom:internal This function is internal, can be overrided.
function _setFees(uint buyFeePercentage, uint sellFeePercentage, uint p2pFeePercentage) internal
{
function _setFees(
uint buyFeePercentage,
uint sellFeePercentage,
uint p2pFeePercentage
) internal {
fees[0] = buyFeePercentage;
fees[1] = sellFeePercentage;
fees[2] = p2pFeePercentage;
}
}
}
65 changes: 53 additions & 12 deletions contracts/ERC20/ERC20.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,25 @@ import "../common/Ownable.sol";

interface IERC20 {
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event Approval(
address indexed owner,
address indexed spender,
uint256 value
);

function totalSupply() external view returns (uint256);

function balanceOf(address account) external view returns (uint256);

function transfer(address to, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);

function allowance(
address owner,
address spender
) external view returns (uint256);

function approve(address spender, uint256 amount) external returns (bool);

function transferFrom(
address from,
address to,
Expand All @@ -22,7 +35,9 @@ interface IERC20 {

interface IERC20Metadata is IERC20 {
function name() external view returns (string memory);

function symbol() external view returns (string memory);

function decimals() external view returns (uint8);
}

Expand Down Expand Up @@ -58,21 +73,32 @@ contract ERC20 is Context, IERC20, IERC20Metadata, Ownable {
return _totalSupply;
}

function balanceOf(address account) public view virtual override returns (uint256) {
function balanceOf(
address account
) public view virtual override returns (uint256) {
return _balances[account];
}

function transfer(address to, uint256 amount) public virtual override returns (bool) {
function transfer(
address to,
uint256 amount
) public virtual override returns (bool) {
address owner = _msgSender();
_transfer(owner, to, amount);
return true;
}

function allowance(address owner, address spender) public view virtual override returns (uint256) {
function allowance(
address owner,
address spender
) public view virtual override returns (uint256) {
return _allowances[owner][spender];
}

function approve(address spender, uint256 amount) public virtual override returns (bool) {
function approve(
address spender,
uint256 amount
) public virtual override returns (bool) {
address owner = _msgSender();
_approve(owner, spender, amount);
return true;
Expand All @@ -89,16 +115,25 @@ contract ERC20 is Context, IERC20, IERC20Metadata, Ownable {
return true;
}

function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
function increaseAllowance(
address spender,
uint256 addedValue
) public virtual returns (bool) {
address owner = _msgSender();
_approve(owner, spender, allowance(owner, spender) + addedValue);
return true;
}

function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
function decreaseAllowance(
address spender,
uint256 subtractedValue
) public virtual returns (bool) {
address owner = _msgSender();
uint256 currentAllowance = allowance(owner, spender);
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
require(
currentAllowance >= subtractedValue,
"ERC20: decreased allowance below zero"
);
unchecked {
_approve(owner, spender, currentAllowance - subtractedValue);
}
Expand All @@ -117,7 +152,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata, Ownable {
_beforeTokenTransfer(from, to, amount);

uint256 fromBalance = _balances[from];
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
require(
fromBalance >= amount,
"ERC20: transfer amount exceeds balance"
);
unchecked {
_balances[from] = fromBalance - amount;
// Overflow not possible: the sum of all balances is capped by totalSupply, and the sum is preserved by
Expand Down Expand Up @@ -182,7 +220,10 @@ contract ERC20 is Context, IERC20, IERC20Metadata, Ownable {
) internal virtual {
uint256 currentAllowance = allowance(owner, spender);
if (currentAllowance != type(uint256).max) {
require(currentAllowance >= amount, "ERC20: insufficient allowance");
require(
currentAllowance >= amount,
"ERC20: insufficient allowance"
);
unchecked {
_approve(owner, spender, currentAllowance - amount);
}
Expand All @@ -200,4 +241,4 @@ contract ERC20 is Context, IERC20, IERC20Metadata, Ownable {
address to,
uint256 amount
) internal virtual {}
}
}
50 changes: 31 additions & 19 deletions contracts/ERC20/UniswapV2AutoSwapToken.sol
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ import "./interfaces/UniswapV2Interfaces.sol";
/// @author Filosofía Codigo
/// @notice You can use this contract launch your own token or to study the Uniswap V2 ecosystem.
/// @dev Based on top OpenZeppelin contracts but changed balances from private to internal for flexibility
abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken
{
abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken {
/// @notice Percentage of total supply that have to be accumulated as fees to trigger the autoswap and send the fees to the autoSwapReciever
uint256 public minTokensBeforeSwap;
/// @notice Address that will recieve fees on base token denomination
Expand All @@ -33,18 +32,28 @@ abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken
/// @param routerAddress You can support such DEXes by setting the router address in this param. Many projects such as Pancakeswap, Sushiswap or Quickswap are compatible with Uniswap V2
/// @param baseTokenAddress Token address that this will be paired with on the DEX. Fees will be sent to the autoSwapReciever in the base token denomination
/// @param minTokensBeforeSwapPercent Percentage of total supply that have to be accumulated as fees to trigger the autoswap and send the fees to the autoSwapReciever
constructor(string memory name, string memory symbol,
constructor(
string memory name,
string memory symbol,
uint totalSupply_,
uint buyFeePercentage, uint sellFeePercentage, uint p2pFeePercentage,
uint buyFeePercentage,
uint sellFeePercentage,
uint p2pFeePercentage,
address autoSwapReciever_,
address routerAddress,
address baseTokenAddress,
uint minTokensBeforeSwapPercent) UniswapV2FeeToken(name, symbol,
totalSupply_,
buyFeePercentage, sellFeePercentage, p2pFeePercentage,
address(this),
routerAddress,
baseTokenAddress
uint minTokensBeforeSwapPercent
)
UniswapV2FeeToken(
name,
symbol,
totalSupply_,
buyFeePercentage,
sellFeePercentage,
p2pFeePercentage,
address(this),
routerAddress,
baseTokenAddress
)
{
autoSwapReciever = autoSwapReciever_;
Expand All @@ -62,12 +71,12 @@ abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken
/// @dev Swaps all the fees collected to base tokens and send it to the autoSwapReciever
function swap() private lockTheSwap {
uint totalSwap = balanceOf(address(this));
if(minTokensBeforeSwap > totalSwap) return;
if(totalSwap <= 0) return;
if (minTokensBeforeSwap > totalSwap) return;
if (totalSwap <= 0) return;

address[] memory sellPath = new address[](2);
sellPath[0] = address(this);
sellPath[1] = address(baseToken);
sellPath[1] = address(baseToken);

_approve(address(this), address(router), totalSwap);
router.swapExactTokensForTokensSupportingFeeOnTransferTokens(
Expand All @@ -77,7 +86,7 @@ abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken
autoSwapReciever,
block.timestamp
);

emit Swap(totalSwap);
}

Expand All @@ -87,16 +96,19 @@ abstract contract UniswapV2AutoSwapToken is UniswapV2FeeToken
address to,
uint256 amount
) internal virtual override {
if(isFeeActive)
{
if (isFeeActive) {
swap();
}
super._transfer(from, to, amount);
}

/// @notice Change the minimum ammount of fees collected to trigger the autoswap
/// @param percentage Percentage of total supply that have to be accumulated as fees to trigger the autoswap and send the fees to the autoSwapReciever
function setMinTokensBeforeSwapPercent(uint256 percentage) public onlyOwner {
minTokensBeforeSwap = (totalSupply() * percentage) / (10**(feeDecimals + 2));
function setMinTokensBeforeSwapPercent(
uint256 percentage
) public onlyOwner {
minTokensBeforeSwap =
(totalSupply() * percentage) /
(10 ** (feeDecimals + 2));
}
}
}
Loading

0 comments on commit 33e8f52

Please sign in to comment.