Skip to content

Instantly share code, notes, and snippets.

@rahulrajpl
Created March 11, 2019 19:18
Show Gist options
  • Save rahulrajpl/a5bd87b2fc0bb1917d76c7b4a544322b to your computer and use it in GitHub Desktop.
Save rahulrajpl/a5bd87b2fc0bb1917d76c7b4a544322b 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 payable admin; //School Administrator
address payable ManagerContract;
address instructor;
int courseNo;
struct Marks{
int midsem;
int endsem;
int attendance;
}
mapping (int => Marks) student;
mapping (int => bool) isEnrolled;
modifier onlyOwner{
require(msg.sender == admin);
_;
}
constructor(int c, address inst, address payable adm) public {
courseNo = c; // Initializing course Number
instructor = inst; // Initializing Instructor address
admin = adm; // Initializing School Administrator
ManagerContract = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
}
function kill() public onlyOwner {
//The admin has the right to kill the contract at any time.
//Take care that no one else is able to kill the contract
selfdestruct(admin);
}
function enroll(int rollNo) public {
//This function can only be called by the ManagerContract
//Enroll a student in the course if not already registered
if(msg.sender == admin){
if(isEnrolled[rollNo] == false){
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
if(msg.sender == instructor){
if(isEnrolled[rollNo]==true){
student[rollNo].attendance++;
}
}
}
function addMidSemMarks(int rollNo, int marks) public{
//Only the instructor can add midsem marks
//Make sure that the student is enrolled in the course
if(msg.sender == instructor){
if(isEnrolled[rollNo]==true){
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
if(msg.sender == instructor){
if(isEnrolled[rollNo]==true){
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
if(msg.sender == admin && isEnrolled[rollNo]==true){
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
if(msg.sender == admin && isEnrolled[rollNo]==true){
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
if(msg.sender == admin && isEnrolled[rollNo]==true){
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
if(isEnrolled[rollNo]==true){
return true;
}
else{
return false;
}
}
}
pragma solidity >0.4.16;
contract HelloWorld {
uint256 public rollno = 18111000;
mapping (uint => Student) public students;
address admin;
address payable wallet;
mapping (address => uint256) public balances;
struct Student{
uint _id;
string _firstname;
string _lastname;
}
modifier onlyOwner(){
require(msg.sender == admin);
_;
}
event Purchase(
address indexed _buyer,
uint256 _amount
);
constructor(address payable _wallet) public {
admin = msg.sender;
wallet = _wallet;
}
function buyToken() public payable {
// Buy a Token
balances[msg.sender] += 1;
// also send ether to wallet.
wallet.transfer(msg.value);
emit Purchase(msg.sender, 1);
}
//Fallback function - can be called from outside contract only
function() external payable {
buyToken();
}
/* function addStudent
(
string memory _firstname,
string memory _lastname
)
public
onlyOwner
{
rollno += 1;
students[rollno] = Student(rollno, _firstname, _lastname);
}
*/
}
pragma solidity 0.5.1;
import "./Course.sol";
contract Manager {
//Address of the school administrator
address payable admin;
mapping (address => int) student;
mapping (address => bool) isStudent;
mapping (int => bool) isCourse;
mapping (int => Course) course;
int rollCount = 19111000;
//Constructor
constructor() public {
//Initialising Manager
admin = 0xCA35b7d915458EF540aDe6068dFe2F44E8fa733c;
}
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);
if(msg.sender == admin) {
selfdestruct(admin);
}
}
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
if(isStudent[msg.sender] == false){
student[msg.sender] = rollCount;
rollCount += 1;
isStudent[msg.sender] = true;
}
}
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);
if(msg.sender == admin){
if(isCourse[courseNo] == false){
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
if (isCourse[courseNo] == true && isStudent[msg.sender]==true){
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)
// if(isStudent[msg.sender]==true){
// int roll = student[msg.sender];
// }
if (isCourse[courseNo] == true){
if(course[courseNo].isEnroll(student[msg.sender]) == true){
int r1 = course[courseNo].getMidsemMarks(student[msg.sender]);
int r2 = course[courseNo].getEndsemMarks(student[msg.sender]);
int r3 = course[courseNo].getAttendance(student[msg.sender]);
return (r1, r2, r3);
}
}
}
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
if(isStudent[msg.sender] == true){
return student[msg.sender];
}
else{
return -1;
}
}
}
[
{
"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