Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lab2 #5

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4,300 changes: 4,300 additions & 0 deletions Betting Contract/build/contracts/Betting.json

Large diffs are not rendered by default.

827 changes: 827 additions & 0 deletions Betting Contract/build/contracts/Migrations.json

Large diffs are not rendered by default.

66 changes: 58 additions & 8 deletions Betting Contract/contracts/betting.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
pragma solidity 0.4.19;

pragma solidity ^0.4.16;

contract Betting {
/* Constructor function, where owner and outcomes are set */
function Betting(uint[] _outcomes) public {
gamblerA = 0;
gamblerB = 0;
oracle = msg.sender;
owner = msg.sender;

for (uint i = 0; i < _outcomes.length; i++) {
outcomes[i] = _outcomes[i];
}
}

/* Fallback function */
Expand Down Expand Up @@ -36,35 +43,78 @@ contract Betting {
event BetClosed();

/* Uh Oh, what are these? */
modifier ownerOnly() {_;}
modifier oracleOnly() {_;}
modifier outcomeExists(uint outcome) {_;}
modifier ownerOnly() {
require(msg.sender == owner);
_;
}

modifier oracleOnly() {
require(msg.sender == oracle);
_;
}

modifier outcomeExists(uint outcome) {
require(outcomes[outcome] >= 0);
_;
}

/* Owner chooses their trusted Oracle */
function chooseOracle(address _oracle) public ownerOnly() returns (address) {
oracle = _oracle;
return oracle;
}

/* Gamblers place their bets, preferably after calling checkOutcomes */
function makeBet(uint _outcome) public payable returns (bool) {
assert(msg.value > 0);

bets[msg.sender] = Bet(_outcome, msg.value, true);

if (gamblerA == 0) {
gamblerA = msg.sender;
} else if (gamblerB == 0 && msg.sender != gamblerA) {
gamblerB = msg.sender;
}

BetMade(msg.sender);
return true;
}

/* The oracle chooses which outcome wins */
/* The oracle chooses which outcome wins */
function makeDecision(uint _outcome) public oracleOnly() outcomeExists(_outcome) {
Bet storage betA = bets[gamblerA];
Bet storage betB = bets[gamblerB];
uint pot = betA.amount + betB.amount;
if (betA.outcome == _outcome) {
winnings[gamblerA] += pot;
} else if (betB.outcome == _outcome) {
winnings[gamblerB] += pot;
}

contractReset();
}

/* Allow anyone to withdraw their winnings safely (if they have enough) */
function withdraw(uint withdrawAmount) public returns (uint) {
if (this.balance > withdrawAmount) {
msg.sender.transfer(withdrawAmount);
}
return this.balance;
}

/* Allow anyone to check the outcomes they can bet on */
function checkOutcomes(uint outcome) public view returns (uint) {
return outcomes[outcome];
}

/* Allow anyone to check if they won any bets */
function checkWinnings() public view returns(uint) {
return winnings[msg.sender];
}

/* Call delete() to reset certain state variables. Which ones? That's upto you to decide */
function contractReset() public ownerOnly() {
delete(gamblerA);
delete(gamblerB);
}
}
2 changes: 1 addition & 1 deletion Betting Contract/migrations/2_deploy_contracts.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
var Betting = artifacts.require("./Betting.sol");

module.exports = function(deployer) {
deployer.deploy(Betting, ["A", "B", "C"]);
deployer.deploy(Betting);
};
18 changes: 16 additions & 2 deletions Intro to Solidity/contracts/concatenate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,22 @@ pragma solidity 0.4.19;


contract Concatenate {
function concatWithoutImport(string _s, string _t) public returns (string) {
}
string a;
string b;

function Concatenate(string _a, string _b) { a = _a; b = _b; strConcat(a, b); }

function strConcat(string _a, string _b) internal returns (string){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory ab = new string(_ba.length + _bb.length);
bytes memory ba = bytes(ab);
uint k = 0;
for (uint i = 0; i < _ba.length; i++) ba[k++] = _ba[i];
for (i = 0; i < _bb.length; i++) ba[k++] = _bb[i];

return string(ba);
}

/* BONUS */
function concatWithImport(string s, string t) public returns (string) {
Expand Down
20 changes: 20 additions & 0 deletions Intro to Solidity/contracts/fibonacci.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,29 @@ pragma solidity 0.4.19;
contract Fibonacci {
/* Carry out calculations to find the nth Fibonacci number */
function fibRecur(uint n) public pure returns (uint) {
if (n == 0) {
return 0;
}
if (n == 1) {
return 1;
}
if (n >= 2) {
return fibRecur(n - 1) + fibRecur(n - 2);
}
}

/* Carry out calculations to find the nth Fibonacci number */
function fibIter(uint n) public returns (uint) {
uint prev = 0;
uint curr = 1;
uint result = prev + curr;

for (uint256 i = 0; i < n; i++) {
prev = curr;
curr = result;
result = prev + curr;
}

return result;
}
}
8 changes: 5 additions & 3 deletions Intro to Solidity/contracts/greeter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ pragma solidity 0.4.19;


contract Greeter {
string private greeting;

function Greeter(string _greeter) public {
string private greeting = 'hello world!';

function Greeter() public {
}

function greet() public view returns (string) {
function greet() public constant returns (string) {
return greeting;
}
}
6 changes: 5 additions & 1 deletion Intro to Solidity/contracts/xor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ pragma solidity 0.4.19;


contract XOR {
function xor(uint a, uint b) public pure returns (uint) {
function xor(uint inputA, uint inputB) public pure returns (uint) {
if (inputA == 0 && inputB == 0) { return 1; }
if (inputA == 1 && inputB == 1) { return 1; }
if (inputA == 0 && inputB == 1) { return 0; }
if (inputA == 1 && inputB == 0) { return 0; }
}
}