Skip to content

Commit

Permalink
feat: move code
Browse files Browse the repository at this point in the history
  • Loading branch information
gfyrag committed Oct 16, 2024
1 parent 15ae219 commit 0c1ab69
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 44 deletions.
45 changes: 1 addition & 44 deletions internal/storage/ledger/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func (s *Store) CommitTransaction(ctx context.Context, tx *ledger.Transaction) e
}
}

postCommitVolumes, err := s.updateVolumes(ctx, volumeUpdates(tx)...)
postCommitVolumes, err := s.updateVolumes(ctx, tx.VolumeUpdates()...)
if err != nil {
return errors.Wrap(err, "failed to update balances")
}
Expand Down Expand Up @@ -559,46 +559,3 @@ func filterAccountAddressOnTransactions(address string, source, destination bool
return strings.Join(parts, " or ")
}
}

func volumeUpdates(transaction *ledger.Transaction) []ledger.AccountsVolumes {
aggregatedVolumes := make(map[string]map[string][]ledger.Posting)
for _, posting := range transaction.Postings {
if _, ok := aggregatedVolumes[posting.Source]; !ok {
aggregatedVolumes[posting.Source] = make(map[string][]ledger.Posting)
}
aggregatedVolumes[posting.Source][posting.Asset] = append(aggregatedVolumes[posting.Source][posting.Asset], posting)

if posting.Source == posting.Destination {
continue
}

if _, ok := aggregatedVolumes[posting.Destination]; !ok {
aggregatedVolumes[posting.Destination] = make(map[string][]ledger.Posting)
}
aggregatedVolumes[posting.Destination][posting.Asset] = append(aggregatedVolumes[posting.Destination][posting.Asset], posting)
}

ret := make([]ledger.AccountsVolumes, 0)
for account, movesByAsset := range aggregatedVolumes {
for asset, postings := range movesByAsset {
volumes := ledger.NewEmptyVolumes()
for _, posting := range postings {
if account == posting.Source {
volumes.Output.Add(volumes.Output, posting.Amount)
}
if account == posting.Destination {
volumes.Input.Add(volumes.Input, posting.Amount)
}
}

ret = append(ret, ledger.AccountsVolumes{
Account: account,
Asset: asset,
Input: volumes.Input,
Output: volumes.Output,
})
}
}

return ret
}
44 changes: 44 additions & 0 deletions internal/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,50 @@ func (tx Transaction) InvolvedAccounts() []string {
return slices.Compact(ret)
}

// todo: add unit tests!
func (tx Transaction) VolumeUpdates() []AccountsVolumes {
aggregatedVolumes := make(map[string]map[string][]Posting)
for _, posting := range tx.Postings {
if _, ok := aggregatedVolumes[posting.Source]; !ok {
aggregatedVolumes[posting.Source] = make(map[string][]Posting)
}
aggregatedVolumes[posting.Source][posting.Asset] = append(aggregatedVolumes[posting.Source][posting.Asset], posting)

if posting.Source == posting.Destination {
continue
}

if _, ok := aggregatedVolumes[posting.Destination]; !ok {
aggregatedVolumes[posting.Destination] = make(map[string][]Posting)
}
aggregatedVolumes[posting.Destination][posting.Asset] = append(aggregatedVolumes[posting.Destination][posting.Asset], posting)
}

ret := make([]AccountsVolumes, 0)
for account, movesByAsset := range aggregatedVolumes {
for asset, postings := range movesByAsset {
volumes := NewEmptyVolumes()
for _, posting := range postings {
if account == posting.Source {
volumes.Output.Add(volumes.Output, posting.Amount)
}
if account == posting.Destination {
volumes.Input.Add(volumes.Input, posting.Amount)
}
}

ret = append(ret, AccountsVolumes{
Account: account,
Asset: asset,
Input: volumes.Input,
Output: volumes.Output,
})
}
}

return ret
}

func (tx Transaction) MarshalJSON() ([]byte, error) {
type Aux Transaction
type Ret struct {
Expand Down

0 comments on commit 0c1ab69

Please sign in to comment.