Skip to content

Instantly share code, notes, and snippets.

@ahmedali8
Created May 14, 2020 05:43
Show Gist options
  • Select an option

  • Save ahmedali8/cfe84f5af0df2de5a2d23b6633448508 to your computer and use it in GitHub Desktop.

Select an option

Save ahmedali8/cfe84f5af0df2de5a2d23b6633448508 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.6.6+commit.6c089d02.js&optimize=false&gist=
pragma solidity ^0.6.0;
contract first {
enum Reg { // custom datatype for constant values
onsite, // 0
online // 1
}
struct Student { // custom dataType for students
uint RollNo;
string name;
Reg regisType; // registration type i.e onsite or online
}
mapping (uint => Student) feedStudent; // feeding data in dictionary
event IssueVoucher (uint, string); // voucher issuance
modifier onlyOnsite(Reg regisType) { // check for only onsite voucher creation
if (regisType == Reg.onsite) {
_; // underscore means run the function
}
}
function createVoucher(uint _RollNo, string memory _name, Reg _regisType) public onlyOnsite(_regisType) { // create voucher if onsite student passes the check
feedStudent[_RollNo] = Student(_RollNo, _name, _regisType); // feeding data in mapping
emit IssueVoucher(_RollNo, _name); // isuue voucher
}
}
pragma solidity ^0.6.0;
contract myContract {
uint peopleCount = 0;
mapping(uint => Person) public people ;
address owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(owner == msg.sender);
_;
}
struct Person {
uint id;
string firstname;
string lastname;
}
function addPerson(string memory _firstname, string memory _lastname) onlyOwner public {
people[peopleCount] = Person(peopleCount, _firstname, _lastname);
peopleCount++;
}
function peopleCounter() view public returns(uint) {
return peopleCount;
}
}
pragma solidity ^0.6.0;
contract myContract {
uint peopleCount = 0;
mapping(uint => Person) public people ;
uint openingTime = 1583904775 ; //using epoch time
modifier onlyWhileOpen() {
require(block.timestamp >= openingTime);
// current block time
_;
}
struct Person {
uint id;
string firstname;
string lastname;
}
function addPerson(string memory _firstname, string memory _lastname) onlyWhileOpen public {
people[peopleCount] = Person(peopleCount, _firstname, _lastname);
peopleCount++;
}
function peopleCounter() view public returns(uint) {
return peopleCount;
}
}
pragma solidity ^0.6.0;
contract First {
int[] arr1 = [98, 87, 90];
int[] arr2 = new int[] (4);
int[] arr3;
}
pragma solidity ^0.6.0;
contract Vehicle {
function doService() public virtual returns(string memory) {
return "This is Vehicle";
}
}
contract Car is Vehicle {
function doService() public virtual override returns(string memory) {
return "This is Car";
}
}
contract Bus is Car {
function doService() public virtual override returns(string memory) {
return "This is Bus";
}
}
contract Truck is Bus {
function doService() public override returns(string memory) {
return "This is Truck";
}
}
contract WorkShop {
event logString(string);
function provideService() public {
//Own instances
Vehicle v = new Vehicle();
emit logString(v.doService());
Car c = new Car();
emit logString(c.doService());
Bus b = new Bus();
emit logString(b.doService());
Truck t = new Truck();
emit logString(t.doService());
//Now Parent to Child instances (agr Child ne parent ko override kia hua hoga then child ka return hoga otherwise paent ka)
Vehicle v_c = new Car();
emit logString(v_c.doService());
Car c_b = new Bus();
emit logString(c_b.doService());
Bus b_t = new Truck();
emit logString(b_t.doService());
}
}
pragma solidity ^0.6.0;
contract enrollStudents {
address payable private myAddress = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
uint numberOfStudents = 0;
enum Gender {
male, //0
female //1
}
enum Reg {
online, //0
onsite //1
}
struct Student {
uint rollNo;
string name;
Gender gender;
Reg regisType;
bool degree;
}
mapping (uint => Student) studentsData;
modifier verify() {
if(msg.value >= 2 ether) {
_;
}
}
function enrollStu(uint _rollNo, string memory _name, Gender _gender, Reg _regisType, bool _degree) public payable verify() returns(uint) {
myAddress.transfer(msg.value);
studentsData[_rollNo] = Student(_rollNo, _name, _gender, _regisType, _degree);
return (numberOfStudents+1);
}
function balanceCheck() public view returns(uint) {
return myAddress.balance;
}
}
pragma solidity ^0.6.0;
contract first {
//event logString(string);
event loguint(uint);
function callEvents() public {
emit loguint(block.difficulty);
}
}
pragma solidity ^0.6.0;
contract First{
bytes b = new bytes(0);
function updateBytes() public returns(bytes memory){
b = "Hello World";
return b;
}
}
pragma solidity ^0.6.0;
contract Election_Dapp {
string public candidateName;
function Election() public {
candidateName = "Candidate 1";
}
function setCandidate (string memory _name) public {
candidateName = _name;
}
}
pragma solidity ^0.6.0;
contract MyMapping{
enum gender {male, female}
struct Student{
string name;
gender myGender;
}
mapping (uint =>Student) Students;
function addStudent(uint rollNo, string memory name, gender _gen) public{
Student memory newStudent = Student(name, _gen);
Students[rollNo] = newStudent;
}
function getStudent(uint rollNo) public view returns(string memory, uint){
return (Students[rollNo].name, uint(Students[rollNo].myGender));
}
}
pragma solidity ^0.6.0;
contract enums {
enum State {
Waiting,
Ready,
Active
}
State public state;
constructor() public {
state = State.Waiting;
}
function activate() public {
state = State.Active;
}
function isActive() view public returns(bool) {
return (state == State.Active);
}
}
pragma solidity ^0.6.0;
// Our first contract is a faucet!
contract Faucet {
// Give out ether to anyone who asks
function withdraw(uint withdraw_amount) external {
// Limit withdrawal amount
require(withdraw_amount <= 100000000000000000);
// Send the amount to the address that requested it
msg.sender.transfer(withdraw_amount);
}
// Accept any incoming amount
fallback() external payable {
}
}
pragma solidity ^0.6.0;
contract FibbonaciSeries {
uint[] FibboSeries = [1, 1];
uint public calculatedNum;
function FibSeries(uint _inNum) public returns(uint) {
if(_inNum >= 3) {
uint _num = FibSeries(_inNum - 1) + FibSeries(_inNum - 2);
FibboSeries.push(_num);
return calculatedNum = _num;
}
else {
return 1;
}
}
}
pragma solidity ^0.6.0;
contract HelloWorld {
string public stateVariable = "Hello World";
function GetHelloWorld() public view returns (string memory) {
return stateVariable;
}
}
pragma solidity ^0.6.0;
contract first {
event loguint(uint8);
function loops() public {
for (uint8 i = 0; i < 8; i++) {
if (i == 4) {
continue;
}
emit loguint(i);
}
}
}
pragma solidity ^0.6.0;
contract hashing {
function hashFunction(string memory _text, uint _num, address _addr) public view returns(bytes32) {
return keccak256(abi.encodePacked(_text, _num, _addr));
}
}
pragma solidity ^0.6.0;
contract New{
int public a = 20;
function doSomething() public{
a = a+20;
}
}
pragma solidity ^0.6.0;
/* An interface can only contains
definition and function declaration */
//Parent
interface Ibank {
function deposit() external payable returns(bool);
function withDraw(uint _amount) external returns(bool);
function checkBalance() external returns(uint);
}
//Child
contract iBank is Ibank {
//state variable
address payable owner;
//mappings
mapping(address => uint) Balances;
mapping(address => bool) AccountCreated;
//address array
address[] addresses;
//Events
event acCreated(address);
event depositDone(address, uint);
//Modifiers
modifier onlyAccountHolder() {
require(AccountCreated[msg.sender], "Access Denied");
_;
}
modifier onlyOwner() {
require(msg.sender == owner, "Access Denied");
_;
}
//will work only first time when deployed
constructor() payable public {
owner = msg.sender;
require(msg.value >= 50 ether, "Insufficient Amount, Bank cannot be created");
}
function deposit() external override payable returns(bool) {
//condition to validate deposit and address
require(msg.value > 0);
//account creation
if(!AccountCreated[msg.sender]) {
// not-true = false
/* if the function is not created then in mapping the default
value to the key would be false so the condition satisfies */
//pushes the address to the array
addresses.push(msg.sender);
//as the account is created so it sets the value of mapping to true
AccountCreated[msg.sender] = true;
//fire event
emit acCreated(msg.sender);
}
//amount deposition
Balances[msg.sender] += msg.value; //incrementing the amount in Balances mapping key
emit depositDone(msg.sender, msg.value); //fire event
return true;
}
//only account holders can access these function:
function withDraw(uint _amount) external override onlyAccountHolder() returns(bool) {
//local variables
uint accountBalance = Balances[msg.sender];
uint amount = _amount * 1000000000000000000;
//condition: Only valid accounts can withdraw not more than its balance
require(accountBalance > amount, "Insufficient Account Balance");
//decrement in account's balance
Balances[msg.sender] -= amount;
//transfer fund
msg.sender.transfer(amount);
//fire event
emit depositDone(msg.sender, amount);
return true;
}
function checkBalance() external override onlyAccountHolder() returns(uint) {
return Balances[msg.sender];
}
//only owner can access these functions:
function totalBankFund() external view onlyOwner() returns(uint) {
return address(this).balance; //this refers to this contract's address
}
function totalAccounts() external view onlyOwner() returns(uint) {
return addresses.length;
}
function closeBank() external onlyOwner {
selfdestruct(owner); //destroys the contract and send the funds to the owner's address i.e owner
}
}
pragma solidity ^0.6.0;
contract first {
function result(uint a) public view returns(string memory) {
if(a >= 75 && a <= 100) {
return "Passed";
}
else {
return "Failed";
}
}
}
pragma solidity ^0.6.0;
contract New{
int public a = 20;
function doSomething() public{
a = a+20;
}
}
contract LoadNew{
function loadNew(address _contractAdd) public returns(address){
New newContract = New(_contractAdd);
newContract.doSomething();
return address(newContract);
}
}
pragma solidity ^0.6.0;
contract First{
mapping (int => string) names;
function updateValue(int a, string memory b) public {
names[a] = b;
}
function getValues(int a) public view returns(string memory) {
return names[a];
}
}
pragma solidity ^0.6.0;
contract mappings {
//1. mappings
//2. C.R.U.D
//3. Default values
//4. Exotic mapping 1: Nested Mapping
//5. Exotic mapping 2: array inside mapping
//1. Declare mapping
mapping(address => uint) balances;
mapping(address => mapping(address => bool)) approved;
mapping(address => uint[]) scores;
function foo(address sender) external {
balances[msg.sender] = 10; //Add
balances[msg.sender]; //Read
balances[msg.sender]= 20; //Update
delete balances[msg.sender]; //Delete
//3. Default values
//balances[someAddressThatDoesNotExist] => 0
//4. Exotic Mapping 1:
approved[msg.sender][sender] = true; //Add
approved[msg.sender][sender]; //Read
approved[msg.sender][sender] = false; //Update
delete approved[msg.sender][sender]; //Delete
//5. Exotic Mapping 2:
scores[msg.sender].push(10);
scores[msg.sender][0];
scores[msg.sender][0] = 20;
delete scores[msg.sender][0];
}
}
pragma solidity ^0.6.0;
contract First {
modifier verify(uint age) {
if (age > 40) {
_; //if true then execute the function
}
}
function getValues(uint a) public view verify(a) returns(uint){
return a;
}
}
pragma solidity ^0.6.0;
contract sumContract {
function sum(int _a, int _b) public returns(int) {
return (_a + _b);
}
}
contract multiContract is sumContract {
function mult(int _a, int _b) public returns(int) {
return (_a * _b);
}
}
contract divideContract is sumContract {
function div(int _a, int _b) public returns(int) {
return (_a / _b);
}
}
contract subContract is sumContract, multiContract, divideContract {
function sub(int _a, int _b) public returns(int) {
return (_a - _b);
}
}
contract client {
function workWithInheritance() public returns(int) {
int a = 10;
int b = 30;
subContract pc = new subContract();
}
}
pragma solidity ^0.6.0;
contract First{
mapping(string => mapping(int => string)) stuCourses;
function addCourse() public {
stuCourses["PIAIC001"][1] = "BCC";
stuCourses["PIAIC001"][2] = "IOT";
stuCourses["PIAIC002"][1] = "AIC";
stuCourses["PIAIC003"][1] = "CNC";
}
function findCourse() public view returns(string memory) {
return stuCourses["PIAIC002"][1];
}
}
pragma solidity ^0.6.0;
import "Hello.sol";
contract client {
function createNew() public returns(address){
New newContract = new New();
newContract.doSomething();
return address(newContract);
}
}
pragma solidity ^0.6.0;
contract Hello {
uint public item = 5;
constructor() public {
item = 10;
}
function getValue() public view returns (uint) {
return item;
}
}
contract client {
function working() public returns(uint) {
Hello h1 = new Hello();
return h1.getValue();
}
}
pragma solidity ^0.6.0;
contract payableExample {
mapping(address => uint) balances; //to collect balances of accounts which they invested
function invest() external payable {
if(msg.value < 1 ether) {
revert(); //stops the execution and returns amount
}
balances[msg.sender] += msg.value;
}
function balanceOf() external view returns(uint) { //to check balance of our contract
return address(this).balance;
}
}
pragma solidity ^0.6.0;
contract First {
enum Gender {
male,
female
}
function doSomeWork() public pure returns (Gender) {
Gender g = Gender.male;
return g;
}
}
pragma solidity ^0.6.0;
contract sendingMoney {
mapping(address => uint) balances;
address payable wallet;
event Purchase(
address _buyer,
uint _amount
);
constructor(address payable _wallet) public {
wallet = _wallet;
}
function buyToken() public payable {
//buy buy Token
balances[msg.sender] += 1;
//send ether to wallet
wallet.transfer(msg.value);
emit Purchase(msg.sender, 1);
}
}
pragma solidity ^0.6.0;
contract First{
address payable myAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
function myFunds() public payable returns(bool) {
bool isFundsSent = myAddress.send(1 ether);
return isFundsSent;
}
}
pragma solidity ^0.6.0;
contract First {
function getValue() public returns (string memory){
string[2] memory a = [string("A"), "B"];
string[2] memory b = [string("C"), "D"];
a = b;
b[1] = "E";
return a[1];
}
/*
function assignment8() public returns(int) {
int[2] memory rule8LocalVar1 = [int(1), 2];
int[2] memory rule8LocalVar2 = [int(3), 4];
rule8LocalVar1 = rule8LocalVar2;
rule8LocalVar2[1] = 10;
return rule8LocalVar1[1];
} */
}
pragma solidity ^0.6.0;
contract First{
enum Gender {
male,
female
}
struct Student{
string name;
uint age;
bool isFeePaid;
Gender gender;
}
Student stu = Student("Ahmed", 20, true, Gender.male);
function manageStruct() public pure returns(uint) {
Student memory s1 = Student("Rehan", 30, false, Gender.male);
return s1.age;
}
}
pragma solidity ^0.6.0;
contract eventsExample {
event newTrade(
uint date,
address from,
address to,
uint amount
);
function trade( address to, uint amount) external{
emit newTrade(now, msg.sender, to, amount);
}
}
pragma solidity ^0.6.0;
contract First{
address payable myAddress = 0x14723A09ACff6D2A60DcdF7aA4AFf308FDDC160C;
function sendFunds() public payable {
myAddress.transfer(msg.value);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment