Описание изменений
This commit is contained in:
@@ -1,27 +1,18 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
contract MyContract {
|
||||
address public owner;
|
||||
import "@openzeppelin/contracts/access/Ownable.sol";
|
||||
import "@openzeppelin/contracts/security/ReentrancyGuard.sol";
|
||||
|
||||
contract MyContract is Ownable, ReentrancyGuard {
|
||||
uint256 public price;
|
||||
|
||||
event Purchase(address buyer, uint256 amount);
|
||||
|
||||
constructor() {
|
||||
owner = msg.sender;
|
||||
price = 0.01 ether; // Начальная цена 0.01 ETH
|
||||
}
|
||||
|
||||
modifier onlyOwner() {
|
||||
require(msg.sender == owner, "Only owner can call this function");
|
||||
_;
|
||||
}
|
||||
|
||||
function setOwner(address newOwner) public onlyOwner {
|
||||
require(newOwner != address(0), "New owner cannot be zero address");
|
||||
owner = newOwner;
|
||||
}
|
||||
|
||||
function setPrice(uint256 newPrice) public onlyOwner {
|
||||
price = newPrice;
|
||||
}
|
||||
@@ -30,12 +21,13 @@ contract MyContract {
|
||||
return price;
|
||||
}
|
||||
|
||||
function purchase(uint256 amount) public payable {
|
||||
function purchase(uint256 amount) public payable nonReentrant {
|
||||
require(msg.value == price * amount, "Incorrect payment amount");
|
||||
emit Purchase(msg.sender, amount);
|
||||
}
|
||||
|
||||
function withdraw() public onlyOwner {
|
||||
payable(owner).transfer(address(this).balance);
|
||||
function withdraw() public onlyOwner nonReentrant {
|
||||
(bool success, ) = owner().call{value: address(this).balance}("");
|
||||
require(success, "Transfer failed");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user