MCPcopy Create free account
hub / github.com/devaccuracy/ledgerforge / getLineageSources

Method getLineageSources

lineage.go:838–876  ·  view source on GitHub ↗

getLineageSources retrieves the available fund sources from shadow balances for allocation. Uses a single batch query to fetch all shadow balances instead of N individual queries. Parameters: - ctx context.Context: The context for the operation. - mappings []model.LineageMapping: The lineage mappin

(ctx context.Context, mappings []model.LineageMapping)

Source from the content-addressed store, hash-verified

836// - []LineageSource: The available fund sources.
837// - error: An error if the sources could not be retrieved.
838func (l *LedgerForge) getLineageSources(ctx context.Context, mappings []model.LineageMapping) ([]LineageSource, error) {
839 if len(mappings) == 0 {
840 return nil, nil
841 }
842
843 // Collect all shadow balance IDs for batch query
844 shadowBalanceIDs := make([]string, 0, len(mappings))
845 for _, mapping := range mappings {
846 shadowBalanceIDs = append(shadowBalanceIDs, mapping.ShadowBalanceID)
847 }
848
849 // Fetch all shadow balances in a single query
850 balances, err := l.datasource.GetBalancesByIDsLite(ctx, shadowBalanceIDs)
851 if err != nil {
852 return nil, fmt.Errorf("failed to fetch shadow balances: %w", err)
853 }
854
855 // Build sources from the fetched balances
856 var sources []LineageSource
857 for _, mapping := range mappings {
858 balance, exists := balances[mapping.ShadowBalanceID]
859 if !exists {
860 continue
861 }
862
863 if balance.DebitBalance != nil && balance.CreditBalance != nil && balance.DebitBalance.Cmp(big.NewInt(0)) > 0 {
864 available := new(big.Int).Sub(balance.DebitBalance, balance.CreditBalance)
865 if available.Cmp(big.NewInt(0)) > 0 {
866 sources = append(sources, LineageSource{
867 BalanceID: mapping.ShadowBalanceID,
868 Balance: available,
869 CreatedAt: mapping.CreatedAt,
870 })
871 }
872 }
873 }
874
875 return sources, nil
876}
877
878// calculateAllocation calculates fund allocations based on the specified strategy.
879// Supported strategies are FIFO, LIFO, and PROPORTIONAL.

Calls 1

GetBalancesByIDsLiteMethod · 0.65