Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(api): add spendable balance api #442

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading