From bb43ba68e65a6018f2bc6fae7c1c684f5c9bb94b Mon Sep 17 00:00:00 2001 From: anilcse Date: Thu, 12 Dec 2024 16:36:53 +0530 Subject: [PATCH] Recactor display and add network, asset type wise stats --- cmd/cosmoscope/main.go | 19 +-- go.mod | 3 + go.sum | 5 + internal/cosmos/client.go | 17 +++ internal/portfolio/display.go | 244 +++++++++++++++++++++++++++------- 5 files changed, 226 insertions(+), 62 deletions(-) diff --git a/cmd/cosmoscope/main.go b/cmd/cosmoscope/main.go index 234c11f..32840ee 100644 --- a/cmd/cosmoscope/main.go +++ b/cmd/cosmoscope/main.go @@ -3,7 +3,6 @@ package main import ( "fmt" "sync" - "time" "github.com/anilcse/cosmoscope/internal/config" "github.com/anilcse/cosmoscope/internal/cosmos" @@ -14,7 +13,7 @@ import ( ) func main() { - printHeader() + portfolio.PrintHeader() // Load configuration cfg := config.Load() @@ -71,19 +70,7 @@ func main() { // Collect and display balances balances := portfolio.CollectBalances(balanceChan) - portfolio.DisplayBalances(balances) - portfolio.DisplaySummary(balances) -} -func printHeader() { - fmt.Println("\n\n\n*******************************************************************************") - fmt.Println("* *") - fmt.Println("* *") - fmt.Printf("* BALANCES REPORT (%s) *\n", time.Now().Format("2006-01-02 15:04:05")) - fmt.Println("* *") - fmt.Println("* *") - fmt.Println("*******************************************************************************") - fmt.Println("") - fmt.Println("") - fmt.Println("") + // Print the report + portfolio.PrintBalanceReport(balances) } diff --git a/go.mod b/go.mod index 0777e80..99b8118 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.21 require ( github.com/cosmos/cosmos-sdk v0.50.3 github.com/ethereum/go-ethereum v1.13.8 + github.com/fatih/color v1.15.0 github.com/olekukonko/tablewriter v0.0.5 ) @@ -23,6 +24,8 @@ require ( github.com/go-ole/go-ole v1.2.5 // indirect github.com/gorilla/websocket v1.5.0 // indirect github.com/holiman/uint256 v1.2.4 // indirect + github.com/mattn/go-colorable v0.1.13 // indirect + github.com/mattn/go-isatty v0.0.20 // indirect github.com/mattn/go-runewidth v0.0.13 // indirect github.com/mmcloughlin/addchain v0.4.0 // indirect github.com/rivo/uniseg v0.2.0 // indirect diff --git a/go.sum b/go.sum index 6306e7d..dc0592c 100644 --- a/go.sum +++ b/go.sum @@ -53,6 +53,8 @@ github.com/ethereum/c-kzg-4844 v0.4.0 h1:3MS1s4JtA868KpJxroZoepdV0ZKBp3u/O5HcZ7R github.com/ethereum/c-kzg-4844 v0.4.0/go.mod h1:VewdlzQmpT5QSrVhbBuGoCdFJkpaJlO1aQputP83wc0= github.com/ethereum/go-ethereum v1.13.8 h1:1od+thJel3tM52ZUNQwvpYOeRHlbkVFZ5S8fhi0Lgsg= github.com/ethereum/go-ethereum v1.13.8/go.mod h1:sc48XYQxCzH3fG9BcrXCOOgQk2JfZzNAmIKnceogzsA= +github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs= +github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5 h1:FtmdgXiUlNeRsoNMFlKLDt+S+6hbjVMEW6RGQ7aUf7c= github.com/fjl/memsize v0.0.0-20190710130421-bcb5799ab5e5/go.mod h1:VvhXpOYNQvB+uIk2RvXzuaQtkQJzzIx6lSBe1xv7hi0= github.com/fsnotify/fsnotify v1.6.0 h1:n+5WquG0fcWoWp6xPWfHdbskMCQaFnG6PfBrh1Ky4HY= @@ -102,6 +104,7 @@ github.com/leanovate/gopter v0.2.9 h1:fQjYxZaynp97ozCzfOyOuAGOU4aU/z37zf/tOujFk7 github.com/leanovate/gopter v0.2.9/go.mod h1:U2L/78B+KVFIx2VmW6onHJQzXtFb+p5y3y2Sh+Jxxv8= github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA= github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg= +github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= github.com/mattn/go-runewidth v0.0.9/go.mod h1:H031xJmbD/WCDINGzjvQ9THkh0rPKHF+m2gUSrubnMI= @@ -168,6 +171,8 @@ golang.org/x/mod v0.14.0/go.mod h1:hTbmBsO62+eylJbnUtE2MGJUyE7QWk4xUqPFrRgJ+7c= golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= golang.org/x/sys v0.0.0-20190916202348-b4ddaad3f8a3/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= +golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.8.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.11.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/sys v0.15.0 h1:h48lPFYpsTvQJZF4EKyI4aLHaev3CxivZmv7yZig9pc= diff --git a/internal/cosmos/client.go b/internal/cosmos/client.go index fa391e5..ba5db6c 100644 --- a/internal/cosmos/client.go +++ b/internal/cosmos/client.go @@ -348,3 +348,20 @@ func getActiveEndpoint(endpoints []RestEndpoint) string { return "" } + +func isValidJSONResponse(resp *http.Response, body []byte) bool { + contentType := resp.Header.Get("Content-Type") + if !strings.Contains(strings.ToLower(contentType), "application/json") { + return false + } + + // Check if the response looks like HTML + if strings.Contains(strings.ToLower(string(body)), " summaries[j].USDValue - }) + titleColor.Println("Network Distribution:") + table.Render() + fmt.Println() +} + +func printAssetTypes(balances []Balance) { + types := make(map[string]float64) + var totalValue float64 + + for _, b := range balances { + assetType := "Bank" + if strings.Contains(b.Network, "staking") { + assetType = "Staking" + } else if strings.Contains(b.Network, "rewards") { + assetType = "Rewards" + } else if strings.Contains(b.Network, "Fixed") { + assetType = "Fixed" + } + types[assetType] += b.USDValue + totalValue += b.USDValue + } table := tablewriter.NewWriter(os.Stdout) - table.SetHeader([]string{"Token Name", "Balance", "USD Value", "Share %"}) - table.SetBorder(true) + table.SetHeader([]string{"Type", "USD Value", "Share %"}) + table.SetAutoMergeCells(false) + table.SetRowLine(true) + + // Set all headers to bold + table.SetHeaderColor( + tablewriter.Colors{tablewriter.Bold}, + tablewriter.Colors{tablewriter.Bold}, + tablewriter.Colors{tablewriter.Bold}, + ) - for _, summary := range summaries { + for assetType, value := range types { + share := (value / totalValue) * 100 table.Append([]string{ - summary.TokenName, - utils.FormatAmount(summary.Balance, 6), - fmt.Sprintf("$%.2f", summary.USDValue), - fmt.Sprintf("%.2f%%", summary.Share), + assetType, + fmt.Sprintf("$%.2f", value), + fmt.Sprintf("%.2f%%", share), }) } - table.SetFooter([]string{"Total", "", fmt.Sprintf("$%.2f", totalValue), "100.00%"}) + titleColor.Println("Asset Types:") table.Render() } + +func truncateString(s string, length int) string { + if len(s) <= length { + return s + } + return s[:length-3] + "..." +}