Skip to content

Commit

Permalink
feat(api): add spendable balance api (#442)
Browse files Browse the repository at this point in the history
add spendable balance API

issue: none
  • Loading branch information
ezreal1997 authored Dec 17, 2024
1 parent 6210bab commit d20b723
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 0 deletions.
46 changes: 46 additions & 0 deletions client/server/bank.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ func (s *Server) initBankRoute() {
s.httpMux.HandleFunc("/bank/balances/{address}", utils.AutoWrap(s.aminoCodec, s.GetBalancesByAddress))
s.httpMux.HandleFunc("/bank/balances/{address}/by_denom", utils.AutoWrap(s.aminoCodec, s.GetBalancesByAddressDenom))

s.httpMux.HandleFunc("/bank/spendable_balances/{address}", utils.AutoWrap(s.aminoCodec, s.GetSpendableBalancesByAddress))
s.httpMux.HandleFunc("/bank/spendable_balances/{address}/by_denom", utils.AutoWrap(s.aminoCodec, s.GetSpendableBalancesByAddressDenom))

s.httpMux.HandleFunc("/bank/denom_owners/{denom}", utils.AutoWrap(s.aminoCodec, s.GetDenomOwners))
s.httpMux.HandleFunc("/bank/denom_owners_by_query", utils.AutoWrap(s.aminoCodec, s.GetDenomOwnersByQuery))
}
Expand Down Expand Up @@ -123,6 +126,49 @@ func (s *Server) GetBalancesByAddressDenom(req *getBalancesByAddressDenomRequest
return queryResp, nil
}

// GetSpendableBalancesByAddress queries the spendable balance of all coins for a single account.
func (s *Server) GetSpendableBalancesByAddress(req *getSpendableBalancesByAddressRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().SpendableBalances(queryContext, &banktypes.QuerySpendableBalancesRequest{
Address: mux.Vars(r)["address"],
Pagination: &query.PageRequest{
Key: []byte(req.Pagination.Key),
Offset: req.Pagination.Offset,
Limit: req.Pagination.Limit,
CountTotal: req.Pagination.CountTotal,
Reverse: req.Pagination.Reverse,
},
})
if err != nil {
return nil, err
}

return queryResp, nil
}

// GetSpendableBalancesByAddressDenom queries the spendable balance of a single coin for a single account.
func (s *Server) GetSpendableBalancesByAddressDenom(req *getSpendableBalancesByAddressDenomRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
if err != nil {
return nil, err
}

queryResp, err := s.store.GetBankKeeper().SpendableBalanceByDenom(queryContext, &banktypes.QuerySpendableBalanceByDenomRequest{
Address: mux.Vars(r)["address"],
Denom: req.Denom,
})

if err != nil {
return nil, err
}

return queryResp, nil
}

// GetDenomOwners queries for all account addresses that own a particular token denomination.
func (s *Server) GetDenomOwners(req *getDenomOwnersRequest, r *http.Request) (resp any, err error) {
queryContext, err := s.createQueryContextByHeader(r)
Expand Down
8 changes: 8 additions & 0 deletions client/server/payload.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ type getBalancesByAddressDenomRequest struct {
Denom string `mapstructure:"denom"`
}

type getSpendableBalancesByAddressRequest struct {
Pagination pagination `mapstructure:"pagination"`
}

type getSpendableBalancesByAddressDenomRequest struct {
Denom string `mapstructure:"denom"`
}

type getDenomOwnersRequest struct {
Pagination pagination `mapstructure:"pagination"`
}
Expand Down

0 comments on commit d20b723

Please sign in to comment.