Created
April 19, 2025 20:43
-
-
Save lucaspere/69fc58565ef9851dbcc200cadb5f7695 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.26+commit.8a97fa7a.js&optimize=false&runs=200&gist=
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
import "contracts/BaseRegistry.sol"; | |
error MaximumPontuationExceeded(); | |
error InvalidWithdrawalValue(); | |
error InsufficientBalance(uint balance, uint value); | |
error FailTransferEther(); | |
contract AdvancedRegistry is BaseRegistry { | |
uint public eventsCounter; | |
mapping(address => uint) public fundsToWithdrawl; | |
event AdvancedRegistryEvent(uint indexed counter, string description); | |
event DonationReceived(address indexed donor, uint value); | |
event WithdrawlMade(address indexed destination, uint value); | |
event WithdrawlRequested(address indexed solicitante, uint valor); | |
function withdrawlBalance (address payable _destination, uint _value) public onlyOwner { | |
require(_value > 0, "Value must be greater than zero."); | |
uint currentBalance = address(this).balance; | |
require(_value < currentBalance, "Not enough balance in the contract"); | |
require(_destination != address(0), "Inform a valid address."); | |
(bool success,) = _destination.call{value: _value}(""); | |
if(!success) revert(); | |
emit WithdrawlMade(_destination, _value); | |
} | |
function requestWithdrawl( uint _value) public onlyOwner { | |
require(_value > 0, "Value must be greater than zero."); | |
uint currentBalance = address(this).balance; | |
require(_value <= currentBalance, InsufficientBalance(currentBalance, _value)); | |
fundsToWithdrawl[msg.sender] += _value; | |
emit WithdrawlRequested(msg.sender, _value); | |
} | |
function withdrawl() public onlyOwner { | |
for (uint i = 0; i < fundsToWithdrawl.length; ++i) { | |
address payable destination = address(fundsToWithdrawl[i]); | |
uint value = fundsToWithdrawl[destination]; | |
if (_checkIfShould | |
} | |
function donate() external payable { | |
require (msg.value > 0, "Donation value must be greater than zero."); | |
emit DonationReceived(msg.sender, msg.value); | |
} | |
function getContractBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
function registryEvent(string memory _description) external override { | |
eventsCounter++; | |
emit AdvancedRegistryEvent(eventsCounter, _description); | |
} | |
function definyPontuations(address _user, uint _points) public virtual override onlyOwner { | |
require(_points <= 1000, "Maximum points in advanced registry is 1000 points;"); | |
super.definyPontuations(_user, _points); | |
} | |
function getRegistryType() internal virtual override view returns(string memory) { | |
return "Advanced"; | |
} | |
function getType() public view returns (string memory) { | |
return getRegistryType(); | |
} | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
error DenyAccessOnlyOwner(); | |
error DenyAccessOnlyAdmin(); | |
error DenyAccessAdminOrOwner(); | |
error AddressCannotBeZero(); | |
error UserIsNotAdmin(); | |
error UserAlreadyIsAdmin(); | |
abstract contract BaseRegistry { | |
address owner; | |
struct UserInfo { | |
uint pontuation; | |
bool isAdmin; | |
uint updated_at; | |
} | |
mapping(address => UserInfo) public userInfos; | |
event AdminAdded(address indexed admin); | |
event AdminRemoved(address indexed admin); | |
event newPoints ( | |
address indexed inuser, | |
uint points | |
); | |
constructor() { | |
owner = msg.sender; | |
} | |
function addAdmin(address _newAdmin) public onlyOwner { | |
require(_newAdmin != address(0), AddressCannotBeZero()); | |
UserInfo storage user = userInfos[_newAdmin]; | |
require(user.isAdmin, UserAlreadyIsAdmin()); | |
user.isAdmin= true; | |
user.updated_at = block.timestamp; | |
emit AdminAdded(_newAdmin); | |
} | |
function removeAdmin(address _admin) public onlyOwner { | |
require(_admin != address(0), AddressCannotBeZero()); | |
UserInfo storage user = userInfos[_admin]; | |
require(!user.isAdmin, UserIsNotAdmin()); | |
user.isAdmin = false; | |
user.updated_at = block.timestamp; | |
emit AdminRemoved(_admin); | |
} | |
function definyPontuations(address _user, uint _points) public virtual onlyOwner { | |
require(_user != address(0), AddressCannotBeZero()); | |
UserInfo storage user = userInfos[_user]; | |
user.pontuation = _points; | |
user.updated_at = block.timestamp; | |
emit newPoints(_user, user.pontuation); | |
} | |
modifier onlyOwner() { | |
require(msg.sender == owner, DenyAccessOnlyOwner()); | |
_; | |
} | |
modifier onlyAdmin() { | |
UserInfo storage user = userInfos[msg.sender]; | |
require(user.isAdmin, DenyAccessOnlyAdmin()); | |
_; | |
} | |
modifier onlyOwnerOrAdmin() { | |
UserInfo storage user = userInfos[msg.sender]; | |
require(owner == msg.sender || user.isAdmin, DenyAccessAdminOrOwner()); | |
_; | |
} | |
function registryEvent(string memory _description) external virtual; | |
function getRegistryType() internal virtual returns (string memory) { | |
return "Base"; | |
} | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
import "./IRegistryReader.sol"; | |
contract ExternalReader { | |
IRegistryReader public registryReader; | |
constructor(address _registryReader) { | |
require(_registryReader != address(0), "Registry reader address should not be zero"); | |
registryReader = IRegistryReader(_registryReader); | |
} | |
function currentMessage() external view returns (string memory) { | |
return registryReader.currentMessage(); | |
} | |
function readOwnerRegistry() public view returns(address) { | |
return registryReader.owner(); | |
} | |
function readPontuations(address _user) external view returns (uint) { | |
return registryReader.pontuations(_user); | |
} | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
interface IRegistryReader { | |
function currentMessage() external view returns (string memory); | |
function owner() external view returns (address); | |
function pontuations(address _user) external view returns (uint); | |
} |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
import "contracts/BaseRegistry.sol"; | |
import "contracts/StringUtils.sol"; | |
contract SimplyRegistry is BaseRegistry { | |
using StringUtils for string; | |
string public currentMessage; | |
event MessageUpdated ( | |
string newMessage | |
); | |
event SimplyEventRegistred(string description); | |
function registryEvent(string memory _description) external override { | |
} | |
constructor(string memory _initialMessage) { | |
currentMessage = _initialMessage; | |
} | |
function updateMessage(string memory _message) public { | |
if(!currentMessage.equals(_message)) { | |
currentMessage = _message; | |
emit MessageUpdated(currentMessage); | |
} else { | |
revert("New message equal to the current one"); | |
} | |
} | |
function myPontuation() public view returns(uint) { | |
UserInfo storage user = userInfos[msg.sender]; | |
return user.pontuation; | |
} | |
function getRegistryType() internal virtual override view returns (string memory) { | |
return "Simple"; | |
} | |
function getType() public view returns (string memory) { | |
return getRegistryType(); | |
} | |
} | |
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
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.8.2 <0.9.0; | |
library StringUtils { | |
function equals(string memory s1, string memory s2) internal pure returns (bool) { | |
return keccak256(abi.encodePacked(s1)) == keccak256(abi.encodePacked(s2)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment