coinkrali
Newbie
- BNB
- 0,00000350
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract MyToken is ERC20, Pausable, ReentrancyGuard {
address public owner;
uint256 public buyPrice; // Price of 1 token in BNB
uint256 public maxBuyLimit; // Maximum tokens a user can buy in one transaction
uint256 public totalTokensSold; // Total tokens sold
uint256 public totalBNBReceived; // Total BNB received
uint256 public maxSupply; // Maximum token supply
event TokensBought(address indexed buyer, uint256 amount, uint256 bnbAmount);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(
string memory name,
string memory symbol,
uint256 initialSupply,
uint256 _buyPrice,
uint256 _maxBuyLimit,
uint256 _maxSupply
) ERC20(name, symbol) {
_mint(msg.sender, initialSupply);
owner = msg.sender;
buyPrice = _buyPrice;
maxBuyLimit = _maxBuyLimit;
maxSupply = _maxSupply;
}
receive() external payable {
buyTokens();
}
function buyTokens() public payable whenNotPaused nonReentrant {
require(msg.value > 0, "Insufficient BNB sent");
require(totalSupply() + msg.value / buyPrice <= maxSupply, "Exceeds maximum token supply");
require(msg.value <= maxBuyLimit * buyPrice, "Exceeds maximum buy limit");
uint256 amount = msg.value / buyPrice;
totalTokensSold += amount;
totalBNBReceived += msg.value;
_mint(msg.sender, amount);
emit TokensBought(msg.sender, amount, msg.value);
}
function setBuyPrice(uint256 _newPrice) external onlyOwner {
buyPrice = _newPrice;
}
function setMaxBuyLimit(uint256 _newLimit) external onlyOwner {
maxBuyLimit = _newLimit;
}
function transferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Invalid new owner");
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
function pause() external onlyOwner {
_pause();
}
function unpause() external onlyOwner {
_unpause();
}
function withdrawBNB(uint256 _amount) external onlyOwner {
require(_amount <= address(this).balance, "Insufficient balance");
payable(owner).transfer(_amount);
}
function withdrawTokens(uint256 _amount) external onlyOwner {
_burn(msg.sender, _amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal override {
require(to != address(this) || from == owner, "Token transfers are restricted");
super._beforeTokenTransfer(from, to, amount);
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
}
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/security/Pausable.sol";
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
contract MyToken is ERC20, Pausable, ReentrancyGuard {
address public owner;
uint256 public buyPrice; // Price of 1 token in BNB
uint256 public maxBuyLimit; // Maximum tokens a user can buy in one transaction
uint256 public totalTokensSold; // Total tokens sold
uint256 public totalBNBReceived; // Total BNB received
uint256 public maxSupply; // Maximum token supply
event TokensBought(address indexed buyer, uint256 amount, uint256 bnbAmount);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor(
string memory name,
string memory symbol,
uint256 initialSupply,
uint256 _buyPrice,
uint256 _maxBuyLimit,
uint256 _maxSupply
) ERC20(name, symbol) {
_mint(msg.sender, initialSupply);
owner = msg.sender;
buyPrice = _buyPrice;
maxBuyLimit = _maxBuyLimit;
maxSupply = _maxSupply;
}
receive() external payable {
buyTokens();
}
function buyTokens() public payable whenNotPaused nonReentrant {
require(msg.value > 0, "Insufficient BNB sent");
require(totalSupply() + msg.value / buyPrice <= maxSupply, "Exceeds maximum token supply");
require(msg.value <= maxBuyLimit * buyPrice, "Exceeds maximum buy limit");
uint256 amount = msg.value / buyPrice;
totalTokensSold += amount;
totalBNBReceived += msg.value;
_mint(msg.sender, amount);
emit TokensBought(msg.sender, amount, msg.value);
}
function setBuyPrice(uint256 _newPrice) external onlyOwner {
buyPrice = _newPrice;
}
function setMaxBuyLimit(uint256 _newLimit) external onlyOwner {
maxBuyLimit = _newLimit;
}
function transferOwnership(address _newOwner) external onlyOwner {
require(_newOwner != address(0), "Invalid new owner");
emit OwnershipTransferred(owner, _newOwner);
owner = _newOwner;
}
function pause() external onlyOwner {
_pause();
}
function unpause() external onlyOwner {
_unpause();
}
function withdrawBNB(uint256 _amount) external onlyOwner {
require(_amount <= address(this).balance, "Insufficient balance");
payable(owner).transfer(_amount);
}
function withdrawTokens(uint256 _amount) external onlyOwner {
_burn(msg.sender, _amount);
}
function _beforeTokenTransfer(address from, address to, uint256 amount) internal override {
require(to != address(this) || from == owner, "Token transfers are restricted");
super._beforeTokenTransfer(from, to, amount);
}
modifier onlyOwner() {
require(msg.sender == owner, "Only owner can call this function");
_;
}
}