Algorand tools

Check-in [028e995e40]
Login
Overview
Comment:cmd/algo: attempt, not yet working, to show assets held
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 028e995e40100f2457b752b31f793e2ecc1f8995c1724ec4dd3a15eb8635ab69
User & Date: dnc 2020-01-13 12:26:42
Context
2020-01-13
20:36
cmd/algo: fix assettransfer usage check-in: a8ce0b5300 user: dnc tags: trunk
12:28
Create new branch named "json-pipeline" check-in: 8e95501b31 user: dnc tags: json-pipeline
12:26
cmd/algo: attempt, not yet working, to show assets held check-in: 028e995e40 user: dnc tags: trunk
12:24
helper functions to create new amounts check-in: d621e25489 user: dnc tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to cmd/algo/account.go.

16
17
18
19
20
21
22



23
24
25
26
27
28
29
..
51
52
53
54
55
56
57
58
59


60

61






62
63
64

65
66

67

















68




69
70


























71














72
73
74
75
76
77
78
79
80
package main

import (
	"fmt"
	"os"
	"text/tabwriter"




	"github.com/pkg/errors"
	"src.d10.dev/algo"
	"src.d10.dev/command"
)

func init() {

................................................................................
	if age.Hours() > .5 {
		command.Errorf("algod is out of sync (%s)", age)
		// not fatal
	}

	command.V(1).Infof("connected to %s (%q) at block %d (%s ago)", params.GenesisID, algodAddress(), block.Round, age)

	table := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
	fmt.Fprintln(table, "Address\t Algos\t Status\t")


	// TODO(dnc): concurrency for performance

	for _, arg := range argument {






		account, err := client.AccountInformation(algo.AddressFromArg(arg))
		if err != nil {
			command.Error(err)

			continue
		}



















		// TODO(dnc): what is account.Round?





		algos := float64(account.Amount) / 1_000_000


























		fmt.Fprintf(table, "%s\t %f\t %s\t\n", account.Address, algos, account.Status) // TODO(dnc): better align decimal numbers














	}

	table.Flush()
	fmt.Println("")

	_ = status

	return nil
}







>
>
>







 







|
|
>
>

>

>
>
>
>
>
>
|

<
>


>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
|
<
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>









16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
..
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75

76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103

104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
package main

import (
	"fmt"
	"os"
	"text/tabwriter"

	"github.com/algorand/go-algorand-sdk/client/algod/models"
	"github.com/algorand/go-algorand-sdk/types"
	"github.com/kr/pretty"
	"github.com/pkg/errors"
	"src.d10.dev/algo"
	"src.d10.dev/command"
)

func init() {

................................................................................
	if age.Hours() > .5 {
		command.Errorf("algod is out of sync (%s)", age)
		// not fatal
	}

	command.V(1).Infof("connected to %s (%q) at block %d (%s ago)", params.GenesisID, algodAddress(), block.Round, age)

	var account []models.Account // preserve order of args
	var assetOrder []uint64
	asset := make(map[uint64]models.AssetParams)

	// TODO(dnc): concurrency for performance
	// gather account details
	for _, arg := range argument {
		addr := algo.AddressFromArg(arg)
		_, err := types.DecodeAddress(addr)
		if err != nil {
			command.Error(fmt.Errorf("not an address (%q): %w", addr, err))
			continue
		}
		response, err := client.AccountInformation(addr)
		if err != nil {

			command.Error(fmt.Errorf("failed to query address (%q): %w", addr, err))
			continue
		}
		account = append(account, response)

		for idx, ap := range response.AssetParams {
			// here only if account is *creator* of asset
			_, ok := asset[idx]
			if !ok {
				asset[idx] = ap
				assetOrder = append(assetOrder, idx)
			}
		}

		pretty.Log(response) // troubleshooting
	}

	// gather asset detail
	for _, acct := range account {
		for idx, _ := range acct.Assets {
			_, ok := asset[idx]
			if !ok {
				// TODO(dnc): concurrency for performance
				response, err := client.AssetInformation(idx)
				command.Check(err)
				asset[idx] = response
				assetOrder = append(assetOrder, idx)
			}

		}
	}

	// helper
	unitName := func(idx uint64) string {
		n := asset[idx].UnitName
		if n == "" {
			n = asset[idx].AssetName
		}
		if n == "" {
			n = fmt.Sprintf("asset:%d", idx)
		}
		return n
	}

	// tabular output
	table := tabwriter.NewWriter(os.Stdout, 0, 0, 1, ' ', 0)
	header := "Address\t Algos\t Status\t"
	for _, idx := range assetOrder {
		header = header + fmt.Sprintf(" %s\t", unitName(idx))
	}
	fmt.Fprintln(table, header)

	for _, acct := range account {

		algos := algo.NewAlgoAmount(acct.Amount)
		row := fmt.Sprintf("%s\t %s\t %s\t", acct.Address, algos, acct.Status) // TODO(dnc): better align decimal numbers

		// TODO(dnc): also display acct.Round?

		for _, idx := range assetOrder {
			q, ok := acct.Assets[idx]
			if ok {
				amt := algo.NewAssetAmount(q.Amount, asset[idx].Decimals)
				row = row + fmt.Sprintf(" %s\t", amt.String()) // TODO(dnc): show q.Frozen
			} else {
				// don't show "0", because user may not have whitelisted the asset
				row = row + "  \t"
			}
		}
		fmt.Fprintln(table, row)
	}

	table.Flush()
	fmt.Println("")

	_ = status

	return nil
}