github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/abis/handle_encode.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 abisPkg
     6  
     7  import (
     8  	"fmt"
     9  
    10  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/abi"
    11  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output"
    12  	"github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types"
    13  	"github.com/ethereum/go-ethereum/common/hexutil"
    14  	"github.com/ethereum/go-ethereum/crypto"
    15  )
    16  
    17  func (opts *AbisOptions) HandleEncode(rCtx *output.RenderCtx) error {
    18  	fetchData := func(modelChan chan types.Modeler, errorChan chan error) {
    19  		funcs := abi.ExtractSigs(opts.Encode)
    20  		if len(funcs) == 0 {
    21  			errorChan <- fmt.Errorf("not a valid function signature in HandleEncode: %s", opts.Encode)
    22  			return
    23  		}
    24  		for _, f := range funcs {
    25  			enc := hexutil.Encode(crypto.Keccak256([]byte(f.Signature)))
    26  			if f.FunctionType == "function" {
    27  				enc = enc[:10]
    28  			}
    29  			f.Encoding = enc
    30  			modelChan <- &f
    31  		}
    32  	}
    33  	return output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts())
    34  }