Skip to content

Instantly share code, notes, and snippets.

@superXdev
Last active May 26, 2024 01:25
Show Gist options
  • Save superXdev/fa4a3ca0f4add7136ccb874c7f1c1a9d to your computer and use it in GitHub Desktop.
Save superXdev/fa4a3ca0f4add7136ccb874c7f1c1a9d to your computer and use it in GitHub Desktop.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
// Interface for ERC20 token standard
interface IERC20 {
function balanceOf(address account) external view returns (uint256);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
// Contract implementing basic functionalities for interacting with ERC20 tokens
contract SniffToken {
IERC20 public _token;
constructor(IERC20 _tokenAddress) {
_token = _tokenAddress;
}
// Returns the balance of the specified address
function checkBalance(address targetAddress) public view returns(uint256) {
return _token.balanceOf(targetAddress);
}
// Deposits a specified amount of tokens to the contract from the caller's address
function deposit(uint256 amount) public returns(bool) {
_token.transferFrom(msg.sender, address(this), amount);
return true;
}
}
/**
* @title Bookmark
* @dev Contract for managing a list of books using a struct and array mapping.
*/
contract Bookmark {
struct Book {
string title;
string author;
bool completed;
}
Book[] public books;
// Saves a new book with the provided title and author
function saveBook(string memory _title, string memory _author) public {
books.push(Book(_title, _author, false));
}
// Returns the details of the book at the specified index
function getBook(uint index) external view returns(string memory title, string memory author, bool completed) {
Book storage book = books[index];
return (book.title, book.author, book.completed);
}
// Marks the book at the specified index as completed
function complete(uint index) public {
Book storage book = books[index];
book.completed = true;
}
}
/**
* @title People
* @dev Contract for managing guest information and loans using mappings.
*/
contract People {
mapping(uint => string) public guest;
mapping(address => uint) public loans;
event LoanUpdated(address indexed _borrower, uint _amount);
// Adds a loan for the specified borrower
function addLoan(address borrower, uint amount) public {
require(amount > 10, "Must be greater than 10");
loans[borrower] = amount;
emit LoanUpdated(borrower, amount);
}
// Removes the loan for the specified borrower
function removeLoan(address borrower) public {
delete loans[borrower];
}
// Returns the name of the guest with the specified ID
function getGuest(uint id) public view returns(string memory) {
return guest[id];
}
// Adds a new guest with the specified ID and name
function addGuest(uint id, string memory name) public {
guest[id] = name;
}
// Removes the guest with the specified ID
function removeGuest(uint id) public {
delete guest[id];
}
}
/**
* @title Ownable
* @dev Contract providing basic ownership functionalities.
*/
contract Ownable {
address public owner;
constructor() {
owner = msg.sender;
}
// Modifier to restrict access to only the contract owner
modifier onlyOwner {
require(msg.sender == owner, "Only owner can access this function");
_;
}
// Allows the owner to renounce ownership
function renounceOwnership() public onlyOwner {
owner = address(0);
}
}
/**
* @title Basic
* @dev Contract with basic functionalities and owner restrictions.
*/
contract Basic is Ownable {
string public myName;
bool public indonesian = true;
uint public age = 300;
uint[3] public myNum = [2, 3, 7];
string[] public favorites = ["Game", "Anime", "Movie", "Food"];
address[] public payer;
address private myWallet = 0x70F657164e5b75689b64B7fd1fA275F334f28e18;
constructor(string memory _name) {
myName = _name;
}
// Updates the name of the contract
function updateName(string memory _name) public onlyOwner {
myName = _name;
}
// Returns the wallet address
function getWallet() public view returns(address) {
return myWallet;
}
// Adds a new favorite item
function addFavorite(string memory favorite) public onlyOwner returns(bool) {
favorites.push(favorite);
return true;
}
// Removes the favorite item at the specified index
function removeFavorite(uint index) public onlyOwner {
delete favorites[index];
}
// Adds two numbers and returns the result
function add(uint a, uint b) external pure returns(uint) {
return a + b;
}
// Checks if a number is even or odd and returns the result
function evenOrOdd(uint num) external pure returns(string memory) {
if(num % 2 == 0) {
return "even";
}
return "odd";
// return num % 2 == 0 ? "even" : "odd";
}
// Function to receive Ether
receive() external payable {
payer.push(msg.sender);
}
// Fallback function to refund Ether
fallback() external payable {
(bool sent, ) = address(this).call{value: msg.value}("");
require(sent, "Failed to refund");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment