Skip to content

Instantly share code, notes, and snippets.

@KBPsystem777
Created May 23, 2022 15:26
Show Gist options
  • Save KBPsystem777/ecd012747f120ec5573639a627dc70cb to your computer and use it in GitHub Desktop.
Save KBPsystem777/ecd012747f120ec5573639a627dc70cb to your computer and use it in GitHub Desktop.
Payable counter contract to increment/decrement. This accepts Ethers in order to run the functions and the owner has the capability to withdraw funds generated from running the increment/decrement function
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Counter {
uint public count;
address payable owner;
uint256 public FEE = 2 ether;
constructor() payable {
owner = payable(msg.sender);
}
// Function to get the current count
function get() public view returns (uint) {
return count;
}
// Function to increment count by 1
// @notice - Caller of this function need to pay 1 Ether in order to run this.
function inc() public payable {
require(msg.value == FEE, "Need to send 2 ether in order to run this function");
count += 1;
}
// Function to decrement count by 1
// @notice - Caller of this function need to pay 1 Ether in order to run this.
function dec() public payable {
// This function will fail if count = 0
require(msg.value == FEE, "Need to send 2 ether in order to run this function");
count -= 1;
}
// @notice Fetch the current contract balance or total ethers sent to this contract
function getBalance() public view returns(uint256) {
return address(this).balance;
}
modifier shouldBeTheOwner() {
require(msg.sender == owner, "You should be the owner of this running in order to withdraw funds!");
_;
}
// @notice Withdraw function. This will withdraw all the funds from the contract from the minting
function withdraw() public shouldBeTheOwner {
require(address(this).balance > 0, "Contract Balance: 0; Nothing to withdraw here!");
payable(msg.sender).transfer(address(this).balance);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment