Skip to content

Commit

Permalink
Add fee estimation to wallet
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Sep 4, 2024
1 parent 48a142e commit 0c449a9
Show file tree
Hide file tree
Showing 25 changed files with 5,551 additions and 1,503 deletions.
4 changes: 4 additions & 0 deletions app/appmessage/message.go
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,8 @@ const (
CmdGetMempoolEntriesByAddressesResponseMessage
CmdGetCoinSupplyRequestMessage
CmdGetCoinSupplyResponseMessage
CmdGetFeeEstimateRequestMessage
CmdGetFeeEstimateResponseMessage
)

// ProtocolMessageCommandToString maps all MessageCommands to their string representation
Expand Down Expand Up @@ -300,6 +302,8 @@ var RPCMessageCommandToString = map[MessageCommand]string{
CmdGetMempoolEntriesByAddressesResponseMessage: "GetMempoolEntriesByAddressesResponse",
CmdGetCoinSupplyRequestMessage: "GetCoinSupplyRequest",
CmdGetCoinSupplyResponseMessage: "GetCoinSupplyResponse",
CmdGetFeeEstimateRequestMessage: "GetFeeEstimateRequest",
CmdGetFeeEstimateResponseMessage: "GetFeeEstimateResponse",
}

// Message is an interface that describes a kaspa message. A type that
Expand Down
47 changes: 47 additions & 0 deletions app/appmessage/rpc_fee_estimate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package appmessage

// GetFeeEstimateRequestMessage is an appmessage corresponding to
// its respective RPC message
type GetFeeEstimateRequestMessage struct {
baseMessage
}

// Command returns the protocol command string for the message
func (msg *GetFeeEstimateRequestMessage) Command() MessageCommand {
return CmdGetFeeEstimateRequestMessage
}

// NewGetFeeEstimateRequestMessage returns a instance of the message
func NewGetFeeEstimateRequestMessage() *GetFeeEstimateRequestMessage {
return &GetFeeEstimateRequestMessage{}
}

type RPCFeeRateBucket struct {
Feerate float64
EstimatedSeconds float64
}

type RPCFeeEstimate struct {
PriorityBucket RPCFeeRateBucket
NormalBuckets []RPCFeeRateBucket
LowBuckets []RPCFeeRateBucket
}

// GetCoinSupplyResponseMessage is an appmessage corresponding to
// its respective RPC message
type GetFeeEstimateResponseMessage struct {
baseMessage
Estimate RPCFeeEstimate

Error *RPCError
}

// Command returns the protocol command string for the message
func (msg *GetFeeEstimateResponseMessage) Command() MessageCommand {
return CmdGetFeeEstimateResponseMessage
}

// NewGetFeeEstimateResponseMessage returns a instance of the message
func NewGetFeeEstimateResponseMessage() *GetFeeEstimateResponseMessage {
return &GetFeeEstimateResponseMessage{}
}
33 changes: 32 additions & 1 deletion cmd/kaspawallet/config.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
package main

import (
"os"

"github.com/kaspanet/kaspad/infrastructure/config"
"github.com/pkg/errors"
"os"

"github.com/jessevdk/go-flags"
)
Expand Down Expand Up @@ -62,6 +63,8 @@ type sendConfig struct {
SendAmount string `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount). If --from-address was used, will send all only from the specified addresses."`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
MaxFeeRate float64 `long:"max-fee-rate" short:"m" description:"Maximum fee rate in Sompi/gram to use for the transaction. The wallet will take the maximum between the fee estimate from the connected node and this value."`
FeeRate float64 `long:"fee-rate" short:"r" description:"Fee rate in Sompi/gram to use for the transaction. This option will override any fee estimate from the connected node."`
Verbose bool `long:"show-serialized" short:"s" description:"Show a list of hex encoded sent transactions"`
config.NetworkFlags
}
Expand All @@ -79,6 +82,8 @@ type createUnsignedTransactionConfig struct {
SendAmount string `long:"send-amount" short:"v" description:"An amount to send in Kaspa (e.g. 1234.12345678)"`
IsSendAll bool `long:"send-all" description:"Send all the Kaspa in the wallet (mutually exclusive with --send-amount)"`
UseExistingChangeAddress bool `long:"use-existing-change-address" short:"u" description:"Will use an existing change address (in case no change address was ever used, it will use a new one)"`
MaxFeeRate float64 `long:"max-fee-rate" short:"m" description:"Maximum fee rate in Sompi/gram to use for the transaction. The wallet will take the maximum between the fee estimate from the connected node and this value."`
FeeRate float64 `long:"fee-rate" short:"r" description:"Fee rate in Sompi/gram to use for the transaction. This option will override any fee estimate from the connected node."`
config.NetworkFlags
}

Expand Down Expand Up @@ -316,6 +321,19 @@ func validateCreateUnsignedTransactionConf(conf *createUnsignedTransactionConfig

return errors.New("exactly one of '--send-amount' or '--all' must be specified")
}

if conf.MaxFeeRate < 0 {
return errors.New("--max-fee-rate must be a positive number")
}

if conf.FeeRate < 0 {
return errors.New("--fee-rate must be a positive number")
}

if conf.MaxFeeRate > 0 && conf.FeeRate > 0 {
return errors.New("at most one of '--max-fee-rate' or '--fee-rate' can be specified")
}

return nil
}

Expand All @@ -325,6 +343,19 @@ func validateSendConfig(conf *sendConfig) error {

return errors.New("exactly one of '--send-amount' or '--all' must be specified")
}

if conf.MaxFeeRate < 0 {
return errors.New("--max-fee-rate must be a positive number")
}

if conf.FeeRate < 0 {
return errors.New("--fee-rate must be a positive number")
}

if conf.MaxFeeRate > 0 && conf.FeeRate > 0 {
return errors.New("at most one of '--max-fee-rate' or '--fee-rate' can be specified")
}

return nil
}

Expand Down
11 changes: 11 additions & 0 deletions cmd/kaspawallet/create_unsigned_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"context"
"fmt"
"math"
"os"

"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/client"
Expand All @@ -26,12 +27,22 @@ func createUnsignedTransaction(conf *createUnsignedTransactionConfig) error {
return err
}

feeRate := &pb.FeeRate{
FeeRate: &pb.FeeRate_Max{Max: math.MaxFloat64},
}
if conf.FeeRate > 0 {
feeRate.FeeRate = &pb.FeeRate_Exact{Exact: conf.FeeRate}
} else if conf.MaxFeeRate > 0 {
feeRate.FeeRate = &pb.FeeRate_Max{Max: conf.MaxFeeRate}
}

response, err := daemonClient.CreateUnsignedTransactions(ctx, &pb.CreateUnsignedTransactionsRequest{
From: conf.FromAddresses,
Address: conf.ToAddress,
Amount: sendAmountSompi,
IsSendAll: conf.IsSendAll,
UseExistingChangeAddress: conf.UseExistingChangeAddress,
FeeRate: feeRate,
})
if err != nil {
return err
Expand Down
Loading

0 comments on commit 0c449a9

Please sign in to comment.