diff --git a/api/service/restclientsupport/service.go b/api/service/restclientsupport/service.go index 0c1186c2f..e1d07aa12 100644 --- a/api/service/restclientsupport/service.go +++ b/api/service/restclientsupport/service.go @@ -34,6 +34,7 @@ type Service struct { CreateTransactionForPickWinner func() error messageChan chan *msg_pb.Message CallFaucetContract func(common.Address) common.Hash + GetAccountBalance func(common.Address) (*big.Int, error) } // New returns new client support service. @@ -41,12 +42,13 @@ func New( CreateTransactionForEnterMethod func(int64, string) error, GetResult func(string) ([]string, []*big.Int), CreateTransactionForPickWinner func() error, - CallFaucetContract func(common.Address) common.Hash) *Service { + CallFaucetContract func(common.Address) common.Hash, GetAccountBalance func(common.Address) (*big.Int, error)) *Service { return &Service{ CreateTransactionForEnterMethod: CreateTransactionForEnterMethod, GetResult: GetResult, CreateTransactionForPickWinner: CreateTransactionForPickWinner, CallFaucetContract: CallFaucetContract, + GetAccountBalance: GetAccountBalance, } } @@ -108,12 +110,25 @@ type Response struct { // GetBalance implements the GetFreeToken interface to request free token. func (s *Service) GetBalance(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "application/json") - key := r.FormValue("key") + addressHex := r.FormValue("key") + fmt.Println("fundMe: address", addressHex) + res := &Response{Success: false} - res.Players = append(res.Players, key) - // res.Balances = append(res.Players, GETMYBALANCE) // implements this - json.NewEncoder(w).Encode(res) + if s.GetAccountBalance == nil { + json.NewEncoder(w).Encode(res) + return + } + res.Players = append(res.Players, addressHex) + address := common.HexToAddress(addressHex) + balance, err := s.GetAccountBalance(address) + if err != nil { + json.NewEncoder(w).Encode(res) + return + } + res.Balances = append(res.Players, balance.String()) + res.Success = true + json.NewEncoder(w).Encode(res) } // FundMe implements the GetFreeToken interface to request free token. @@ -123,7 +138,7 @@ func (s *Service) FundMe(w http.ResponseWriter, r *http.Request) { fmt.Println("fundMe: address", addressHex) res := &Response{Success: false} - if s.CreateTransactionForEnterMethod == nil { + if s.CallFaucetContract == nil { json.NewEncoder(w).Encode(res) return } diff --git a/cmd/restclientsupport/main.go b/cmd/restclientsupport/main.go index 4f7b31bb8..3800bf791 100644 --- a/cmd/restclientsupport/main.go +++ b/cmd/restclientsupport/main.go @@ -7,7 +7,7 @@ import ( ) func main() { - s := restclientsupport.New(nil, nil, nil) + s := restclientsupport.New(nil, nil, nil, nil, nil) s.StartService() fmt.Println("Server started") select {} diff --git a/node/contract.go b/node/contract.go index 6bed29176..e63a05c1d 100644 --- a/node/contract.go +++ b/node/contract.go @@ -139,13 +139,13 @@ func (node *Node) GetNonceOfAddress(address common.Address) uint64 { } // GetBalanceOfAddress returns balance of an address. -func (node *Node) GetBalanceOfAddress(address common.Address) *big.Int { +func (node *Node) GetBalanceOfAddress(address common.Address) (*big.Int, error) { state, err := node.blockchain.State() if err != nil { log.Error("Failed to get chain state", "Error", err) - return nil + return nil, err } - return state.GetBalance(address) + return state.GetBalance(address), nil } // AddFaucetContractToPendingTransactions adds the faucet contract the genesis block. diff --git a/node/demo_contract.go b/node/demo_contract.go index b1477fdc0..7ec6ea186 100644 --- a/node/demo_contract.go +++ b/node/demo_contract.go @@ -101,7 +101,8 @@ func (node *Node) GetResultDirectly(priKey string) (players []string, balances [ utils.GetLogInstance().Error("Error when HexToECDSA") } address := crypto.PubkeyToAddress(key.PublicKey) - balances = append(balances, node.GetBalanceOfAddress(address)) + balance, err := node.GetBalanceOfAddress(address) + balances = append(balances, balance) } return players, balances } @@ -110,7 +111,8 @@ func (node *Node) GetResultDirectly(priKey string) (players []string, balances [ func (node *Node) GenerateResultDirectly(addresses []common.Address) (players []string, balances []*big.Int) { for _, address := range addresses { players = append(players, address.String()) - balances = append(balances, node.GetBalanceOfAddress(address)) + balance, _ := node.GetBalanceOfAddress(address) + balances = append(balances, balance) } fmt.Println("generate result", players, balances) return players, balances diff --git a/node/service_setup.go b/node/service_setup.go index 813b04447..304ea51e6 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.CallFaucetContract)) + node.CreateTransactionForEnterMethod, node.GetResult, node.CreateTransactionForPickWinner, node.CallFaucetContract, node.GetBalanceOfAddress)) // Register randomness service node.serviceManager.RegisterService(service.Randomness, randomness.New(node.DRand)) // Register explorer service.