Contract Name:
SonicRegistrar
Contract Source Code:
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)
pragma solidity ^0.8.20;
import {Context} from "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* The initial owner is set to the address provided by the deployer. This can
* later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
/**
* @dev The caller account is not authorized to perform an operation.
*/
error OwnableUnauthorizedAccount(address account);
/**
* @dev The owner is not a valid owner account. (eg. `address(0)`)
*/
error OwnableInvalidOwner(address owner);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the address provided by the deployer as the initial owner.
*/
constructor(address initialOwner) {
if (initialOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(initialOwner);
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
if (owner() != _msgSender()) {
revert OwnableUnauthorizedAccount(_msgSender());
}
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby disabling any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
if (newOwner == address(0)) {
revert OwnableInvalidOwner(address(0));
}
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)
pragma solidity ^0.8.20;
/**
* @dev Provides information about the current execution context, including the
* sender of the transaction and its data. While these are generally available
* via msg.sender and msg.data, they should not be accessed in such a direct
* manner, since when dealing with meta-transactions the account sending and
* paying for execution may not be the actual sender (as far as an application
* is concerned).
*
* This contract is only required for intermediate, library-like contracts.
*/
abstract contract Context {
function _msgSender() internal view virtual returns (address) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes calldata) {
return msg.data;
}
function _contextSuffixLength() internal view virtual returns (uint256) {
return 0;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v5.1.0) (utils/ReentrancyGuard.sol)
pragma solidity ^0.8.20;
/**
* @dev Contract module that helps prevent reentrant calls to a function.
*
* Inheriting from `ReentrancyGuard` will make the {nonReentrant} modifier
* available, which can be applied to functions to make sure there are no nested
* (reentrant) calls to them.
*
* Note that because there is a single `nonReentrant` guard, functions marked as
* `nonReentrant` may not call one another. This can be worked around by making
* those functions `private`, and then adding `external` `nonReentrant` entry
* points to them.
*
* TIP: If EIP-1153 (transient storage) is available on the chain you're deploying at,
* consider using {ReentrancyGuardTransient} instead.
*
* TIP: If you would like to learn more about reentrancy and alternative ways
* to protect against it, check out our blog post
* https://blog.openzeppelin.com/reentrancy-after-istanbul/[Reentrancy After Istanbul].
*/
abstract contract ReentrancyGuard {
// Booleans are more expensive than uint256 or any type that takes up a full
// word because each write operation emits an extra SLOAD to first read the
// slot's contents, replace the bits taken up by the boolean, and then write
// back. This is the compiler's defense against contract upgrades and
// pointer aliasing, and it cannot be disabled.
// The values being non-zero value makes deployment a bit more expensive,
// but in exchange the refund on every call to nonReentrant will be lower in
// amount. Since refunds are capped to a percentage of the total
// transaction's gas, it is best to keep them low in cases like this one, to
// increase the likelihood of the full refund coming into effect.
uint256 private constant NOT_ENTERED = 1;
uint256 private constant ENTERED = 2;
uint256 private _status;
/**
* @dev Unauthorized reentrant call.
*/
error ReentrancyGuardReentrantCall();
constructor() {
_status = NOT_ENTERED;
}
/**
* @dev Prevents a contract from calling itself, directly or indirectly.
* Calling a `nonReentrant` function from another `nonReentrant`
* function is not supported. It is possible to prevent this from happening
* by making the `nonReentrant` function external, and making it call a
* `private` function that does the actual work.
*/
modifier nonReentrant() {
_nonReentrantBefore();
_;
_nonReentrantAfter();
}
function _nonReentrantBefore() private {
// On the first call to nonReentrant, _status will be NOT_ENTERED
if (_status == ENTERED) {
revert ReentrancyGuardReentrantCall();
}
// Any calls to nonReentrant after this point will fail
_status = ENTERED;
}
function _nonReentrantAfter() private {
// By storing the original value once again, a refund is triggered (see
// https://eips.ethereum.org/EIPS/eip-2200)
_status = NOT_ENTERED;
}
/**
* @dev Returns true if the reentrancy guard is currently set to "entered", which indicates there is a
* `nonReentrant` function in the call stack.
*/
function _reentrancyGuardEntered() internal view returns (bool) {
return _status == ENTERED;
}
}
// SPDX-License-Identifier: MIT
/*
/$$ /$$ /$$$$$$$ /$$$$$$ /$$ /$$ /$$ /$$ /$$ /$$$$$$ /$$$$$$$ /$$$$$$$
| $$ /$$/| $$__ $$ /$$__ $$| $$ /$ | $$| $$$ | $$ | $$ /$$__ $$| $$__ $$ /$$__ $$
| $$ /$$/ | $$ \ $$| $$ \ $$| $$ /$$$| $$| $$$$| $$ | $$ | $$ \ $$| $$ \ $$| $$ \__/
| $$$$$/ | $$$$$$$/| $$ | $$| $$/$$ $$ $$| $$ $$ $$ | $$ | $$$$$$$$| $$$$$$$ | $$$$$$
| $$ $$ | $$__ $$| $$ | $$| $$$$_ $$$$| $$ $$$$ | $$ | $$__ $$| $$__ $$ \____ $$
| $$\ $$ | $$ \ $$| $$ | $$| $$$/ \ $$$| $$\ $$$ | $$ | $$ | $$| $$ \ $$ /$$ \ $$
| $$ \ $$| $$ | $$| $$$$$$/| $$/ \ $$| $$ \ $$ | $$$$$$$$| $$ | $$| $$$$$$$/| $$$$$$/
|__/ \__/|__/ |__/ \______/ |__/ \__/|__/ \__/ |________/|__/ |__/|_______/ \______/
krownlabs.app
x.com/krownlabs
discord.gg/KTU4krfhrG
*/
pragma solidity ^0.8.20;
import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
contract SonicRegistrar is Ownable, ReentrancyGuard {
uint256 public constant THREECHAR = 15 ether;
uint256 public constant FOURCHAR = 10 ether;
uint256 public constant FIVECHAR = 7.5 ether;
uint256 public constant SIXPLUSCHAR = 5 ether;
uint256 public constant YEAR = 365 days;
uint256 public constant MAX_REGISTRATION_YEARS = 5;
struct DiscountTier {
uint256 yearCount;
uint256 discount;
}
DiscountTier[] public discountTiers;
ISonicRegistry public registry;
event DomainRegistered(string name, address owner, uint256 price, uint256 yearCount);
event DomainRenewed(string name, uint256 tokenId, uint256 price, uint256 yearCount);
event PaymentWithdrawn(address to, uint256 amount);
constructor(address _registry) Ownable(msg.sender) {
registry = ISonicRegistry(_registry);
discountTiers.push(DiscountTier({yearCount: 2, discount: 500}));
discountTiers.push(DiscountTier({yearCount: 3, discount: 1000}));
discountTiers.push(DiscountTier({yearCount: 4, discount: 1500}));
discountTiers.push(DiscountTier({yearCount: 5, discount: 2000}));
}
function getBasePrice(string memory name) public pure returns (uint256) {
uint256 length = bytes(name).length;
if (length == 3) return THREECHAR;
if (length == 4) return FOURCHAR;
if (length == 5) return FIVECHAR;
return SIXPLUSCHAR;
}
function getDiscount(uint256 yearCount) public view returns (uint256) {
if (yearCount == 1) return 0;
for (uint256 i = 0; i < discountTiers.length; i++) {
if (discountTiers[i].yearCount == yearCount) {
return discountTiers[i].discount;
}
}
return 0;
}
function calculatePrice(string memory name, uint256 yearCount) public view returns (uint256) {
require(yearCount > 0 && yearCount <= MAX_REGISTRATION_YEARS, "Invalid year count");
uint256 basePrice = getBasePrice(name);
uint256 totalBasePrice = basePrice * yearCount;
uint256 discount = getDiscount(yearCount);
if (discount == 0) return totalBasePrice;
uint256 discountAmount = (totalBasePrice * discount) / 10000;
return totalBasePrice - discountAmount;
}
function register(string calldata name, uint256 yearCount) external payable nonReentrant {
require(isValidName(name), "Invalid name");
require(registry.available(name), "Name taken");
require(yearCount > 0 && yearCount <= MAX_REGISTRATION_YEARS, "Invalid year count");
uint256 price = calculatePrice(name, yearCount);
require(msg.value == price, "Incorrect payment");
registry.register(name, msg.sender, yearCount * YEAR);
emit DomainRegistered(name, msg.sender, price, yearCount);
}
function renew(string calldata name, uint256 yearCount) external payable nonReentrant {
uint256 tokenId = registry.nameToTokenId(name);
require(tokenId != 0, "Domain not found");
require(yearCount > 0 && yearCount <= MAX_REGISTRATION_YEARS, "Invalid year count");
uint256 price = calculatePrice(name, yearCount);
require(msg.value == price, "Incorrect payment");
registry.extend(tokenId, yearCount * YEAR);
emit DomainRenewed(name, tokenId, price, yearCount);
}
function getRenewalPrice(string calldata name, uint256 yearCount) external view returns (uint256) {
require(registry.nameToTokenId(name) != 0, "Domain not found");
return calculatePrice(name, yearCount);
}
function isValidName(string memory name) public pure returns (bool) {
bytes memory nameBytes = bytes(name);
uint256 length = nameBytes.length;
if (length < 3 || length > 64) return false;
if (!isLetter(nameBytes[0])) return false;
for (uint i = 0; i < length; i++) {
if (!isValidChar(nameBytes[i])) return false;
if (i > 0 && nameBytes[i] == '-' && nameBytes[i-1] == '-')
return false;
}
if (nameBytes[length-1] == '-') return false;
return true;
}
function isLetter(bytes1 char) internal pure returns (bool) {
return (char >= 0x61 && char <= 0x7A); // a-z
}
function isNumber(bytes1 char) internal pure returns (bool) {
return (char >= 0x30 && char <= 0x39); // 0-9
}
function isValidChar(bytes1 char) internal pure returns (bool) {
return isLetter(char) || isNumber(char) || char == 0x2D; // a-z, 0-9, -
}
function withdraw() external onlyOwner {
uint256 balance = address(this).balance;
require(balance > 0, "No balance");
(bool success, ) = owner().call{value: balance}("");
require(success, "Withdrawal failed");
emit PaymentWithdrawn(owner(), balance);
}
receive() external payable {}
}
interface ISonicRegistry {
function register(string memory name, address owner, uint256 duration) external returns (uint256);
function extend(uint256 tokenId, uint256 duration) external;
function available(string memory name) external view returns (bool);
function nameToTokenId(string memory name) external view returns (uint256);
}