Skip to content

Instantly share code, notes, and snippets.

@rahulrajpl
Created March 8, 2019 08:06
Show Gist options
  • Save rahulrajpl/201084ec950682db71146ab9a06e1e1e to your computer and use it in GitHub Desktop.
Save rahulrajpl/201084ec950682db71146ab9a06e1e1e 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.4.17+commit.bdeb9e52.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 {
}
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
}
function enroll(int rollNo) public {
//This function can only be called by the ManagerContract
//Enroll a student in the course if not already registered
}
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
}
function addMidSemMarks(int rollNo, int marks) public{
//Only the instructor can add midsem marks
//Make sure that the student is enrolled in the course
}
function addEndSemMarks(int rollNo, int marks) public{
//Only the instructor can add endsem marks
//Make sure that the student is enrolled in the course
}
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
}
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
}
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
}
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
}
}
pragma solidity ^0.4.16;
contract HelloWorld {
uint256 counter = 5;
function add() public{
counter++;
}
function subtract() public{
counter--;
}
function getcounter() public constant returns (uint256){
return counter;
}
}
pragma solidity 0.5.1;
import "<location>/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{
}
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
}
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
}
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
}
function regCourse(int courseNo) public{
//Register the student in the course if he is a student on roll and the courseNo is valid
}
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)
}
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
}
}
[
{
"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"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment