/**
*Submitted for verification at Etherscan.io on 2021-02-28
*/
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;
library EnumerableSet {
// To implement this library for multiple types with as little code
// repetition as possible, we write it in terms of a generic Set type with
// bytes32 values.
// The Set implementation uses private functions, and user-facing
// implementations (such as AddressSet) are just wrappers around the
// underlying Set.
// This means that we can only create new EnumerableSets for types that fit
// in bytes32.
struct Set {
// Storage of set values
bytes32[] _values;
// Position of the value in the `values` array, plus 1 because index 0
// means a value is not in the set.
mapping (bytes32 => uint256) _indexes;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
return true;
} else {
return false;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slot
uint256 valueIndex = set._indexes[value];
if (valueIndex != 0) { // Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}.
uint256 toDeleteIndex = valueIndex - 1;
uint256 lastIndex = set._values.length - 1;
// When the value to delete is the last one, the swap operation is unnecessary. However, since this occurs
// so rarely, we still do the swap anyway to avoid the gas cost of adding an 'if' statement.
bytes32 lastvalue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastvalue;
// Update the index for the moved value
set._indexes[lastvalue] = toDeleteIndex + 1; // All indexes are 1-based
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slot
delete set._indexes[value];
return true;
} else {
return false;
}
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function _length(Set storage set) private view returns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position index in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - index must be strictly less than {length}.
*/
function _at(Set storage set, uint256 index) private view returns (bytes32) {
require(set._values.length > index, "EnumerableSet: index out of bounds");
return set._values[index];
}
// Bytes32Set
struct Bytes32Set {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _add(set._inner, value);
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _remove(set._inner, value);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
return _contains(set._inner, value);
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(Bytes32Set storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position index in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - index must be strictly less than {length}.
*/
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
return _at(set._inner, index);
}
// AddressSet
struct AddressSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(AddressSet storage set, address value) internal returns (bool) {
return _add(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(AddressSet storage set, address value) internal returns (bool) {
return _remove(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(AddressSet storage set, address value) internal view returns (bool) {
return _contains(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(AddressSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position index in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - index must be strictly less than {length}.
*/
function at(AddressSet storage set, uint256 index) internal view returns (address) {
return address(uint160(uint256(_at(set._inner, index))));
}
// UintSet
struct UintSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(UintSet storage set, uint256 value) internal returns (bool) {
return _add(set._inner, bytes32(value));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(UintSet storage set, uint256 value) internal returns (bool) {
return _remove(set._inner, bytes32(value));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(UintSet storage set, uint256 value) internal view returns (bool) {
return _contains(set._inner, bytes32(value));
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function length(UintSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position index in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - index must be strictly less than {length}.
*/
function at(UintSet storage set, uint256 index) internal view returns (uint256) {
return uint256(_at(set._inner, index));
}
}
// File: openzeppelin-solidity\contracts\token\ERC20\IERC20.sol
/**
// File: openzeppelin-solidity\contracts\utils\Address.sol
/**
contract fBNB is IERC20 {
using Address for address;
enum TxType { FromExcluded, ToExcluded, BothExcluded, Standard }
mapping (address => uint256) private rBnbBalance;
mapping (address => uint256) private tBnbBalance;
mapping (address => mapping (address => uint256)) private _allowances;
EnumerableSet.AddressSet excluded;
uint256 private tBnbSupply;
uint256 private rBnbSupply;
uint256 private feesAccrued;
string private _name = 'FEG Wrapped BNB';
string private _symbol = 'fBNB';
uint8 private _decimals = 18;
address private op;
address private op2;
event Deposit(address indexed dst, uint amount);
event Withdrawal(address indexed src, uint amount);
receive() external payable {
deposit();
}
constructor () {
op = address(0x00461eDCaDC9389F57bBfc3913afC10E3B5E92B5);
op2 = op;
EnumerableSet.add(excluded, address(0)); // stablity - zen.
emit Transfer(address(0), msg.sender, 0);
}
function name() public view returns (string memory) {
return _name;
}
function symbol() public view returns (string memory) {
return _symbol;
}
function decimals() public view returns (uint8) {
return _decimals;
}
function totalSupply() public view override returns (uint256) {
return tBnbSupply;
}
function balanceOf(address account) public view override returns (uint256) {
if (EnumerableSet.contains(excluded, account)) return tBnbBalance[account];
(uint256 r, uint256 t) = currentSupply();
return (rBnbBalance[account] * t) / r;
}
function transfer(address recipient, uint256 amount) public override returns (bool) {
_transfer(msg.sender, recipient, amount);
return true;
}
function allowance(address owner, address spender) public view override returns (uint256) {
return _allowances[owner][spender];
}
function approve(address spender, uint256 amount) public override returns (bool) {
_approve(msg.sender, spender, amount);
return true;
}
function transferFrom(address sender, address recipient, uint256 amount) public override returns (bool) {
_transfer(sender, recipient, amount);
_approve(sender, msg.sender, _allowances[sender][msg.sender] - amount);
return true;
}
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender] + addedValue);
return true;
}
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
_approve(msg.sender, spender, _allowances[msg.sender][spender] - subtractedValue);
return true;
}
function isExcluded(address account) public view returns (bool) {
return EnumerableSet.contains(excluded, account);
}
function totalFees() public view returns (uint256) {
return feesAccrued;
}
function deposit() public payable {
require(msg.value > 0, "can't deposit nothing");
(uint256 r, uint256 t) = currentSupply();
tBnbSupply += msg.value;
uint256 fee = msg.value / 100;
uint256 df = fee / 8;
uint256 net = fee != 0 ? (msg.value - (fee)) : msg.value;
if(isExcluded(msg.sender)){
tBnbBalance[msg.sender] += (msg.value - fee);
}
feesAccrued += fee;
rBnbBalance[op] += ((df * r) / t);
rBnbSupply += (((net + df) * r) / t);
rBnbBalance[msg.sender] += ((net * r) / t);
emit Deposit(msg.sender, msg.value);
}
function withdraw(uint amt) public {
require(balanceOf(msg.sender) >= amt && amt <= totalSupply(), "invalid amt");
(uint256 r, uint256 t) = currentSupply();
uint256 fee = amt / 100;
uint256 wf = fee / 8;
uint256 net = amt - fee;
if(isExcluded(msg.sender)) {
tBnbBalance[msg.sender] -= amt;
rBnbBalance[msg.sender] -= ((amt * r) / t);
} else {
rBnbBalance[msg.sender] -= ((amt * r) / t);
}
tBnbSupply -= (net + wf);
rBnbSupply -= (((net + wf) * r ) / t);
rBnbBalance[op] += ((wf * r) / t);
feesAccrued += wf;
payable(msg.sender).transfer(net);
emit Withdrawal(msg.sender, net);
}
function rBnbToEveryone(uint256 amt) public {
require(!isExcluded(msg.sender), "not allowed");
(uint256 r, uint256 t) = currentSupply();
rBnbBalance[msg.sender] -= ((amt * r) / t);
rBnbSupply -= ((amt * r) / t);
feesAccrued += amt;
}
function excludeFromFees(address account) external {
require(msg.sender == op2, "op only");
require(!EnumerableSet.contains(excluded, account), "address excluded");
if(rBnbBalance[account] > 0) {
(uint256 r, uint256 t) = currentSupply();
tBnbBalance[account] = (rBnbBalance[account] * (t)) / (r);
}
EnumerableSet.add(excluded, account);
}
function includeInFees(address account) external {
require(msg.sender == op2, "op only");
require(EnumerableSet.contains(excluded, account), "address excluded");
tBnbBalance[account] = 0;
EnumerableSet.remove(excluded, account);
}
function tBnbFromrBnb(uint256 rBnbAmount) external view returns (uint256) {
(uint256 r, uint256 t) = currentSupply();
return (rBnbAmount * t) / r;
}
function _approve(address owner, address spender, uint256 amount) private {
require(owner != address(0), "ERC20: approve from the zero address");
require(spender != address(0), "ERC20: approve to the zero address");
_allowances[owner][spender] = amount;
emit Approval(owner, spender, amount);
}
function getTtype(address sender, address recipient) internal view returns (TxType t) {
bool isSenderExcluded = EnumerableSet.contains(excluded, sender);
bool isRecipientExcluded = EnumerableSet.contains(excluded, recipient);
if (isSenderExcluded && !isRecipientExcluded) {
t = TxType.FromExcluded;
} else if (!isSenderExcluded && isRecipientExcluded) {
t = TxType.ToExcluded;
} else if (!isSenderExcluded && !isRecipientExcluded) {
t = TxType.Standard;
} else if (isSenderExcluded && isRecipientExcluded) {
t = TxType.BothExcluded;
} else {
t = TxType.Standard;
}
return t;
}
function _transfer(address sender, address recipient, uint256 amt) private {
require(sender != address(0), "ERC20: transfer from the zero address");
require(recipient != address(0), "ERC20: transfer to the zero address");
require(amt > 0, "Transfer amt must be greater than zero");
(uint256 r, uint256 t) = currentSupply();
uint256 fee = amt / 100;
TxType tt = getTtype(sender, recipient);
if (tt == TxType.ToExcluded) {
rBnbBalance[sender] -= ((amt * r) / t);
tBnbBalance[recipient] += (amt - fee);
rBnbBalance[recipient] += (((amt - fee) * r) / t);
} else if (tt == TxType.FromExcluded) {
tBnbBalance[sender] -= (amt);
rBnbBalance[sender] -= ((amt * r) / t);
rBnbBalance[recipient] += (((amt - fee) * r) / t);
} else if (tt == TxType.BothExcluded) {
tBnbBalance[sender] -= (amt);
rBnbBalance[sender] -= ((amt * r) / t);
tBnbBalance[recipient] += (amt - fee);
rBnbBalance[recipient] += (((amt - fee) * r) / t);
} else {
rBnbBalance[sender] -= ((amt * r) / t);
rBnbBalance[recipient] += (((amt - fee) * r) / t);
}
rBnbSupply -= ((fee * r) / t);
feesAccrued += fee;
emit Transfer(sender, recipient, amt - fee);
}
function currentSupply() public view returns(uint256, uint256) {
if(rBnbSupply == 0 || tBnbSupply == 0) return (1000000000, 1);
uint256 rSupply = rBnbSupply;
uint256 tSupply = tBnbSupply;
for (uint256 i = 0; i < EnumerableSet.length(excluded); i++) {
if (rBnbBalance[EnumerableSet.at(excluded, i)] > rSupply || tBnbBalance[EnumerableSet.at(excluded, i)] > tSupply) return (rBnbSupply, tBnbSupply);
rSupply -= (rBnbBalance[EnumerableSet.at(excluded, i)]);
tSupply -= (tBnbBalance[EnumerableSet.at(excluded, i)]);
}
if (rSupply < rBnbSupply / tBnbSupply) return (rBnbSupply, tBnbSupply);
return (rSupply, tSupply);
}
function setOp(address opper, address opper2) external {
require(msg.sender == op, "only op can call");
op = opper;
op2 = opper2;
}
}