Описание изменений

This commit is contained in:
2025-02-19 21:34:34 +03:00
parent 808e9aee6a
commit be08978fef
8 changed files with 68 additions and 123 deletions

View File

@@ -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");
}
}