Add txid return in rest api for puzzle game

pull/808/head
Rongjian Lan 6 years ago
parent 957845db40
commit ce29e1ad09
  1. 13
      api/service/restclientsupport/puzzle_service.go
  2. 13
      api/service/restclientsupport/service.go
  3. 44
      node/puzzle_contract.go

@ -32,13 +32,14 @@ func (s *Service) Play(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return
} }
txID, err := s.CreateTransactionForPlayMethod(key, amountInt)
if err := s.CreateTransactionForPlayMethod(key, amountInt); err != nil { if err != nil {
utils.GetLogInstance().Error("puzzle-play, error", err) utils.GetLogInstance().Error("puzzle-play, error", err)
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return
} }
res.Success = true res.Success = true
res.TxID = txID
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }
@ -59,13 +60,15 @@ func (s *Service) Payout(w http.ResponseWriter, r *http.Request) {
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return 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) utils.GetLogInstance().Error("Payout error", err)
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return
} }
res.Success = true res.Success = true
res.TxID = txID
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }
@ -84,11 +87,13 @@ func (s *Service) End(w http.ResponseWriter, r *http.Request) {
return 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) utils.GetLogInstance().Error("Payout error", err)
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
return return
} }
res.Success = true res.Success = true
res.TxID = txID
json.NewEncoder(w).Encode(res) json.NewEncoder(w).Encode(res)
} }

@ -35,9 +35,9 @@ type Service struct {
messageChan chan *msg_pb.Message messageChan chan *msg_pb.Message
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, int64) error CreateTransactionForPlayMethod func(string, int64) (string, error)
CreateTransactionForPayoutMethod func(common.Address, int, string) error CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error)
CreateTransactionForEndMethod func(common.Address) error CreateTransactionForEndMethod func(common.Address) (string, error)
} }
// New returns new client support service. // New returns new client support service.
@ -46,9 +46,9 @@ func New(
GetResult func(string) ([]string, []*big.Int), GetResult func(string) ([]string, []*big.Int),
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, int64) error, CreateTransactionForPlayMethod func(string, int64) (string, error),
CreateTransactionForPayoutMethod func(common.Address, int, string) error, CreateTransactionForPayoutMethod func(common.Address, int, string) (string, error),
CreateTransactionForEndMethod func(common.Address) error) *Service { CreateTransactionForEndMethod func(common.Address) (string, error)) *Service {
return &Service{ return &Service{
CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, CreateTransactionForEnterMethod: CreateTransactionForEnterMethod,
GetResult: GetResult, GetResult: GetResult,
@ -124,6 +124,7 @@ func (s *Service) Run() *http.Server {
type Response struct { type Response struct {
Players []string `json:"players"` Players []string `json:"players"`
Balances []string `json:"balances"` Balances []string `json:"balances"`
TxID string `json:"txid"`
Success bool `json:"success"` Success bool `json:"success"`
} }

@ -51,18 +51,18 @@ func (node *Node) AddPuzzleContract() {
} }
// CreateTransactionForPlayMethod generates transaction for play method and add it into pending tx list. // 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 var err error
toAddress := node.PuzzleContractAddress toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI)) abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil { if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to generate staking contract's ABI", "error", err) utils.GetLogInstance().Error("puzzle-play: Failed to generate staking contract's ABI", "error", err)
return err return "", err
} }
bytesData, err := abi.Pack(Play) bytesData, err := abi.Pack(Play)
if err != nil { if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to generate ABI function bytes data", "error", err) utils.GetLogInstance().Error("puzzle-play: Failed to generate ABI function bytes data", "error", err)
return err return "", err
} }
Stake := big.NewInt(0) Stake := big.NewInt(0)
@ -73,10 +73,10 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) er
balance, err := node.GetBalanceOfAddress(address) balance, err := node.GetBalanceOfAddress(address)
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(Stake) == -1 { } else if balance.Cmp(Stake) == -1 {
utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance) utils.GetLogInstance().Error("puzzle-play: insufficient fund", "error", err, "stake", Stake, "balance", balance)
return ErrPuzzleInsufficientFund return "", ErrPuzzleInsufficientFund
} }
nonce := node.GetNonceOfAddress(address) nonce := node.GetNonceOfAddress(address)
tx := types.NewTransaction( tx := types.NewTransaction(
@ -91,37 +91,37 @@ func (node *Node) CreateTransactionForPlayMethod(priKey string, amount int64) er
if err != nil { if err != nil {
utils.GetLogInstance().Error("puzzle-play: Failed to get private key", "error", err) 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 { if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx}) node.addPendingTransactions(types.Transactions{signedTx})
return nil return signedTx.Hash().Hex(), nil
} }
utils.GetLogInstance().Error("puzzle-play: Unable to call enter method", "error", err) 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. // 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 var err error
toAddress := node.PuzzleContractAddress toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI)) abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil { if err != nil {
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, steps string // add params for address payable player, uint8 new_level, steps string
fmt.Println("Payout: address", address) fmt.Println("Payout: address", address)
bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(level)), sequence) bytesData, err := abi.Pack(Payout, address, big.NewInt(int64(level)), sequence)
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
} }
key := node.PuzzleManagerPrivateKey key := node.PuzzleManagerPrivateKey
if key == nil { if key == nil {
return fmt.Errorf("PuzzleManagerPrivateKey is nil") return "", fmt.Errorf("PuzzleManagerPrivateKey is nil")
} }
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
Amount := big.NewInt(0) Amount := big.NewInt(0)
@ -137,37 +137,37 @@ func (node *Node) CreateTransactionForPayoutMethod(address common.Address, level
if err != nil { if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err) 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 { if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx}) node.addPendingTransactions(types.Transactions{signedTx})
return nil return signedTx.Hash().Hex(), nil
} }
utils.GetLogInstance().Error("Unable to call enter method", "error", err) 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. // 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 var err error
toAddress := node.PuzzleContractAddress toAddress := node.PuzzleContractAddress
abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI)) abi, err := abi.JSON(strings.NewReader(contracts.PuzzleABI))
if err != nil { if err != nil {
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, steps string // add params for address payable player, uint8 new_level, steps string
fmt.Println("EndGame: address", address) fmt.Println("EndGame: address", address)
bytesData, err := abi.Pack(EndGame, address) bytesData, err := abi.Pack(EndGame, address)
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
} }
key := node.PuzzleManagerPrivateKey key := node.PuzzleManagerPrivateKey
if key == nil { if key == nil {
return fmt.Errorf("PuzzleManagerPrivateKey is nil") return "", fmt.Errorf("PuzzleManagerPrivateKey is nil")
} }
nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey)) nonce := node.GetNonceOfAddress(crypto.PubkeyToAddress(key.PublicKey))
Amount := big.NewInt(0) Amount := big.NewInt(0)
@ -183,12 +183,12 @@ func (node *Node) CreateTransactionForEndMethod(address common.Address) error {
if err != nil { if err != nil {
utils.GetLogInstance().Error("Failed to get private key", "error", err) 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 { if signedTx, err := types.SignTx(tx, types.HomesteadSigner{}, key); err == nil {
node.addPendingTransactions(types.Transactions{signedTx}) node.addPendingTransactions(types.Transactions{signedTx})
return nil return signedTx.Hash().Hex(), nil
} }
utils.GetLogInstance().Error("Unable to call enter method", "error", err) utils.GetLogInstance().Error("Unable to call enter method", "error", err)
return err return "", err
} }

Loading…
Cancel
Save