From d20b723fd464b967c8c79eb35070a73dc86b0aef Mon Sep 17 00:00:00 2001 From: Rayden Ge Date: Tue, 17 Dec 2024 20:51:38 +0100 Subject: [PATCH] feat(api): add spendable balance api (#442) add spendable balance API issue: none --- client/server/bank.go | 46 ++++++++++++++++++++++++++++++++++++++++ client/server/payload.go | 8 +++++++ 2 files changed, 54 insertions(+) diff --git a/client/server/bank.go b/client/server/bank.go index 3a4c82b5..a6c2fb23 100644 --- a/client/server/bank.go +++ b/client/server/bank.go @@ -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)) } @@ -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) diff --git a/client/server/payload.go b/client/server/payload.go index d1de39e1..023a6d28 100644 --- a/client/server/payload.go +++ b/client/server/payload.go @@ -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"` }