From c3a23cae1018aa0919f049fe482b4ed232c503b2 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sun, 28 Apr 2019 17:19:23 -0700 Subject: [PATCH 1/2] Add comments --- contracts/Lottery.sol | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) diff --git a/contracts/Lottery.sol b/contracts/Lottery.sol index adbc9000b..0b14a6361 100644 --- a/contracts/Lottery.sol +++ b/contracts/Lottery.sol @@ -1,26 +1,37 @@ pragma solidity >=0.4.22; contract Lottery { - string internal constant ENTER_MESSAGE = "The player needs to stake at least 0.1 ether"; - string internal constant RESTRICTED_MESSAGE = "Only manager can do"; + string internal constant INSUFFICIENT_FUND_MESSAGE = "Insufficient Fund"; + string internal constant RESTRICTED_MESSAGE = "Unauthorized Access"; - address public manager; - address payable[] public players; + address public manager; // The adress of the owner of this contract + address payable[] public players; // The players of current session constructor() public { manager = msg.sender; } + /** + * @dev The player enters into the current lottery session by + * paying at least 0.1 token. + */ function enter() public payable { - require(msg.value > .1 ether, ENTER_MESSAGE); + require(msg.value > .1 ether, INSUFFICIENT_FUND_MESSAGE); players.push(msg.sender); } + /** + * @dev Random number used for picking the winner. + */ function random() private view returns (uint) { return uint(keccak256(abi.encodePacked(now, msg.sender, this))); } + /** + * @dev Randomly select one of the players in current session as winner + * and send all the token in smart contract balance to it. + */ function pickWinner() public payable restricted { uint index = random() % players.length; players[index].transfer(address(this).balance); @@ -32,6 +43,9 @@ contract Lottery { _; } + /** + * @dev Returns a list of all players in the current session. + */ function getPlayers() public view returns (address payable[] memory) { return players; } From c58df54091242c3d1fcfe60bcc57ce26d986a672 Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Sun, 28 Apr 2019 17:26:42 -0700 Subject: [PATCH 2/2] Regen go file --- contracts/Lottery.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/contracts/Lottery.go b/contracts/Lottery.go index 8df9fee99..6041503c7 100644 --- a/contracts/Lottery.go +++ b/contracts/Lottery.go @@ -31,7 +31,7 @@ var ( const LotteryABI = "[{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"pickWinner\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"enter\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"constructor\"}]" // LotteryBin is the compiled bytecode used for deploying new contracts. -const LotteryBin = `0x608060405234801561001057600080fd5b50600080546001600160a01b031916331790556104ec806100326000396000f3fe60806040526004361061004a5760003560e01c8063481c6a751461004f5780635d495aea146100805780638b5b9ccc1461008a578063e97dcb62146100ef578063f71d96cb146100f7575b600080fd5b34801561005b57600080fd5b50610064610121565b604080516001600160a01b039092168252519081900360200190f35b610088610130565b005b34801561009657600080fd5b5061009f61028c565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100db5781810151838201526020016100c3565b505050509050019250505060405180910390f35b6100886102ef565b34801561010357600080fd5b506100646004803603602081101561011a57600080fd5b50356103a9565b6000546001600160a01b031681565b60005460408051808201909152601381527f4f6e6c79206d616e616765722063616e20646f000000000000000000000000006020820152906001600160a01b031633146101fe57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101c35781810151838201526020016101ab565b50505050905090810190601f1680156101f05780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060015460009061020d6103d1565b81151561021657fe5b06905060018181548110151561022857fe5b60009182526020822001546040516001600160a01b0390911691303180156108fc02929091818181858888f1935050505015801561026a573d6000803e3d6000fd5b5060408051600081526020810191829052516102889160019161040b565b5050565b606060018054806020026020016040519081016040528092919081815260200182805480156102e457602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116102c6575b505050505090505b90565b67016345785d8a000034116040518060600160405280602c8152602001610495602c913990151561036557604051600160e51b62461bcd028152600401808060200182810382528381815181526020019150805190602001908083836000838110156101c35781810151838201526020016101ab565b506001805480820182556000919091527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b03191633179055565b60018054829081106103b757fe5b6000918252602090912001546001600160a01b0316905081565b604080514260208083019190915233606090811b8385015230901b6054830152825160488184030181526068909201909252805191012090565b828054828255906000526020600020908101928215610460579160200282015b8281111561046057825182546001600160a01b0319166001600160a01b0390911617825560209092019160019091019061042b565b5061046c929150610470565b5090565b6102ec91905b8082111561046c5780546001600160a01b031916815560010161047656fe54686520706c61796572206e6565647320746f207374616b65206174206c6561737420302e31206574686572a165627a7a72305820159638f94fdba6b1dc61d4071f6673028ee53a12a39d66f7ec00701aed93a0b60029` +const LotteryBin = `0x608060405234801561001057600080fd5b50600080546001600160a01b031916331790556104d8806100326000396000f3fe60806040526004361061004a5760003560e01c8063481c6a751461004f5780635d495aea146100805780638b5b9ccc1461008a578063e97dcb62146100ef578063f71d96cb146100f7575b600080fd5b34801561005b57600080fd5b50610064610121565b604080516001600160a01b039092168252519081900360200190f35b610088610130565b005b34801561009657600080fd5b5061009f61028c565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100db5781810151838201526020016100c3565b505050509050019250505060405180910390f35b6100886102ef565b34801561010357600080fd5b506100646004803603602081101561011a57600080fd5b50356103c1565b6000546001600160a01b031681565b60005460408051808201909152601381527f556e617574686f72697a656420416363657373000000000000000000000000006020820152906001600160a01b031633146101fe57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101c35781810151838201526020016101ab565b50505050905090810190601f1680156101f05780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b5060015460009061020d6103e9565b81151561021657fe5b06905060018181548110151561022857fe5b60009182526020822001546040516001600160a01b0390911691303180156108fc02929091818181858888f1935050505015801561026a573d6000803e3d6000fd5b50604080516000815260208101918290525161028891600191610423565b5050565b606060018054806020026020016040519081016040528092919081815260200182805480156102e457602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116102c6575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e64000000000000000000000000000000602082015267016345785d8a0000341161037d57604051600160e51b62461bcd028152600401808060200182810382528381815181526020019150805190602001908083836000838110156101c35781810151838201526020016101ab565b506001805480820182556000919091527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf60180546001600160a01b03191633179055565b60018054829081106103cf57fe5b6000918252602090912001546001600160a01b0316905081565b604080514260208083019190915233606090811b8385015230901b6054830152825160488184030181526068909201909252805191012090565b828054828255906000526020600020908101928215610478579160200282015b8281111561047857825182546001600160a01b0319166001600160a01b03909116178255602090920191600190910190610443565b50610484929150610488565b5090565b6102ec91905b808211156104845780546001600160a01b031916815560010161048e56fea165627a7a7230582045d0ba2aa43cdf6d10a8a4c13e1bbe1b2860c498f3727cb51b84378e1d6e99120029` // DeployLottery deploys a new Ethereum contract, binding an instance of Lottery to it. func DeployLottery(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Lottery, error) {