mirror of
https://github.com/hanzo-fi/payments.git
synced 2026-10-30 20:31:20 +00:00
* 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
49 lines
1.0 KiB
Go
49 lines
1.0 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"math/big"
|
|
"time"
|
|
|
|
"github.com/formancehq/payments/internal/models"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func (s *Service) PoolsBalancesAt(
|
|
ctx context.Context,
|
|
poolID uuid.UUID,
|
|
at time.Time,
|
|
) ([]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.BalancesGetAt(ctx, pool.PoolAccounts[i], at)
|
|
if err != nil {
|
|
return nil, newStorageError(err, "cannot get 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
|
|
}
|