show

package
v0.10.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 11, 2024 License: Apache-2.0 Imports: 21 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	Cmd = &cobra.Command{
		Use:     "show [address]",
		Short:   "Show balance and other information",
		Aliases: []string{"s"},
		Args:    cobra.MaximumNArgs(1),
		Run: func(cmd *cobra.Command, args []string) {
			cfg := cliConfig.Global()
			npa := common.GetNPASelection(cfg)

			// Determine which address to show. If an explicit argument was given, use that
			// otherwise use the default account.
			var targetAddress string
			switch {
			case len(args) >= 1:

				targetAddress = args[0]
			case npa.Account != nil:

				targetAddress = npa.Account.Address
			default:

				cobra.CheckErr("no address given and no wallet configured")
			}

			ctx := context.Background()
			c, err := connection.Connect(ctx, npa.Network)
			cobra.CheckErr(err)

			addr, _, err := common.ResolveLocalAccountOrAddress(npa.Network, targetAddress)
			cobra.CheckErr(err)

			height, err := common.GetActualHeight(
				ctx,
				c.Consensus(),
			)
			cobra.CheckErr(err)

			ownerQuery := &staking.OwnerQuery{
				Owner:  addr.ConsensusAddress(),
				Height: height,
			}

			consensusAccount, err := c.Consensus().Staking().Account(ctx, ownerQuery)
			cobra.CheckErr(err)

			fmt.Printf("Address: %s\n", addr)
			fmt.Println()
			fmt.Printf("=== CONSENSUS LAYER (%s) ===\n", npa.NetworkName)
			fmt.Printf("  Nonce: %d\n", consensusAccount.General.Nonce)
			fmt.Println()

			var (
				outgoingDelegations          map[staking.Address]*staking.DelegationInfo
				outgoingDebondingDelegations map[staking.Address][]*staking.DebondingDelegationInfo
			)
			if showDelegations {
				outgoingDelegations, err = c.Consensus().Staking().DelegationInfosFor(ctx, ownerQuery)
				cobra.CheckErr(err)
				outgoingDebondingDelegations, err = c.Consensus().Staking().DebondingDelegationInfosFor(ctx, ownerQuery)
				cobra.CheckErr(err)
			}

			prettyPrintAccountBalanceAndDelegationsFrom(
				npa.Network,
				addr,
				consensusAccount.General,
				outgoingDelegations,
				outgoingDebondingDelegations,
				"  ",
				os.Stdout,
			)

			if len(consensusAccount.General.Allowances) > 0 {
				fmt.Println("  Allowances for this Account:")
				prettyPrintAllowances(
					npa.Network,
					addr,
					consensusAccount.General.Allowances,
					"    ",
					os.Stdout,
				)
				fmt.Println()
			}

			if showDelegations {
				incomingDelegations, err := c.Consensus().Staking().DelegationsTo(ctx, ownerQuery)
				cobra.CheckErr(err)
				incomingDebondingDelegations, err := c.Consensus().Staking().DebondingDelegationsTo(ctx, ownerQuery)
				cobra.CheckErr(err)

				if len(incomingDelegations) > 0 {
					fmt.Println("  Active Delegations to this Account:")
					prettyPrintDelegationsTo(
						npa.Network,
						addr,
						consensusAccount.Escrow.Active,
						incomingDelegations,
						"    ",
						os.Stdout,
					)
					fmt.Println()
				}
				if len(incomingDebondingDelegations) > 0 {
					fmt.Println("  Debonding Delegations to this Account:")
					prettyPrintDelegationsTo(
						npa.Network,
						addr,
						consensusAccount.Escrow.Debonding,
						incomingDebondingDelegations,
						"    ",
						os.Stdout,
					)
					fmt.Println()
				}
			}

			cs := consensusAccount.Escrow.CommissionSchedule
			if len(cs.Rates) > 0 || len(cs.Bounds) > 0 {
				fmt.Println("  Commission Schedule:")
				cs.PrettyPrint(ctx, "    ", os.Stdout)
				fmt.Println()
			}

			sa := consensusAccount.Escrow.StakeAccumulator
			if len(sa.Claims) > 0 {
				fmt.Println("  Stake Accumulator:")
				sa.PrettyPrint(ctx, "    ", os.Stdout)
				fmt.Println()
			}

			if npa.ParaTime != nil {

				round := client.RoundLatest
				if h := common.GetHeight(); h != consensus.HeightLatest {
					blk, err := c.Consensus().RootHash().GetLatestBlock(
						ctx,
						&roothash.RuntimeRequest{
							RuntimeID: npa.ParaTime.Namespace(),
							Height:    height,
						},
					)
					cobra.CheckErr(err)
					round = blk.Header.Round
				}

				rtBalances, err := c.Runtime(npa.ParaTime).Accounts.Balances(ctx, round, *addr)
				cobra.CheckErr(err)

				var hasNonZeroBalance bool
				for _, balance := range rtBalances.Balances {
					if hasNonZeroBalance = !balance.IsZero(); hasNonZeroBalance {
						break
					}
				}

				nonce, err := c.Runtime(npa.ParaTime).Accounts.Nonce(ctx, round, *addr)
				cobra.CheckErr(err)
				hasNonZeroNonce := nonce > 0

				if hasNonZeroBalance || hasNonZeroNonce {
					fmt.Printf("=== %s PARATIME ===\n", npa.ParaTimeName)
					fmt.Printf("  Nonce: %d\n", nonce)
					fmt.Println()

					if hasNonZeroBalance {
						fmt.Printf("  Balances for all denominations:\n")
						for denom, balance := range rtBalances.Balances {
							fmtAmnt := helpers.FormatParaTimeDenomination(npa.ParaTime, types.NewBaseUnits(balance, denom))
							amnt, symbol, _ := strings.Cut(fmtAmnt, " ")

							fmt.Printf("  - Amount: %s\n", amnt)
							fmt.Printf("    Symbol: %s\n", symbol)
						}

						fmt.Println()
					}

					if showDelegations {
						rtDelegations, _ := c.Runtime(npa.ParaTime).ConsensusAccounts.Delegations(
							ctx,
							round,
							&consensusaccounts.DelegationsQuery{
								From: *addr,
							},
						)
						rtUndelegations, _ := c.Runtime(npa.ParaTime).ConsensusAccounts.Undelegations(
							ctx,
							round,
							&consensusaccounts.UndelegationsQuery{
								To: *addr,
							},
						)
						prettyPrintParaTimeDelegations(ctx, c, height, npa, addr, rtDelegations, rtUndelegations, "  ", os.Stdout)
					}
				}
			}
		},
	}
)

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL