Skip to content

Commit

Permalink
feat(cmd/account): Show debonding delegations in paratimes
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Sep 11, 2023
1 parent 830666d commit 1180e9b
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 22 deletions.
79 changes: 61 additions & 18 deletions cmd/account/show/delegations.go
Expand Up @@ -260,16 +260,13 @@ func prettyPrintParaTimeDelegations(
c connection.Connection,
height int64,
npa *common.NPASelection,
addr *types.Address,
rtDelegations []*consensusaccounts.ExtendedDelegationInfo,
prefix string,
w io.Writer,
) {
type extendedDelegationInfo struct {
to types.Address
amount quantity.Quantity
shares quantity.Quantity
}

var total quantity.Quantity
delegations := make([]extendedDelegationInfo, 0, len(rtDelegations))
delegations := make([]delegationDescription, 0, len(rtDelegations))
for _, di := range rtDelegations {
// For each destination we need to fetch the pool.
destAccount, err := c.Consensus().Staking().Account(ctx, &staking.OwnerQuery{
Expand All @@ -282,20 +279,66 @@ func prettyPrintParaTimeDelegations(
amount, _ := destAccount.Escrow.Active.StakeForShares(&di.Shares)
_ = total.Add(amount)

delegations = append(delegations, extendedDelegationInfo{
to: di.To,
amount: *amount,
shares: di.Shares,
delegations = append(delegations, delegationDescription{
address: di.To.ConsensusAddress(),
self: di.To.Equal(*addr),
amount: *amount,
shares: di.Shares,
endTime: beacon.EpochInvalid,
})
}

fmt.Printf(" Active delegations from this Account:\n")
fmt.Printf(" Total: %s\n", helpers.FormatConsensusDenomination(npa.Network, total))
fmt.Println()
innerPrefix := prefix + " "

fmt.Fprintf(w, "%sActive Delegations from this Account:\n", prefix)
fmt.Fprintf(w, "%sTotal: %s\n", innerPrefix, helpers.FormatConsensusDenomination(npa.Network, total))
fmt.Fprintln(w)

sort.Sort(byEndTimeAmountAddress(delegations))
prettyPrintDelegationDescriptions(npa.Network, delegations, "To:", innerPrefix, w)
fmt.Fprintln(w)
}

func prettyPrintParaTimeUndelegations(

Check failure on line 302 in cmd/account/show/delegations.go

View workflow job for this annotation

GitHub Actions / lint

302-344 lines are duplicate of `cmd/account/show/delegations.go:258-300` (dupl)
ctx context.Context,
c connection.Connection,
height int64,
npa *common.NPASelection,
addr *types.Address,
rtUndelegations []*consensusaccounts.UndelegationInfo,
prefix string,
w io.Writer,
) {
var total quantity.Quantity
delegations := make([]delegationDescription, 0, len(rtUndelegations))
for _, udi := range rtUndelegations {
// For each destination we need to fetch the pool.
destAccount, err := c.Consensus().Staking().Account(ctx, &staking.OwnerQuery{
Owner: udi.From.ConsensusAddress(),
Height: height,
})
cobra.CheckErr(err)

// Then we can compute the current amount.
amount, _ := destAccount.Escrow.Debonding.StakeForShares(&udi.Shares)
_ = total.Add(amount)

fmt.Printf(" Delegations:\n")
for _, di := range delegations {
fmt.Printf(" - To: %s\n", di.to)
fmt.Printf(" Amount: %s (%s shares)\n", helpers.FormatConsensusDenomination(npa.Network, di.amount), di.shares)
delegations = append(delegations, delegationDescription{
address: udi.From.ConsensusAddress(),
self: udi.From.Equal(*addr),
amount: *amount,
shares: udi.Shares,
endTime: udi.Epoch,
})
}

innerPrefix := prefix + " "

fmt.Fprintf(w, "%sDebonding Delegations from this Account:\n", prefix)
fmt.Fprintf(w, "%sTotal: %s\n", innerPrefix, helpers.FormatConsensusDenomination(npa.Network, total))
fmt.Fprintln(w)

sort.Sort(byEndTimeAmountAddress(delegations))
prettyPrintDelegationDescriptions(npa.Network, delegations, "To:", innerPrefix, w)
fmt.Fprintln(w)
}
16 changes: 13 additions & 3 deletions cmd/account/show/show.go
Expand Up @@ -110,6 +110,7 @@ var (
" ",
os.Stdout,
)
fmt.Println()
}

if showDelegations {
Expand Down Expand Up @@ -191,7 +192,6 @@ var (
hasNonZeroNonce := nonce > 0

if hasNonZeroBalance || hasNonZeroNonce {
fmt.Println()
fmt.Printf("=== %s PARATIME ===\n", npa.ParaTimeName)
fmt.Printf(" Nonce: %d\n", nonce)
fmt.Println()
Expand All @@ -218,8 +218,18 @@ var (
},
)
if err == nil && len(rtDelegations) > 0 {
prettyPrintParaTimeDelegations(ctx, c, height, npa, rtDelegations)
fmt.Println()
prettyPrintParaTimeDelegations(ctx, c, height, npa, addr, rtDelegations, " ", os.Stdout)
}

rtUndelegations, err := c.Runtime(npa.ParaTime).ConsensusAccounts.Undelegations(
ctx,
round,
&consensusaccounts.UndelegationsQuery{
To: *addr,
},
)
if err == nil && len(rtUndelegations) > 0 {
prettyPrintParaTimeUndelegations(ctx, c, height, npa, addr, rtUndelegations, " ", os.Stdout)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion examples/account/show-eth.out
Expand Up @@ -6,7 +6,6 @@ Address: oasis1qzplmfaeywvtc2qnylyhk0uzcxr4y5s3euhaug7q
Total: 0.0 TEST
Available: 0.0 TEST


=== sapphire PARATIME ===
Nonce: 0

Expand Down

0 comments on commit 1180e9b

Please sign in to comment.