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 13, 2023
1 parent 3e55842 commit 0aa6145
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 27 deletions.
75 changes: 55 additions & 20 deletions cmd/account/show/delegations.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,16 +260,14 @@ func prettyPrintParaTimeDelegations(
c connection.Connection,
height int64,
npa *common.NPASelection,
addr *types.Address,
rtDelegations []*consensusaccounts.ExtendedDelegationInfo,
rtUndelegations []*consensusaccounts.UndelegationInfo,
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))
var totalDeg quantity.Quantity
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 @@ -280,22 +278,59 @@ func prettyPrintParaTimeDelegations(

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

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

delegations = append(delegations, extendedDelegationInfo{
to: di.To,
amount: *amount,
shares: di.Shares,
var totalUndeg quantity.Quantity
undelegations := 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)
_ = totalUndeg.Add(amount)

undelegations = append(undelegations, delegationDescription{
address: udi.From.ConsensusAddress(),
self: udi.From.Equal(*addr),
amount: *amount,
shares: udi.Shares,
endTime: udi.Epoch,
})
}

innerPrefix := prefix + " "

if len(delegations) > 0 {
fmt.Fprintf(w, "%sActive Delegations from this Account:\n", prefix)
fmt.Fprintf(w, "%sTotal: %s\n", innerPrefix, helpers.FormatConsensusDenomination(npa.Network, totalDeg))
fmt.Fprintln(w)

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

fmt.Printf(" Active delegations from this Account:\n")
fmt.Printf(" Total: %s\n", helpers.FormatConsensusDenomination(npa.Network, total))
fmt.Println()
if len(undelegations) > 0 {
fmt.Fprintf(w, "%sDebonding Delegations from this Account:\n", prefix)
fmt.Fprintf(w, "%sTotal: %s\n", innerPrefix, helpers.FormatConsensusDenomination(npa.Network, totalUndeg))
fmt.Fprintln(w)

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)
sort.Sort(byEndTimeAmountAddress(undelegations))
prettyPrintDelegationDescriptions(npa.Network, undelegations, "To:", innerPrefix, w)
fmt.Fprintln(w)
}
}
16 changes: 10 additions & 6 deletions cmd/account/show/show.go
Original file line number Diff line number Diff line change
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 @@ -210,17 +210,21 @@ var (
}

if showDelegations {
rtDelegations, err := c.Runtime(npa.ParaTime).ConsensusAccounts.Delegations(
rtDelegations, _ := c.Runtime(npa.ParaTime).ConsensusAccounts.Delegations(
ctx,
round,
&consensusaccounts.DelegationsQuery{
From: *addr,
},
)
if err == nil && len(rtDelegations) > 0 {
prettyPrintParaTimeDelegations(ctx, c, height, npa, rtDelegations)
fmt.Println()
}
rtUndelegations, _ := c.Runtime(npa.ParaTime).ConsensusAccounts.Undelegations(
ctx,
round,
&consensusaccounts.UndelegationsQuery{
To: *addr,
},
)
prettyPrintParaTimeDelegations(ctx, c, height, npa, addr, rtDelegations, rtUndelegations, " ", os.Stdout)
}
}
}
Expand Down
1 change: 0 additions & 1 deletion examples/account/show-eth.out
Original file line number Diff line number Diff line change
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 0aa6145

Please sign in to comment.