github.com/ethersphere/bee/v2@v2.2.0/pkg/api/balances.go (about)

     1  // Copyright 2020 The Swarm Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package api
     6  
     7  import (
     8  	"errors"
     9  	"net/http"
    10  
    11  	"github.com/ethersphere/bee/v2/pkg/accounting"
    12  	"github.com/ethersphere/bee/v2/pkg/bigint"
    13  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    14  	"github.com/ethersphere/bee/v2/pkg/swarm"
    15  	"github.com/gorilla/mux"
    16  )
    17  
    18  const (
    19  	errCantBalances   = "Cannot get balances"
    20  	errCantBalance    = "Cannot get balance"
    21  	errNoBalance      = "No balance for peer"
    22  	errInvalidAddress = "Invalid address"
    23  )
    24  
    25  type balanceResponse struct {
    26  	Peer              string         `json:"peer"`
    27  	Balance           *bigint.BigInt `json:"balance"`
    28  	ThresholdReceived *bigint.BigInt `json:"thresholdreceived"`
    29  	ThresholdGiven    *bigint.BigInt `json:"thresholdgiven"`
    30  }
    31  
    32  type balancesResponse struct {
    33  	Balances []balanceResponse `json:"balances"`
    34  }
    35  
    36  func (s *Service) balancesHandler(w http.ResponseWriter, _ *http.Request) {
    37  	logger := s.logger.WithName("get_consumed").Build()
    38  
    39  	balances, err := s.accounting.Balances()
    40  	if err != nil {
    41  		jsonhttp.InternalServerError(w, errCantBalances)
    42  		logger.Debug("get balances failed", "error", err)
    43  		logger.Error(nil, "get balances failed")
    44  		return
    45  	}
    46  
    47  	balResponses := make([]balanceResponse, len(balances))
    48  	i := 0
    49  	for k := range balances {
    50  		balResponses[i] = balanceResponse{
    51  			Peer:    k,
    52  			Balance: bigint.Wrap(balances[k]),
    53  		}
    54  		i++
    55  	}
    56  
    57  	jsonhttp.OK(w, balancesResponse{Balances: balResponses})
    58  }
    59  
    60  func (s *Service) peerBalanceHandler(w http.ResponseWriter, r *http.Request) {
    61  	logger := s.logger.WithName("get_consumed_by_peer").Build()
    62  
    63  	paths := struct {
    64  		Peer swarm.Address `map:"peer" validate:"required"`
    65  	}{}
    66  	if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
    67  		response("invalid path params", logger, w)
    68  		return
    69  	}
    70  
    71  	balance, err := s.accounting.Balance(paths.Peer)
    72  	if err != nil {
    73  		if errors.Is(err, accounting.ErrPeerNoBalance) {
    74  			jsonhttp.NotFound(w, errNoBalance)
    75  			return
    76  		}
    77  		logger.Debug("get peer balance failed", "peer_address", paths.Peer, "error", err)
    78  		logger.Error(nil, "get peer balance failed", "peer_address", paths.Peer)
    79  		jsonhttp.InternalServerError(w, errCantBalance)
    80  		return
    81  	}
    82  
    83  	jsonhttp.OK(w, balanceResponse{
    84  		Peer:    paths.Peer.String(),
    85  		Balance: bigint.Wrap(balance),
    86  	})
    87  }
    88  
    89  func (s *Service) compensatedBalancesHandler(w http.ResponseWriter, _ *http.Request) {
    90  	logger := s.logger.WithName("get_balances").Build()
    91  
    92  	balances, err := s.accounting.CompensatedBalances()
    93  	if err != nil {
    94  		jsonhttp.InternalServerError(w, errCantBalances)
    95  		logger.Debug("get compensated balances failed", "error", err)
    96  		logger.Error(nil, "get compensated balances failed")
    97  		return
    98  	}
    99  
   100  	balResponses := make([]balanceResponse, len(balances))
   101  	i := 0
   102  	for k := range balances {
   103  		balResponses[i] = balanceResponse{
   104  			Peer:    k,
   105  			Balance: bigint.Wrap(balances[k]),
   106  		}
   107  		i++
   108  	}
   109  
   110  	jsonhttp.OK(w, balancesResponse{Balances: balResponses})
   111  }
   112  
   113  func (s *Service) compensatedPeerBalanceHandler(w http.ResponseWriter, r *http.Request) {
   114  	logger := s.logger.WithName("get_balances_by_peer").Build()
   115  
   116  	paths := struct {
   117  		Peer swarm.Address `map:"peer" validate:"required"`
   118  	}{}
   119  	if response := s.mapStructure(mux.Vars(r), &paths); response != nil {
   120  		response("invalid path params", logger, w)
   121  		return
   122  	}
   123  
   124  	balance, err := s.accounting.CompensatedBalance(paths.Peer)
   125  	if err != nil {
   126  		if errors.Is(err, accounting.ErrPeerNoBalance) {
   127  			jsonhttp.NotFound(w, errNoBalance)
   128  			return
   129  		}
   130  		s.logger.Debug("get compensated balances failed", "peer_address", paths.Peer, "error", err)
   131  		s.logger.Error(nil, "get compensated balances failed", "peer_address", paths.Peer)
   132  		jsonhttp.InternalServerError(w, errCantBalance)
   133  		return
   134  	}
   135  
   136  	jsonhttp.OK(w, balanceResponse{
   137  		Peer:    paths.Peer.String(),
   138  		Balance: bigint.Wrap(balance),
   139  	})
   140  }