mirror of
https://github.com/hanzo-fi/stack.git
synced 2026-10-30 20:34:43 +00:00
85 lines
2.2 KiB
Go
85 lines
2.2 KiB
Go
package v1
|
|
|
|
import (
|
|
"math/big"
|
|
"net/http"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/pointer"
|
|
|
|
"github.com/formancehq/stack/libs/go-libs/bun/bunpaginate"
|
|
|
|
"github.com/formancehq/ledger/internal/api/backend"
|
|
"github.com/formancehq/ledger/internal/storage/ledgerstore"
|
|
sharedapi "github.com/formancehq/stack/libs/go-libs/api"
|
|
"github.com/formancehq/stack/libs/go-libs/query"
|
|
)
|
|
|
|
func buildAggregatedBalancesQuery(r *http.Request) (query.Builder, error) {
|
|
if address := r.URL.Query().Get("address"); address != "" {
|
|
return query.Match("address", address), nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
func getBalancesAggregated(w http.ResponseWriter, r *http.Request) {
|
|
options, err := getPaginatedQueryOptionsOfPITFilter(r)
|
|
if err != nil {
|
|
sharedapi.BadRequest(w, ErrValidation, err)
|
|
return
|
|
}
|
|
|
|
query := ledgerstore.NewGetAggregatedBalancesQuery(*options)
|
|
query.Options.QueryBuilder, err = buildAggregatedBalancesQuery(r)
|
|
|
|
balances, err := backend.LedgerFromContext(r.Context()).GetAggregatedBalances(r.Context(), query)
|
|
if err != nil {
|
|
sharedapi.InternalServerError(w, r, err)
|
|
return
|
|
}
|
|
|
|
sharedapi.Ok(w, balances)
|
|
}
|
|
|
|
func getBalances(w http.ResponseWriter, r *http.Request) {
|
|
l := backend.LedgerFromContext(r.Context())
|
|
|
|
q, err := bunpaginate.Extract[ledgerstore.GetAccountsQuery](r, func() (*ledgerstore.GetAccountsQuery, error) {
|
|
options, err := getPaginatedQueryOptionsOfPITFilterWithVolumes(r)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
options.QueryBuilder, err = buildAccountsFilterQuery(r)
|
|
return pointer.For(ledgerstore.NewGetAccountsQuery(*options)), nil
|
|
})
|
|
if err != nil {
|
|
sharedapi.BadRequest(w, ErrValidation, err)
|
|
return
|
|
}
|
|
|
|
cursor, err := l.GetAccountsWithVolumes(r.Context(), q.WithExpandVolumes())
|
|
if err != nil {
|
|
sharedapi.InternalServerError(w, r, err)
|
|
return
|
|
}
|
|
|
|
ret := make([]map[string]map[string]*big.Int, 0)
|
|
for _, item := range cursor.Data {
|
|
e := map[string]map[string]*big.Int{
|
|
item.Address: {},
|
|
}
|
|
for asset, volumes := range item.Volumes {
|
|
e[item.Address][asset] = volumes.Balance()
|
|
}
|
|
ret = append(ret, e)
|
|
}
|
|
|
|
sharedapi.RenderCursor(w, sharedapi.Cursor[map[string]map[string]*big.Int]{
|
|
PageSize: cursor.PageSize,
|
|
HasMore: cursor.HasMore,
|
|
Previous: cursor.Previous,
|
|
Next: cursor.Next,
|
|
Data: ret,
|
|
})
|
|
}
|