Files
payments/internal/api/services/pools_balances.go
CrimsonandGitHub 5bc95257a2 feat(pools): Add new endpoint that returns latest pool balance (#442)
* Add method to fetch latest balance from storage

* Add functionality to fetch latest pool balance if at value is current time

* Add new API endpoint for fetching latest pool balances

* Generate openapi specs for new latest balances endpoint

* Add e2e test for new pool balances endpoint
2025-05-14 14:57:07 +00:00

47 lines
1012 B
Go

package services
import (
"context"
"math/big"
"github.com/formancehq/payments/internal/models"
"github.com/google/uuid"
)
func (s *Service) PoolsBalances(
ctx context.Context,
poolID uuid.UUID,
) ([]models.AggregatedBalance, error) {
pool, err := s.storage.PoolsGet(ctx, poolID)
if err != nil {
return nil, newStorageError(err, "cannot get pool")
}
res := make(map[string]*big.Int)
for i := range pool.PoolAccounts {
balances, err := s.storage.BalancesGetLatest(ctx, pool.PoolAccounts[i])
if err != nil {
return nil, newStorageError(err, "cannot get latest balances")
}
for _, balance := range balances {
amount, ok := res[balance.Asset]
if !ok {
amount = big.NewInt(0)
}
amount.Add(amount, balance.Balance)
res[balance.Asset] = amount
}
}
balances := make([]models.AggregatedBalance, 0, len(res))
for asset, amount := range res {
balances = append(balances, models.AggregatedBalance{
Asset: asset,
Amount: amount,
})
}
return balances, nil
}