Skip to content

Commit

Permalink
Add read-only operation in Solidity to improve performance
Browse files Browse the repository at this point in the history
  • Loading branch information
simonesestito committed Jan 11, 2024
1 parent d69e795 commit b929285
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 283 deletions.
4 changes: 2 additions & 2 deletions smart_contract/Geth.sol
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "./node_modules/@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "./node_modules/@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

/**
* @dev Extension of {ERC20} that allows {DomainMarketplace} to get full allowance
Expand Down
38 changes: 31 additions & 7 deletions smart_contract/GethDomain.sol
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ contract DomainMarketplace is ERC721Royalty, Ownable {
event IpfsOverwritten(bytes indexed domain, address indexed owner);

// Costruttore del contratto
constructor() ERC721("GethDomain", "GETHD") Ownable(msg.sender){
constructor() ERC721("GethDomain", "GETHD") Ownable(msg.sender) {
payGeth = IERC20(0xa514C64fd0e5Fe44B2C4448AfB8C6f3268232169);
}

Expand All @@ -62,7 +62,7 @@ contract DomainMarketplace is ERC721Royalty, Ownable {

// function approveGeth(uint256 amount) external {
// require(payGeth.approve(address(this), amount), "Approval failed");
// }
// }

// Funzione per acquistare un dominio ex novo
function purchaseNewDomain(bytes calldata domain, bytes calldata torOrIpfs, bool isTor) external {
Expand All @@ -82,7 +82,7 @@ contract DomainMarketplace is ERC721Royalty, Ownable {
keys.push(domain);
domains[domain].resoldTimes++;
}

function purchaseExistingDomain(bytes calldata domain) external {
uint256 id = uint256(keccak256(abi.encodePacked(domain)));
address owner = ownerOf(id);
Expand All @@ -100,8 +100,8 @@ contract DomainMarketplace is ERC721Royalty, Ownable {

//GESTIONE ROYALTIES

address receiver;
uint256 royaltyAmount;// Trasferisce il compenso all'acquirente originale (generatore)
address receiver;
uint256 royaltyAmount;// Trasferisce il compenso all'acquirente originale (generatore)
if (domains[domain].resoldTimes > 2) {

// Calcola il compenso per l'acquirente originale (generatore)
Expand Down Expand Up @@ -137,7 +137,7 @@ contract DomainMarketplace is ERC721Royalty, Ownable {
require(msg.sender == ownerOf(id), "Not the domain owner");
_;
}

// Funzione per mettere un dominio in vendita
function sellDomain(bytes calldata domain, uint32 price) external onlyDomainOwner(domain) returns (uint256 prezzo){
// uint256 id = uint256(keccak256(abi.encodePacked(domain)));
Expand All @@ -150,7 +150,7 @@ contract DomainMarketplace is ERC721Royalty, Ownable {
// evento per notificare gli altri utenti che un certo dominio è in vendita
emit DomainForSale(domain, msg.sender, price);
return price;
}
}

function retrieveDomain(bytes calldata domain) external onlyDomainOwner(domain){
// uint256 id = uint256(keccak256(abi.encodePacked(domain)));
Expand Down Expand Up @@ -187,4 +187,28 @@ contract DomainMarketplace is ERC721Royalty, Ownable {
function getId(bytes calldata domain) external pure returns (uint256 id) {
return uint256(keccak256(abi.encodePacked(domain)));
}

function getMyDomainIndexes() external view returns (uint256[] memory) {
uint256[] memory myDomains = new uint256[](balanceOf(msg.sender));
uint256 j = 0;
for (uint256 i = 0; i < keys.length; i++) {
// Collect the indexes of all the domains owned by current user
bytes memory domain = keys[i];
if (ownerOf(uint256(keccak256(abi.encodePacked(domain)))) == msg.sender) {
myDomains[j] = i;
j += 1;
}
}
return myDomains;
}

function findDomainById(uint256 id) external view returns (uint256) {
for (uint256 i = 0; i < keys.length; i++) {
bytes memory domain = keys[i];
if (uint256(keccak256(abi.encodePacked(domain))) == id) {
return i;
}
}
return 0;
}
}
Loading

0 comments on commit b929285

Please sign in to comment.