Last active
September 11, 2024 16:33
-
-
Save AntonVoronezh/8aa4ce017ddb6ac0a3e3c4a60fe4ac85 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.8.22+commit.4fc1097e.js&optimize=false&runs=200&gist=
This file contains 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
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract RegisterDomainName { | |
address owner; | |
constructor() { | |
owner = msg.sender; | |
} | |
uint oneYearPrice = 1; | |
uint prolongRatio = 1; | |
struct DomainName { | |
address domainNameAddress; | |
uint domainNameDate; | |
uint domainNamePrice; | |
uint domainNameYears; | |
} | |
mapping(string => DomainName) public registeredDomains; | |
modifier onlyOwner() { | |
require(msg.sender == owner, "You are not an owner!"); | |
_; | |
} | |
function setOneYearPrice(uint _newPrice) public onlyOwner { | |
oneYearPrice = _newPrice; | |
} | |
function setProlongRatio(uint _newRatio) public onlyOwner { | |
prolongRatio = _newRatio; | |
} | |
function buyDomainName(string memory _domainName, uint _years) public payable { | |
require(_years >= 1 && _years <= 10, "from 1 to 10 years"); | |
require(msg.value < oneYearPrice * _years, "little money fo domen"); | |
require(registeredDomains[_domainName].domainNameDate != 0, "domain is busy"); | |
require((block.timestamp - (365 days * registeredDomains[_domainName].domainNameYears)) < 0, "domain is busy"); | |
DomainName memory newDomainName = DomainName({ | |
domainNameAddress: msg.sender, | |
domainNameDate: block.timestamp, | |
domainNamePrice: oneYearPrice * _years, | |
domainNameYears: _years | |
}); | |
registeredDomains[_domainName] = newDomainName; | |
} | |
function prolongDomainName(string memory _domainName, uint _years) public payable { | |
require(msg.sender == registeredDomains[_domainName].domainNameAddress, "it's not yours"); | |
DomainName memory newDomainName = DomainName({ | |
domainNameAddress: msg.sender, | |
domainNameDate: block.timestamp, | |
domainNamePrice: (oneYearPrice * _years) * prolongRatio, | |
domainNameYears: _years | |
}); | |
registeredDomains[_domainName] = newDomainName; | |
} | |
function getAddressByDomainName(string memory _domainName) public view returns(address) { | |
return registeredDomains[_domainName].domainNameAddress; | |
} | |
function withdrawMoney() public onlyOwner { | |
address payable receiver = payable(msg.sender); | |
uint balance = address(this).balance; | |
receiver.transfer(balance); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment