sequentialAllocation allocates funds sequentially from sources (used for FIFO/LIFO). Parameters: - sources []LineageSource: The available fund sources in order. - amount *big.Int: The amount to allocate. Returns: - []Allocation: The calculated allocations.
(sources []LineageSource, amount *big.Int)
| 915 | // Returns: |
| 916 | // - []Allocation: The calculated allocations. |
| 917 | func (l *LedgerForge) sequentialAllocation(sources []LineageSource, amount *big.Int) []Allocation { |
| 918 | var allocations []Allocation |
| 919 | |
| 920 | // Skip if amount is zero or negative |
| 921 | if amount.Cmp(big.NewInt(0)) <= 0 { |
| 922 | return nil |
| 923 | } |
| 924 | |
| 925 | remaining := new(big.Int).Set(amount) |
| 926 | |
| 927 | for _, source := range sources { |
| 928 | if remaining.Cmp(big.NewInt(0)) <= 0 { |
| 929 | break |
| 930 | } |
| 931 | |
| 932 | alloc := new(big.Int) |
| 933 | if source.Balance.Cmp(remaining) >= 0 { |
| 934 | alloc.Set(remaining) |
| 935 | } else { |
| 936 | alloc.Set(source.Balance) |
| 937 | } |
| 938 | |
| 939 | // Skip zero allocations |
| 940 | if alloc.Cmp(big.NewInt(0)) <= 0 { |
| 941 | continue |
| 942 | } |
| 943 | |
| 944 | allocations = append(allocations, Allocation{ |
| 945 | BalanceID: source.BalanceID, |
| 946 | Amount: alloc, |
| 947 | }) |
| 948 | |
| 949 | remaining.Sub(remaining, alloc) |
| 950 | } |
| 951 | |
| 952 | // Log warning if we couldn't allocate the full amount |
| 953 | if remaining.Cmp(big.NewInt(0)) > 0 { |
| 954 | logrus.Warnf("sequential allocation: could not allocate full amount, %s remaining unallocated", remaining.String()) |
| 955 | } |
| 956 | |
| 957 | return allocations |
| 958 | } |
| 959 | |
| 960 | // proportionalAllocation allocates funds proportionally across all sources. |
| 961 | // |