Skip to content

Instantly share code, notes, and snippets.

@gsthina
Created May 20, 2021 08:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsthina/0f3d34757f8e89e64d7b6b5907f0baf0 to your computer and use it in GitHub Desktop.
Save gsthina/0f3d34757f8e89e64d7b6b5907f0baf0 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.4.18+commit.9cf6e910.js&optimize=false&gist=
pragma solidity ^0.4.16;
interface High4Teens {
function transferFrom(address from, address to, uint tokens);
}
contract Airdrop {
High4Teens public token;
address public tokenHolder;
uint amountToTransfer;
function Airdrop(address addressOfToken, address addressOfHolder, uint fixedAmount){
token = High4Teens(addressOfToken);
tokenHolder = addressOfHolder;
amountToTransfer = fixedAmount;
}
function drop() public {
token.transferFrom(tokenHolder, msg.sender, amountToTransfer);
}
}
pragma solidity 0.5.1;
import './Ownable.sol';
contract AnimalContract is Ownable{
event animalAdded(address owner, string animalName);
enum AnimalType {DOG, CAT}
struct Animal {
string name;
uint age;
AnimalType animalType;
}
mapping (address => Animal[]) ownerToAnimals;
function _addAnimal(string memory _name, uint _age, AnimalType _animalType) internal onlyOwner returns (uint) {
emit animalAdded(msg.sender, _name);
return ownerToAnimals[msg.sender].push(Animal(_name, _age, _animalType)) - 1;
}
function getAnimal(uint _id) public view returns (string memory){
return ownerToAnimals[msg.sender][_id].name;
}
}
pragma solidity >=0.4.22 <0.6.0;
contract Ballot {
struct Voter {
uint weight;
bool voted;
uint8 vote;
address delegate;
}
struct Proposal {
uint voteCount;
}
address chairperson;
mapping(address => Voter) voters;
Proposal[] proposals;
/// Create a new ballot with $(_numProposals) different proposals.
constructor(uint8 _numProposals) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
proposals.length = _numProposals;
}
/// Give $(toVoter) the right to vote on this ballot.
/// May only be called by $(chairperson).
function giveRightToVote(address toVoter) public {
if (msg.sender != chairperson || voters[toVoter].voted) return;
voters[toVoter].weight = 1;
}
/// Delegate your vote to the voter $(to).
function delegate(address to) public {
Voter storage sender = voters[msg.sender]; // assigns reference
if (sender.voted) return;
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender)
to = voters[to].delegate;
if (to == msg.sender) return;
sender.voted = true;
sender.delegate = to;
Voter storage delegateTo = voters[to];
if (delegateTo.voted)
proposals[delegateTo.vote].voteCount += sender.weight;
else
delegateTo.weight += sender.weight;
}
/// Give a single vote to proposal $(toProposal).
function vote(uint8 toProposal) public {
Voter storage sender = voters[msg.sender];
if (sender.voted || toProposal >= proposals.length) return;
sender.voted = true;
sender.vote = toProposal;
proposals[toProposal].voteCount += sender.weight;
}
function winningProposal() public view returns (uint8 _winningProposal) {
uint256 winningVoteCount = 0;
for (uint8 prop = 0; prop < proposals.length; prop++)
if (proposals[prop].voteCount > winningVoteCount) {
winningVoteCount = proposals[prop].voteCount;
_winningProposal = prop;
}
}
}
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./ballot.sol";
contract test3 {
Ballot ballotToTest;
function beforeAll () public {
ballotToTest = new Ballot(2);
}
function checkWinningProposal () public {
ballotToTest.vote(1);
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal");
}
function checkWinninProposalWithReturnValue () public view returns (bool) {
return ballotToTest.winningProposal() == 1;
}
}
pragma solidity ^0.4.16;
interface High4Teens {
function transfer(address receiver, uint amount);
}
contract Crowdsale {
address public beneficiary;
uint public fundingGoal;
uint public totalAmountRaised;
uint public crowdSaleDeadline;
uint public tokenPrice;
High4Teens public token;
mapping(address => uint) public balanceOf;
bool fundingGoalReached = false;
bool crowdSaleClosed = false;
/**
* Constructor
* ifSuccessfulSendTo: Address where funds should be sent if sale reaches target
* goalInEther: What is the target goal for the crowdsale in ethers.
* durationInMinutes: How long will the crowdsale be running.
* tokenPriceInEther: How much does each token cost
* addressOfToken: Where is the token contract deployed.
*/
function Crowdsale(
address ifSuccessfulSendTo,
uint goalInEther,
uint durationInMinutes,
uint tokenPriceInEther,
address addressOfToken
) {
beneficiary = ifSuccessfulSendTo;
fundingGoal = goalInEther;
crowdSaleDeadline = now + durationInMinutes * 1 minutes;
tokenPrice = tokenPriceInEther * 1 ether;
token = High4Teens(addressOfToken);
}
/**
* Fallback function
*
* Default function which gets called when someone sends money to the contract. Will be used for joining sale.
*/
function () payable {
require(!crowdSaleClosed);
uint amount = msg.value;
balanceOf[msg.sender] += amount;
totalAmountRaised += amount;
token.transfer(msg.sender, amount/tokenPrice);
}
/**
* Modifier used to check if deadline for crowdsale has passed
*/
modifier afterDeadline() {
if(now >= crowdSaleDeadline){
_;
}
}
/**
* Check if the funding goal was reached. Will only be checked if afterDeadline modifier above is true.
*
*/
function checkGoalReached() afterDeadline {
if(totalAmountRaised >= fundingGoal){
fundingGoalReached = true;
}
crowdSaleClosed = true;
}
/**
* Withdraw the funds
*
* Will withdraw the money after the deadline has been reached. If the goal was reached, only the owner can withdraw money to the beneficiary account.
* If you goal was not reached, everyone who participated can withdraw their share.
*/
function safeWithdrawal() afterDeadline {
if(!fundingGoalReached){
uint amount = balanceOf[msg.sender];
balanceOf[msg.sender] = 0;
if(amount > 0){
if(msg.sender.send(amount)){
}
}
}
if(fundingGoalReached && msg.sender == beneficiary){
if(!beneficiary.send(totalAmountRaised)){
fundingGoalReached = false;
}
}
}
}
pragma solidity 0.5.1;
import './Animal.sol';
contract DogContract is AnimalContract {
modifier costs(uint amount){
require (msg.value >= amount);
_;
if(msg.value > amount){
msg.sender.transfer(msg.value - amount);
}
}
function addDog(string memory _name, uint _age) public payable costs(500) returns (uint){
return _addAnimal(_name, _age, AnimalType.DOG);
}
function getBalance() public view returns (uint) {
return address(this).balance;
}
}
pragma solidity 0.5.1;
contract DogContract{
function addDog(string memory _name, uint _age) public payable returns (uint);
function getBalance() public view returns (uint);
}
contract ExternalContract {
DogContract dc = DogContract(0x83171cf735D1BA4AAaf1571DE744697A8AFBA3Dd);
function addExternalDog(string memory _name, uint _age) public payable returns (uint){
return dc.addDog.value(msg.value)(_name, _age);
}
function getExternalBalance() public view returns (uint){
return dc.getBalance();
}
}
pragma solidity 0.5.1;
contract C {
// (2**256 - 1) + 1 = 0
function overflow() public pure returns (uint256 _overflow) {
uint256 max = 2**256 -1;
return max + 1;
}
// 0 - 1 = 2**256 - 1
function underflow() public pure returns (uint256 _underflow) {
uint256 min = 0;
return min - 1;
}
}
pragma solidity 0.5.1;
contract Ownable{
address public owner;
modifier onlyOwner(){
require (msg.sender == owner);
_;
}
constructor() public {
owner = msg.sender;
}
function transferOwnership(address newOwner) public onlyOwner {
// require(newOwner != address(0));
owner = newOwner;
}
}
pragma solidity 0.5.1;
library SafeMath {
/**
* @dev Multiplies two numbers, throws on overflow.
*/
function mul(uint256 a, uint256 b) internal pure returns (uint256 c) {
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the
// benefit is lost if 'b' is also tested.
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522
if (a == 0) {
return 0;
}
c = a * b;
assert(c / a == b);
return c;
}
/**
* @dev Integer division of two numbers, truncating the quotient.
*/
function div(uint256 a, uint256 b) internal pure returns (uint256) {
// assert(b > 0); // Solidity automatically throws when dividing by 0
// uint256 c = a / b;
// assert(a == b * c + a % b); // There is no case in which this doesn't hold
return a / b;
}
/**
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend).
*/
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
assert(b <= a);
return a - b;
}
/**
* @dev Adds two numbers, throws on overflow.
*/
function add(uint256 a, uint256 b) internal pure returns (uint256 c) {
c = a + b;
assert(c >= a);
return c;
}
}
contract C {
using SafeMath for uint256;
// (2**256 - 1) + 1 = 0
function overflow() public pure returns (uint256 _overflow) {
uint256 max = 2**256 -1;
return max.add(1);
}
// 0 - 1 = 2**256 - 1
function underflow() public pure returns (uint256 _underflow) {
uint256 min = 0;
return min.sub(1);
}
}
pragma solidity ^0.4.18;
// ----------------------------------------------------------------------------
// High4Teens token contract
//
// Deployed to : 0xEb7208A7453dFC3C7380170A99fc5156A160Cd75
// Symbol : H4T
// Name : High4Teens
// Total supply: 100000000
// Decimals : 18
//
// Enjoy.
//
// (c) by Moritz Neto with BokkyPooBah / Bok Consulting Pty Ltd Au 2017. The MIT Licence.
// ----------------------------------------------------------------------------
// ----------------------------------------------------------------------------
// Safe maths
// ----------------------------------------------------------------------------
contract SafeMath {
function safeAdd(uint a, uint b) public pure returns (uint c) {
c = a + b;
require(c >= a);
}
function safeSub(uint a, uint b) public pure returns (uint c) {
require(b <= a);
c = a - b;
}
function safeMul(uint a, uint b) public pure returns (uint c) {
c = a * b;
require(a == 0 || c / a == b);
}
function safeDiv(uint a, uint b) public pure returns (uint c) {
require(b > 0);
c = a / b;
}
}
// ----------------------------------------------------------------------------
// ERC Token Standard #20 Interface
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// ----------------------------------------------------------------------------
contract ERC20Interface {
function totalSupply() public view returns (uint);
function balanceOf(address tokenOwner) public view returns (uint balance);
function allowance(address tokenOwner, address spender) public view returns (uint remaining);
function transfer(address to, uint tokens) public returns (bool success);
function approve(address spender, uint tokens) public returns (bool success);
function transferFrom(address from, address to, uint tokens) public returns (bool success);
event Transfer(address indexed from, address indexed to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
// ----------------------------------------------------------------------------
// Contract function to receive approval and execute function in one call
//
// Borrowed from MiniMeToken
// ----------------------------------------------------------------------------
contract ApproveAndCallFallBack {
function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
// ----------------------------------------------------------------------------
// Owned contract
// ----------------------------------------------------------------------------
contract Owned {
address public owner;
address public newOwner;
event OwnershipTransferred(address indexed _from, address indexed _to);
function Owned() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner);
_;
}
function transferOwnership(address _newOwner) public onlyOwner {
newOwner = _newOwner;
}
function acceptOwnership() public {
require(msg.sender == newOwner);
OwnershipTransferred(owner, newOwner);
owner = newOwner;
newOwner = address(0);
}
}
// ----------------------------------------------------------------------------
// ERC20 Token, with the addition of symbol, name and decimals and assisted
// token transfers
// ----------------------------------------------------------------------------
contract High4Teens is ERC20Interface, Owned, SafeMath {
string public symbol;
string public name;
uint8 public decimals;
uint public _totalSupply;
mapping(address => uint) balances;
mapping(address => mapping(address => uint)) allowed;
// ------------------------------------------------------------------------
// Constructor
// ------------------------------------------------------------------------
function High4Teens() public {
symbol = "H4T";
name = "High4Teens";
decimals = 18;
_totalSupply = 100000000000000000000000000;
balances[msg.sender] = _totalSupply;
Transfer(address(0), msg.sender, _totalSupply);
}
// ------------------------------------------------------------------------
// Total supply
// ------------------------------------------------------------------------
function totalSupply() public view returns (uint) {
return _totalSupply - balances[address(0)];
}
// ------------------------------------------------------------------------
// Get the token balance for account tokenOwner
// ------------------------------------------------------------------------
function balanceOf(address tokenOwner) public view returns (uint balance) {
return balances[tokenOwner];
}
// ------------------------------------------------------------------------
// Transfer the balance from token owner's account to to account
// - Owner's account must have sufficient balance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transfer(address to, uint tokens) public returns (bool success) {
balances[msg.sender] = safeSub(balances[msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
Transfer(msg.sender, to, tokens);
return true;
}
// ------------------------------------------------------------------------
// Token owner can approve for spender to transferFrom(...) tokens
// from the token owner's account
//
// https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20-token-standard.md
// recommends that there are no checks for the approval double-spend attack
// as this should be implemented in user interfaces
// ------------------------------------------------------------------------
function approve(address spender, uint tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
Approval(msg.sender, spender, tokens);
return true;
}
// ------------------------------------------------------------------------
// Transfer tokens from the from account to the to account
//
// The calling account must already have sufficient tokens approve(...)-d
// for spending from the from account and
// - From account must have sufficient balance to transfer
// - Spender must have sufficient allowance to transfer
// - 0 value transfers are allowed
// ------------------------------------------------------------------------
function transferFrom(address from, address to, uint tokens) public returns (bool success) {
balances[from] = safeSub(balances[from], tokens);
allowed[from][msg.sender] = safeSub(allowed[from][msg.sender], tokens);
balances[to] = safeAdd(balances[to], tokens);
Transfer(from, to, tokens);
return true;
}
// ------------------------------------------------------------------------
// Returns the amount of tokens approved by the owner that can be
// transferred to the spender's account
// ------------------------------------------------------------------------
function allowance(address tokenOwner, address spender) public view returns (uint remaining) {
return allowed[tokenOwner][spender];
}
// ------------------------------------------------------------------------
// Token owner can approve for spender to transferFrom(...) tokens
// from the token owner's account. The spender contract function
// receiveApproval(...) is then executed
// ------------------------------------------------------------------------
function approveAndCall(address spender, uint tokens, bytes data) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
Approval(msg.sender, spender, tokens);
ApproveAndCallFallBack(spender).receiveApproval(msg.sender, tokens, this, data);
return true;
}
// ------------------------------------------------------------------------
// Don't accept ETH
// ------------------------------------------------------------------------
function () public payable {
revert();
}
// ------------------------------------------------------------------------
// Owner can transfer out any accidentally sent ERC20 tokens
// ------------------------------------------------------------------------
function transferAnyERC20Token(address tokenAddress, uint tokens) public onlyOwner returns (bool success) {
return ERC20Interface(tokenAddress).transfer(owner, tokens);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment