From ce29e1ad09a3c2d9021217ad56b80b89eec9a0df Mon Sep 17 00:00:00 2001 From: Rongjian Lan Date: Thu, 2 May 2019 00:01:58 -0700 Subject: [PATCH] Add txid return in rest api for puzzle game --- .../restclientsupport/puzzle_service.go | 13 ++++-- api/service/restclientsupport/service.go | 13 +++--- node/puzzle_contract.go | 44 +++++++++---------- 3 files changed, 38 insertions(+), 32 deletions(-) diff --git a/api/service/restclientsupport/puzzle_service.go b/api/service/restclientsupport/puzzle_service.go index 31190d085..8876c1339 100644 --- a/api/service/restclientsupport/puzzle_service.go +++ b/api/service/restclientsupport/puzzle_service.go @@ -32,13 +32,14 @@ func (s *Service) Play(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(res) return } - - if err := s.CreateTransactionForPlayMethod(key, amountInt); err != nil { + txID, err := s.CreateTransactionForPlayMethod(key, amountInt) + if err != nil { utils.GetLogInstance().Error("puzzle-play, error", err) json.NewEncoder(w).Encode(res) return } res.Success = true + res.TxID = txID json.NewEncoder(w).Encode(res) } @@ -59,13 +60,15 @@ func (s *Service) Payout(w http.ResponseWriter, r *http.Request) { json.NewEncoder(w).Encode(res) return } + txID, err := s.CreateTransactionForPayoutMethod(common.HexToAddress(address), newLevelInt, sequence) - if err = s.CreateTransactionForPayoutMethod(common.HexToAddress(address), newLevelInt, sequence); err != nil { + if err != nil { utils.GetLogInstance().Error("Payout error", err) json.NewEncoder(w).Encode(res) return } res.Success = true + res.TxID = txID json.NewEncoder(w).Encode(res) } @@ -84,11 +87,13 @@ func (s *Service) End(w http.ResponseWriter, r *http.Request) { return } - if err := s.CreateTransactionForEndMethod(common.HexToAddress(address)); err != nil { + txID, err := s.CreateTransactionForEndMethod(common.HexToAddress(address)) + if err != nil { utils.GetLogInstance().Error("Payout error", err) json.NewEncoder(w).Encode(res) return } res.Success = true + res.TxID = txID json.NewEncoder(w).Encode(res) } diff --git a/api/service/restclientsupport/service.go b/api/service/restclientsupport/service.go index 1da77d868..aa81d8af7 100644 --- a/api/service/restclientsupport/service.go +++ b/api/service/restclientsupport/service.go @@ -35,9 +35,9 @@ type Service struct { messageChan chan *msg_pb.Message CallFaucetContract func(common.Address) common.Hash GetAccountBalance func(common.Address) (*big.Int, error) - CreateTransactionForPlayMethod func(string, int64) error - CreateTransactionForPayoutMethod func(common.Address, int, string) error - CreateTransactionForEndMethod func(common.Address) error + CreateTransactionForPlayMethod func(string, int64) (string, error) + CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error) + CreateTransactionForEndMethod func(common.Address) (string, error) } // New returns new client support service. @@ -46,9 +46,9 @@ 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, int64) error, - CreateTransactionForPayoutMethod func(common.Address, int, string) error, - CreateTransactionForEndMethod func(common.Address) error) *Service { + CreateTransactionForPlayMethod func(string, int64) (string, error), + CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error), + CreateTransactionForEndMethod func(common.Address) (string, error)) *Service { return &Service{ CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, GetResult: GetResult, @@ -124,6 +124,7 @@ func (s *Service) Run() *http.Server { type Response struct { Players []string `json:"players"` Balances []string `json:"balances"` + TxID string `json:"txid"` Success bool `json:"success"` } diff --git a/node/puzzle_contract.go b/node/puzzle_contract.go index e498efe18..c66e6f63a 100644 --- a/node/puzzle_contract.go +++ b/node/puzzle_contract.go @@ -51,18 +51,18 @@ func (node *Node) AddPuzzleContract() { } // CreateTransactionForPlayMethod generates transaction for play method and add it into pending tx list. -func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) error { +func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) (string, error) { var err error toAddress := node.PuzzleContractAddress 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) - return err + return "", err } bytesData, err := abi.Pack(Play) if err != nil { utils.GetLogInstance().Error("puzzle-play: Failed to generate ABI function bytes data", "error", err) - return err + return "", err } Stake := big.NewInt(0) @@ -73,10 +73,10 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) er balance, err := node.GetBalanceOfAddress(address) if err != nil { utils.GetLogInstance().Error("puzzle-play: can not get address", "error", err) - return err + return "", err } else if balance.Cmp(Stake) == -1 { utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance) - return ErrPuzzleInsufficientFund + return "", ErrPuzzleInsufficientFund } nonce := node.GetNonceOfAddress(address) tx := types.NewTransaction( @@ -91,37 +91,37 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) er if err != nil { utils.GetLogInstance().Error("puzzle-play: Failed to get private key", "error", err) - return err + return "", err } if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil { node.addPendingTransactions(types.Transactions{signedTx}) - return nil + return signedTx.Hash().Hex(), nil } utils.GetLogInstance().Error("puzzle-play: Unable to call enter method", "error", err) - return err + return "", err } // CreateTransactionForPayoutMethod generates transaction for payout method and add it into pending tx list. -func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level int, sequence string) error { +func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level int, sequence string) (string, error) { var err error toAddress := node.PuzzleContractAddress abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI)) if err != nil { 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, 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 + return "", err } key := node.PuzzleManagerPrivateKey if key == nil { - return fmt.Errorf("PuzzleManagerPrivateKey is nil") + return "", fmt.Errorf("PuzzleManagerPrivateKey is nil") } nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) Amount := big.NewInt(0) @@ -137,37 +137,37 @@ func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level if err != nil { utils.GetLogInstance().Error("Failed to get private key", "error", err) - return err + return "", err } if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil { node.addPendingTransactions(types.Transactions{signedTx}) - return nil + return signedTx.Hash().Hex(), nil } utils.GetLogInstance().Error("Unable to call enter method", "error", err) - return err + return "", err } // CreateTransactionForEndMethod generates transaction for endGame method and add it into pending tx list. -func (node *Node) CreateTransactionForEndMethod(address common.Address) error { +func (node *Node) CreateTransactionForEndMethod(address common.Address) (string, error) { var err error toAddress := node.PuzzleContractAddress abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI)) if err != nil { 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, steps string fmt.Println("EndGame: address", address) bytesData, err := abi.Pack(EndGame, address) if err != nil { utils.GetLogInstance().Error("Failed to generate ABI function bytes data", "error", err) - return err + return "", err } key := node.PuzzleManagerPrivateKey if key == nil { - return fmt.Errorf("PuzzleManagerPrivateKey is nil") + return "", fmt.Errorf("PuzzleManagerPrivateKey is nil") } nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) Amount := big.NewInt(0) @@ -183,12 +183,12 @@ func (node *Node) CreateTransactionForEndMethod(address common.Address) error { if err != nil { utils.GetLogInstance().Error("Failed to get private key", "error", err) - return err + return "", err } if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil { node.addPendingTransactions(types.Transactions{signedTx}) - return nil + return signedTx.Hash().Hex(), nil } utils.GetLogInstance().Error("Unable to call enter method", "error", err) - return err + return "", err }