Skip to content

Commit

Permalink
cmd/pool: add sats_per_vbyte to account new
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveblue committed Sep 30, 2021
1 parent 68a38b3 commit e502ffd
Showing 1 changed file with 58 additions and 15 deletions.
73 changes: 58 additions & 15 deletions cmd/pool/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ var newAccountCommand = cli.Command{
"within",
Value: defaultFundingConfTarget,
},
cli.Uint64Flag{
Name: "sat_per_vbyte",
Usage: "the fee rate expressed in sat/vbyte that " +
"should be used for the account creation transaction",
},
cli.BoolFlag{
Name: "force",
Usage: "skip account fee confirmation",
Expand All @@ -119,10 +124,37 @@ func newAccount(ctx *cli.Context) error {

req := &poolrpc.InitAccountRequest{
AccountValue: amt,
Fees: &poolrpc.InitAccountRequest_ConfTarget{
Initiator: defaultInitiator,
}

satPerVByte := ctx.Uint64("sat_per_vbyte")
confTarget := ctx.Uint64("conf_target")

switch {
case satPerVByte > 0:
// Enforce a minimum fee rate of 253 sat/kw by rounding up if 1
// sat/byte is used.
feeRate := chainfee.SatPerKVByte(satPerVByte * 1000).FeePerKWeight()
if feeRate < chainfee.FeePerKwFloor {
feeRate = chainfee.FeePerKwFloor
}
req.Fees = &poolrpc.InitAccountRequest_FeeRateSatPerKw{
FeeRateSatPerKw: uint64(feeRate),
}

case ctx.IsSet("conf_target"):
if confTarget == 0 {
return fmt.Errorf("specified confirmation target must be " +
"greater than 0")
}

req.Fees = &poolrpc.InitAccountRequest_ConfTarget{
ConfTarget: uint32(ctx.Uint64("conf_target")),
},
Initiator: defaultInitiator,
}

default:
return fmt.Errorf("either sat/vbyte or confirmation target " +
"must be specified")
}

// Parse the expiry in either of its forms. We'll always prefer the
Expand Down Expand Up @@ -150,8 +182,10 @@ func newAccount(ctx *cli.Context) error {
// present it to the user.
if !ctx.Bool("force") {
if err := printAccountFees(
client, btcutil.Amount(amt),
uint32(ctx.Uint64("conf_target")),
client,
btcutil.Amount(amt),
satPerVByte,
uint32(confTarget),
); err != nil {
return err
}
Expand All @@ -173,27 +207,36 @@ func newAccount(ctx *cli.Context) error {
}

func printAccountFees(client poolrpc.TraderClient, amt btcutil.Amount,
confTarget uint32) error {
satPerVByte uint64, confTarget uint32) error {

req := &poolrpc.QuoteAccountRequest{
AccountValue: uint64(amt),
Fees: &poolrpc.QuoteAccountRequest_ConfTarget{
ConfTarget: confTarget,
},
if confTarget == 0 {
fmt.Println("-- Account Funding Details --")
fmt.Printf("Amount: %v\n", amt)
fmt.Printf("Fee rate (estimated): %d sat/vByte\n", satPerVByte)

return nil
}
resp, err := client.QuoteAccount(context.Background(), req)

resp, err := client.QuoteAccount(
context.Background(),
&poolrpc.QuoteAccountRequest{
AccountValue: uint64(amt),
Fees: &poolrpc.QuoteAccountRequest_ConfTarget{
ConfTarget: confTarget,
},
},
)
if err != nil {
return fmt.Errorf("unable to estimate on-chain fees: "+
"%v", err)
}

feeRate := chainfee.SatPerKWeight(resp.MinerFeeRateSatPerKw)
satPerVByte := float64(feeRate.FeePerKVByte()) / 1000

feePerKVByte := float64(feeRate.FeePerKVByte()) / 1000
fmt.Println("-- Account Funding Details --")
fmt.Printf("Amount: %v\n", amt)
fmt.Printf("Confirmation target: %v blocks\n", confTarget)
fmt.Printf("Fee rate (estimated): %.1f sat/vByte\n", satPerVByte)
fmt.Printf("Fee rate (estimated): %.1f sat/vByte\n", feePerKVByte)
fmt.Printf("Total miner fee (estimated): %v\n",
btcutil.Amount(resp.MinerFeeTotal))

Expand Down

0 comments on commit e502ffd

Please sign in to comment.