Skip to content

Instantly share code, notes, and snippets.

@bhavishyagopesh
Created March 17, 2019 00:58
Show Gist options
  • Select an option

  • Save bhavishyagopesh/ff8626a4e533bcb52d3d3c42ee08ac2c to your computer and use it in GitHub Desktop.

Select an option

Save bhavishyagopesh/ff8626a4e533bcb52d3d3c42ee08ac2c 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.5.1+commit.c8a2cb62.js&optimize=false&gist=
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.5.1;
contract Course {
address admin;
address ManagerContract;
address instructor;
int courseNo;
struct Marks{
int midsem;
int endsem;
int attendance;
}
mapping (int => Marks) student;
mapping (int => bool) isEnrolled;
constructor(int c, address inst, address adm) public {
ManagerContract = msg.sender;
courseNo = c;
instructor = inst;
admin = adm;
}
function kill() public{
//The admin has the right to kill the contract at any time.
//Take care that no one else is able to kill the contract
require(
msg.sender == admin,
"Sender not authorized."
);
address payable tmpaddr = address(uint160(admin));
selfdestruct(tmpaddr);
}
function enroll(int rollNo) public {
//This function can only be called by the ManagerContract
//Enroll a student in the course if not already registered
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
! isEnrolled[rollNo],
"Already enrolled."
);
student[rollNo].midsem = 0;
student[rollNo].endsem = 0;
student[rollNo].attendance = 0;
isEnrolled[rollNo] = true;
}
function markAttendance(int rollNo) public{
//Only the instructor can mark the attendance
//Increment the attendance variable by one
//Make sure the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].attendance += 1;
}
function addMidSemMarks(int rollNo, int marks) public{
//Only the instructor can add midsem marks
//Make sure that the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].midsem += marks;
}
function addEndSemMarks(int rollNo, int marks) public{
//Only the instructor can add endsem marks
//Make sure that the student is enrolled in the course
require(
msg.sender == instructor,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
student[rollNo].endsem += marks;
}
function getMidsemMarks(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].midsem);
}
function getEndsemMarks(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].endsem);
}
function getAttendance(int rollNo) public view returns(int) {
//Can only be called by the ManagerContract
//return the midSem, endSem and attendance of the student
//Make sure to check the student is enrolled
require(
msg.sender == ManagerContract,
"Sender not authorized."
);
require(
isEnrolled[rollNo],
"Not enrolled."
);
return (student[rollNo].attendance);
}
function isEnroll(int rollNo) public view returns(bool){
//Returns if a roll no. is enrolled in a particular course or not
//Can be accessed by anyone
return (isEnrolled[rollNo]);
}
}
pragma solidity 0.5.1;
import "./Course.sol" ;
contract Manager {
//Address of the school administrator
address admin;
mapping (address => int) student;
mapping (address => bool) isStudent;
mapping (int => bool) isCourse;
mapping (int => Course) course;
int rollCount = 19111000;
//Constructor
constructor() public{
admin = msg.sender;
}
function kill() public{
//The admin has the right to kill the contract at any time.
//Take care that no one else is able to kill the contract
require(
msg.sender == admin,
"Sender not authorized."
);
address payable tmpaddr1 = address(uint160(admin));
selfdestruct(tmpaddr1);
}
function addStudent() public{
//Anyone on the network can become a student if not one already
//Remember to assign the new student a unique roll number
require (
!isStudent[msg.sender],
"Already a student."
);
student[msg.sender] = rollCount;
rollCount += 1;
}
function addCourse(int courseNo, address instructor) public{
//Add a new course with course number as courseNo, and instructor at address instructor
//Note that only the admin can add a new course. Also, don't create a new course if course already exists
require(
msg.sender == admin,
"Sender not authorized."
);
require(
!isCourse[courseNo],
"Course already exist."
);
course[courseNo] = new Course(courseNo,instructor, admin);
}
function regCourse(int courseNo) public{
//Register the student in the course if he is a student on roll and the courseNo is valid
require(
isStudent[msg.sender],
"Not a valid student."
);
require(
isCourse[courseNo],
"Not a valid course."
);
// Register
course[courseNo].enroll(student[msg.sender]);
}
function getMyMarks(int courseNo) public view returns(int, int, int) {
//Check the courseNo for validity
//Should only work for valid students of that course
//Returns a tuple (midsem, endsem, attendance)
require(
isCourse[courseNo],
"Not a valid course."
);
require(
course[courseNo].isEnroll(student[msg.sender]),
"Not an enrolled student."
);
int ms = course[courseNo].getMidsemMarks(student[msg.sender]);
int es = course[courseNo].getEndsemMarks(student[msg.sender]);
int at = course[courseNo].getAttendance(student[msg.sender]);
return (ms, es, at);
}
function getMyRollNo() public view returns(int) {
//Utility function to help a student if he/she forgets the roll number
//Should only work for valid students
//Returns roll number as int
require(
isStudent[msg.sender],
"Not a valid student."
);
return (student[msg.sender]);
}
}
[
{
"constant": true,
"inputs": [],
"name": "greeter",
"outputs": [
{
"name": "",
"type": "string"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "rollNo",
"type": "int256"
}
],
"name": "addMe",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": true,
"inputs": [
{
"name": "rollNo",
"type": "int256"
}
],
"name": "myAnswer",
"outputs": [
{
"name": "",
"type": "bytes32"
}
],
"payable": false,
"stateMutability": "pure",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
}
]
{
"accounts": {},
"linkReferences": {},
"transactions": [],
"abis": {}
}
{
"accounts": {
"account{0}": "0xca35b7d915458ef540ade6068dfe2f44e8fa733c"
},
"linkReferences": {},
"transactions": [
{
"timestamp": 1552107766751,
"record": {
"value": "0",
"parameters": [],
"to": "created{undefined}",
"name": "kill",
"inputs": "()",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107771252,
"record": {
"value": "0",
"parameters": [],
"to": "created{undefined}",
"name": "kill",
"inputs": "()",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107771973,
"record": {
"value": "0",
"parameters": [
""
],
"to": "created{undefined}",
"name": "markAttendance",
"inputs": "(int256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107772478,
"record": {
"value": "0",
"parameters": [
""
],
"to": "created{undefined}",
"name": "markAttendance",
"inputs": "(int256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107772851,
"record": {
"value": "0",
"parameters": [
""
],
"to": "created{undefined}",
"name": "markAttendance",
"inputs": "(int256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107773083,
"record": {
"value": "0",
"parameters": [
""
],
"to": "created{undefined}",
"name": "markAttendance",
"inputs": "(int256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107774417,
"record": {
"value": "0",
"parameters": [
"19111000"
],
"to": "created{undefined}",
"name": "enroll",
"inputs": "(int256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1552107775195,
"record": {
"value": "0",
"parameters": [
"25",
""
],
"to": "created{undefined}",
"name": "addMidSemMarks",
"inputs": "(int256,int256)",
"type": "function",
"from": "account{0}"
}
}
],
"abis": {}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment