Skip to content

create_tx returns wrong fee in TransactionDetails #660

@icota

Description

@icota

Code:

    #[test]
    fn test_create_psbt() {
        let wallet =
            Wallet::new("wpkh([96f9192b/84h/0h/0h]tpubDCxzhZZE31g2EqSv1UajMAw5Hd62htydz9r2XBkrccHgBh8uw3n62zr6Zjmj64tfTk8Tjxo6VctjUMAh5DXWTErfQPC6RmQhTdtNnXuTXTQ/0/*)",
                        Some("wpkh([96f9192b/84h/0h/0h]tpubDCxzhZZE31g2EqSv1UajMAw5Hd62htydz9r2XBkrccHgBh8uw3n62zr6Zjmj64tfTk8Tjxo6VctjUMAh5DXWTErfQPC6RmQhTdtNnXuTXTQ/1/*)"),
                        Network::Testnet,
                        MemoryDatabase::new()).unwrap();

        let client = get_electrum_client(-1, "ssl://electrum.blockstream.info:60002").unwrap();
        let blockchain = ElectrumBlockchain::from(client);
        wallet.sync(&blockchain, SyncOptions { progress: None }).unwrap();

        let send_to = Address::from_str("tb1ql7w62elx9ucw4pj5lgw4l028hmuw80sndtntxt").unwrap();

        let mut builder = wallet.build_tx();
        builder
            .add_recipient(send_to.script_pubkey(), 8630)
            .enable_rbf()
            .fee_rate(FeeRate::from_sat_per_vb(2.01));

        let (psbt, details) = builder.finish().unwrap();

        let encoded = base64::encode(&serialize(&psbt));

        println!("{:#?}", details);
        println!("Serialised PSBT: {}", encoded);
    }

Output:

TransactionDetails {
    transaction: None,
    txid: 5b26b2f1d02a3e4071b59eafb9f850be7d0473932c89fab1eb87431786e97454,
    received: 0,
    sent: 8859,
    fee: Some(
        286,
    ),
    confirmation_time: None,
}

Serialised PSBT: cHNidP8BAFIBAAAAAdOTVyY/5hjDQYUKam9+g6bkbxJ4PwvD3f1Saf/KEDVgAQAAAAD9////AbYhAAAAAAAAFgAU/52lZ+YvMOqGVPodX71HvvjjvhPM3CIAAAEA3gIAAAAAAQGf1tOQsvoLPsvez3A80v2JnOBScd2/7kkBqLrQhQUZgQEAAAAA/f///wLoAwAAAAAAABYAFMdzEdB6CzwaFgVhItyENxRYlPAXmyIAAAAAAAAWABTnWaUiYJz7lGIpOKDhy2pDK8WddgJHMEQCIGT75qgVcsjPfDInYlkLAnO+0AhSHdIel7O9wnpndNtFAiAJBZpGgEymbymAkWdTPw/KiR0bz40NOSmyqZtnE09tqgEhAnqMGbHDlAgnb9QreIPgd3/Ysnc7p/NdaFLQrMWiLrUX2WciAAEBH5siAAAAAAAAFgAU51mlImCc+5RiKTig4ctqQyvFnXYiBgMGUe86OhUasoZU7VPVXj8wODEei5kKSGWu/tUHEW1m9hiW+RkrVAAAgAAAAIAAAACAAQAAAAUAAAAAAA==

At first glance 8630 + 286 = 8916 is greater than both TransactionDetails sent value and the balance of UTXOs of the provided tpub which at this time is 8859 sats.

Back of envelope calculations say fee for this tx should be 229 sats. Parsing the above PSBT with Core's decodepsbt confirms it:

                ]
            }
        ],
        "outputs": [
            {}
        ],
        "fee": 0.00000229
    }
]

Build environment

BDK tag/commit: a328607
OS+version: Ubuntu 22.04
Rust/Cargo version: 1.54.0
Rust/Cargo target: x86_64

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions