Skip to content

Instantly share code, notes, and snippets.

@umahatokula
Last active March 17, 2023 20:22
Show Gist options
  • Save umahatokula/a93b90b8f63fc9dc277c6cab8547bd4e to your computer and use it in GitHub Desktop.
Save umahatokula/a93b90b8f63fc9dc277c6cab8547bd4e to your computer and use it in GitHub Desktop.
A collection of practice smart contract using the Solidity programming language
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/utils/math/SafeMath.sol";
contract TranferTokens {
using SafeMath for uint;
address public owner;
// map user address to balance
mapping(address => uint) public balances;
modifier onlyOwner() {
require(msg.sender == owner, "You are not the owner.");
_;
}
// Constructor
constructor() {
owner = msg.sender;
}
// Fallback functions
receive() external payable {}
fallback() external payable {}
function getCustomerBalance() external view returns (uint) {
// return the balance of the caller
return balances[msg.sender];
}
function transferToken(
address to,
uint transferAmount
) external payable returns (bool) {
// check balance
require(balances[msg.sender] >= transferAmount, "insufficient balance");
// prevent underflow
balances[msg.sender] = balances[msg.sender].sub(transferAmount);
// transfer token
(bool success, ) = to.call{value: transferAmount}("");
require(success, "Call failed");
// update receiver's balance
balances[to] = balances[to].add(transferAmount);
return true;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.9;
contract VotingSystem {
// Option to be voted on
string public option;
// Mapping of voter addresses to their vote status
mapping(address => bool) public voters;
// Number of votes for each option
uint256 public voteCount;
// Event to be emitted when a new vote is cast
event VoteCast(address voter);
// Constructor function to set the option to be voted on
constructor(string memory _option) {
option = _option;
}
// Function to cast a vote for the given option
function castVote() public {
// Ensure voter has not already voted
require(!voters[msg.sender], "You have already voted");
// Mark voter as having voted
voters[msg.sender] = true;
// Increment vote count for option
voteCount++;
// Emit VoteCast event
emit VoteCast(msg.sender);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment