Created
July 16, 2023 06:59
-
-
Save parnaa/a3b2441eca6695b4101d4ea9711e273e 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.8.19+commit.7dd6d404.js&optimize=false&runs=200&gist=
This file contains 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: unlicensed | |
pragma solidity 0.8.19; | |
contract calculator{ | |
function add(uint256 a, uint256 b) public pure returns(uint256 _result){ | |
return a+b; | |
} | |
function subtract(uint256 a, uint256 b) public pure returns(uint256 _result){ | |
return (a-b); | |
} | |
function multiply(uint256 a, uint256 b) public pure returns(uint256 _result){ | |
return (a*b); | |
} | |
function divide(uint256 a, uint256 b) public pure returns(uint256 _result){ | |
return (a/b); | |
} | |
} | |
contract maths is calculator{ | |
} |
This file contains 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: MIT | |
pragma solidity 0.6.12; | |
contract kart { | |
uint public counter = 1; | |
struct Product { | |
uint productId; | |
string desc; | |
string title; | |
address payable seller; | |
address buyer; | |
bool delivered; | |
uint price; // in ether | |
} | |
Product[] public products; | |
//events | |
event registered(string title,uint productId); | |
event bought(uint productId, address buyer); | |
event delivery(uint productId); | |
// Seller has to register and create product | |
function register(string memory _title,string memory _desc, uint _price) public { | |
require(_price > 0,"Price cannot be less than equal to 0"); | |
Product memory tempProduct; | |
tempProduct.title = _title; | |
tempProduct.desc = _desc; | |
tempProduct.price = _price; | |
tempProduct.seller = payable(msg.sender); | |
tempProduct.productId = counter; | |
products.push(tempProduct); | |
counter++; // counter = counter +1; | |
emit registered(tempProduct.title,tempProduct.productId); | |
} | |
// Buyer will pay and buy the product | |
function buy(uint _productId) payable public { | |
// seller should not buy himself | |
require(products[_productId - 1].seller != msg.sender,"Seller cannot buy"); | |
// buyer should transfer the price of the product | |
require(msg.value == products[_productId - 1].price * 10**18); | |
products[_productId - 1].buyer = msg.sender; | |
emit bought(_productId,msg.sender); | |
} | |
// Buyer confirms delivery and contract will pay to seller | |
function delivered(uint _productId) public { | |
//only buyer can call this function | |
Product memory tempProduct = products[_productId - 1]; | |
require(tempProduct.buyer == msg.sender,"Only buyer should call this"); | |
tempProduct.delivered = true; | |
products[_productId-1]=tempProduct; | |
tempProduct.seller.transfer(tempProduct.price * 10**18); | |
emit delivery(_productId); | |
} | |
} |
This file contains 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: unlicensed | |
pragma solidity 0.8.19; | |
contract greetings{ | |
string public greet; | |
constructor(string memory _s) | |
{ | |
greet = _s; | |
} | |
} | |
contract greetingsFor{ | |
string public name; | |
string public description; | |
constructor(string memory _n, string memory _d){ | |
name = _n; | |
description = _d; | |
} | |
} | |
contract messages is greetingsFor, greetings{ | |
string public greetmessage; | |
constructor(string memory _name, string memory _d, string memory _greet) greetingsFor(_name, _d) greetings(_greet){ | |
greetmessage = string(abi.encodePacked(greetings.greet," ", greetingsFor.name,"! ",greetingsFor.description)); | |
} | |
} | |
contract HelloWorld is messages("World","The World is beautiful!","Hello"){} | |
contract ByeWorld is messages("World"," Thank you for everything!","Good Bye "){} | |
contract HelloLearners is messages("Learners","You all are great!","Hello"){} | |
contract ByeLearners is messages("Learners","Thank you for being with us! Will meet you all soon","Good Bye"){} |
This file contains 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: unlicensed | |
pragma solidity ^0.8.18; | |
contract JobPortal{ | |
function addNewApplicant() public{ | |
} | |
function getApplicantDetails() public{ | |
} | |
function getApplicantType() public{ | |
} | |
function addNewJob() public{ | |
} | |
function getJobDetails() public{ | |
} | |
function applyForJob() public{ | |
} | |
function provideRatingToApplicant() public { | |
} | |
function fetchApplicantRating() public { | |
} | |
} |
This file contains 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: unlicensed | |
pragma solidity ^0.8.0; | |
contract AccessControlled{ | |
struct delegate | |
{ | |
address delegate; | |
bool status; | |
} | |
mapping(address => delegate) private delegates; | |
modifier onlyDelegates{ | |
require(delegates[msg.sender].status,"Only Delegates have access to this function"); | |
_; | |
} | |
constructor(){ | |
delegates[msg.sender] = delegate(msg.sender,true); | |
} | |
function addDelegate(address _newdeleg) public onlyDelegates{ | |
delegates[_newdeleg] = delegate(_newdeleg, true); | |
} | |
function revokeDelegate(address _deleg) public onlyDelegates{ | |
delegates[_deleg] = delegate(_deleg, false); | |
} | |
function isDelegate(address _deleg) public view returns(bool){ | |
return delegates[_deleg].status; | |
} | |
} | |
contract PropertyContract is AccessControlled{ | |
struct Property{ | |
string name; | |
uint256 lat; | |
uint256 long; | |
uint256 area; | |
bool status; | |
string location; | |
string country; | |
address owner; | |
} | |
mapping(uint256 => Property) public PropertyDetails; | |
event recordPropertyEvent(uint256 _propID, address _owner,uint256 area); | |
event transferPropertyEvent(uint256 _propID, address _sender, address _recipient); | |
function recordProperty(uint256 _prop_id,string memory _name,uint256 _lat,uint256 _long, uint256 _area, string memory _location, string memory _country, address _powner) public onlyDelegates{ | |
PropertyDetails[_prop_id] = Property(_name,_lat,_long,_area,true,_location,_country,_powner); | |
emit recordPropertyEvent(_prop_id,_powner,_area); | |
} | |
function transfer_property(uint256 _pid, address _newOwner ) external { | |
require(ownerOfProperty(_pid) == msg.sender,"Only property owner can transfer"); | |
PropertyDetails[_pid].owner = _newOwner; | |
emit transferPropertyEvent(_pid,msg.sender,_newOwner); | |
} | |
function ownerOfProperty(uint256 _pid) public view returns(address _owner){ | |
return(PropertyDetails[_pid].owner); | |
} | |
} |
This file contains 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: unlicensed | |
pragma solidity ^0.8.0; | |
contract Store{ | |
string store_name; | |
address store_owner; | |
modifier onlyStore{ | |
require(msg.sender == store_owner,"Only store owner can change book details"); | |
_; | |
} | |
constructor(string memory _name){ | |
store_name = _name; | |
store_owner = msg.sender; | |
} | |
function setStoreName(string memory _name) public onlyStore { | |
store_name = _name; | |
} | |
function setOwner(address _owner) public{ | |
require(msg.sender == store_owner,"Only store owner can change book details"); | |
store_owner = _owner; | |
} | |
} | |
//Playing with the struct DataType | |
contract BookStore is Store{ | |
struct Book{ | |
uint256 book_id; | |
string book_name; | |
string publisher_name; | |
string author_name; | |
uint256 stocks; | |
uint256 price; | |
bool availability_status; | |
string[] publishing_countries; | |
} | |
mapping(uint256 => address) public book_purchased_by; | |
mapping(uint256 => Book) public book; | |
uint256[] public booklist; | |
uint256[] public purchased_book_records; | |
constructor(string memory _name) Store(_name){ | |
} | |
function setBookDetails(uint256 _book_id, string memory _name, string memory _author_name, string memory _pname, uint256 _p, uint256 _stocks, string[] memory _default_country ) public onlyStore{ | |
book[_book_id] =Book(_book_id,_name,_pname,_author_name,_stocks,_p,true,_default_country); | |
booklist.push(_book_id); | |
} | |
function addPublishingCountries(string memory _c1, uint8 _book_id) public onlyStore{ | |
book[_book_id].publishing_countries.push(_c1); | |
} | |
function getBookDetails(uint256 _book_id) public view returns(Book memory _b){ | |
return(book[_book_id]); | |
} | |
function getBookName(uint256 _book_id) public view returns(string memory _s){ | |
return book[_book_id].book_name; | |
} | |
function getBookPrice(uint256 _book_id) public view returns(uint256 _p){ | |
return book[_book_id].price; | |
} | |
function buy_book(uint256 _book_id) public payable{ | |
require(msg.value == getBookPrice(_book_id),"You have to send the book price"); | |
require(book[_book_id].availability_status,"Book not available"); | |
book_purchased_by[_book_id] = msg.sender; | |
book[_book_id].stocks--; | |
if(book[_book_id].stocks == 0) | |
book[_book_id].availability_status = false; | |
purchased_book_records.push(_book_id); | |
payable(store_owner).transfer(msg.value); | |
} | |
} |
This file contains 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: unlicensed | |
pragma solidity ^0.8.0; | |
contract TestDataTypes{ | |
bytes1 public b1 = 0x34; | |
bytes2 public b2 = 0x1234; | |
bytes3 public b3 = 0x123456; | |
bytes4 public b4 = 0x12345618; | |
bytes4 public b8 = 0x12334567; | |
uint256 starttime = block.timestamp; | |
address firstaddress = 0x5B38Da6a701c568545dCfcB03FcB875f56beddC4; | |
address secondaddress = 0xdD870fA1b7C4700F2BD7f44238821C26f7392148; | |
function checkMyBlance() public view returns(uint256 _mybalance){ | |
return(firstaddress.balance); | |
} | |
enum Suit{ | |
diamond,club, heart, spade | |
} | |
enum SuitValue{ | |
Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace | |
} | |
struct Card{ | |
Suit suit; | |
SuitValue value; | |
} | |
Card public mycard; | |
function pick_a_card(Suit _s, SuitValue _v) public returns(Suit, SuitValue) { | |
mycard.suit = _s; | |
mycard.value = _v; | |
return(mycard.suit,mycard.value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment