Skip to content

Creating some Custom Errors #160

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions contracts/Shared Wallet/SharedWallet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
pragma solidity ^0.8.9;

contract SharedWallet {
error Insufficient__Balance( );
address private _owner;

mapping(address => bool) private _owners;
Expand Down Expand Up @@ -44,14 +45,18 @@ contract SharedWallet {

// withdraw fund from the shared wallet into the signer of transaction
function withdraw(uint256 amount) public validOwner {
require(address(this).balance >= amount);
if(address(this).balance<amount){
revert Insufficient__Balance();
}
payable(msg.sender).transfer(amount);
emit WithdrawFunds(msg.sender, amount);
}

// transfer fund from the shared wallet into another address
function transferTo(address payable to, uint256 amount) public validOwner {
require(address(this).balance >= amount);
if(address(this).balance<amount){
revert Insufficient__Balance();
}
payable(to).transfer(amount);
emit TransferFunds(msg.sender, to, amount);
}
Expand Down
12 changes: 8 additions & 4 deletions contracts/TimeLock/TimeLock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ pragma solidity ^0.8.9;
import '@openzeppelin/contracts/token/ERC20/IERC20.sol';

contract TimeLock{

error Only__Owner_Can_Withdraw();
error Cannot_Withdraw_Before_EndTime(uint end_Time );
uint public immutable endTime; //end time
address payable public immutable owner;

Expand All @@ -22,9 +23,12 @@ contract TimeLock{
receive() external payable {}

function withdraw(address token, uint amount) external {
require(msg.sender == owner, "Only owner can withdraw");
require(block.timestamp >= endTime, "Cannot withdraw before endTime");

if(msg.sender!=owner){
revert Only__Owner_Can_Withdraw();
}
if(block.timestamp<endTime){
revert Cannot_Withdraw_Before_EndTime(endTime);
}
//number of tokens = 0, so we are just withdrawing ETH
if(token == address(0)){
owner.transfer(amount);
Expand Down
36 changes: 22 additions & 14 deletions contracts/Voting/Election.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
pragma solidity ^0.8.4;

contract Election {

error Voter__Already_Voted();
error Voter__Has__No_Right_To_Vote();
error Only__Chairperson_Can_Choose_Voters();
// It will represent a single voter.
struct Voter {
uint weight; // weight is accumulated by delegation
Expand Down Expand Up @@ -46,14 +48,13 @@ contract Election {
// Give `voter` the right to vote on this ballot.
// May only be called by `chairperson`.
function giveRightToVote(address voter) external {
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
if(msg.sender!=chairperson){
revert Only__Chairperson_Can_Choose_Voters();
}
if(voters[voter].voted){
revert Voter__Already_Voted() ;
}

require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
Expand All @@ -62,9 +63,12 @@ contract Election {
function delegate(address to) external {
// assigns reference
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "You have no right to vote");
require(!sender.voted, "You already voted.");

if(sender.weight==0){
revert Voter__Has__No_Right_To_Vote();
}
if(sender.voted){
revert Voter__Already_Voted();
}
require(to != msg.sender, "Self-delegation is disallowed.");
while (voters[to].delegate != address(0)) {
to = voters[to].delegate;
Expand Down Expand Up @@ -96,8 +100,12 @@ contract Election {
/// to proposal `proposals[proposal].name`.
function vote(uint proposal) external {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
if(sender.weight==0){
revert Voter__Has__No_Right_To_Vote();
}
if(sender.voted){
revert Voter__Already_Voted();
}
sender.voted = true;
sender.vote = proposal;

Expand Down
Loading