-
Notifications
You must be signed in to change notification settings - Fork 0
/
EnsWithSales.sol
107 lines (87 loc) · 3.49 KB
/
EnsWithSales.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
contract BionicOwlsEns {
mapping(address => string) public addresses;
mapping(string => address) public names;
mapping(string => address) public creators;
mapping(address => bool) public nameCreationStatus;
mapping(string => uint256) public namePrices;
string[] public allNames;
string[] public allNamesForSale;
function createName(string memory name) public {
require(names[name] == address(0), "Name already exists");
require(!nameCreationStatus[msg.sender], "You have already created a name");
creators[name] = msg.sender;
names[name] = msg.sender;
addresses[msg.sender] = name;
allNames.push(name);
nameCreationStatus[msg.sender] = true;
}
function transferName(string memory name, address newOwner) public {
require(names[name] == msg.sender, "You don't own this name");
names[name] = newOwner;
addresses[newOwner] = name;
addresses[msg.sender] = "";
nameCreationStatus[msg.sender] = false;
nameCreationStatus[newOwner] = true;
}
function deleteName(string memory name) public {
require(names[name] == msg.sender, "You don't own this name");
addresses[msg.sender] = "";
delete names[name];
delete creators[name];
nameCreationStatus[msg.sender] = false;
}
function getCreator(string memory name) public view returns (address) {
return creators[name];
}
function getOwner(string memory name) public view returns (address) {
return names[name];
}
function setNamePrice(string memory name, uint256 price) public {
require(names[name] == msg.sender, "You don't own this name");
namePrices[name] = price;
allNamesForSale.push(name);
}
function DeListName(string memory name) public {
require(names[name] == msg.sender, "You don't own this name");
namePrices[name] = 0;
deleteNameFromAllNames(name);
}
function purchaseName(string memory name) public payable {
require(names[name] != address(0), "Name does not exist");
require(namePrices[name] != 0, "Name is not for sale");
require(msg.value >= namePrices[name], "Insufficient payment");
address owner = names[name];
names[name] = msg.sender;
addresses[owner] = "";
addresses[msg.sender] = name;
nameCreationStatus[owner] = false;
nameCreationStatus[msg.sender] = true;
namePrices[name] = 0;
(bool success, ) = payable(owner).call{value: msg.value}("");
require(success, "Transfer failed.");
deleteNameFromAllNamesForSale(name);
}
function getAllNames() public view returns (string[] memory) {
return allNamesForSale;
}
function deleteNameFromAllNamesForSale(string memory name) internal {
for (uint i = 0; i < allNamesForSale.length; i++) {
if (keccak256(bytes(allNamesForSale[i])) == keccak256(bytes(name))) {
allNamesForSale[i] = allNamesForSale[allNamesForSale.length - 1];
allNamesForSale.pop();
break;
}
}
}
function deleteNameFromAllNames(string memory name) internal {
for (uint i = 0; i < allNames.length; i++) {
if (keccak256(bytes(allNames[i])) == keccak256(bytes(name))) {
allNames[i] = allNames[allNames.length - 1];
allNames.pop();
break;
}
}
}
}