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

Method getSourceAndDestination

transaction.go:150–209  ·  view source on GitHub ↗

getSourceAndDestination retrieves the source and destination balances for a transaction. It checks if the source or destination starts with "@", indicating the need to create or retrieve a balance by indicator. If not, it retrieves the balances by their IDs. When EnableQueuedChecks is enabled in the

(ctx context.Context, transaction *model.Transaction)

Source from the content-addressed store, hash-verified

148// - destination *model.Balance: A pointer to the destination Balance model.
149// - err error: An error if the balances could not be retrieved.
150func (l *LedgerForge) getSourceAndDestination(ctx context.Context, transaction *model.Transaction) (source *model.Balance, destination *model.Balance, err error) {
151 ctx, span := tracer.Start(ctx, "GetSourceAndDestination")
152 defer span.End()
153
154 var sourceBalance, destinationBalance *model.Balance
155
156 cfg := l.config
157
158 // Check if Source starts with "@"
159 if strings.HasPrefix(transaction.Source, "@") {
160 sourceBalance, err = l.getOrCreateBalanceByIndicator(ctx, transaction.Source, transaction.Currency)
161 if err != nil {
162 span.RecordError(err)
163 logrus.WithError(err).Error("source balance lookup failed")
164 return nil, nil, err
165 }
166 // Update transaction source with the balance ID
167 transaction.Source = sourceBalance.BalanceID
168 span.SetAttributes(attribute.String("source.balance_id", sourceBalance.BalanceID))
169 } else {
170 // Use GetBalanceByID with queued checks if enabled, otherwise use lite version
171 if cfg.Transaction.EnableQueuedChecks {
172 sourceBalance, err = l.datasource.GetBalanceByID(transaction.Source, []string{}, true)
173 } else {
174 sourceBalance, err = l.datasource.GetBalanceByIDLite(transaction.Source)
175 }
176 if err != nil {
177 span.RecordError(err)
178 logrus.WithError(err).Error("source balance lookup failed")
179 return nil, nil, err
180 }
181 }
182
183 // Check if Destination starts with "@"
184 if strings.HasPrefix(transaction.Destination, "@") {
185 destinationBalance, err = l.getOrCreateBalanceByIndicator(ctx, transaction.Destination, transaction.Currency)
186 if err != nil {
187 span.RecordError(err)
188 logrus.WithError(err).Error("destination balance lookup failed")
189 return nil, nil, err
190 }
191 // Update transaction destination with the balance ID
192 transaction.Destination = destinationBalance.BalanceID
193 span.SetAttributes(attribute.String("destination.balance_id", destinationBalance.BalanceID))
194 } else {
195 // Use GetBalanceByID with queued checks if enabled, otherwise use lite version
196 if cfg.Transaction.EnableQueuedChecks {
197 destinationBalance, err = l.datasource.GetBalanceByID(transaction.Destination, []string{}, true)
198 } else {
199 destinationBalance, err = l.datasource.GetBalanceByIDLite(transaction.Destination)
200 }
201 if err != nil {
202 span.RecordError(err)
203 logrus.WithError(err).Error("destination balance lookup failed")
204 return nil, nil, err
205 }
206 }
207 span.AddEvent("Retrieved source and destination balances")

Callers 2

Calls 5

GetBalanceByIDMethod · 0.65
GetBalanceByIDLiteMethod · 0.65
StartMethod · 0.45
ErrorMethod · 0.45

Tested by

no test coverage detected