Skip to content

Commit

Permalink
Add fee rate to kaspawallet parse
Browse files Browse the repository at this point in the history
  • Loading branch information
someone235 committed Sep 4, 2024
1 parent 78ca616 commit 70bcc31
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions cmd/kaspawallet/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type broadcastConfig struct {
}

type parseConfig struct {
KeysFile string `long:"keys-file" short:"f" description:"Keys file location (default: ~/.kaspawallet/keys.json (*nix), %USERPROFILE%\\AppData\\Local\\Kaspawallet\\key.json (Windows))"`
Transaction string `long:"transaction" short:"t" description:"The transaction to parse (encoded in hex)"`
TransactionFile string `long:"transaction-file" short:"F" description:"The file containing the transaction to parse (encoded in hex)"`
Verbose bool `long:"verbose" short:"v" description:"Verbose: show transaction inputs"`
Expand Down
13 changes: 9 additions & 4 deletions cmd/kaspawallet/daemon/server/split_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/kaspanet/kaspad/domain/consensus/utils/utxo"
"github.com/kaspanet/kaspad/domain/miningmanager/mempool"
"github.com/kaspanet/kaspad/util"
"github.com/kaspanet/kaspad/util/txmass"
)

// maybeAutoCompoundTransaction checks if a transaction's mass is higher that what is allowed for a standard
Expand Down Expand Up @@ -218,30 +219,34 @@ func (s *server) createSplitTransaction(transaction *serialization.PartiallySign
}

func (s *server) estimateMassAfterSignatures(transaction *serialization.PartiallySignedTransaction) (uint64, error) {
return EstimateMassAfterSignatures(transaction, s.keysFile.ECDSA, s.keysFile.MinimumSignatures, s.txMassCalculator)
}

func EstimateMassAfterSignatures(transaction *serialization.PartiallySignedTransaction, ecdsa bool, minimumSignatures uint32, txMassCalculator *txmass.Calculator) (uint64, error) {
transaction = transaction.Clone()
var signatureSize uint64
if s.keysFile.ECDSA {
if ecdsa {
signatureSize = secp256k1.SerializedECDSASignatureSize
} else {
signatureSize = secp256k1.SerializedSchnorrSignatureSize
}

for i, input := range transaction.PartiallySignedInputs {
for j, pubKeyPair := range input.PubKeySignaturePairs {
if uint32(j) >= s.keysFile.MinimumSignatures {
if uint32(j) >= minimumSignatures {
break
}
pubKeyPair.Signature = make([]byte, signatureSize+1) // +1 for SigHashType
}
transaction.Tx.Inputs[i].SigOpCount = byte(len(input.PubKeySignaturePairs))
}

transactionWithSignatures, err := libkaspawallet.ExtractTransactionDeserialized(transaction, s.keysFile.ECDSA)
transactionWithSignatures, err := libkaspawallet.ExtractTransactionDeserialized(transaction, ecdsa)
if err != nil {
return 0, err
}

return s.txMassCalculator.CalculateTransactionMass(transactionWithSignatures), nil
return txMassCalculator.CalculateTransactionMass(transactionWithSignatures), nil
}

func (s *server) moreUTXOsForMergeTransaction(alreadySelectedUTXOs []*libkaspawallet.UTXO, requiredAmount uint64, feeRate float64) (
Expand Down
25 changes: 22 additions & 3 deletions cmd/kaspawallet/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,17 @@ package main
import (
"encoding/hex"
"fmt"
"io/ioutil"
"strings"

"github.com/kaspanet/kaspad/cmd/kaspawallet/daemon/server"
"github.com/kaspanet/kaspad/cmd/kaspawallet/keys"
"github.com/kaspanet/kaspad/cmd/kaspawallet/libkaspawallet/serialization"
"github.com/kaspanet/kaspad/domain/consensus/utils/consensushashing"
"github.com/kaspanet/kaspad/domain/consensus/utils/constants"
"github.com/kaspanet/kaspad/domain/consensus/utils/txscript"
"github.com/kaspanet/kaspad/util/txmass"
"github.com/pkg/errors"
"io/ioutil"
"strings"
)

func parse(conf *parseConfig) error {
Expand All @@ -20,6 +24,11 @@ func parse(conf *parseConfig) error {
return errors.Errorf("Both --transaction and --transaction-file cannot be passed at the same time")
}

keysFile, err := keys.ReadKeysFile(conf.NetParams(), conf.KeysFile)
if err != nil {
return err
}

transactionHex := conf.Transaction
if conf.TransactionFile != "" {
transactionHexBytes, err := ioutil.ReadFile(conf.TransactionFile)
Expand All @@ -33,6 +42,8 @@ func parse(conf *parseConfig) error {
if err != nil {
return err
}

txMassCalculator := txmass.NewCalculator(conf.NetParams().MassPerTxByte, conf.NetParams().MassPerScriptPubKeyByte, conf.NetParams().MassPerSigOp)
for i, transaction := range transactions {

partiallySignedTransaction, err := serialization.DeserializePartiallySignedTransaction(transaction)
Expand Down Expand Up @@ -78,7 +89,15 @@ func parse(conf *parseConfig) error {
}
fmt.Println()

fmt.Printf("Fee:\t%d Sompi\n\n", allInputSompi-allOutputSompi)
fee := allInputSompi - allOutputSompi
fmt.Printf("Fee:\t%d Sompi\n\n", fee)
mass, err := server.EstimateMassAfterSignatures(partiallySignedTransaction, keysFile.ECDSA, keysFile.MinimumSignatures, txMassCalculator)
if err != nil {
return err
}

feeRate := float64(fee) / float64(mass)
fmt.Printf("Fee rate: %.2f Sompi/Gram\n", feeRate)
}

return nil
Expand Down

0 comments on commit 70bcc31

Please sign in to comment.