From 5b89a04765edce203b2a5831181669e4f917bb62 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 12 Mar 2019 20:50:48 -0700 Subject: [PATCH] add loterry contract --- contracts/Lottery.go | 295 ++++++++++++++++++++++++++++++++++++++++++ contracts/Lottery.sol | 38 ++++++ 2 files changed, 333 insertions(+) create mode 100644 contracts/Lottery.go create mode 100644 contracts/Lottery.sol diff --git a/contracts/Lottery.go b/contracts/Lottery.go new file mode 100644 index 000000000..c9584ecc5 --- /dev/null +++ b/contracts/Lottery.go @@ -0,0 +1,295 @@ +// Code generated - DO NOT EDIT. +// This file is a generated binding and any manual changes will be lost. + +package contracts + +import ( + "math/big" + "strings" + + "github.com/ethereum/go-ethereum/accounts/abi" + "github.com/ethereum/go-ethereum/accounts/abi/bind" + "github.com/ethereum/go-ethereum/common" + "github.com/harmony-one/harmony/core/types" +) + +// LotteryABI is the input ABI used to generate the binding from. +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 = `0x608060405234801561001057600080fd5b5060008054600160a060020a03191633179055610567806100326000396000f3fe608060405260043610610066577c01000000000000000000000000000000000000000000000000000000006000350463481c6a75811461006b5780635d495aea1461009c5780638b5b9ccc146100a6578063e97dcb621461010b578063f71d96cb14610113575b600080fd5b34801561007757600080fd5b5061008061013d565b60408051600160a060020a039092168252519081900360200190f35b6100a461014c565b005b3480156100b257600080fd5b506100bb6102bf565b60408051602080825283518183015283519192839290830191858101910280838360005b838110156100f75781810151838201526020016100df565b505050509050019250505060405180910390f35b6100a4610322565b34801561011f57600080fd5b506100806004803603602081101561013657600080fd5b5035610400565b600054600160a060020a031681565b60005460408051808201909152601381527f4f6e6c79206d616e616765722063616e20646f00000000000000000000000000602082015290600160a060020a03163314610231576040517f08c379a00000000000000000000000000000000000000000000000000000000081526004018080602001828103825283818151815260200191508051906020019080838360005b838110156101f65781810151838201526020016101de565b50505050905090810190601f1680156102235780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b50600154600090610240610428565b81151561024957fe5b06905060018181548110151561025b57fe5b6000918252602082200154604051600160a060020a0390911691303180156108fc02929091818181858888f1935050505015801561029d573d6000803e3d6000fd5b5060408051600081526020810191829052516102bb9160019161046c565b5050565b6060600180548060200260200160405190810160405280929190818152602001828054801561031757602002820191906000526020600020905b8154600160a060020a031681526001909101906020018083116102f9575b505050505090505b90565b662386f26fc100003411606060405190810160405280602c8152602001610510602c91399015156103af576040517f08c379a0000000000000000000000000000000000000000000000000000000008152600401808060200182810382528381815181526020019150805190602001908083836000838110156101f65781810151838201526020016101de565b506001805480820182556000919091527fb10e2d527612073b26eecdfd717e6a320cf44b4afac2b0732d9fcbe2b7fa0cf601805473ffffffffffffffffffffffffffffffffffffffff191633179055565b600180548290811061040e57fe5b600091825260209091200154600160a060020a0316905081565b60408051426020808301919091526c010000000000000000000000003381028385015230026054830152825160488184030181526068909201909252805191012090565b8280548282559060005260206000209081019282156104ce579160200282015b828111156104ce578251825473ffffffffffffffffffffffffffffffffffffffff1916600160a060020a0390911617825560209092019160019091019061048c565b506104da9291506104de565b5090565b61031f91905b808211156104da57805473ffffffffffffffffffffffffffffffffffffffff191681556001016104e456fe54686520706c61796572206e6565647320746f207374616b65206174206c6561737420302e31206574686572a165627a7a7230582012c4ec768933ccbf32406bc87055c43425580211c880b5c00813c5e26271ed250029` + +// 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) { + parsed, err := abi.JSON(strings.NewReader(LotteryABI)) + if err != nil { + return common.Address{}, nil, nil, err + } + address, tx, contract, err := bind.DeployContract(auth, parsed, common.FromHex(LotteryBin), backend) + if err != nil { + return common.Address{}, nil, nil, err + } + return address, tx, &Lottery{LotteryCaller: LotteryCaller{contract: contract}, LotteryTransactor: LotteryTransactor{contract: contract}, LotteryFilterer: LotteryFilterer{contract: contract}}, nil +} + +// Lottery is an auto generated Go binding around an Ethereum contract. +type Lottery struct { + LotteryCaller // Read-only binding to the contract + LotteryTransactor // Write-only binding to the contract + LotteryFilterer // Log filterer for contract events +} + +// LotteryCaller is an auto generated read-only Go binding around an Ethereum contract. +type LotteryCaller struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// LotteryTransactor is an auto generated write-only Go binding around an Ethereum contract. +type LotteryTransactor struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// LotteryFilterer is an auto generated log filtering Go binding around an Ethereum contract events. +type LotteryFilterer struct { + contract *bind.BoundContract // Generic contract wrapper for the low level calls +} + +// LotterySession is an auto generated Go binding around an Ethereum contract, +// with pre-set call and transact options. +type LotterySession struct { + Contract *Lottery // Generic contract binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// LotteryCallerSession is an auto generated read-only Go binding around an Ethereum contract, +// with pre-set call options. +type LotteryCallerSession struct { + Contract *LotteryCaller // Generic contract caller binding to set the session for + CallOpts bind.CallOpts // Call options to use throughout this session +} + +// LotteryTransactorSession is an auto generated write-only Go binding around an Ethereum contract, +// with pre-set transact options. +type LotteryTransactorSession struct { + Contract *LotteryTransactor // Generic contract transactor binding to set the session for + TransactOpts bind.TransactOpts // Transaction auth options to use throughout this session +} + +// LotteryRaw is an auto generated low-level Go binding around an Ethereum contract. +type LotteryRaw struct { + Contract *Lottery // Generic contract binding to access the raw methods on +} + +// LotteryCallerRaw is an auto generated low-level read-only Go binding around an Ethereum contract. +type LotteryCallerRaw struct { + Contract *LotteryCaller // Generic read-only contract binding to access the raw methods on +} + +// LotteryTransactorRaw is an auto generated low-level write-only Go binding around an Ethereum contract. +type LotteryTransactorRaw struct { + Contract *LotteryTransactor // Generic write-only contract binding to access the raw methods on +} + +// NewLottery creates a new instance of Lottery, bound to a specific deployed contract. +func NewLottery(address common.Address, backend bind.ContractBackend) (*Lottery, error) { + contract, err := bindLottery(address, backend, backend, backend) + if err != nil { + return nil, err + } + return &Lottery{LotteryCaller: LotteryCaller{contract: contract}, LotteryTransactor: LotteryTransactor{contract: contract}, LotteryFilterer: LotteryFilterer{contract: contract}}, nil +} + +// NewLotteryCaller creates a new read-only instance of Lottery, bound to a specific deployed contract. +func NewLotteryCaller(address common.Address, caller bind.ContractCaller) (*LotteryCaller, error) { + contract, err := bindLottery(address, caller, nil, nil) + if err != nil { + return nil, err + } + return &LotteryCaller{contract: contract}, nil +} + +// NewLotteryTransactor creates a new write-only instance of Lottery, bound to a specific deployed contract. +func NewLotteryTransactor(address common.Address, transactor bind.ContractTransactor) (*LotteryTransactor, error) { + contract, err := bindLottery(address, nil, transactor, nil) + if err != nil { + return nil, err + } + return &LotteryTransactor{contract: contract}, nil +} + +// NewLotteryFilterer creates a new log filterer instance of Lottery, bound to a specific deployed contract. +func NewLotteryFilterer(address common.Address, filterer bind.ContractFilterer) (*LotteryFilterer, error) { + contract, err := bindLottery(address, nil, nil, filterer) + if err != nil { + return nil, err + } + return &LotteryFilterer{contract: contract}, nil +} + +// bindLottery binds a generic wrapper to an already deployed contract. +func bindLottery(address common.Address, caller bind.ContractCaller, transactor bind.ContractTransactor, filterer bind.ContractFilterer) (*bind.BoundContract, error) { + parsed, err := abi.JSON(strings.NewReader(LotteryABI)) + if err != nil { + return nil, err + } + return bind.NewBoundContract(address, parsed, caller, transactor, filterer), nil +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Lottery *LotteryRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _Lottery.Contract.LotteryCaller.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Lottery *LotteryRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Lottery.Contract.LotteryTransactor.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Lottery *LotteryRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Lottery.Contract.LotteryTransactor.contract.Transact(opts, method, params...) +} + +// Call invokes the (constant) contract method with params as input values and +// sets the output to result. The result type might be a single field for simple +// returns, a slice of interfaces for anonymous returns and a struct for named +// returns. +func (_Lottery *LotteryCallerRaw) Call(opts *bind.CallOpts, result interface{}, method string, params ...interface{}) error { + return _Lottery.Contract.contract.Call(opts, result, method, params...) +} + +// Transfer initiates a plain transaction to move funds to the contract, calling +// its default method if one is available. +func (_Lottery *LotteryTransactorRaw) Transfer(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Lottery.Contract.contract.Transfer(opts) +} + +// Transact invokes the (paid) contract method with params as input values. +func (_Lottery *LotteryTransactorRaw) Transact(opts *bind.TransactOpts, method string, params ...interface{}) (*types.Transaction, error) { + return _Lottery.Contract.contract.Transact(opts, method, params...) +} + +// GetPlayers is a free data retrieval call binding the contract method 0x8b5b9ccc. +// +// Solidity: function getPlayers() constant returns(address[]) +func (_Lottery *LotteryCaller) GetPlayers(opts *bind.CallOpts) ([]common.Address, error) { + var ( + ret0 = new([]common.Address) + ) + out := ret0 + err := _Lottery.contract.Call(opts, out, "getPlayers") + return *ret0, err +} + +// GetPlayers is a free data retrieval call binding the contract method 0x8b5b9ccc. +// +// Solidity: function getPlayers() constant returns(address[]) +func (_Lottery *LotterySession) GetPlayers() ([]common.Address, error) { + return _Lottery.Contract.GetPlayers(&_Lottery.CallOpts) +} + +// GetPlayers is a free data retrieval call binding the contract method 0x8b5b9ccc. +// +// Solidity: function getPlayers() constant returns(address[]) +func (_Lottery *LotteryCallerSession) GetPlayers() ([]common.Address, error) { + return _Lottery.Contract.GetPlayers(&_Lottery.CallOpts) +} + +// Manager is a free data retrieval call binding the contract method 0x481c6a75. +// +// Solidity: function manager() constant returns(address) +func (_Lottery *LotteryCaller) Manager(opts *bind.CallOpts) (common.Address, error) { + var ( + ret0 = new(common.Address) + ) + out := ret0 + err := _Lottery.contract.Call(opts, out, "manager") + return *ret0, err +} + +// Manager is a free data retrieval call binding the contract method 0x481c6a75. +// +// Solidity: function manager() constant returns(address) +func (_Lottery *LotterySession) Manager() (common.Address, error) { + return _Lottery.Contract.Manager(&_Lottery.CallOpts) +} + +// Manager is a free data retrieval call binding the contract method 0x481c6a75. +// +// Solidity: function manager() constant returns(address) +func (_Lottery *LotteryCallerSession) Manager() (common.Address, error) { + return _Lottery.Contract.Manager(&_Lottery.CallOpts) +} + +// Players is a free data retrieval call binding the contract method 0xf71d96cb. +// +// Solidity: function players( uint256) constant returns(address) +func (_Lottery *LotteryCaller) Players(opts *bind.CallOpts, arg0 *big.Int) (common.Address, error) { + var ( + ret0 = new(common.Address) + ) + out := ret0 + err := _Lottery.contract.Call(opts, out, "players", arg0) + return *ret0, err +} + +// Players is a free data retrieval call binding the contract method 0xf71d96cb. +// +// Solidity: function players( uint256) constant returns(address) +func (_Lottery *LotterySession) Players(arg0 *big.Int) (common.Address, error) { + return _Lottery.Contract.Players(&_Lottery.CallOpts, arg0) +} + +// Players is a free data retrieval call binding the contract method 0xf71d96cb. +// +// Solidity: function players( uint256) constant returns(address) +func (_Lottery *LotteryCallerSession) Players(arg0 *big.Int) (common.Address, error) { + return _Lottery.Contract.Players(&_Lottery.CallOpts, arg0) +} + +// Enter is a paid mutator transaction binding the contract method 0xe97dcb62. +// +// Solidity: function enter() returns() +func (_Lottery *LotteryTransactor) Enter(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Lottery.contract.Transact(opts, "enter") +} + +// Enter is a paid mutator transaction binding the contract method 0xe97dcb62. +// +// Solidity: function enter() returns() +func (_Lottery *LotterySession) Enter() (*types.Transaction, error) { + return _Lottery.Contract.Enter(&_Lottery.TransactOpts) +} + +// Enter is a paid mutator transaction binding the contract method 0xe97dcb62. +// +// Solidity: function enter() returns() +func (_Lottery *LotteryTransactorSession) Enter() (*types.Transaction, error) { + return _Lottery.Contract.Enter(&_Lottery.TransactOpts) +} + +// PickWinner is a paid mutator transaction binding the contract method 0x5d495aea. +// +// Solidity: function pickWinner() returns() +func (_Lottery *LotteryTransactor) PickWinner(opts *bind.TransactOpts) (*types.Transaction, error) { + return _Lottery.contract.Transact(opts, "pickWinner") +} + +// PickWinner is a paid mutator transaction binding the contract method 0x5d495aea. +// +// Solidity: function pickWinner() returns() +func (_Lottery *LotterySession) PickWinner() (*types.Transaction, error) { + return _Lottery.Contract.PickWinner(&_Lottery.TransactOpts) +} + +// PickWinner is a paid mutator transaction binding the contract method 0x5d495aea. +// +// Solidity: function pickWinner() returns() +func (_Lottery *LotteryTransactorSession) PickWinner() (*types.Transaction, error) { + return _Lottery.Contract.PickWinner(&_Lottery.TransactOpts) +} diff --git a/contracts/Lottery.sol b/contracts/Lottery.sol new file mode 100644 index 000000000..c95a46368 --- /dev/null +++ b/contracts/Lottery.sol @@ -0,0 +1,38 @@ +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"; + + address public manager; + address payable[] public players; + + constructor() public { + manager = msg.sender; + } + + function enter() public payable { + require(msg.value > .01 ether, ENTER_MESSAGE); + + players.push(msg.sender); + } + + function random() private view returns (uint) { + return uint(keccak256(abi.encodePacked(now, msg.sender, this))); + } + + function pickWinner() public payable restricted { + uint index = random() % players.length; + players[index].transfer(address(this).balance); + players = new address payable[](0); + } + + modifier restricted() { + require(msg.sender == manager, RESTRICTED_MESSAGE); + _; + } + + function getPlayers() public view returns (address payable[] memory) { + return players; + } +}