文件 1 的 1:RICH.sol
pragma solidity 0.8.20;
interface IERC20 {
function totalSupply() external view returns (uint256);
function decimals() external view returns (uint8);
function symbol() external view returns (string memory);
function name() external view returns (string memory);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address _owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
library SafeERC20 {
error SafeERC20FailedOperation(address token);
error SafeERC20FailedDecreaseAllowance(address spender, uint256 currentAllowance, uint256 requestedDecrease);
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transfer, (to, value)));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
function trySafeTransfer(IERC20 token, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(token, abi.encodeCall(token.transfer, (to, value)));
}
function trySafeTransferFrom(IERC20 token, address from, address to, uint256 value) internal returns (bool) {
return _callOptionalReturnBool(token, abi.encodeCall(token.transferFrom, (from, to, value)));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 oldAllowance = token.allowance(address(this), spender);
forceApprove(token, spender, oldAllowance + value);
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 requestedDecrease) internal {
unchecked {
uint256 currentAllowance = token.allowance(address(this), spender);
if (currentAllowance < requestedDecrease) {
revert SafeERC20FailedDecreaseAllowance(spender, currentAllowance, requestedDecrease);
}
forceApprove(token, spender, currentAllowance - requestedDecrease);
}
}
function forceApprove(IERC20 token, address spender, uint256 value) internal {
bytes memory approvalCall = abi.encodeCall(token.approve, (spender, value));
if (!_callOptionalReturnBool(token, approvalCall)) {
_callOptionalReturn(token, abi.encodeCall(token.approve, (spender, 0)));
_callOptionalReturn(token, approvalCall);
}
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
let success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
if iszero(success) {
let ptr := mload(0x40)
returndatacopy(ptr, 0, returndatasize())
revert(ptr, returndatasize())
}
returnSize := returndatasize()
returnValue := mload(0)
}
if (returnSize == 0 ? address(token).code.length == 0 : returnValue != 1) {
revert SafeERC20FailedOperation(address(token));
}
}
function _callOptionalReturnBool(IERC20 token, bytes memory data) private returns (bool) {
bool success;
uint256 returnSize;
uint256 returnValue;
assembly ("memory-safe") {
success := call(gas(), token, 0, add(data, 0x20), mload(data), 0, 0x20)
returnSize := returndatasize()
returnValue := mload(0)
}
return success && (returnSize == 0 ? address(token).code.length > 0 : returnValue == 1);
}
}
interface IDEXFactory {
function createPair(address tokenA, address tokenB) external returns (address pair);
}
interface IDEXRouter {
function factory() external pure returns (address);
function WETH() external pure returns (address);
function addLiquidity(
address tokenA,
address tokenB,
uint amountADesired,
uint amountBDesired,
uint amountAMin,
uint amountBMin,
address to,
uint deadline
) external returns (uint amountA, uint amountB, uint liquidity);
function addLiquidityETH(
address token,
uint amountTokenDesired,
uint amountTokenMin,
uint amountETHMin,
address to,
uint deadline
) external payable returns (uint amountToken, uint amountETH, uint liquidity);
function swapExactTokensForTokensSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
function swapExactETHForTokensSupportingFeeOnTransferTokens(
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external payable;
function swapExactTokensForETHSupportingFeeOnTransferTokens(
uint amountIn,
uint amountOutMin,
address[] calldata path,
address to,
uint deadline
) external;
}
interface IDividendDistributor {
function setDistributionCriteria(uint256 _minPeriod, uint256 _minDistribution) external;
function setShare(address shareholder, uint256 amount) external;
function deposit() external payable;
function process(uint256 gas) external;
}
contract DividendDistributor is IDividendDistributor {
using SafeERC20 for IERC20;
address _token;
struct Share {
uint256 amount;
uint256 totalExcluded;
uint256 totalRealised;
}
IDEXRouter router;
address routerAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
IERC20 RewardToken = IERC20(0xdAC17F958D2ee523a2206206994597C13D831ec7);
address[] shareholders;
mapping (address => uint256) shareholderIndexes;
mapping (address => uint256) shareholderClaims;
mapping (address => Share) public shares;
uint256 public totalShares;
uint256 public totalDividends;
uint256 public totalDistributed;
uint256 public dividendsPerShare;
uint256 public dividendsPerShareAccuracyFactor = 10 ** 18;
uint256 public minPeriod = 30 minutes;
uint256 public minDistribution = 1 * (10 ** 4);
uint256 currentIndex;
bool initialized;
modifier initialization() {
require(!initialized);
_;
initialized = true;
}
modifier onlyToken() {
require(msg.sender == _token); _;
}
constructor (address _router) {
router = _router != address(0) ? IDEXRouter(_router) : IDEXRouter(routerAddress);
_token = msg.sender;
}
function setDistributionCriteria(uint256 newMinPeriod, uint256 newMinDistribution) external override onlyToken {
minPeriod = newMinPeriod;
minDistribution = newMinDistribution;
}
function setShare(address shareholder, uint256 amount) external override onlyToken {
if(shares[shareholder].amount > 0){
distributeDividend(shareholder);
}
if(amount > 0 && shares[shareholder].amount == 0){
addShareholder(shareholder);
}else if(amount == 0 && shares[shareholder].amount > 0){
removeShareholder(shareholder);
}
totalShares = (totalShares - shares[shareholder].amount) + amount;
shares[shareholder].amount = amount;
shares[shareholder].totalExcluded = getCumulativeDividends(shares[shareholder].amount);
}
function deposit() external payable override onlyToken {
uint256 balanceBefore = RewardToken.balanceOf(address(this));
address[] memory path = new address[](2);
path[0] = router.WETH();
path[1] = address(RewardToken);
router.swapExactETHForTokensSupportingFeeOnTransferTokens{value: msg.value}(
0,
path,
address(this),
block.timestamp
);
uint256 amount = RewardToken.balanceOf(address(this)) - balanceBefore;
totalDividends = totalDividends + amount;
dividendsPerShare = dividendsPerShare + (dividendsPerShareAccuracyFactor * amount / totalShares);
}
function process(uint256 gas) external override onlyToken {
uint256 shareholderCount = shareholders.length;
if(shareholderCount == 0) { return; }
uint256 iterations = 0;
uint256 gasUsed = 0;
uint256 gasLeft = gasleft();
while(gasUsed < gas && iterations < shareholderCount) {
if(currentIndex >= shareholderCount){ currentIndex = 0; }
if(shouldDistribute(shareholders[currentIndex])){
distributeDividend(shareholders[currentIndex]);
}
gasUsed = gasUsed + (gasLeft - gasleft());
gasLeft = gasleft();
currentIndex++;
iterations++;
}
}
function shouldDistribute(address shareholder) internal view returns (bool) {
return shareholderClaims[shareholder] + minPeriod < block.timestamp
&& getUnpaidEarnings(shareholder) > minDistribution;
}
function distributeDividend(address shareholder) internal {
if(shares[shareholder].amount == 0){ return; }
uint256 amount = getUnpaidEarnings(shareholder);
if(amount > 0){
totalDistributed = totalDistributed + amount;
RewardToken.safeTransfer(shareholder, amount);
shareholderClaims[shareholder] = block.timestamp;
shares[shareholder].totalRealised = shares[shareholder].totalRealised + amount;
shares[shareholder].totalExcluded = getCumulativeDividends(shares[shareholder].amount);
}
}
function claimDividend() external {
require(shouldDistribute(msg.sender), "Too soon. Need to wait!");
distributeDividend(msg.sender);
}
function getUnpaidEarnings(address shareholder) public view returns (uint256) {
if(shares[shareholder].amount == 0){ return 0; }
uint256 shareholderTotalDividends = getCumulativeDividends(shares[shareholder].amount);
uint256 shareholderTotalExcluded = shares[shareholder].totalExcluded;
if(shareholderTotalDividends <= shareholderTotalExcluded){ return 0; }
return shareholderTotalDividends - shareholderTotalExcluded;
}
function getCumulativeDividends(uint256 share) internal view returns (uint256) {
return share * dividendsPerShare / dividendsPerShareAccuracyFactor;
}
function addShareholder(address shareholder) internal {
shareholderIndexes[shareholder] = shareholders.length;
shareholders.push(shareholder);
}
function removeShareholder(address shareholder) internal {
shareholders[shareholderIndexes[shareholder]] = shareholders[shareholders.length-1];
shareholderIndexes[shareholders[shareholders.length-1]] = shareholderIndexes[shareholder];
shareholders.pop();
}
function getUserRewardStatus(address userAddress) external view returns(uint256, uint256) {
return (shareholderClaims[userAddress], getUnpaidEarnings(userAddress));
}
}
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
this;
return msg.data;
}
}
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor() {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view virtual returns (address) {
return _owner;
}
modifier onlyOwner() {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
contract RICH is IERC20, Ownable {
string constant _name = "RICH";
string constant _symbol = "RICH";
uint8 constant _decimals = 18;
address DEAD = 0x000000000000000000000000000000000000dEaD;
address ZERO = 0x0000000000000000000000000000000000000000;
address routerAddress = 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D;
address RewardToken = 0xdAC17F958D2ee523a2206206994597C13D831ec7;
address marketingAddress = 0x47656Bb850A38d0F9E945Ea519a8f7711E0D06F6;
uint256 _totalSupply = 250_000_000 * (10 ** _decimals);
mapping (address => uint256) _balances;
mapping (address => mapping (address => uint256)) _allowances;
mapping (address => bool) public isFeeExempt;
mapping (address => bool) public isTxLimitExempt;
mapping (address => bool) public isDividendExempt;
uint256 public marketingFee = 1;
uint256 public holdersRewardFee = 2;
uint256 public totalFee = 3;
IDEXRouter public router;
address public pair;
bool public tradingOpen = false;
DividendDistributor public dividendDistributor;
uint256 distributorGas = 500000;
bool inSwap;
bool public swapEnabled = true;
uint256 public swapThreshold = _totalSupply / 100000;
uint256 public swapThresholdCnt = 0;
uint256 public maxThresholdCnt = 4;
modifier lockTheSwap {
inSwap = true;
_;
inSwap = false;
}
constructor () {
router = IDEXRouter(routerAddress);
pair = IDEXFactory(router.factory()).createPair(router.WETH(), address(this));
_allowances[address(this)][address(router)] = type(uint256).max;
dividendDistributor = new DividendDistributor(address(router));
isFeeExempt[msg.sender] = true;
isFeeExempt[address(this)] = true;
isTxLimitExempt[msg.sender] = true;
isDividendExempt[pair] = true;
isDividendExempt[msg.sender] = true;
isDividendExempt[address(this)] = true;
isDividendExempt[DEAD] = true;
isDividendExempt[ZERO] = true;
_balances[msg.sender] = _totalSupply;
emit Transfer(address(0), msg.sender, _totalSupply);
}
receive() external payable { }
function name() external pure override returns (string memory) { return _name; }
function symbol() external pure override returns (string memory) { return _symbol; }
function decimals() external pure override returns (uint8) { return _decimals; }
function totalSupply() external view override returns (uint256) { return _totalSupply; }
function getCirculatingSupply() public view returns (uint256) {
return _totalSupply - (balanceOf(DEAD) + balanceOf(ZERO));
}
function balanceOf(address account) public view override returns (uint256) { return _balances[account]; }
function allowance(address holder, address spender) external view override returns (uint256) { return _allowances[holder][spender]; }
function approve(address spender, uint256 amount) public override returns (bool) {
_allowances[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function approveMax(address spender) external returns (bool) {
return approve(spender, type(uint256).max);
}
function changeIsFeeExempt(address holder, bool exempt) external onlyOwner {
isFeeExempt[holder] = exempt;
}
function changeIsTxLimitExempt(address holder, bool exempt) external onlyOwner {
isTxLimitExempt[holder] = exempt;
}
function changeIsDividendExempt(address holder, bool exempt) external onlyOwner {
require(holder != address(this) && holder != pair);
isDividendExempt[holder] = exempt;
if(exempt){
dividendDistributor.setShare(holder, 0);
}else{
dividendDistributor.setShare(holder, _balances[holder]);
}
}
function changeSwapSettings(bool enableSwap, uint256 newSwapLimit) external onlyOwner {
swapEnabled = enableSwap;
swapThreshold = newSwapLimit;
}
function changeDistributionCriteria(uint256 newinPeriod, uint256 newMinDistribution) external onlyOwner {
dividendDistributor.setDistributionCriteria(newinPeriod, newMinDistribution);
}
function changeDistributorSettings(uint256 gas) external onlyOwner {
require(gas < 750000);
distributorGas = gas;
}
function transfer(address recipient, uint256 amount) external override returns (bool) {
return _transferFrom(msg.sender, recipient, amount);
}
function transferFrom(address sender, address recipient, uint256 amount) external override returns (bool) {
if(_allowances[sender][msg.sender] != type(uint256).max){
_allowances[sender][msg.sender] = _allowances[sender][msg.sender] - amount;
}
return _transferFrom(sender, recipient, amount);
}
function _transferFrom(address sender, address recipient, uint256 amount) internal returns (bool) {
if(inSwap){ return _basicTransfer(sender, recipient, amount); }
require(isTxLimitExempt[sender] || tradingOpen, "Trading not open yet");
if(recipient == pair && !inSwap && swapEnabled && _balances[address(this)] >= swapThreshold){ swap(); }
_balances[sender] = _balances[sender] - amount;
uint256 finalAmount = (!isFeeExempt[sender] && !isFeeExempt[recipient]) && (sender == pair || recipient == pair)
? takeFee(sender, amount) : amount;
_balances[recipient] = _balances[recipient] + finalAmount;
if(!isDividendExempt[sender]) {
try dividendDistributor.setShare(sender, _balances[sender]) {} catch {}
}
if(!isDividendExempt[recipient]) {
try dividendDistributor.setShare(recipient, _balances[recipient]) {} catch {}
}
try dividendDistributor.process(distributorGas) {} catch {}
emit Transfer(sender, recipient, finalAmount);
return true;
}
function _basicTransfer(address sender, address recipient, uint256 amount) internal returns (bool) {
_balances[sender] = _balances[sender] - amount;
_balances[recipient] = _balances[recipient] + amount;
emit Transfer(sender, recipient, amount);
return true;
}
function takeFee(address sender, uint256 amount) internal returns (uint256) {
uint256 feeAmount = amount * totalFee / 100;
_balances[address(this)] = _balances[address(this)] + feeAmount;
emit Transfer(sender, address(this), feeAmount);
return amount - feeAmount;
}
function tradingStatus(bool newStatus) public onlyOwner {
tradingOpen = newStatus;
}
function setMaxThresholdCnt(uint256 count) public onlyOwner {
maxThresholdCnt = count;
}
function changeMarketingWallet(address newMarketing) public onlyOwner {
marketingAddress = newMarketing;
}
function swap() internal lockTheSwap {
swapThresholdCnt++;
if(swapThresholdCnt < maxThresholdCnt && _balances[address(this)] < swapThreshold * 10){
return;
}
uint256 amountToSwap = _balances[address(this)];
address[] memory path = new address[](2);
path[0] = address(this);
path[1] = router.WETH();
router.swapExactTokensForETHSupportingFeeOnTransferTokens(
amountToSwap,
0,
path,
address(this),
block.timestamp
);
uint256 amountETH = address(this).balance;
uint256 amountETHReflection = amountETH * holdersRewardFee / totalFee;
uint256 amountETHMarketing = amountETH - amountETHReflection;
address[] memory pathM = new address[](2);
pathM[0] = router.WETH();
pathM[1] = address(RewardToken);
router.swapExactETHForTokensSupportingFeeOnTransferTokens{value: amountETHMarketing}(
0,
pathM,
marketingAddress,
block.timestamp
);
try dividendDistributor.deposit{value: amountETHReflection}() {} catch {}
swapThresholdCnt = 0;
}
}