Merge pull request #806 from harmony-one/rj_cello

Rest api for play and payout
pull/813/head
Rongjian Lan 6 years ago committed by GitHub
commit 5db7072189
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 70
      api/service/restclientsupport/puzzle_service.go
  2. 69
      api/service/restclientsupport/service.go
  3. 28
      contracts/Puzzle.go
  4. 4
      contracts/Puzzle.sol
  5. 21
      node/node_genesis.go
  6. 33
      node/puzzle_contract.go

@ -0,0 +1,70 @@
package restclientsupport
import (
"encoding/json"
"fmt"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/harmony/internal/utils"
"net/http"
"strconv"
)
// Play triggers play method of puzzle smart contract.
func (s *Service) Play(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
key := r.FormValue("key")
amount := r.FormValue("amount")
amountInt, err := strconv.ParseInt(amount, 10, 64)
fmt.Println("Play: key", key, "amount", amountInt)
res := &Response{
Success: false,
}
if err != nil {
json.NewEncoder(w).Encode(res)
return
}
if s.CreateTransactionForPlayMethod == nil {
fmt.Println("puzzle-play no method", key)
json.NewEncoder(w).Encode(res)
return
}
if err := s.CreateTransactionForPlayMethod(key, amountInt); err != nil {
utils.GetLogInstance().Error("puzzle-play, error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
json.NewEncoder(w).Encode(res)
}
// Payout triggers play payout of puzzle smart contract.
func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
address := r.FormValue("address")
newLevel := r.FormValue("new_level")
sequence := r.FormValue("sequence")
newLevelInt, err := strconv.Atoi(newLevel)
fmt.Println("Payout: address", address, "new_level", newLevelInt)
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
if err = s.CreateTransactionForPayoutMethod(common.HexToAddress(address), newLevelInt, sequence); err != nil {
utils.GetLogInstance().Error("Payout error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
json.NewEncoder(w).Encode(res)
}

@ -35,8 +35,8 @@ type Service struct {
messageChan chan *msg_pb.Message
CallFaucetContract func(common.Address) common.Hash
GetAccountBalance func(common.Address) (*big.Int, error)
CreateTransactionForPlayMethod func(string, string) error
CreateTransactionForPayoutMethod func(string, int, string) error
CreateTransactionForPlayMethod func(string, int64) error
CreateTransactionForPayoutMethod func(common.Address, int, string) error
}
// New returns new client support service.
@ -45,8 +45,8 @@ func New(
GetResult func(string) ([]string, []*big.Int),
CreateTransactionForPickWinner func() error,
CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error),
CreateTransactionForPlayMethod func(string, string) error,
CreateTransactionForPayoutMethod func(string, int, string) error) *Service {
CreateTransactionForPlayMethod func(string, int64) error,
CreateTransactionForPayoutMethod func(common.Address, int, string) error) *Service {
return &Service{
CreateTransactionForEnterMethod: CreateTransactionForEnterMethod,
GetResult: GetResult,
@ -100,11 +100,11 @@ func (s *Service) Run() *http.Server {
s.router.Path("/winner").HandlerFunc(s.Winner)
// Routing for puzzle app.
// Set up router for play.
s.router.Path("/play").Queries("address", "{[0-9A-Fa-fx]*?}", "amount", "{[0-9]*?}").HandlerFunc(s.Play).Methods("GET")
s.router.Path("/play").HandlerFunc(s.Play)
// Set up router for payout.
s.router.Path("/payout").Queries("key", "{[0-9A-Fa-fx]*?}", "level", "{[0-9]*?}", "sequence", "{[A-Za-z]*?}").HandlerFunc(s.Payout).Methods("GET")
s.router.Path("/payout").Queries("address", "{[0-9A-Fa-fx]*?}", "level", "{[0-9]*?}", "sequence", "{[A-Za-z]*?}").HandlerFunc(s.Payout).Methods("GET")
s.router.Path("/payout").HandlerFunc(s.Payout)
// Do serving now.
utils.GetLogInstance().Info("Listening on ", "port: ", Port)
@ -124,7 +124,7 @@ type Response struct {
func (s *Service) GetBalance(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
addressHex := r.FormValue("key")
fmt.Println("fundMe: address", addressHex)
fmt.Println("GetBalance: address", addressHex)
res := &Response{Success: false}
if s.GetAccountBalance == nil {
@ -239,61 +239,6 @@ func (s *Service) Winner(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(res)
}
// Play triggers play method of puzzle smart contract.
func (s *Service) Play(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
key := r.FormValue("key")
amount := r.FormValue("amount")
fmt.Println("puzzle-play", key)
res := &Response{
Success: false,
}
if s.CreateTransactionForPlayMethod == nil {
fmt.Println("puzzle-play no method", key)
json.NewEncoder(w).Encode(res)
return
}
if err := s.CreateTransactionForPlayMethod(key, amount); err != nil {
utils.GetLogInstance().Error("puzzle-play, error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
json.NewEncoder(w).Encode(res)
}
// Payout triggers play payout of puzzle smart contract.
func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
key := r.FormValue("key")
level := r.FormValue("level")
sequence := r.FormValue("sequence")
fmt.Println("payout: key", key, "level", level, "sequence", sequence)
levelInt, err := strconv.Atoi(level)
if err != nil {
utils.GetLogInstance().Error("error", err)
}
fmt.Println("play")
res := &Response{
Success: false,
}
if s.CreateTransactionForPayoutMethod == nil {
json.NewEncoder(w).Encode(res)
return
}
if err = s.CreateTransactionForPayoutMethod(key, levelInt, sequence); err != nil {
utils.GetLogInstance().Error("error", err)
json.NewEncoder(w).Encode(res)
return
}
res.Success = true
json.NewEncoder(w).Encode(res)
}
// NotifyService notify service
func (s *Service) NotifyService(params map[string]interface{}) {
return

@ -28,10 +28,10 @@ var (
)
// PuzzleABI is the input ABI used to generate the binding from.
const PuzzleABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"endGame\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"string\"}],\"name\":\"payout\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"}],\"name\":\"setLevel\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"play\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"resetPlayer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"reset\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"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\"}]"
const PuzzleABI = "[{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"endGame\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"manager\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"},{\"name\":\"\",\"type\":\"string\"}],\"name\":\"payout\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"},{\"name\":\"level\",\"type\":\"uint256\"}],\"name\":\"setLevel\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[],\"name\":\"getPlayers\",\"outputs\":[{\"name\":\"\",\"type\":\"address[]\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"play\",\"outputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[{\"name\":\"player\",\"type\":\"address\"}],\"name\":\"resetPlayer\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":false,\"inputs\":[],\"name\":\"reset\",\"outputs\":[],\"payable\":false,\"stateMutability\":\"nonpayable\",\"type\":\"function\"},{\"constant\":true,\"inputs\":[{\"name\":\"\",\"type\":\"uint256\"}],\"name\":\"players\",\"outputs\":[{\"name\":\"\",\"type\":\"address\"}],\"payable\":false,\"stateMutability\":\"view\",\"type\":\"function\"},{\"inputs\":[],\"payable\":true,\"stateMutability\":\"payable\",\"type\":\"constructor\"}]"
// PuzzleBin is the compiled bytecode used for deploying new contracts.
const PuzzleBin = `0x608060405234801561001057600080fd5b50600480546001600160a01b0319163317905561096d806100326000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101e557806393e84cd91461024a578063c95e090914610252578063d826f88f14610285578063f71d96cb1461029a57610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101ac575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102c4565b005b3480156100cc57600080fd5b506100d56103ad565b604080516001600160a01b039092168252519081900360200190f35b6100be6004803603606081101561010757600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561013757600080fd5b82018360208201111561014957600080fd5b8035906020019184600183028401116401000000008311171561016b57600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506103bc945050505050565b3480156101b857600080fd5b506100be600480360360408110156101cf57600080fd5b506001600160a01b038135169060200135610567565b3480156101f157600080fd5b506101fa610583565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561023657818101518382015260200161021e565b505050509050019250505060405180910390f35b6100be6105e6565b34801561025e57600080fd5b506100be6004803603602081101561027557600080fd5b50356001600160a01b031661071d565b34801561029157600080fd5b506100be6107f0565b3480156102a657600080fd5b506100d5600480360360208110156102bd57600080fd5b50356108d3565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461038b57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b83811015610350578181015183820152602001610338565b50505050905090810190601f16801561037d5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461044657604051600160e51b62461bcd028152602060048201818152835160248401528351909283926044909101919085019080838360008315610350578181015183820152602001610338565b506001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146104ee57604051600160e51b62461bcd028152602060048201818152835160248401528351909283926044909101919085019080838360008315610350578181015183820152602001610338565b506001600160a01b03831660009081526020819052604090205482036105148484610567565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f1935050505015801561055f573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156105db57602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116105bd575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561067557604051600160e51b62461bcd028152602060048201818152835160248401528351909283926044909101919085019080838360008315610350578181015183820152602001610338565b503360009081526003602052604090205460ff166106ea57336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b031633146107a757604051600160e51b62461bcd028152602060048201818152835160248401528351909283926044909101919085019080838360008315610350578181015183820152602001610338565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461087a57604051600160e51b62461bcd028152602060048201818152835160248401528351909283926044909101919085019080838360008315610350578181015183820152602001610338565b5060055460005b818110156108c15760006005828154811061089857fe5b6000918252602090912001546001600160a01b031690506108b88161071d565b50600101610881565b5060006108cf6005826108fa565b5050565b600581815481106108e057fe5b6000918252602090912001546001600160a01b0316905081565b81548183558181111561091e5760008381526020902061091e918101908301610923565b505050565b6105e391905b8082111561093d5760008155600101610929565b509056fea165627a7a7230582062a9296ff3394d1dc60def0222978a34a1ef4444a5fcee620e83cc345fd292660029`
const PuzzleBin = `0x6080604052600480546001600160a01b03191633179055610984806100256000396000f3fe6080604052600436106100865760003560e01c80638b5b9ccc116100595780638b5b9ccc146101f257806393e84cd914610257578063c95e09091461025f578063d826f88f14610292578063f71d96cb146102a757610086565b80632a035b6c1461008b578063481c6a75146100c057806352bcd7c8146100f1578063722dcd8f146101b9575b600080fd5b34801561009757600080fd5b506100be600480360360208110156100ae57600080fd5b50356001600160a01b03166102d1565b005b3480156100cc57600080fd5b506100d56103ba565b604080516001600160a01b039092168252519081900360200190f35b3480156100fd57600080fd5b506100be6004803603606081101561011457600080fd5b6001600160a01b038235169160208101359181019060608101604082013564010000000081111561014457600080fd5b82018360208201111561015657600080fd5b8035906020019184600183028401116401000000008311171561017857600080fd5b91908080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509295506103c9945050505050565b3480156101c557600080fd5b506100be600480360360408110156101dc57600080fd5b506001600160a01b038135169060200135610576565b3480156101fe57600080fd5b50610207610592565b60408051602080825283518183015283519192839290830191858101910280838360005b8381101561024357818101518382015260200161022b565b505050509050019250505060405180910390f35b6100be6105f5565b34801561026b57600080fd5b506100be6004803603602081101561028257600080fd5b50356001600160a01b031661072f565b34801561029e57600080fd5b506100be610803565b3480156102b357600080fd5b506100d5600480360360208110156102ca57600080fd5b50356108e9565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461039857604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360005b8381101561035d578181015183820152602001610345565b50505050905090810190601f16801561038a5780820380516001836020036101000a031916815260200191505b509250505060405180910390fd5b506001600160a01b03166000908152600260205260409020805460ff19169055565b6004546001600160a01b031681565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461045457604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b038316600090815260026020908152604091829020548251808401909352601f83527f506c61796572206973206e6f7420696e20616e206163746976652067616d65009183019190915260ff1615156001146104fd57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b03831660009081526020819052604090205482036105238484610576565b6001600160a01b03841660008181526001602052604080822054905160059091048402929183156108fc02918491818181858888f1935050505015801561056e573d6000803e3d6000fd5b505050505050565b6001600160a01b03909116600090815260208190526040902055565b606060058054806020026020016040519081016040528092919081815260200182805480156105ea57602002820191906000526020600020905b81546001600160a01b031681526001909101906020018083116105cc575b505050505090505b90565b60408051808201909152601181527f496e73756666696369656e742046756e6400000000000000000000000000000060208201526801158e460913d0000034101561068557604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b503360009081526003602052604090205460ff1615156106fc57336000818152600360205260408120805460ff191660019081179091556005805491820181559091527f036b6384b5eca791c62761152d0c79bb0604c104a5fb6f4eb0703f3154bb3db00180546001600160a01b03191690911790555b3360009081526020818152604080832083905560018083528184203490556002909252909120805460ff19169091179055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b031633146107ba57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b506001600160a01b031660009081526020818152604080832083905560028252808320805460ff1990811690915560018352818420849055600390925290912080549091169055565b6004546040805180820190915260138152600160681b72556e617574686f72697a656420416363657373026020820152906001600160a01b0316331461088e57604051600160e51b62461bcd0281526004018080602001828103825283818151815260200191508051906020019080838360008381101561035d578181015183820152602001610345565b5060055460005b818110156108d75760006005828154811015156108ae57fe5b6000918252602090912001546001600160a01b031690506108ce8161072f565b50600101610895565b5060006108e5600582610911565b5050565b60058054829081106108f757fe5b6000918252602090912001546001600160a01b0316905081565b8154818355818111156109355760008381526020902061093591810190830161093a565b505050565b6105f291905b808211156109545760008155600101610940565b509056fea165627a7a723058204706efee8388d70aabafd388a42b5b54a6268e521a1d1401aec9adf14b0e6f980029`
// DeployPuzzle deploys a new Ethereum contract, binding an instance of Puzzle to it.
func DeployPuzzle(auth *bind.TransactOpts, backend bind.ContractBackend) (common.Address, *types.Transaction, *Puzzle, error) {
@ -268,42 +268,42 @@ func (_Puzzle *PuzzleCallerSession) Players(arg0 *big.Int) (common.Address, erro
// EndGame is a paid mutator transaction binding the contract method 0x2a035b6c.
//
// Solidity: function endGame(player address) returns()
// Solidity: function endGame(address player) returns()
func (_Puzzle *PuzzleTransactor) EndGame(opts *bind.TransactOpts, player common.Address) (*types.Transaction, error) {
return _Puzzle.contract.Transact(opts, "endGame", player)
}
// EndGame is a paid mutator transaction binding the contract method 0x2a035b6c.
//
// Solidity: function endGame(player address) returns()
// Solidity: function endGame(address player) returns()
func (_Puzzle *PuzzleSession) EndGame(player common.Address) (*types.Transaction, error) {
return _Puzzle.Contract.EndGame(&_Puzzle.TransactOpts, player)
}
// EndGame is a paid mutator transaction binding the contract method 0x2a035b6c.
//
// Solidity: function endGame(player address) returns()
// Solidity: function endGame(address player) returns()
func (_Puzzle *PuzzleTransactorSession) EndGame(player common.Address) (*types.Transaction, error) {
return _Puzzle.Contract.EndGame(&_Puzzle.TransactOpts, player)
}
// Payout is a paid mutator transaction binding the contract method 0x52bcd7c8.
//
// Solidity: function payout(player address, level uint256, string) returns()
// Solidity: function payout(address player, uint256 level, string ) returns()
func (_Puzzle *PuzzleTransactor) Payout(opts *bind.TransactOpts, player common.Address, level *big.Int, arg2 string) (*types.Transaction, error) {
return _Puzzle.contract.Transact(opts, "payout", player, level, arg2)
}
// Payout is a paid mutator transaction binding the contract method 0x52bcd7c8.
//
// Solidity: function payout(player address, level uint256, string) returns()
// Solidity: function payout(address player, uint256 level, string ) returns()
func (_Puzzle *PuzzleSession) Payout(player common.Address, level *big.Int, arg2 string) (*types.Transaction, error) {
return _Puzzle.Contract.Payout(&_Puzzle.TransactOpts, player, level, arg2)
}
// Payout is a paid mutator transaction binding the contract method 0x52bcd7c8.
//
// Solidity: function payout(player address, level uint256, string) returns()
// Solidity: function payout(address player, uint256 level, string ) returns()
func (_Puzzle *PuzzleTransactorSession) Payout(player common.Address, level *big.Int, arg2 string) (*types.Transaction, error) {
return _Puzzle.Contract.Payout(&_Puzzle.TransactOpts, player, level, arg2)
}
@ -352,42 +352,42 @@ func (_Puzzle *PuzzleTransactorSession) Reset() (*types.Transaction, error) {
// ResetPlayer is a paid mutator transaction binding the contract method 0xc95e0909.
//
// Solidity: function resetPlayer(player address) returns()
// Solidity: function resetPlayer(address player) returns()
func (_Puzzle *PuzzleTransactor) ResetPlayer(opts *bind.TransactOpts, player common.Address) (*types.Transaction, error) {
return _Puzzle.contract.Transact(opts, "resetPlayer", player)
}
// ResetPlayer is a paid mutator transaction binding the contract method 0xc95e0909.
//
// Solidity: function resetPlayer(player address) returns()
// Solidity: function resetPlayer(address player) returns()
func (_Puzzle *PuzzleSession) ResetPlayer(player common.Address) (*types.Transaction, error) {
return _Puzzle.Contract.ResetPlayer(&_Puzzle.TransactOpts, player)
}
// ResetPlayer is a paid mutator transaction binding the contract method 0xc95e0909.
//
// Solidity: function resetPlayer(player address) returns()
// Solidity: function resetPlayer(address player) returns()
func (_Puzzle *PuzzleTransactorSession) ResetPlayer(player common.Address) (*types.Transaction, error) {
return _Puzzle.Contract.ResetPlayer(&_Puzzle.TransactOpts, player)
}
// SetLevel is a paid mutator transaction binding the contract method 0x722dcd8f.
//
// Solidity: function setLevel(player address, level uint256) returns()
// Solidity: function setLevel(address player, uint256 level) returns()
func (_Puzzle *PuzzleTransactor) SetLevel(opts *bind.TransactOpts, player common.Address, level *big.Int) (*types.Transaction, error) {
return _Puzzle.contract.Transact(opts, "setLevel", player, level)
}
// SetLevel is a paid mutator transaction binding the contract method 0x722dcd8f.
//
// Solidity: function setLevel(player address, level uint256) returns()
// Solidity: function setLevel(address player, uint256 level) returns()
func (_Puzzle *PuzzleSession) SetLevel(player common.Address, level *big.Int) (*types.Transaction, error) {
return _Puzzle.Contract.SetLevel(&_Puzzle.TransactOpts, player, level)
}
// SetLevel is a paid mutator transaction binding the contract method 0x722dcd8f.
//
// Solidity: function setLevel(player address, level uint256) returns()
// Solidity: function setLevel(address player, uint256 level) returns()
func (_Puzzle *PuzzleTransactorSession) SetLevel(player common.Address, level *big.Int) (*types.Transaction, error) {
return _Puzzle.Contract.SetLevel(&_Puzzle.TransactOpts, player, level)
}

@ -15,7 +15,7 @@ contract Puzzle {
address public manager; // The adress of the owner of this contract
address payable[] public players; // all players
constructor() public {
constructor() public payable {
manager = msg.sender;
}
@ -46,7 +46,7 @@ contract Puzzle {
/**
* @dev pay the player if they have crossed their last best level.
*/
function payout(address payable player, uint level, string memory /*sequence*/) public payable restricted {
function payout(address payable player, uint level, string memory /*sequence*/) public restricted {
require(playerInGame[player] == true, NOT_IN_GAME);
uint progress = level - playerLevel[player]; //if a later transaction for a higher level comes earlier.

@ -20,9 +20,9 @@ const (
// FakeAddressNumber is the number of fake address.
FakeAddressNumber = 100
// TotalInitFund is the initial total fund for the contract deployer.
TotalInitFund = 9000000
TotalInitFund = 1000000100
// InitFreeFundInEther is the initial fund for sample accounts.
InitFreeFundInEther = 1000
InitFreeFundInEther = 100
)
// GenesisBlockSetup setups a genesis blockchain.
@ -39,6 +39,11 @@ func (node *Node) GenesisBlockSetup(db ethdb.Database, shardID uint32, isArchiva
genesisAlloc[contractDeployerAddress] = core.GenesisAccount{Balance: contractDeployerFunds}
node.ContractDeployerKey = contractDeployerKey
// Add puzzle fund
puzzleFunds := big.NewInt(TotalInitFund)
puzzleFunds = puzzleFunds.Mul(puzzleFunds, big.NewInt(params.Ether))
genesisAlloc[common.HexToAddress(contract.PuzzleAccounts[0].Address)] = core.GenesisAccount{Balance: puzzleFunds}
if shardID == 0 {
// Accounts used by validator/nodes to stake and participate in the network.
AddNodeAddressesToGenesisAlloc(genesisAlloc)
@ -94,12 +99,12 @@ func AddNodeAddressesToGenesisAlloc(genesisAlloc core.GenesisAlloc) {
address := common.HexToAddress(account.Address)
genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds}
}
for _, account := range contract.NewNodeAccounts {
testBankFunds := big.NewInt(InitFreeFundInEther)
testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether))
address := common.HexToAddress(account.Address)
genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds}
}
//for _, account := range contract.NewNodeAccounts {
// testBankFunds := big.NewInt(InitFreeFundInEther)
// testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether))
// address := common.HexToAddress(account.Address)
// genesisAlloc[address] = core.GenesisAccount{Balance: testBankFunds}
//}
for _, account := range contract.DemoAccounts {
testBankFunds := big.NewInt(InitFreeFundInEther)
testBankFunds = testBankFunds.Mul(testBankFunds, big.NewInt(params.Ether))

@ -22,6 +22,8 @@ const (
Payout = "payout"
)
var OneEther = big.NewInt(params.Ether)
// AddPuzzleContract adds the demo puzzle contract the genesis block.
func (node *Node) AddPuzzleContract() {
// Add a puzzle demo contract.
@ -39,7 +41,7 @@ func (node *Node) AddPuzzleContract() {
contractFunds := big.NewInt(PuzzleFund)
contractFunds = contractFunds.Mul(contractFunds, big.NewInt(params.Ether))
demoContract, _ := types.SignTx(
types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*10, nil, dataEnc),
types.NewContractCreation(uint64(0), node.Consensus.ShardID, contractFunds, params.TxGasContractCreation*1000, nil, dataEnc),
types.HomesteadSigner{},
priKey)
node.PuzzleContractAddress = crypto.CreateAddress(crypto.PubkeyToAddress(priKey.PublicKey), uint64(0))
@ -47,12 +49,10 @@ func (node *Node) AddPuzzleContract() {
node.addPendingTransactions(types.Transactions{demoContract})
}
// CreateTransactionForPlayMethod generates transaction for enter method and add it into pending tx list.
func (node *Node) CreateTransactionForPlayMethod(priKey string, amount string) error {
// CreateTransactionForPlayMethod generates transaction for play method and add it into pending tx list.
func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) error {
var err error
toAddress := node.PuzzleContractAddress
GameStake := new(big.Int)
GameStake, _ = GameStake.SetString(amount, 10)
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to generate staking contract's ABI", "error", err)
@ -64,23 +64,26 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount string) e
return err
}
Stake := big.NewInt(0)
Stake = Stake.Mul(OneEther, big.NewInt(amount))
key, err := crypto.HexToECDSA(priKey)
address := crypto.PubkeyToAddress(key.PublicKey)
balance, err := node.GetBalanceOfAddress(address)
if err != nil {
utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err)
return err
} else if balance.Cmp(GameStake) == -1 {
utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err)
} else if balance.Cmp(Stake) == -1 {
utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance)
return ErrPuzzleInsufficientFund
}
nonce := node.GetNonceOfAddress(address)
tx := types.NewTransaction(
nonce,
toAddress,
0,
GameStake,
params.TxGas*10,
node.NodeConfig.ShardID,
Stake,
params.TxGas*100,
nil,
bytesData,
)
@ -98,7 +101,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount string) e
}
// CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list.
func (node *Node) CreateTransactionForPayoutMethod(address string, level int, sequence string) error {
func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level int, sequence string) error {
var err error
toAddress := node.PuzzleContractAddress
@ -107,7 +110,9 @@ func (node *Node) CreateTransactionForPayoutMethod(address string, level int, se
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return err
}
bytesData, err := abi.Pack("payout", address, level, sequence)
// add params for address payable player, uint8 new_level, steps string
fmt.Println("Payout: address", address)
bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(level)), sequence)
if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return err
@ -122,9 +127,9 @@ func (node *Node) CreateTransactionForPayoutMethod(address string, level int, se
tx := types.NewTransaction(
nonce,
toAddress,
0,
node.NodeConfig.ShardID,
Amount,
params.TxGas*10000,
params.TxGas*10,
nil,
bytesData,
)

Loading…
Cancel
Save