TrueBlocks / trueblocks-core

The main repository for the TrueBlocks system
https://trueblocks.io
GNU General Public License v3.0
1.04k stars 194 forks source link

chifra names - create an example using Names to add, edit names. #3770

Closed tjayrush closed 3 weeks ago

tjayrush commented 3 weeks ago

This code can be modified to use the SDK

package main

import (
    "bufio"
    "fmt"
    "os"
    "os/exec"
    "strings"
)

const (
    defSource   = "EtherScan.io"
    defDecimals = ""
    defTag      = "30-Contracts"
)

func readInput(prompt string, defaultValue string) string {
    reader := bufio.NewReader(os.Stdin)
    fmt.Print(prompt)
    input, _ := reader.ReadString('\n')
    input = strings.TrimSpace(input)
    if input == "" {
        return defaultValue
    }
    return input
}

func main() {
    var address, name, tag, source, symbol, decimals, descr string

    if len(os.Args) > 1 {
        address = os.Args[1]
        fmt.Println("Enter an address: " + address)
    } else {
        address = readInput("Enter an address: ", "")
    }

    if len(os.Args) > 2 {
        name = os.Args[2]
        fmt.Println("Enter a name: " + name)
    } else {
        name = readInput("Enter a name: ", "")
    }

    if len(os.Args) > 3 {
        tag = os.Args[3]
        fmt.Println("Enter a tag (def: 30-Contracts): " + tag)
    } else {
        tag = readInput("Enter a tag (def: 30-Contracts): ", defTag)
    }

    if len(os.Args) > 4 {
        source = os.Args[4]
        fmt.Println("Enter a source (def: EtherScan.io): " + source)
    } else {
        source = readInput("Enter a source (def: EtherScan.io): ", defSource)
    }

    if len(os.Args) > 5 {
        symbol = os.Args[5]
    }

    if len(os.Args) > 6 {
        decimals = os.Args[6]
    } else {
        decimals = defDecimals
    }

    if len(os.Args) > 7 {
        descr = os.Args[7]
    }

    fmt.Println("---------------------- Adding -----------------------")
    fmt.Printf("Address: %s\n", address)
    fmt.Printf("Name: %s\n", name)
    fmt.Printf("TAG: %s\n", tag)
    fmt.Printf("SOURCE: %s\n", source)
    fmt.Printf("DECIMALS: %s\n", decimals)
    fmt.Printf("SYMBOL: %s\n", symbol)
    fmt.Printf("DESCR: %s\n", descr)

    os.Setenv("TB_NAME_ADDRESS", address)
    os.Setenv("TB_NAME_NAME", name)
    os.Setenv("TB_NAME_TAG", tag)
    os.Setenv("TB_NAME_SOURCE", source)
    os.Setenv("TB_NAME_SYMBOL", symbol)
    os.Setenv("TB_NAME_DECIMALS", decimals)
    os.Setenv("TB_NAME_DESCR", descr)

    exec.Command("/Users/your_user/.local/bin/chifra/ethNames", "--create").Run()
    exec.Command("cp", "-pf", os.Getenv("HOME")+"/Library/Application Support/TrueBlocks/names/names.tab", os.Getenv("HOME")+"/Development/trueblocks-core/src/other/install/names/names.tab").Run()
    exec.Command("/Users/your_user/.local/bin/chifra", "names", "--all", "--expand", address).Run()
}
tjayrush commented 3 weeks ago

Done.