Skip to content

Commit

Permalink
Merge pull request #4524 from filecoin-project/feat/withdraw-market-f…
Browse files Browse the repository at this point in the history
…unds-cli

Add a market WithdrawBalance CLI
  • Loading branch information
magik6k authored Oct 23, 2020
2 parents 2c3d804 + 6842c03 commit 7d9e8f9
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions cli/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ import (
"golang.org/x/xerrors"

"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/go-state-types/crypto"
"github.com/filecoin-project/specs-actors/actors/builtin/market"
"github.com/filecoin-project/specs-actors/v2/actors/builtin"

"github.com/filecoin-project/lotus/chain/actors"
types "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/lib/tablewriter"
)
Expand All @@ -34,6 +38,7 @@ var walletCmd = &cli.Command{
walletSign,
walletVerify,
walletDelete,
walletMarket,
},
}

Expand Down Expand Up @@ -471,3 +476,94 @@ var walletDelete = &cli.Command{
return api.WalletDelete(ctx, addr)
},
}

var walletMarket = &cli.Command{
Name: "market",
Usage: "Interact with market balances",
Subcommands: []*cli.Command{
walletMarketWithdraw,
},
}

var walletMarketWithdraw = &cli.Command{
Name: "withdraw",
Usage: "Withdraw funds from the Storage Market Actor",
ArgsUsage: "[amount (FIL) optional, otherwise will withdraw max available]",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from",
Usage: "Specify address to withdraw funds from, otherwise it will use the default wallet address",
Aliases: []string{"f"},
},
},
Action: func(cctx *cli.Context) error {
api, closer, err := GetFullNodeAPI(cctx)
if err != nil {
return xerrors.Errorf("getting node API: %w", err)
}
defer closer()
ctx := ReqContext(cctx)

var addr address.Address
if cctx.String("from") != "" {
addr, err = address.NewFromString(cctx.String("from"))
if err != nil {
return xerrors.Errorf("parsing from address: %w", err)
}
} else {
addr, err = api.WalletDefaultAddress(ctx)
if err != nil {
return xerrors.Errorf("getting default wallet address: %w", err)
}
}

bal, err := api.StateMarketBalance(ctx, addr, types.EmptyTSK)
if err != nil {
return xerrors.Errorf("getting market balance for address %s: %w", addr.String(), err)
}

avail := big.Subtract(bal.Escrow, bal.Locked)
amt := avail

if cctx.Args().Present() {
f, err := types.ParseFIL(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing 'amount' argument: %w", err)
}

amt = abi.TokenAmount(f)
}

if amt.GreaterThan(avail) {
return xerrors.Errorf("can't withdraw more funds than available; requested: %s; available: %s", amt, avail)
}

if avail.IsZero() {
return xerrors.Errorf("zero unlocked funds available to withdraw")
}

params, err := actors.SerializeParams(&market.WithdrawBalanceParams{
ProviderOrClientAddress: addr,
Amount: amt,
})
if err != nil {
return xerrors.Errorf("serializing params: %w", err)
}

fmt.Printf("Submitting WithdrawBalance message for amount %s for address %s\n", types.FIL(amt), addr.String())
smsg, err := api.MpoolPushMessage(ctx, &types.Message{
To: builtin.StorageMarketActorAddr,
From: addr,
Value: types.NewInt(0),
Method: builtin.MethodsMarket.WithdrawBalance,
Params: params,
}, nil)
if err != nil {
return xerrors.Errorf("submitting WithdrawBalance message: %w", err)
}

fmt.Printf("WithdrawBalance message cid: %s\n", smsg.Cid())

return nil
},
}

0 comments on commit 7d9e8f9

Please sign in to comment.