Created
October 31, 2023 21:40
-
-
Save ocb013/c1241981dea44ed7916d5962dcf133f7 to your computer and use it in GitHub Desktop.
ENS с проверками
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.8.0; | |
//SPDX-License-Identifier: GPL-3.0 | |
contract EnsDomen { | |
constructor() { | |
owner = msg.sender; | |
} | |
receive() external payable {} | |
fallback() external {} | |
modifier checkOwner() { | |
require(msg.sender == owner, 'You are not the owner!'); | |
_; | |
} | |
modifier checkPeriod(uint _period) { | |
require(_period < 11 && _period > 0, 'Unsupported period!'); | |
_; | |
} | |
address internal owner; | |
uint public priceOneYear = 100; | |
uint public coefficient = 2; | |
function setPrice(uint _price) public checkOwner { | |
priceOneYear = _price; | |
} | |
function setCoefficient(uint _coefficient) public checkOwner { | |
coefficient = _coefficient; | |
} | |
struct Domen { | |
uint timestamp; | |
uint amount; | |
address addr; | |
uint period; | |
} | |
mapping(string => Domen) public domenStructure; | |
function createDomen(string memory _name, uint _period) public payable checkPeriod(_period) { | |
require(msg.value == (priceOneYear * _period), 'Wrong amount!'); | |
if (domenStructure[_name].amount != 0) { | |
if (!checkExpire(_name)) { | |
revert("Domen is busy"); | |
} | |
} | |
Domen memory domen = Domen(block.timestamp, msg.value, msg.sender, _period); | |
domenStructure[_name] = domen; | |
} | |
function viewDomen(string memory _name) public view returns(address) { | |
return domenStructure[_name].addr; | |
} | |
function renewDomen(string memory _name, uint _period) public payable checkPeriod(_period) { | |
require(msg.sender == domenStructure[_name].addr, 'You are not the domen owner!'); | |
require(msg.value == (priceOneYear * _period * coefficient), 'Wrong amount!'); | |
domenStructure[_name].amount += msg.value; | |
domenStructure[_name].period += _period; | |
} | |
function withdrawMoney(address payable _to) public checkOwner { | |
_to.transfer(address(this).balance); | |
} | |
function checkExpire(string memory _name) public view returns(bool) { | |
uint _periodDifference = block.timestamp - domenStructure[_name].timestamp; | |
bool isExpired = _periodDifference > domenStructure[_name].period * 365*24*60*60; | |
return isExpired; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment