github.com/TrueBlocks/trueblocks-core/src/apps/chifra@v0.0.0-20241022031540-b362680128f7/internal/names/handle_autoname.go (about) 1 package namesPkg 2 3 import ( 4 "fmt" 5 6 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/base" 7 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/logger" 8 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/names" 9 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/output" 10 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/types" 11 "github.com/TrueBlocks/trueblocks-core/src/apps/chifra/pkg/utils" 12 ) 13 14 func (opts *NamesOptions) HandleAutoname(rCtx *output.RenderCtx) error { 15 name, err := opts.readContractAndClean("") 16 if err != nil { 17 return err 18 } else if name == nil { 19 latest := opts.Conn.GetLatestBlockNumber() 20 proxy, err := opts.Conn.GetContractProxyAt(opts.AutonameAddr, latest) 21 if err != nil { 22 return err 23 } 24 if proxy != base.ZeroAddr { 25 opts.AutonameAddr = proxy 26 name, err = opts.readContractAndClean("proxy") 27 if err != nil { 28 return err 29 } 30 } 31 } 32 33 message := "No name has been updated. Is the contract a token?" 34 if name != nil { 35 message = fmt.Sprintf( 36 "Updated name for %s, %s. ERC-20 token: %t, ERC-721 NFT: %t, Symbol: %s, Decimals: %d", 37 name.Address, 38 name.Name, 39 name.IsErc20, 40 name.IsErc721, 41 name.Symbol, 42 name.Decimals, 43 ) 44 } 45 46 if !utils.IsFuzzing() { 47 logger.Info(message) 48 } 49 50 if opts.Globals.IsApiMode() { 51 fetchData := func(modelChan chan types.Modeler, errorChan chan error) { 52 modelChan <- &types.Message{ 53 Msg: message, 54 } 55 } 56 _ = output.StreamMany(rCtx, fetchData, opts.Globals.OutputOpts()) 57 } 58 return nil 59 } 60 61 // readContractAndClean will read contract data and call `cleanName` for the given address 62 func (opts *NamesOptions) readContractAndClean(s string) (name *types.Name, err error) { 63 chain := opts.Globals.Chain 64 65 name = &types.Name{ 66 Address: opts.AutonameAddr, 67 Source: "TrueBlocks.io", 68 IsCustom: true, 69 } 70 if _, err = cleanName(chain, name); err != nil { 71 err = fmt.Errorf("autoname %s: %w", opts.Autoname, err) 72 return 73 } 74 75 if !name.IsErc20 && !name.IsErc721 { 76 a := "address" 77 if s != "" { 78 a = s 79 } 80 logger.Warn(a, name.Address, "is not a token, ignoring...") 81 name = nil 82 return 83 } 84 85 if _, err = names.LoadNamesMap(chain, types.Custom, []string{}); err != nil { 86 return 87 } 88 89 if err = names.CreateName(names.DatabaseCustom, chain, name); err != nil { 90 err = fmt.Errorf("while updating %s: %w", opts.Autoname, err) 91 return 92 } 93 94 return 95 }