Skip to content

Commit

Permalink
feat: lotus wallet market add (adds funds to storage market actor)
Browse files Browse the repository at this point in the history
  • Loading branch information
dirkmc authored and bibibong committed Jan 9, 2021
1 parent 4c6178d commit d5f4e4e
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 2 deletions.
2 changes: 2 additions & 0 deletions api/api_full.go
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,8 @@ type FullNode interface {
// along with the address removal.
MsigRemoveSigner(ctx context.Context, msig address.Address, proposer address.Address, toRemove address.Address, decrease bool) (cid.Cid, error)

// MarketAddBalance adds funds to the market actor
MarketAddBalance(ctx context.Context, wallet, addr address.Address, amt types.BigInt) (cid.Cid, error)
// MarketReserveFunds reserves funds for a deal
MarketReserveFunds(ctx context.Context, wallet address.Address, addr address.Address, amt types.BigInt) (cid.Cid, error)
// MarketReleaseFunds releases funds reserved by MarketReserveFunds
Expand Down
5 changes: 5 additions & 0 deletions api/apistruct/struct.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ type FullNodeStruct struct {
MarketReserveFunds func(ctx context.Context, wallet address.Address, addr address.Address, amt types.BigInt) (cid.Cid, error) `perm:"sign"`
MarketReleaseFunds func(ctx context.Context, addr address.Address, amt types.BigInt) error `perm:"sign"`
MarketWithdraw func(ctx context.Context, wallet, addr address.Address, amt types.BigInt) (cid.Cid, error) `perm:"sign"`
MarketAddBalance func(ctx context.Context, wallet, addr address.Address, amt types.BigInt) (cid.Cid, error) `perm:"sign"`

PaychGet func(ctx context.Context, from, to address.Address, amt types.BigInt) (*api.ChannelInfo, error) `perm:"sign"`
PaychGetWaitReady func(context.Context, cid.Cid) (address.Address, error) `perm:"sign"`
Expand Down Expand Up @@ -1223,6 +1224,10 @@ func (c *FullNodeStruct) MsigRemoveSigner(ctx context.Context, msig address.Addr
return c.Internal.MsigRemoveSigner(ctx, msig, proposer, toRemove, decrease)
}

func (c *FullNodeStruct) MarketAddBalance(ctx context.Context, wallet address.Address, addr address.Address, amt types.BigInt) (cid.Cid, error) {
return c.Internal.MarketAddBalance(ctx, wallet, addr, amt)
}

func (c *FullNodeStruct) MarketReserveFunds(ctx context.Context, wallet address.Address, addr address.Address, amt types.BigInt) (cid.Cid, error) {
return c.Internal.MarketReserveFunds(ctx, wallet, addr, amt)
}
Expand Down
72 changes: 72 additions & 0 deletions cli/wallet.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,6 +513,7 @@ var walletMarket = &cli.Command{
Usage: "Interact with market balances",
Subcommands: []*cli.Command{
walletMarketWithdraw,
walletMarketAdd,
},
}

Expand Down Expand Up @@ -810,3 +811,74 @@ var walletVoteWithdraw = &cli.Command{
return nil
},
}

var walletMarketAdd = &cli.Command{
Name: "add",
Usage: "Add funds to the Storage Market Actor",
ArgsUsage: "<amount>",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "from",
Usage: "Specify address to move funds from, otherwise it will use the default wallet address",
Aliases: []string{"f"},
},
&cli.StringFlag{
Name: "address",
Usage: "Market address to move funds to (account or miner actor address, defaults to --from address)",
Aliases: []string{"a"},
},
},
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)

// Get amount param
if !cctx.Args().Present() {
return fmt.Errorf("must pass amount to add")
}
f, err := types.ParseEPK(cctx.Args().First())
if err != nil {
return xerrors.Errorf("parsing 'amount' argument: %w", err)
}

amt := abi.TokenAmount(f)

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

// Get address param
addr := from
if cctx.String("address") != "" {
addr, err = address.NewFromString(cctx.String("address"))
if err != nil {
return xerrors.Errorf("parsing market address: %w", err)
}
}

// Add balance to market actor
fmt.Printf("Submitting Add Balance message for amount %s for address %s\n", types.EPK(amt), addr)
smsg, err := api.MarketAddBalance(ctx, from, addr, amt)
if err != nil {
return xerrors.Errorf("add balance error: %w", err)
}

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

return nil
},
}
23 changes: 23 additions & 0 deletions documentation/en/api-methods.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
* [LogList](#LogList)
* [LogSetLevel](#LogSetLevel)
* [Market](#Market)
* [MarketAddBalance](#MarketAddBalance)
* [MarketReleaseFunds](#MarketReleaseFunds)
* [MarketReserveFunds](#MarketReserveFunds)
* [MarketWithdraw](#MarketWithdraw)
Expand Down Expand Up @@ -1653,6 +1654,28 @@ Response: `{}`
## Market


### MarketAddBalance
MarketAddBalance adds funds to the market actor


Perms: sign

Inputs:
```json
[
"f01234",
"f01234",
"0"
]
```

Response:
```json
{
"/": "bafy2bzacea3wsdh6y3a36tb3skempjoxqpuyompjbmfeyf34fi3uy6uue42v4"
}
```

### MarketReleaseFunds
MarketReleaseFunds releases funds reserved by MarketReserveFunds

Expand Down
28 changes: 26 additions & 2 deletions node/impl/market/market.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,45 @@ package market
import (
"context"

"github.com/ipfs/go-cid"
"go.uber.org/fx"

"github.com/EpiK-Protocol/go-epik/chain/actors"
marketactor "github.com/EpiK-Protocol/go-epik/chain/actors/builtin/market"
"github.com/EpiK-Protocol/go-epik/chain/market"
"github.com/EpiK-Protocol/go-epik/chain/types"
"github.com/EpiK-Protocol/go-epik/node/impl/full"
"github.com/filecoin-project/go-address"

"github.com/ipfs/go-cid"
)

type MarketAPI struct {
fx.In

full.MpoolAPI
FMgr *market.FundManager
}

func (a *MarketAPI) MarketAddBalance(ctx context.Context, wallet, addr address.Address, amt types.BigInt) (cid.Cid, error) {
params, err := actors.SerializeParams(&addr)
if err != nil {
return cid.Undef, err
}

smsg, aerr := a.MpoolPushMessage(ctx, &types.Message{
To: marketactor.Address,
From: wallet,
Value: amt,
Method: marketactor.Methods.AddBalance,
Params: params,
}, nil)

if aerr != nil {
return cid.Undef, aerr
}

return smsg.Cid(), nil
}

func (a *MarketAPI) MarketReserveFunds(ctx context.Context, wallet address.Address, addr address.Address, amt types.BigInt) (cid.Cid, error) {
return a.FMgr.Reserve(ctx, wallet, addr, amt)
}
Expand Down

0 comments on commit d5f4e4e

Please sign in to comment.