Skip to content

Instantly share code, notes, and snippets.

@spandan114
Last active December 28, 2021 20:31
Show Gist options
  • Save spandan114/5a015837a40a01d3f2076e1828fae0cb to your computer and use it in GitHub Desktop.
Save spandan114/5a015837a40a01d3f2076e1828fae0cb to your computer and use it in GitHub Desktop.
Solidity smart contracts
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
struct Student{
string name ;
uint rollno;
}
contract Crud{
Student[] students;
uint rollno = 1;
function createStudent(string memory _name) public{
Student memory newStudent = Student(_name,rollno);
students.push(newStudent);
rollno++;
}
function getIndex(uint _rollno) view internal returns(uint){
for(uint i = 0;i<students.length;i++){
if(students[i].rollno == _rollno){
return i;
}
}
revert("User does not exist !");
}
function findStudent(uint _rollno) view public returns(uint,string memory){
uint i = getIndex(_rollno);
return(students[i].rollno,students[i].name);
}
function updateStudent(uint _rollno,string memory _name) public{
uint i = getIndex(_rollno);
students[i].name = _name;
}
function destroyStudent(uint _rollno) public{
uint i = getIndex(_rollno);
delete students[i];
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.5.0 <0.9.0;
contract Lottery{
address public manager;
address payable[] public participent;
constructor(){
manager = msg.sender;
}
receive() external payable{
require(msg.value == 2 ether);
participent.push(payable(msg.sender));
}
function getBalance() public view returns(uint){
require(msg.sender == manager);
return address(this).balance;
}
function random() public view returns(uint){
return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,participent.length)));
}
function selectWinner() public {
require(msg.sender == manager);
require(participent.length == 3);
uint randomVal = random();
address payable winner;
uint index = randomVal % participent.length;
winner = participent[index];
winner.transfer(getBalance());
participent = new address payable[](0);
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC20/ERC20.sol";
contract MyToken is ERC20{
address public admin;
constructor() ERC20("My Token","MTN"){
//Total supply of token
_mint(msg.sender,10000*10**18);
admin = msg.sender;
}
function mint(address to,uint amount) external{
require(msg.sender == admin,"Only admin can control supply");
_mint(to,amount);
}
function burnToken(uint amount) external{
_burn(msg.sender,amount);
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
struct Candidate{
string name;
uint age;
uint voteCount;
}
struct Voter{
string name;
uint age;
uint vote;
bool voted;
}
contract Election{
address public owner;
string public electionName;
mapping(address => Voter) public voters;
Candidate[] public candidates;
uint public totalVotes;
modifier ownerOnly{
require(msg.sender == owner,"You dont have rights to perform this operation !");
_;
}
function startElection(string memory _electionName) public {
owner = msg.sender;
electionName=_electionName;
}
function addCandidate(string memory _name,uint _age) ownerOnly public {
candidates.push(Candidate(_name,_age,0));
}
function vote(uint _voteIndex,uint _age,string memory _name) public {
require(_age > 18,"You are not eligible for vote !");
require(!voters[msg.sender].voted,"You are alredy voted !");
voters[msg.sender].age = _age;
voters[msg.sender].name = _name;
voters[msg.sender].vote = _voteIndex;
voters[msg.sender].voted = true;
candidates[_voteIndex].voteCount += 1;
totalVotes +=1;
}
function selectWinner() public view returns (uint selectWinner_){
uint winningVoteCount = 0;
for (uint p = 0; p < candidates.length; p++) {
if (candidates[p].voteCount > winningVoteCount) {
winningVoteCount = candidates[p].voteCount;
selectWinner_ = p;
}
}
}
function winnerName() public view returns(string memory winnerName_){
winnerName_ = candidates[selectWinner()].name;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment