Fix estimateFee change value

This commit is contained in:
Ori Newman 2024-09-18 13:45:32 +03:00
parent af93d5fdfe
commit 2d25c3d7a3
3 changed files with 9 additions and 8 deletions

View File

@ -198,7 +198,7 @@ func (s *server) selectUTXOsWithPreselected(preSelectedUTXOs []*walletUTXO, allo
totalValue += utxo.UTXOEntry.Amount()
// We're overestimating a bit by assuming that any transaction will have a change output
fee, err = s.estimateFee(selectedUTXOs, feeRate, maxFee, true)
fee, err = s.estimateFee(selectedUTXOs, feeRate, maxFee, spendAmount)
if err != nil {
return false, err
}
@ -257,7 +257,7 @@ func (s *server) selectUTXOsWithPreselected(preSelectedUTXOs []*walletUTXO, allo
return selectedUTXOs, totalReceived, totalValue - totalSpend, nil
}
func (s *server) estimateFee(selectedUTXOs []*libkaspawallet.UTXO, feeRate float64, maxFee uint64, assumeChange bool) (uint64, error) {
func (s *server) estimateFee(selectedUTXOs []*libkaspawallet.UTXO, feeRate float64, maxFee uint64, recipientValue uint64) (uint64, error) {
fakePubKey := [util.PublicKeySize]byte{}
fakeAddr, err := util.NewAddressPublicKey(fakePubKey[:], s.params.Prefix)
if err != nil {
@ -271,15 +271,15 @@ func (s *server) estimateFee(selectedUTXOs []*libkaspawallet.UTXO, feeRate float
// This is an approximation for the distribution of value between the recipient output and the change output.
var mockPayments []*libkaspawallet.Payment
if assumeChange {
if totalValue > recipientValue {
mockPayments = []*libkaspawallet.Payment{
{
Address: fakeAddr,
Amount: totalValue * 99 / 100,
Amount: recipientValue,
},
{
Address: fakeAddr,
Amount: totalValue / 100,
Amount: totalValue - recipientValue, // We ignore the fee since we expect it to be insignificant in mass calculation.
},
}
} else {

View File

@ -72,7 +72,7 @@ func (s *server) mergeTransaction(
totalValue += output.Value
}
// We're overestimating a bit by assuming that any transaction will have a change output
fee, err := s.estimateFee(utxos, feeRate, maxFee, true)
fee, err := s.estimateFee(utxos, feeRate, maxFee, sentValue)
if err != nil {
return nil, err
}
@ -253,7 +253,7 @@ func (s *server) createSplitTransaction(transaction *serialization.PartiallySign
totalSompi += selectedUTXOs[i-startIndex].UTXOEntry.Amount()
}
if len(selectedUTXOs) != 0 {
fee, err := s.estimateFee(selectedUTXOs, feeRate, maxFee, false)
fee, err := s.estimateFee(selectedUTXOs, feeRate, maxFee, totalSompi)
if err != nil {
return nil, err
}

View File

@ -90,12 +90,13 @@ func parse(conf *parseConfig) error {
fmt.Println()
fee := allInputSompi - allOutputSompi
fmt.Printf("Fee:\t%d Sompi (%f KAS)\n\n", fee, float64(fee)/float64(constants.SompiPerKaspa))
fmt.Printf("Fee:\t%d Sompi (%f KAS)\n", fee, float64(fee)/float64(constants.SompiPerKaspa))
mass, err := server.EstimateMassAfterSignatures(partiallySignedTransaction, keysFile.ECDSA, keysFile.MinimumSignatures, txMassCalculator)
if err != nil {
return err
}
fmt.Printf("Mass: %d grams\n", mass)
feeRate := float64(fee) / float64(mass)
fmt.Printf("Fee rate: %.2f Sompi/Gram\n", feeRate)
}