Skip to content

Instantly share code, notes, and snippets.

@madhawav
Created January 20, 2019 16:29
Show Gist options
  • Save madhawav/7e7681137927adcc4253772486fba735 to your computer and use it in GitHub Desktop.
Save madhawav/7e7681137927adcc4253772486fba735 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.16+commit.d7661dd9.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.4.16;
contract CashJoin{
address master = msg.sender;
address opponent = 0;
bool _init = false;
uint reward;
bool _completed = false;
function init() payable {
require(_init == false);
require(msg.value > 1 ether);
reward = msg.value * 2;
_init = true;
}
function join() payable {
require(_init);
require(opponent == 0);
require(master != msg.sender);
require(msg.value == reward/2);
opponent = msg.sender;
}
function winMaster(){
require(opponent != 0);
require(_init);
require(!_completed);
master.transfer(reward);
reward = 0;
_completed = true;
}
function getStatus() public constant returns (address, address, uint) {
require(_init);
require(!_completed);
require(opponent != 0);
return (master, opponent, reward);
}
}
pragma solidity ^0.4.16;
contract HelloWorld {
uint256 counter = 5;
address owner = msg.sender; //set owner as msg.sender
function add() public {
counter++;
}
function subtract() public { //decreases counter by 1
counter--;
}
function getCounter() public constant returns (uint256) {
return counter;
}
function kill() public
{
//self-destruct function,
if(msg.sender == owner)
selfdestruct(owner);
}
function () public payable {
}
}
pragma solidity ^0.4.16;
/**
* A Two Player TicTacToe Game played using a smart contract.
* P.S. I am just starting to learn solidity.
* Author: madhawav
*/
contract TicTacToe
{
uint boardWidth;
uint[] cells;
address master = msg.sender;
address player1;
address player2;
uint nextTurn = 0;
/**
* Must be called by the initiator of contract. Starts a game between caller and a specified opponent
*/
function init(uint size, address opponent) public{
if(msg.sender == master && opponent != master)
{
boardWidth = size;
cells = new uint[](size*size);
player1 = master;
player2 = opponent;
nextTurn = 1;
}
}
/**
* Preview board
*/
function getBoard() public constant returns (uint[]) {
return cells;
}
/**
* View players in game
*/
function getPlayers() public constant returns (address,address){
return (player1, player2);
}
function _setCell(uint x, uint y, uint val) internal {
cells[y*boardWidth + x] = val;
}
/**
* Returns true if my turn
*/
function checkMyTurn() constant returns (bool){
if(checkResult() != 0)
return false;
address turnAddress = player1;
if(nextTurn == 2)
turnAddress = player2;
if(msg.sender == turnAddress)
return true;
return false;
}
function _passTurn() internal {
if(nextTurn == 1)
nextTurn = 2;
else
nextTurn = 1;
}
function _getCell(uint x, uint y) internal constant returns (uint){
return cells[y*boardWidth + x];
}
/**
* Play my turn
*/
function playCell(uint x, uint y) public {
if(checkMyTurn())
{
if(_getCell(x,y) == 0)
{
_setCell(x,y,nextTurn);
_passTurn();
}
}
}
/**
* Check game status
* 0: Game on
* 1: Player 1 has won
* 2: Player 2 has won
* 3: Game has tied
* 4: Game has not started yet
*/
function checkResult() constant returns (uint){
if(nextTurn == 0)
return 4; // Game has not started yet
if(isWin(1))
return 1;
if(isWin(2))
return 2;
//Check for blank cells
for(uint i = 0; i < boardWidth*boardWidth; i++)
{
if(cells[i] == 0)
return 0; // Game still on
}
return 3; //Draw
}
function isWin(uint player) internal constant returns (bool) {
// Check rows
for(uint y = 0; y < boardWidth; y++){
bool failed = false;
for(uint x = 0; x < boardWidth; x++){
if(_getCell(x,y) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
//Check columns
for(x = 0; x < boardWidth; x++){
failed = false;
for(y = 0; y < boardWidth; y++){
if(_getCell(x,y) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
// Positive Diagonal
failed = false;
for(x = 0; x < boardWidth; x++)
{
if(_getCell(x,x) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
// Negative Diagonal
failed = false;
for(x = 0; x < boardWidth; x++)
{
if(_getCell(boardWidth - x - 1,x) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
}
pragma solidity ^0.4.16;
/**
* Two player TicTacToe Game played using Smart Contracts.
* The player 1 deploys the smart contract and calls init, specifying the board size. The init transaction should carry atleast 1 ether. The winner's reward will be 2 * init amount.
* The player 2 joins the game. The transaction value should match init transaction value.
* Player 2 makes the first move using playCell call.
* Players make moves in turns.
* Winner receives the reward.
* A player can check whether its their move using getStatus call.
* getBoard call can be used to preview game board.
* getStatus call can be used to get stage (preinit, prejoin, started, player1Won, player2Won, drawn), players' addresses and winner's reward.
*
* Author: madhawav
*/
contract TicTacToeCash {
address master = msg.sender;
address player1 = 0;
address player2 = 0;
uint nextTurn = 0;
uint boardWidth;
uint[] cells;
Stages stage = Stages.preinit;
uint reward;
enum Stages{
preinit, prejoin, started, player1Won, player2Won, drawn
}
/**
* Start game as player 1
*/
function init(uint size) payable {
require(stage == Stages.preinit);
require(master == msg.sender);
require(size >= 2);
require(msg.value >= 1 ether);
reward = msg.value * 2;
player1 = msg.sender;
boardWidth = size;
cells = new uint[](size*size);
stage = Stages.prejoin;
}
/**
* Preview board.
*/
function getBoard() public constant returns (uint[]) {
require(stage == Stages.started || stage == Stages.player1Won || stage == Stages.player2Won || stage == Stages.drawn);
require(msg.sender == player1 || msg.sender == player2);
return cells;
}
/**
* Join as player 2
*/
function join() payable {
require(stage == Stages.prejoin);
require(player2 == 0);
require(player1 != msg.sender);
require(msg.value == reward/2);
player2 = msg.sender;
nextTurn = 2;
stage = Stages.started;
}
function _getCell(uint x, uint y) internal constant returns (uint){
return cells[y*boardWidth + x];
}
function _setCell(uint x, uint y, uint val) internal {
cells[y*boardWidth + x] = val;
}
/**
* Returns true if my turn
*/
function checkMyTurn() constant returns (bool){
require(stage==Stages.started);
require(msg.sender == player1 || msg.sender == player2);
address turnAddress = player1;
if(nextTurn == 2)
turnAddress = player2;
if(msg.sender == turnAddress)
return true;
return false;
}
function _passTurn() internal {
if(nextTurn == 1)
nextTurn = 2;
else
nextTurn = 1;
}
/**
* Play my turn
*/
function playCell(uint x, uint y) public {
require(stage==Stages.started);
require(x < boardWidth && y < boardWidth);
require(msg.sender == player1 || msg.sender == player2);
require(checkMyTurn());
require(_getCell(x,y) == 0);
_setCell(x,y,nextTurn);
_passTurn();
updateState();
}
function updateState() internal {
require(stage == Stages.started);
if(isWin(1))
{
player1.transfer(reward);
stage = Stages.player1Won;
}
if(isWin(2))
{
player2.transfer(reward);
stage = Stages.player2Won;
}
//Check for blank cells
for(uint i = 0; i < boardWidth*boardWidth; i++)
{
if(cells[i] == 0)
{
return; // Game still on
}
}
stage = Stages.drawn;
}
/**
* Retrieve status of game
*/
function getStatus() constant returns (Stages, address, address, uint) {
require(msg.sender == player1 || (stage == Stages.prejoin || msg.sender == player2));
return (stage, player1, player2, reward);
}
function isWin(uint player) internal constant returns (bool) {
// Check rows
for(uint y = 0; y < boardWidth; y++){
bool failed = false;
for(uint x = 0; x < boardWidth; x++){
if(_getCell(x,y) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
//Check columns
for(x = 0; x < boardWidth; x++){
failed = false;
for(y = 0; y < boardWidth; y++){
if(_getCell(x,y) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
// Positive Diagonal
failed = false;
for(x = 0; x < boardWidth; x++)
{
if(_getCell(x,x) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
// Negative Diagonal
failed = false;
for(x = 0; x < boardWidth; x++)
{
if(_getCell(boardWidth - x - 1,x) != player)
{
failed = true;
break;
}
}
if(!failed)
return true;
}
}
pragma solidity ^0.4.16;
contract TransferLearn
{
mapping (address => uint) balance;
function accept() payable {
require(msg.value == 2 ether);
balance[msg.sender] += msg.value;
}
function getBalance() public constant returns (uint) {
return balance[msg.sender];
}
function refund(uint amountRequested) public {
require(amountRequested > 0 && amountRequested <= balance[msg.sender]);
balance[msg.sender] -= amountRequested;
msg.sender.transfer(amountRequested); // contract transfers ether to msg.sender's address
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment