add a command to check wallet balance
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/chain"
|
"github.com/filecoin-project/go-lotus/chain"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
"github.com/ipfs/go-filestore"
|
"github.com/ipfs/go-filestore"
|
||||||
@@ -72,6 +73,7 @@ type API interface {
|
|||||||
|
|
||||||
WalletNew(context.Context, string) (address.Address, error)
|
WalletNew(context.Context, string) (address.Address, error)
|
||||||
WalletList(context.Context) ([]address.Address, error)
|
WalletList(context.Context) ([]address.Address, error)
|
||||||
|
WalletBalance(context.Context, address.Address) (types.BigInt, error)
|
||||||
// // import
|
// // import
|
||||||
// // export
|
// // export
|
||||||
// // (on cli - cmd to list associations)
|
// // (on cli - cmd to list associations)
|
||||||
|
|||||||
+8
-2
@@ -5,6 +5,7 @@ import (
|
|||||||
|
|
||||||
"github.com/filecoin-project/go-lotus/chain"
|
"github.com/filecoin-project/go-lotus/chain"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
|
|
||||||
"github.com/ipfs/go-cid"
|
"github.com/ipfs/go-cid"
|
||||||
"github.com/libp2p/go-libp2p-core/peer"
|
"github.com/libp2p/go-libp2p-core/peer"
|
||||||
@@ -25,8 +26,9 @@ type Struct struct {
|
|||||||
MinerStart func(context.Context, address.Address) error
|
MinerStart func(context.Context, address.Address) error
|
||||||
MinerCreateBlock func(context.Context, address.Address, *chain.TipSet, []chain.Ticket, chain.ElectionProof, []*chain.SignedMessage) (*chain.BlockMsg, error)
|
MinerCreateBlock func(context.Context, address.Address, *chain.TipSet, []chain.Ticket, chain.ElectionProof, []*chain.SignedMessage) (*chain.BlockMsg, error)
|
||||||
|
|
||||||
WalletNew func(context.Context, string) (address.Address, error)
|
WalletNew func(context.Context, string) (address.Address, error)
|
||||||
WalletList func(context.Context) ([]address.Address, error)
|
WalletList func(context.Context) ([]address.Address, error)
|
||||||
|
WalletBalance func(context.Context, address.Address) (types.BigInt, error)
|
||||||
|
|
||||||
ClientImport func(ctx context.Context, path string) (cid.Cid, error)
|
ClientImport func(ctx context.Context, path string) (cid.Cid, error)
|
||||||
ClientListImports func(ctx context.Context) ([]Import, error)
|
ClientListImports func(ctx context.Context) ([]Import, error)
|
||||||
@@ -99,4 +101,8 @@ func (c *Struct) WalletList(ctx context.Context) ([]address.Address, error) {
|
|||||||
return c.Internal.WalletList(ctx)
|
return c.Internal.WalletList(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (c *Struct) WalletBalance(ctx context.Context, a address.Address) (types.BigInt, error) {
|
||||||
|
return c.Internal.WalletBalance(ctx, a)
|
||||||
|
}
|
||||||
|
|
||||||
var _ API = &Struct{}
|
var _ API = &Struct{}
|
||||||
|
|||||||
@@ -507,3 +507,24 @@ func (cs *ChainStore) LoadMessagesFromCids(cids []cid.Cid) ([]*SignedMessage, er
|
|||||||
|
|
||||||
return msgs, nil
|
return msgs, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (cs *ChainStore) GetBalance(addr address.Address) (types.BigInt, error) {
|
||||||
|
ts := cs.GetHeaviestTipSet()
|
||||||
|
stcid, err := cs.TipSetState(ts.Cids())
|
||||||
|
if err != nil {
|
||||||
|
return types.BigInt{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
cst := hamt.CSTFromBstore(cs.bs)
|
||||||
|
state, err := LoadStateTree(cst, stcid)
|
||||||
|
if err != nil {
|
||||||
|
return types.BigInt{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
act, err := state.GetActor(addr)
|
||||||
|
if err != nil {
|
||||||
|
return types.BigInt{}, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return act.Balance, nil
|
||||||
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package cli
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
"gopkg.in/urfave/cli.v2"
|
"gopkg.in/urfave/cli.v2"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -12,6 +13,7 @@ var walletCmd = &cli.Command{
|
|||||||
Subcommands: []*cli.Command{
|
Subcommands: []*cli.Command{
|
||||||
walletNew,
|
walletNew,
|
||||||
walletList,
|
walletList,
|
||||||
|
walletBalance,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,3 +64,28 @@ var walletList = &cli.Command{
|
|||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var walletBalance = &cli.Command{
|
||||||
|
Name: "balance",
|
||||||
|
Usage: "get account balance",
|
||||||
|
Action: func(cctx *cli.Context) error {
|
||||||
|
api, err := getAPI(cctx)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
ctx := reqContext(cctx)
|
||||||
|
|
||||||
|
addr, err := address.NewFromString(cctx.Args().First())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
balance, err := api.WalletBalance(ctx, addr)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
fmt.Printf("%s\n", balance.String())
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|||||||
@@ -66,6 +66,7 @@ require (
|
|||||||
go4.org v0.0.0-20190313082347-94abd6928b1d // indirect
|
go4.org v0.0.0-20190313082347-94abd6928b1d // indirect
|
||||||
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
|
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
|
||||||
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
|
golang.org/x/xerrors v0.0.0-20190513163551-3ee3066db522
|
||||||
|
google.golang.org/appengine v1.4.0 // indirect
|
||||||
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
|
gopkg.in/urfave/cli.v2 v2.0.0-20180128182452-d3ae77c26ac8
|
||||||
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
|
launchpad.net/gocheck v0.0.0-20140225173054-000000000087 // indirect
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@ import (
|
|||||||
"github.com/filecoin-project/go-lotus/build"
|
"github.com/filecoin-project/go-lotus/build"
|
||||||
"github.com/filecoin-project/go-lotus/chain"
|
"github.com/filecoin-project/go-lotus/chain"
|
||||||
"github.com/filecoin-project/go-lotus/chain/address"
|
"github.com/filecoin-project/go-lotus/chain/address"
|
||||||
|
"github.com/filecoin-project/go-lotus/chain/types"
|
||||||
"github.com/filecoin-project/go-lotus/miner"
|
"github.com/filecoin-project/go-lotus/miner"
|
||||||
"github.com/filecoin-project/go-lotus/node/client"
|
"github.com/filecoin-project/go-lotus/node/client"
|
||||||
|
|
||||||
@@ -113,6 +114,10 @@ func (a *API) WalletList(ctx context.Context) ([]address.Address, error) {
|
|||||||
return a.Wallet.ListAddrs()
|
return a.Wallet.ListAddrs()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *API) WalletBalance(ctx context.Context, addr address.Address) (types.BigInt, error) {
|
||||||
|
return a.Chain.GetBalance(addr)
|
||||||
|
}
|
||||||
|
|
||||||
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
func (a *API) NetConnect(ctx context.Context, p peer.AddrInfo) error {
|
||||||
return a.Host.Connect(ctx, p)
|
return a.Host.Connect(ctx, p)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user