From 5eac1d2b0699a179de8ae788c595a833a3ba7218 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Wed, 24 Apr 2019 13:44:45 -0700 Subject: [PATCH] add FundMe skeleton --- api/service/restclientsupport/service.go | 36 +++++++++++++++++++++++- node/service_setup.go | 2 +- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/api/service/restclientsupport/service.go b/api/service/restclientsupport/service.go index dcdc48d9a..11fdb943d 100644 --- a/api/service/restclientsupport/service.go +++ b/api/service/restclientsupport/service.go @@ -9,11 +9,16 @@ import ( "net/http" "strconv" + "github.com/ethereum/go-ethereum/common" "github.com/gorilla/mux" msg_pb "github.com/harmony-one/harmony/api/proto/message" "github.com/harmony-one/harmony/internal/utils" ) +/* + * This service is specific to support the lottery app. + */ + // Constants for rest client support service. const ( Port = "30000" @@ -34,11 +39,12 @@ func New( CreateTransactionForEnterMethod func(int64, string) error, GetResult func(string) ([]string, []*big.Int), CreateTransactionForPickWinner func() error, -) *Service { + CallFaucetContract func(common.Address) common.Hash) *Service { return &Service{ CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, GetResult: GetResult, CreateTransactionForPickWinner: CreateTransactionForPickWinner, + CallFaucetContract: CallFaucetContract, } } @@ -72,6 +78,10 @@ func (s *Service) Run() *http.Server { s.router.Path("/result").Queries("key", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.Result).Methods("GET") s.router.Path("/result").HandlerFunc(s.Result) + // Set up router for tx. + s.router.Path("/fundme").Queries("key", "{[0-9A-Fa-fx]*?}").HandlerFunc(s.FundMe).Methods("GET") + s.router.Path("/fundme").HandlerFunc(s.FundMe) + // Set up router for tx. s.router.Path("/winner").HandlerFunc(s.Winner) @@ -89,6 +99,30 @@ type Response struct { Success bool `json:"success"` } +// FundMe implements the GetFreeToken interface to request free token. +func (s *Service) FundMe(w http.ResponseWriter, r *http.Request) { + // (ctx context.Context, request *proto.GetFreeTokenRequest) (*proto.GetFreeTokenResponse, error) { + // var address common.Address + // address.SetBytes(request.Address) + // // log.Println("Returning GetFreeTokenResponse for address: ", address.Hex()) + // return &proto.GetFreeTokenResponse{TxId: s.callFaucetContract(address).Bytes()}, nil + w.Header().Set("Content-Type", "application/json") + key := r.FormValue("key") + + // TODO(RJ): implement the logic similar to get FreeToken with key. + + // GetFreeToken implements the GetFreeToken interface to request free token. + // func (s *Server) GetFreeToken(ctx context.Context, request *proto.GetFreeTokenRequest) (*proto.GetFreeTokenResponse, error) { + // var address common.Address + // address.SetBytes(request.Address) + // // log.Println("Returning GetFreeTokenResponse for address: ", address.Hex()) + // return &proto.GetFreeTokenResponse{TxId: s.callFaucetContract(address).Bytes()}, nil + // } + + res := &Response{Success: false} + json.NewEncoder(w).Encode(res) +} + // Enter triggers enter method of smart contract. func (s *Service) Enter(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") diff --git a/node/service_setup.go b/node/service_setup.go index 590f2025d..813b04447 100644 --- a/node/service_setup.go +++ b/node/service_setup.go @@ -64,7 +64,7 @@ func (node *Node) setupForBeaconLeader() { // Register client new support service. // TODO(minhdoan): Also consider provide clientsupport/restclientsupport for other shards in the future. node.serviceManager.RegisterService(service.RestClientSupport, restclientsupport.New( - node.CreateTransactionForEnterMethod, node.GetResult, node.CreateTransactionForPickWinner)) + node.CreateTransactionForEnterMethod, node.GetResult, node.CreateTransactionForPickWinner, node.CallFaucetContract)) // Register randomness service node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) // Register explorer service.