Add get balance api

pull/788/head
Rongjian Lan 6 years ago
parent da685146a3
commit 169b1c6902
  1. 27
      api/service/restclientsupport/service.go
  2. 2
      cmd/restclientsupport/main.go
  3. 6
      node/contract.go
  4. 6
      node/demo_contract.go
  5. 2
      node/service_setup.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
}

@ -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 {}

@ -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.

@ -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

@ -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.

Loading…
Cancel
Save