Skip to content

Instantly share code, notes, and snippets.

@vijayindalkar
Created July 19, 2020 10:34
Show Gist options
  • Save vijayindalkar/83d4cd3e05b32ad5368266b43628a873 to your computer and use it in GitHub Desktop.
Save vijayindalkar/83d4cd3e05b32ad5368266b43628a873 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.7.0;
/**
* @title Storage
* @dev Store & retreive value in a variable
*/
contract Storage {
uint256 number;
/**
* @dev Store value in variable
* @param num value to store
*/
function store(uint256 num) public {
number = num;
}
/**
* @dev Return value
* @return value of 'number'
*/
function retreive() public view returns (uint256){
return number;
}
}
pragma solidity >=0.4.22 <0.7.0;
/**
* @title Owner
* @dev Set & change owner
*/
contract Owner {
address private owner;
// event for EVM logging
event OwnerSet(address indexed oldOwner, address indexed newOwner);
// modifier to check if caller is owner
modifier isOwner() {
// If the first argument of 'require' evaluates to 'false', execution terminates and all
// changes to the state and to Ether balances are reverted.
// This used to consume all gas in old EVM versions, but not anymore.
// It is often a good idea to use 'require' to check if functions are called correctly.
// As a second argument, you can also provide an explanation about what went wrong.
require(msg.sender == owner, "Caller is not owner");
_;
}
/**
* @dev Set contract deployer as owner
*/
constructor() public {
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor
emit OwnerSet(address(0), owner);
}
/**
* @dev Change owner
* @param newOwner address of new owner
*/
function changeOwner(address newOwner) public isOwner {
emit OwnerSet(owner, newOwner);
owner = newOwner;
}
/**
* @dev Return owner address
* @return address of owner
*/
function getOwner() external view returns (address) {
return owner;
}
}
pragma solidity >=0.4.22 <0.7.0;
/**
* @title Ballot
* @dev Implements voting process along with vote delegation
*/
contract Ballot {
struct Voter {
uint weight; // weight is accumulated by delegation
bool voted; // if true, that person already voted
address delegate; // person delegated to
uint vote; // index of the voted proposal
}
struct Proposal {
// If you can limit the length to a certain number of bytes,
// always use one of bytes1 to bytes32 because they are much cheaper
bytes32 name; // short name (up to 32 bytes)
uint voteCount; // number of accumulated votes
}
address public chairperson;
mapping(address => Voter) public voters;
Proposal[] public proposals;
/**
* @dev Create a new ballot to choose one of 'proposalNames'.
* @param proposalNames names of proposals
*/
constructor(bytes32[] memory proposalNames) public {
chairperson = msg.sender;
voters[chairperson].weight = 1;
for (uint i = 0; i < proposalNames.length; i++) {
// 'Proposal({...})' creates a temporary
// Proposal object and 'proposals.push(...)'
// appends it to the end of 'proposals'.
proposals.push(Proposal({
name: proposalNames[i],
voteCount: 0
}));
}
}
/**
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'.
* @param voter address of voter
*/
function giveRightToVote(address voter) public {
require(
msg.sender == chairperson,
"Only chairperson can give right to vote."
);
require(
!voters[voter].voted,
"The voter already voted."
);
require(voters[voter].weight == 0);
voters[voter].weight = 1;
}
/**
* @dev Delegate your vote to the voter 'to'.
* @param to address to which vote is delegated
*/
function delegate(address to) public {
Voter storage sender = voters[msg.sender];
require(!sender.voted, "You already voted.");
require(to != msg.sender, "Self-delegation is disallowed.");
while (voters[to].delegate != address(0)) {
to = voters[to].delegate;
// We found a loop in the delegation, not allowed.
require(to != msg.sender, "Found loop in delegation.");
}
sender.voted = true;
sender.delegate = to;
Voter storage delegate_ = voters[to];
if (delegate_.voted) {
// If the delegate already voted,
// directly add to the number of votes
proposals[delegate_.vote].voteCount += sender.weight;
} else {
// If the delegate did not vote yet,
// add to her weight.
delegate_.weight += sender.weight;
}
}
/**
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'.
* @param proposal index of proposal in the proposals array
*/
function vote(uint proposal) public {
Voter storage sender = voters[msg.sender];
require(sender.weight != 0, "Has no right to vote");
require(!sender.voted, "Already voted.");
sender.voted = true;
sender.vote = proposal;
// If 'proposal' is out of the range of the array,
// this will throw automatically and revert all
// changes.
proposals[proposal].voteCount += sender.weight;
}
/**
* @dev Computes the winning proposal taking all previous votes into account.
* @return winningProposal_ index of winning proposal in the proposals array
*/
function winningProposal() public view
returns (uint winningProposal_)
{
uint winningVoteCount = 0;
for (uint p = 0; p < proposals.length; p++) {
if (proposals[p].voteCount > winningVoteCount) {
winningVoteCount = proposals[p].voteCount;
winningProposal_ = p;
}
}
}
/**
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then
* @return winnerName_ the name of the winner
*/
function winnerName() public view
returns (bytes32 winnerName_)
{
winnerName_ = proposals[winningProposal()].name;
}
}
pragma solidity >=0.4.22 <0.7.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./3_Ballot.sol";
contract BallotTest {
bytes32[] proposalNames;
Ballot ballotToTest;
function beforeAll () public {
proposalNames.push(bytes32("candidate1"));
ballotToTest = new Ballot(proposalNames);
}
function checkWinningProposal () public {
ballotToTest.vote(0);
Assert.equal(ballotToTest.winningProposal(), uint(0), "proposal at index 0 should be the winning proposal");
Assert.equal(ballotToTest.winnerName(), bytes32("candidate1"), "candidate1 should be the winner name");
}
function checkWinninProposalWithReturnValue () public view returns (bool) {
return ballotToTest.winningProposal() == 0;
}
}
pragma solidity 0.4.24;
contract myContract{
string value;
constructor() public{
value = "MYvalue";
}
function getter() public view returns(string){
return value;
}
function set(string _value) public{
value = _value;
}
}
// this line is added to create a gist. Empty file is not allowed.
pragma solidity 0.5.1;
contract Enums{
enum State{Waiting,Ready,Active}
State public state;
constructor() public{
state = State.Waiting;
}
function activator() public{
state = State.Active;
}
function isActive() view public returns(bool){
return state == State.Active;
}
}
pragma solidity ^0.4.0;
contract Greeter {
string public yourName; // data
/* This runs when the contract is executed */
function Greeter() public {
yourName = "World";
}
function set(string name)public {
yourName = name;
}
function hello() constant public returns (string) {
return yourName;
}
}
pragma solidity ^0.4.0;
contract Greeter {
string public yourName; // data
/* This runs when the contract is executed */
function Greeter() public {
yourName = "World";
}
function set(string name)public {
yourName = name;
}
function hello() constant public returns (string) {
return yourName;
}
}
pragma solidity ^0.4.0;
contract greeter{
string public yourName;
function Greeter() public{
yourName = "World";
}
function set(string name)public{
yourName = name;
}
function hello() constant public returns(string){
return yourName;
}
}
pragma solidity 0.5.1;
contract myContract{
uint256 public peopleCount = 0 ;
mapping(uint => Person ) public people;
struct Person{
uint id;
string _firstname;
string _lastname;
}
function addPerson(string memory _firstname,string memory _lastname) public {
peopleCount += 1;
people[peopleCount] = Person(peopleCount,_firstname,_lastname);
}
}
pragma solidity ^0.4.0;
contract Coin{
address public minter;
mapping(address => uint) public Balances;
event Sent(address from,address to,uint amount);
function Coin() public{
minter = msg.sender;
}
function mint(address receiver,uint amount) public{
if(msg.sender != minter) return;
Balances[receiver] += amount;
}
function send(address receiver,uint amount)public {
if(Balances[msg.sender]< amount) return;
Balances[msg.sender] -= amount;
Balances[receiver] += amount;
Sent(msg.sender,receiver,amount);
}
}
pragma solidity 0.5.1;
contract myContract{
uint256 public peopleCount = 0;
mapping(uint => Person) public people;
struct Person{
uint id;
string _firstname;
string _lastname;
}
function addperson(string memory _firstname, string memory _lastname) public{
Increment();
people[peopleCount]=Person(peopleCount,_firstname,_lastname);
}
function Increment() internal{
peopleCount += 1;
}
}
pragma solidity 0.5.1;
contract dapp{
uint public peopleCount;
mapping(uint => Person) public people;
address vijay;
modifier onlyOnwer(){
require(msg.sender == vijay);
_;
}
struct Person{
uint id;
string _firstname;
string _lastname;
}
constructor() public{
vijay = msg.sender;
}
function addPerson(string memory _firstname, string memory _lastname) public onlyOnwer{
incrementCount();
people[peopleCount]= Person(peopleCount,_firstname,_lastname);
}
function incrementCount() internal{
peopleCount += 1;
}
}
{
"accounts": {
"account{0}": "0x79F886db382EC1f03320FD1472df9F7a58112567",
"account{1}": "0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"account{2}": "0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"account{3}": "0xe7106B2E48c34f2026c7C2B3c7E21ba88417D3f3"
},
"linkReferences": {},
"transactions": [
{
"timestamp": 1592990441968,
"record": {
"value": "0",
"parameters": [],
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"contractName": "Coin",
"bytecode": "608060405234801561001057600080fd5b50336000806101000a81548173ffffffffffffffffffffffffffffffffffffffff021916908373ffffffffffffffffffffffffffffffffffffffff16021790555061044f806100606000396000f300608060405260043610610062576000357c0100000000000000000000000000000000000000000000000000000000900463ffffffff168063075461721461006757806340c10f19146100be578063a8ec4c421461010b578063d0679d3414610162575b600080fd5b34801561007357600080fd5b5061007c6101af565b604051808273ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff16815260200191505060405180910390f35b3480156100ca57600080fd5b50610109600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190803590602001909291905050506101d4565b005b34801561011757600080fd5b5061014c600480360381019080803573ffffffffffffffffffffffffffffffffffffffff169060200190929190505050610281565b6040518082815260200191505060405180910390f35b34801561016e57600080fd5b506101ad600480360381019080803573ffffffffffffffffffffffffffffffffffffffff16906020019092919080359060200190929190505050610299565b005b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1681565b6000809054906101000a900473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff163373ffffffffffffffffffffffffffffffffffffffff1614151561022f5761027d565b80600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055505b5050565b60016020528060005260406000206000915090505481565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000205410156102e55761041f565b80600160003373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020019081526020016000206000828254039250508190555080600160008473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001908152602001600020600082825401925050819055507f3990db2d31862302a685e8086b5755072a6e2b5b780af1ee81ece35ee3cd3345338383604051808473ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff1681526020018373ffffffffffffffffffffffffffffffffffffffff1673ffffffffffffffffffffffffffffffffffffffff168152602001828152602001935050505060405180910390a15b50505600a165627a7a723058205ff995c1cb4de417fda5222bff3c68caebedafc9d71e605fc4abea4cf52da4c40029",
"linkReferences": {},
"name": "",
"inputs": "()",
"type": "constructor",
"from": "account{0}"
}
},
{
"timestamp": 1592990520907,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"2000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1592990574164,
"record": {
"value": "0",
"parameters": [
"0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990576614,
"record": {
"value": "0",
"parameters": [
"0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990583214,
"record": {
"value": "0",
"parameters": [
"0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990587571,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"2000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990589021,
"record": {
"value": "0",
"parameters": [
"0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990599663,
"record": {
"value": "0",
"parameters": [
"0x5818885759e16be98ade7f25Cbf0a37BD41eD481",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{1}"
}
},
{
"timestamp": 1592990633081,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{0}"
}
},
{
"timestamp": 1592990666440,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{2}"
}
},
{
"timestamp": 1592990678317,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{2}"
}
},
{
"timestamp": 1592990724423,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{2}"
}
},
{
"timestamp": 1592990749406,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"600"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990758390,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"5000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990786501,
"record": {
"value": "0",
"parameters": [
"0xeBc4A4B95f5e0091380924Ee44117900a4A6cF3f",
"5000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "send",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990788044,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990791092,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990791275,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990791445,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990791620,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990791810,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990792023,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"4000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{3}"
}
},
{
"timestamp": 1592990851082,
"record": {
"value": "0",
"parameters": [
"0x79F886db382EC1f03320FD1472df9F7a58112567",
"40000"
],
"to": "created{1592990441968}",
"abi": "0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3",
"name": "mint",
"inputs": "(address,uint256)",
"type": "function",
"from": "account{0}"
}
}
],
"abis": {
"0x5037e1a5e02e081b1b850b130eca7ac17335fdf4c61cc5ff6ae765196fb0d5b3": [
{
"constant": false,
"inputs": [
{
"name": "receiver",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "mint",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"constant": false,
"inputs": [
{
"name": "receiver",
"type": "address"
},
{
"name": "amount",
"type": "uint256"
}
],
"name": "send",
"outputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"payable": false,
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"anonymous": false,
"inputs": [
{
"indexed": false,
"name": "from",
"type": "address"
},
{
"indexed": false,
"name": "to",
"type": "address"
},
{
"indexed": false,
"name": "amount",
"type": "uint256"
}
],
"name": "Sent",
"type": "event"
},
{
"constant": true,
"inputs": [
{
"name": "",
"type": "address"
}
],
"name": "Balances",
"outputs": [
{
"name": "",
"type": "uint256"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
},
{
"constant": true,
"inputs": [],
"name": "minter",
"outputs": [
{
"name": "",
"type": "address"
}
],
"payable": false,
"stateMutability": "view",
"type": "function"
}
]
}
}
pragma solidity ^0.4.0;
contract simpleStorage{
uint storedData;
function set(uint x) public{
storedData = x;
}
function get() constant public returns(uint){
return storedData;
}
function increment(uint n)public{
storedData = storedData + n;
}
function decrement(uint n)public{
storedData = storedData -n;
}
}
pragma solidity ^0.5.1;
contract Stracts{
Person[] public people;
uint256 public peopleCount;
struct Person{
string firstname;
string lastname;
}
function addPerson(string memory firstname,string memory lastname)public{
people.push(Person(firstname,lastname));
peopleCount += 1;
}
}
pragma solidity ^0.4.17;
contract Auction {
// Data
//Structure to hold details of the item
struct Item {
uint itemId; // id of the item
uint[] itemTokens; //tokens bid in favor of the item
}
//Structure to hold the details of a persons
struct Person {
uint remainingTokens; // tokens remaining with bidder
uint personId; // it serves as tokenId as well
address addr;//address of the bidder
}
mapping(address => Person) tokenDetails; //address to person
Person [4] bidders;//Array containing 4 person objects
Item [3] public items;//Array containing 3 item objects
address[3] public winners;//Array for address of winners
address public beneficiary;//owner of the smart contract
uint bidderCount=0;//counter
//functions
function Auction() public payable{ //constructor
//Part 1 Task 1. Initialize beneficiary with address of smart contract’s owner
//Hint. In the constructor,"msg.sender" is the address of the owner.
// ** Start code here. 1 line approximately. **/
address beneficiary = msg.sender;
//** End code here. **/
uint[] memory emptyArray;
items[0] = Item({itemId:0,itemTokens:emptyArray});
//Part 1 Task 2. Initialize two items with at index 1 and 2.
// ** Start code here. 2 lines approximately. **/
items[1] = Items({itemsId:1,itemTokens:emptyArray});
items[2] = Items({itemsId:2,itemTokens:emptyArray})
//** End code here**/
}
function register() public payable{
bidders[bidderCount].personId = bidderCount;
//Part 1 Task 3. Initialize the address of the bidder
/*Hint. Here the bidders[bidderCount].addr should be initialized with address of the registrant.*/
// ** Start code here. 1 line approximately. **/
bidders[bidderCount].addr = address register;
//** End code here. **
bidders[bidderCount].remainingTokens = 5; // only 5 tokens
tokenDetails[msg.sender]=bidders[bidderCount];
bidderCount++;
}
function bid(uint _itemId, uint _count) public payable{
/*
Bids tokens to a particular item.
Arguments:
_itemId -- uint, id of the item
_count -- uint, count of tokens to bid for the item
*/
/*
Part 1 Task 4. Implement the three conditions below.
4.1 If the number of tokens remaining with the bidder is < count of tokens bidded, revert.
4.2 If there are no tokens remaining with the bidder, revert.
4.3 If the id of the item for which bid is placed, is greater than 2, revert.
Hint: "tokenDetails[msg.sender].remainingTokens" gives the details of the number of tokens remaining with the bidder.
*/
// ** Start code here. 2 lines approximately. **/
if()
//** End code here. **
/*Part 1 Task 5. Decrement the remainingTokens by the number of tokens bid and store the value in balance variable.
Hint. "tokenDetails[msg.sender].remainingTokens" should be decremented by "_count". */
// ** Start code here. 1 line approximately. **
uint balance=
//** End code here. **
tokenDetails[msg.sender].remainingTokens=balance;
bidders[tokenDetails[msg.sender].personId].remainingTokens=balance;//updating the same balance in bidders map.
Item storage bidItem = items[_itemId];
for(uint i=0; i<_count;i++) {
bidItem.itemTokens.push(tokenDetails[msg.sender].personId);
}
}
// Part 2 Task 1. Create a modifier named "onlyOwner" to ensure that only owner is allowed to reveal winners
//Hint : Use require to validate if "msg.sender" is equal to the "beneficiary".
modifier onlyOwner {
// ** Start code here. 2 lines approximately. **
_;
//** End code here. **
}
function revealWinners() public onlyOwner{
/*
Iterate over all the items present in the auction.
If at least on person has placed a bid, randomly select the winner */
for (uint id = 0; id < 3; id++) {
Item storage currentItem=items[id];
if(currentItem.itemTokens.length != 0){
// generate random# from block number
uint randomIndex = (block.number / currentItem.itemTokens.length)% currentItem.itemTokens.length;
// Obtain the winning tokenId
uint winnerId = currentItem.itemTokens[randomIndex];
/* Part 1 Task 6. Assign the winners.
Hint." bidders[winnerId] " will give you the person object with the winnerId.
you need to assign the address of the person obtained above to winners[id] */
// ** Start coding here *** 1 line approximately.
//** end code here*
}
}
}
//Miscellaneous methods: Below methods are used to assist Grading. Please DONOT CHANGE THEM.
function getPersonDetails(uint id) public constant returns(uint,uint,address){
return (bidders[id].remainingTokens,bidders[id].personId,bidders[id].addr);
}
}
pragma solidity 0.5.1;
contract myContract{
uint256 public peopleCount = 0;
mapping(uint => Person) public people;
uint256 openingTime= 1595155094;
modifier onlyWhileopen(){
require(block.timestamp >= openingTime);
_;
}
struct Person{
uint id;
string _firstname;
string _lastname;
}
function addperson(string memory _firstname, string memory _lastname) public onlyWhileopen{
Increment();
people[peopleCount]=Person(peopleCount,_firstname,_lastname);
}
function Increment() internal{
peopleCount += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment