github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/export/handle_accounting.go (about)

     1  // Copyright 2021 The TrueBlocks Authors. All rights reserved.
     2  // Use of this source code is governed by a license that can
     3  // be found in the LICENSE file.
     4  
     5  package exportPkg
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/articulate"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/filter"
    13  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/ledger"
    14  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/monitor"
    15  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    16  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    17  )
    18  
    19  func (opts *ExportOptions) HandleAccounting(rCtx *output.RenderCtx, monitorArray []monitor.Monitor) error {
    20  	// TODO: BOGUS - RECONSIDER THIS
    21  	opts.Articulate = true
    22  
    23  	ledgers := &ledger.Ledger{}
    24  	chain := opts.Globals.Chain
    25  	abiCache := articulate.NewAbiCache(opts.Conn, opts.Articulate)
    26  	testMode := opts.Globals.TestMode
    27  	filter := filter.NewFilter(
    28  		opts.Reversed,
    29  		opts.Reverted,
    30  		opts.Fourbytes,
    31  		base.BlockRange{First: opts.FirstBlock, Last: opts.LastBlock},
    32  		base.RecordRange{First: opts.FirstRecord, Last: opts.GetMax()},
    33  	)
    34  
    35  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    36  		visitAppearance := func(app *types.Appearance) error {
    37  			if tx, err := opts.Conn.GetTransactionByAppearance(app, false); err != nil {
    38  				errorChan <- err
    39  				return nil
    40  
    41  			} else {
    42  				passes, _ := filter.ApplyTxFilters(tx)
    43  				if passes {
    44  					if opts.Articulate {
    45  						if err = abiCache.ArticulateTransaction(tx); err != nil {
    46  							errorChan <- err // continue even on error
    47  						}
    48  					}
    49  
    50  					if statements, err := ledgers.GetStatements(opts.Conn, filter, tx); err != nil {
    51  						errorChan <- err
    52  
    53  					} else {
    54  						tx.Statements = &statements
    55  					}
    56  
    57  					modelChan <- tx
    58  				}
    59  				return nil
    60  			}
    61  		}
    62  
    63  		for _, mon := range monitorArray {
    64  			if apps, cnt, err := mon.ReadAndFilterAppearances(filter, true /* withCount */); err != nil {
    65  				errorChan <- err
    66  				return
    67  
    68  			} else if !opts.NoZero || cnt > 0 {
    69  				ledgers = ledger.NewLedger(
    70  					opts.Conn,
    71  					mon.Address,
    72  					opts.FirstBlock,
    73  					opts.LastBlock,
    74  					opts.Globals.Ether,
    75  					testMode,
    76  					opts.NoZero,
    77  					opts.Traces,
    78  					opts.Reversed,
    79  					&opts.Asset,
    80  				)
    81  				_ = ledgers.SetContexts(chain, apps)
    82  
    83  				for _, app := range apps {
    84  					if err := visitAppearance(&app); err != nil {
    85  						errorChan <- err
    86  						return
    87  					}
    88  				}
    89  
    90  			} else {
    91  				errorChan <- fmt.Errorf("no appearances found for %s", mon.Address.Hex())
    92  				continue
    93  			}
    94  		}
    95  	}
    96  
    97  	extraOpts := map[string]any{
    98  		"articulate": opts.Articulate,
    99  		"export":     true,
   100  	}
   101  
   102  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOptsWithExtra(extraOpts))
   103  }