Last active
July 18, 2022 08:07
-
-
Save Oliver-ke/7dcc9f32b6b785d35389de9c2c8c9979 to your computer and use it in GitHub Desktop.
My solidity contracts snippets
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity ^0.5.1; | |
// enums | |
// contract Mycontract2 { | |
// // using enums in solidity | |
// enum State { Waiting, Activated, Pending } | |
// State public state; | |
// constructor() public { | |
// state = State.Waiting; | |
// } | |
// function activated() public { | |
// state = State.Activated; | |
// } | |
// function checkIsActivated() public view returns (bool) { | |
// return state == State.Activated; | |
// } | |
// } | |
// structs | |
// contract Mystruct { | |
// Person[] private people; | |
// struct Person { | |
// string _firestName; | |
// string _lastName; | |
// } | |
// uint256 peopleCount; | |
// constructor() public{ | |
// peopleCount = 0; | |
// } | |
// function addPerson(string memory _firestName, string memory _lastName) public { | |
// people.push(Person(_firestName, _lastName)); | |
// peopleCount++; | |
// } | |
// function getPeopleCount() public view returns(uint256){ | |
// if(peopleCount > 0) { | |
// return peopleCount - 1; | |
// } | |
// return peopleCount; | |
// } | |
// } | |
// mapping | |
// contract MystructWithMapping { | |
// mapping(uint => Person) public people; | |
// uint256 peopleCount; | |
// struct Person { | |
// uint id; | |
// string _firestName; | |
// string _lastName; | |
// } | |
// constructor() public{ | |
// peopleCount = 0; | |
// } | |
// function addPerson(string memory _firestName, string memory _lastName) public { | |
// increementCount(); | |
// people[peopleCount] = Person(peopleCount, _firestName, _lastName); | |
// } | |
// function increementCount() internal { | |
// peopleCount++; | |
// } | |
// function getPeopleCount() public view returns(uint256){ | |
// if(peopleCount > 0) { | |
// return peopleCount - 1; | |
// } | |
// return peopleCount; | |
// } | |
// } | |
// contract CustomModifier { | |
// mapping(uint => Person) public people; | |
// uint256 peopleCount; | |
// address owner; | |
// struct Person { | |
// uint id; | |
// string _firestName; | |
// string _lastName; | |
// } | |
// modifier onlyOwner() { | |
// require(msg.sender == owner); | |
// _; | |
// } | |
// constructor() public{ | |
// peopleCount = 0; | |
// owner = msg.sender; | |
// } | |
// function addPerson(string memory _firestName, string memory _lastName) public onlyOwner { | |
// increementCount(); | |
// people[peopleCount] = Person(peopleCount, _firestName, _lastName); | |
// } | |
// function increementCount() internal { | |
// peopleCount++; | |
// } | |
// function getPeopleCount() public view returns(uint256){ | |
// if(peopleCount > 0) { | |
// return peopleCount - 1; | |
// } | |
// return peopleCount; | |
// } | |
// } | |
// contract CustomTimeModifier { | |
// mapping(uint => Person) public people; | |
// uint256 peopleCount; | |
// uint256 openingTime = 1609818194; | |
// struct Person { | |
// uint id; | |
// string _firestName; | |
// string _lastName; | |
// } | |
// modifier onlyWhileOpen() { | |
// require(block.timestamp >= openingTime); | |
// _; | |
// } | |
// constructor() public{ | |
// peopleCount = 0; | |
// } | |
// function addPerson(string memory _firestName, string memory _lastName) public onlyWhileOpen { | |
// increementCount(); | |
// people[peopleCount] = Person(peopleCount, _firestName, _lastName); | |
// } | |
// function increementCount() internal { | |
// peopleCount++; | |
// } | |
// function getPeopleCount() public view returns(uint256){ | |
// if(peopleCount > 0) { | |
// return peopleCount - 1; | |
// } | |
// return peopleCount; | |
// } | |
// } | |
// contract EtherMarket { | |
// mapping(address => uint256) public balances; | |
// address payable wallet; | |
// // event, indexed allows you to subscribe or listen to certain value | |
// event Purchase( | |
// address indexed _buyer, | |
// uint256 _amount | |
// ); | |
// constructor(address payable _wallet) public { | |
// wallet = _wallet; | |
// } | |
// function() external payable { | |
// buyToken(); | |
// } | |
// function buyToken() public payable { | |
// require(msg.value > 1); | |
// // buy token | |
// balances[msg.sender] += 1; | |
// // send either | |
// wallet.transfer(msg.value); | |
// // trigger event | |
// emit Purchase(msg.sender, 1); | |
// } | |
// } | |
contract Mycontract2 { | |
// solidity datatypes | |
string public value = "this is my initial value"; | |
string public constant day = "hello my constant"; | |
bool public mybool = true; | |
int public myInt = -1; | |
uint public unsigned_int = 34; | |
uint96 public myYubt = 20000000000; | |
enum State { Waiting, Ready, Acting } | |
function set(string memory _value) public { | |
value = _value; | |
} | |
} | |
// using multiple smart contract, parent child relationship | |
contract ERC20Token { | |
mapping(address => uint256) public balances; | |
function mint() public { | |
// this gives the address of the main contract call | |
balances[tx.origin] += 1; | |
} | |
} | |
contract EtherMultiMarket { | |
address payable wallet; | |
address token; | |
constructor(address payable _wallet, address _token) public { | |
wallet = _wallet; | |
token = _token; | |
} | |
function() external payable { | |
buyToken(); | |
} | |
function buyToken() public payable { | |
require(msg.value > 1); | |
ERC20Token _token = ERC20Token(address(token)); | |
_token.mint(); | |
// send either | |
wallet.transfer(msg.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment