github.com/ethersphere/bee/v2@v2.2.0/pkg/api/accounting.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  	"net/http"
     9  
    10  	"github.com/ethersphere/bee/v2/pkg/bigint"
    11  	"github.com/ethersphere/bee/v2/pkg/jsonhttp"
    12  )
    13  
    14  const (
    15  	httpErrGetAccountingInfo = "Cannot get accounting info"
    16  )
    17  
    18  type peerData struct {
    19  	InfoResponse map[string]peerDataResponse `json:"peerData"`
    20  }
    21  
    22  type peerDataResponse struct {
    23  	Balance                  *bigint.BigInt `json:"balance"`
    24  	ConsumedBalance          *bigint.BigInt `json:"consumedBalance"`
    25  	ThresholdReceived        *bigint.BigInt `json:"thresholdReceived"`
    26  	ThresholdGiven           *bigint.BigInt `json:"thresholdGiven"`
    27  	CurrentThresholdReceived *bigint.BigInt `json:"currentThresholdReceived"`
    28  	CurrentThresholdGiven    *bigint.BigInt `json:"currentThresholdGiven"`
    29  	SurplusBalance           *bigint.BigInt `json:"surplusBalance"`
    30  	ReservedBalance          *bigint.BigInt `json:"reservedBalance"`
    31  	ShadowReservedBalance    *bigint.BigInt `json:"shadowReservedBalance"`
    32  	GhostBalance             *bigint.BigInt `json:"ghostBalance"`
    33  }
    34  
    35  func (s *Service) accountingInfoHandler(w http.ResponseWriter, _ *http.Request) {
    36  	logger := s.logger.WithName("get_accounting").Build()
    37  
    38  	infos, err := s.accounting.PeerAccounting()
    39  	if err != nil {
    40  		jsonhttp.InternalServerError(w, httpErrGetAccountingInfo)
    41  		logger.Debug("accounting info failed to load balances")
    42  		logger.Error(err, "can not get accounting info")
    43  		return
    44  	}
    45  
    46  	infoResponses := make(map[string]peerDataResponse, len(infos))
    47  	for k := range infos {
    48  		infoResponses[k] = peerDataResponse{
    49  			Balance:                  bigint.Wrap(infos[k].Balance),
    50  			ConsumedBalance:          bigint.Wrap(infos[k].ConsumedBalance),
    51  			ThresholdReceived:        bigint.Wrap(infos[k].ThresholdReceived),
    52  			ThresholdGiven:           bigint.Wrap(infos[k].ThresholdGiven),
    53  			CurrentThresholdReceived: bigint.Wrap(infos[k].CurrentThresholdReceived),
    54  			CurrentThresholdGiven:    bigint.Wrap(infos[k].CurrentThresholdGiven),
    55  			SurplusBalance:           bigint.Wrap(infos[k].SurplusBalance),
    56  			ReservedBalance:          bigint.Wrap(infos[k].ReservedBalance),
    57  			ShadowReservedBalance:    bigint.Wrap(infos[k].ShadowReservedBalance),
    58  			GhostBalance:             bigint.Wrap(infos[k].GhostBalance),
    59  		}
    60  	}
    61  
    62  	jsonhttp.OK(w, peerData{InfoResponse: infoResponses})
    63  }