Skip to content

Commit de21d03

Browse files
authored
feat(shed): lotus-shed miner list-balances (#12984)
1 parent 2a6935a commit de21d03

File tree

1 file changed

+57
-0
lines changed

1 file changed

+57
-0
lines changed

cmd/lotus-shed/miner.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ import (
4242
"github.com/filecoin-project/lotus/chain/state"
4343
"github.com/filecoin-project/lotus/chain/types"
4444
lcli "github.com/filecoin-project/lotus/cli"
45+
"github.com/filecoin-project/lotus/lib/must"
4546
)
4647

4748
var minerCmd = &cli.Command{
@@ -57,6 +58,7 @@ var minerCmd = &cli.Command{
5758
minerLockedVestedCmd,
5859
minerListVestingCmd,
5960
minerFeesCmd,
61+
minerListBalancesCmd,
6062
},
6163
}
6264

@@ -870,3 +872,58 @@ var minerListVestingCmd = &cli.Command{
870872
return nil
871873
},
872874
}
875+
876+
var minerListBalancesCmd = &cli.Command{
877+
Name: "list-balances",
878+
Usage: "List the balances of all miners with power",
879+
Action: func(cctx *cli.Context) error {
880+
n, acloser, err := lcli.GetFullNodeAPI(cctx)
881+
if err != nil {
882+
return err
883+
}
884+
defer acloser()
885+
ctx := lcli.ReqContext(cctx)
886+
887+
bs := ReadOnlyAPIBlockstore{n}
888+
adtStore := adt.WrapStore(ctx, ipldcbor.NewCborStore(&bs))
889+
890+
head, err := n.ChainHead(ctx)
891+
if err != nil {
892+
return err
893+
}
894+
895+
powerActor, err := n.StateGetActor(ctx, power.Address, head.Key())
896+
if err != nil {
897+
return err
898+
}
899+
powerState, err := power.Load(adtStore, powerActor)
900+
if err != nil {
901+
return err
902+
}
903+
fmt.Printf("Miner Address,QAP (bytes),Available Balance (FIL)\n")
904+
var lowest *big.Int
905+
err = powerState.ForEachClaim(func(minerAddr address.Address, claim power.Claim) error {
906+
actor, err := n.StateGetActor(ctx, minerAddr, head.Key())
907+
if err != nil {
908+
return err
909+
}
910+
minerState, err := miner.Load(adtStore, actor)
911+
if err != nil {
912+
return err
913+
}
914+
balance := must.One(minerState.AvailableBalance(actor.Balance))
915+
if lowest == nil || balance.LessThan(*lowest) {
916+
lowest = &balance
917+
}
918+
fmt.Printf("%s,%s,%s\n", minerAddr, claim.QualityAdjPower, strings.Replace(types.FIL(balance).String(), " FIL", "", 1))
919+
return nil
920+
}, true)
921+
if err != nil {
922+
return err
923+
}
924+
if lowest != nil {
925+
_, _ = fmt.Fprintf(cctx.App.ErrWriter, "Lowest balance: %s\n", types.FIL(*lowest))
926+
}
927+
return nil
928+
},
929+
}

0 commit comments

Comments
 (0)