puzzle contract first version

pull/803/head
ak 6 years ago
parent 444a96fa8b
commit 4aac33cb28
  1. 14
      api/service/restclientsupport/service.go
  2. 14
      node/puzzle_contract.go

@ -36,7 +36,7 @@ type Service struct {
CallFaucetContract func(common.Address) common.Hash CallFaucetContract func(common.Address) common.Hash
GetAccountBalance func(common.Address) (*big.Int, error) GetAccountBalance func(common.Address) (*big.Int, error)
CreateTransactionForPlayMethod func(string) error CreateTransactionForPlayMethod func(string) error
CreateTransactionForPayoutMethod func(string, int) error CreateTransactionForPayoutMethod func(string, int, int, string) error
} }
// New returns new client support service. // New returns new client support service.
@ -46,7 +46,7 @@ func New(
CreateTransactionForPickWinner func() error, CreateTransactionForPickWinner func() error,
CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error), CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error),
CreateTransactionForPlayMethod func(string) error, CreateTransactionForPlayMethod func(string) error,
CreateTransactionForPayoutMethod func(string, int) error) *Service { CreateTransactionForPayoutMethod func(string, int, int, string) error) *Service {
return &Service{ return &Service{
CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, CreateTransactionForEnterMethod: CreateTransactionForEnterMethod,
GetResult: GetResult, GetResult: GetResult,
@ -104,7 +104,7 @@ func (s *Service) Run() *http.Server {
s.router.Path("/play").HandlerFunc(s.Play) s.router.Path("/play").HandlerFunc(s.Play)
// Set up router for payout. // Set up router for payout.
s.router.Path("/payout").Queries("key", "{[0-9A-Fa-fx]*?}", "new_level", "{[0-9]*?}").HandlerFunc(s.Payout).Methods("GET") s.router.Path("/payout").Queries("key", "{[0-9A-Fa-fx]*?}", "new_level", "{[0-9]*?}", "txid", "{[0-9]*?}", "sequence", "{[A-Za-z]*?}").HandlerFunc(s.Payout).Methods("GET")
s.router.Path("/payout").HandlerFunc(s.Payout) s.router.Path("/payout").HandlerFunc(s.Payout)
// Do serving now. // Do serving now.
utils.GetLogInstance().Info("Listening on ", "port: ", Port) utils.GetLogInstance().Info("Listening on ", "port: ", Port)
@ -268,8 +268,10 @@ func (s *Service) Play(w http.ResponseWriter, r *http.Request) {
func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") w.Header().Set("Content-Type", "application/json")
key := r.FormValue("key") key := r.FormValue("key")
newLevel := r.FormValue("new_level") newLevel := r.FormValue("level")
fmt.Println("payout: key", key, "new_level", newLevel) sessionID = r.FormValue("txid")
solutionSequence = r.FormValue("sequence")
fmt.Println("payout: key", key, "level", newLevel, "session", sessionID, "solution sequence", solutionSequence)
newLevelInt, err := strconv.Atoi(newLevel) newLevelInt, err := strconv.Atoi(newLevel)
fmt.Println("play") fmt.Println("play")
@ -282,7 +284,7 @@ func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
return return
} }
if err = s.CreateTransactionForPayoutMethod(key, newLevelInt); err != nil { if err = s.CreateTransactionForPayoutMethod(key, newLevelInt, sessionID, solutionSequence); err != nil {
utils.GetLogInstance().Error("error", err) utils.GetLogInstance().Error("error", err)
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return

@ -22,8 +22,8 @@ const (
Payout = "payout" Payout = "payout"
) )
// OneEther represents one ether. // GameStake represents one ether.
var OneEther = big.NewInt(params.Ether) var GameStake = 2 * big.NewInt(params.Ether)
// AddPuzzleContract adds the demo puzzle contract the genesis block. // AddPuzzleContract adds the demo puzzle contract the genesis block.
func (node *Node) AddPuzzleContract() { func (node *Node) AddPuzzleContract() {
@ -72,7 +72,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string) error {
if err != nil { if err != nil {
utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err) utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err)
return err return err
} else if balance.Cmp(OneEther) == -1 { } else if balance.Cmp(GameStake) == -1 {
utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err) utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err)
return ErrPuzzleInsufficientFund return ErrPuzzleInsufficientFund
} }
@ -81,7 +81,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string) error {
nonce, nonce,
toAddress, toAddress,
0, 0,
OneEther, GameStake,
params.TxGas*10, params.TxGas*10,
nil, nil,
bytesData, bytesData,
@ -100,7 +100,7 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string) error {
} }
// CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list. // CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list.
func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int) error { func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int, session int, sequence string) error {
var err error var err error
toAddress := node.PuzzleContractAddress toAddress := node.PuzzleContractAddress
@ -109,9 +109,7 @@ func (node *Node) CreateTransactionForPayoutMethod(address string, newLevel int)
utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err) utils.GetLogInstance().Error("Failed to generate staking contract's ABI", "error", err)
return err return err
} }
// add params for address payable player, uint8 new_level bytesData, err := abi.Pack("payout", address, newLevel, session, sequence)
// TODO(minh, rj)
bytesData, err := abi.Pack(Payout)
if err != nil { if err != nil {
utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err) utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err)
return err return err

Loading…
Cancel
Save