Skip to content

Commit

Permalink
refactor: use feerate type from bitcoin-ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 30, 2024
1 parent 61cb445 commit 1fa179d
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount
import org.rustbitcoin.bitcoin.FeeRate

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount
import org.rustbitcoin.bitcoin.FeeRate

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion bdk-ffi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ bdk_esplora = { version = "0.17.0", default-features = false, features = ["std",
bdk_electrum = { version = "0.17.0", default-features = false, features = ["use-rustls-ring"] }
bdk_bitcoind_rpc = { version = "0.14.0" }
bitcoin-internals = { version = "0.2.0", features = ["alloc"] }
bitcoin-ffi = { git = "https://github.com/rustaceanrob/bitcoin-ffi.git", branch = "another-type" }
bitcoin-ffi = { git = "https://github.com/bitcoindevkit/bitcoin-ffi", branch = "master" }

uniffi = { version = "=0.28.0" }
thiserror = "1.0.58"
Expand Down
25 changes: 6 additions & 19 deletions bdk-ffi/src/bdk.udl
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,6 @@ interface ExtractTxError {
OtherExtractTxErr();
};

[Error]
enum FeeRateError {
"ArithmeticOverflow"
};

[Error]
interface FromScriptError {
UnrecognizedScript();
Expand Down Expand Up @@ -698,20 +693,6 @@ dictionary OutPoint {
u32 vout;
};

interface FeeRate {
[Name=from_sat_per_vb, Throws=FeeRateError]
constructor(u64 sat_per_vb);

[Name=from_sat_per_kwu]
constructor(u64 sat_per_kwu);

u64 to_sat_per_vb_ceil();

u64 to_sat_per_vb_floor();

u64 to_sat_per_kwu();
};

dictionary TxIn {
OutPoint previous_output;
Script script_sig;
Expand All @@ -732,5 +713,11 @@ typedef extern Network;
[ExternalInterface="bitcoin_ffi"]
typedef extern Amount;

[ExternalInterface="bitcoin_ffi"]
typedef extern FeeRate;

[ExternalInterface="bitcoin_ffi"]
typedef extern ParseAmountError;

[ExternalInterface="bitcoin_ffi"]
typedef extern FeeRateError;
91 changes: 3 additions & 88 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,85 +1,29 @@
use crate::error::{
AddressParseError, FeeRateError, FromScriptError, PsbtError, PsbtParseError, TransactionError,
AddressParseError, FromScriptError, PsbtError, PsbtParseError, TransactionError,
};

use bitcoin_ffi::Script;

use bdk_bitcoind_rpc::bitcoincore_rpc::jsonrpc::serde_json;
use bdk_wallet::bitcoin::address::{NetworkChecked, NetworkUnchecked};
// use bdk_wallet::bitcoin::amount::ParseAmountError;
use bdk_wallet::bitcoin::consensus::encode::serialize;
use bdk_wallet::bitcoin::consensus::Decodable;
use bdk_wallet::bitcoin::io::Cursor;
use bdk_wallet::bitcoin::psbt::ExtractTxError;
use bdk_wallet::bitcoin::Address as BdkAddress;
// use bdk_wallet::bitcoin::Amount as BdkAmount;
use bdk_wallet::bitcoin::FeeRate as BdkFeeRate;
use bdk_wallet::bitcoin::Network;
use bdk_wallet::bitcoin::OutPoint as BdkOutPoint;
use bdk_wallet::bitcoin::Psbt as BdkPsbt;
use bdk_wallet::bitcoin::Transaction as BdkTransaction;
use bdk_wallet::bitcoin::TxIn as BdkTxIn;
use bdk_wallet::bitcoin::TxOut as BdkTxOut;
use bdk_wallet::bitcoin::Txid;
use bitcoin_ffi::Script;

use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};

// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Amount(pub(crate) BdkAmount);
//
// impl Amount {
// pub fn from_sat(sat: u64) -> Self {
// Amount(BdkAmount::from_sat(sat))
// }
//
// pub fn from_btc(btc: f64) -> Result<Self, ParseAmountError> {
// let bdk_amount = BdkAmount::from_btc(btc).map_err(ParseAmountError::from)?;
// Ok(Amount(bdk_amount))
// }
//
// pub fn to_sat(&self) -> u64 {
// self.0.to_sat()
// }
//
// pub fn to_btc(&self) -> f64 {
// self.0.to_btc()
// }
// }
//
// impl From<Amount> for BdkAmount {
// fn from(amount: Amount) -> Self {
// amount.0
// }
// }
//
// impl From<BdkAmount> for Amount {
// fn from(amount: BdkAmount) -> Self {
// Amount(amount)
// }
// }

// #[derive(Clone, Debug, PartialEq, Eq)]
// pub struct Script(pub(crate) BdkScriptBuf);
//
// impl Script {
// pub fn new(raw_output_script: Vec<u8>) -> Self {
// let script: BdkScriptBuf = raw_output_script.into();
// Script(script)
// }
//
// pub fn to_bytes(&self) -> Vec<u8> {
// self.0.to_bytes()
// }
// }
//
// impl From<BdkScriptBuf> for Script {
// fn from(script: BdkScriptBuf) -> Self {
// Script(script)
// }
// }

#[derive(Debug, PartialEq, Eq)]
pub struct Address(BdkAddress<NetworkChecked>);

Expand Down Expand Up @@ -318,35 +262,6 @@ impl From<&BdkTxOut> for TxOut {
}
}

#[derive(Clone, Debug)]
pub struct FeeRate(pub(crate) BdkFeeRate);

impl FeeRate {
pub fn from_sat_per_vb(sat_per_vb: u64) -> Result<Self, FeeRateError> {
let fee_rate: Option<BdkFeeRate> = BdkFeeRate::from_sat_per_vb(sat_per_vb);
match fee_rate {
Some(fee_rate) => Ok(FeeRate(fee_rate)),
None => Err(FeeRateError::ArithmeticOverflow),
}
}

pub fn from_sat_per_kwu(sat_per_kwu: u64) -> Self {
FeeRate(BdkFeeRate::from_sat_per_kwu(sat_per_kwu))
}

pub fn to_sat_per_vb_ceil(&self) -> u64 {
self.0.to_sat_per_vb_ceil()
}

pub fn to_sat_per_vb_floor(&self) -> u64 {
self.0.to_sat_per_vb_floor()
}

pub fn to_sat_per_kwu(&self) -> u64 {
self.0.to_sat_per_kwu()
}
}

#[cfg(test)]
mod tests {
use crate::bitcoin::Address;
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/descriptor.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use crate::error::DescriptorError;
use crate::keys::DescriptorPublicKey;
use crate::keys::DescriptorSecretKey;
use std::fmt::Display;

use bdk_wallet::bitcoin::bip32::Fingerprint;
use bdk_wallet::bitcoin::key::Secp256k1;
Expand All @@ -15,6 +14,7 @@ use bdk_wallet::template::{
};
use bdk_wallet::KeychainKind;

use std::fmt::Display;
use std::str::FromStr;

#[derive(Debug)]
Expand Down
22 changes: 2 additions & 20 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,12 +379,6 @@ pub enum ExtractTxError {
OtherExtractTxErr,
}

#[derive(Debug, thiserror::Error)]
pub enum FeeRateError {
#[error("arithmetic overflow on feerate")]
ArithmeticOverflow,
}

#[derive(Debug, thiserror::Error)]
pub enum FromScriptError {
#[error("script is not a p2pkh, p2sh or witness program")]
Expand Down Expand Up @@ -1225,8 +1219,8 @@ impl From<BdkSqliteError> for SqliteError {
mod test {
use crate::error::{
Bip32Error, Bip39Error, CannotConnectError, DescriptorError, DescriptorKeyError,
ElectrumError, EsploraError, ExtractTxError, FeeRateError, PersistenceError, PsbtError,
PsbtParseError, RequestBuilderError, TransactionError, TxidParseError,
ElectrumError, EsploraError, ExtractTxError, PersistenceError, PsbtError, PsbtParseError,
RequestBuilderError, TransactionError, TxidParseError,
};
use crate::SignerError;

Expand Down Expand Up @@ -1599,18 +1593,6 @@ mod test {
}
}

#[test]
fn test_error_fee_rate() {
let cases = vec![(
FeeRateError::ArithmeticOverflow,
"arithmetic overflow on feerate",
)];

for (error, expected_message) in cases {
assert_eq!(error.to_string(), expected_message);
}
}

#[test]
fn test_error_inspect() {
let cases = vec![(
Expand Down
2 changes: 1 addition & 1 deletion bdk-ffi/src/keys.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use crate::error::{Bip32Error, Bip39Error, DescriptorKeyError};
use std::fmt::Display;

use bdk_wallet::bitcoin::bip32::DerivationPath as BdkDerivationPath;
use bdk_wallet::bitcoin::key::Secp256k1;
Expand All @@ -15,6 +14,7 @@ use bdk_wallet::keys::{
use bdk_wallet::miniscript::descriptor::{DescriptorXKey, Wildcard};
use bdk_wallet::miniscript::BareCtx;

use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;
use std::sync::{Arc, Mutex};
Expand Down
3 changes: 1 addition & 2 deletions bdk-ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ mod types;
mod wallet;

use crate::bitcoin::Address;
use crate::bitcoin::FeeRate;
use crate::bitcoin::OutPoint;
use crate::bitcoin::Psbt;
use crate::bitcoin::Transaction;
Expand All @@ -29,7 +28,6 @@ use crate::error::DescriptorKeyError;
use crate::error::ElectrumError;
use crate::error::EsploraError;
use crate::error::ExtractTxError;
use crate::error::FeeRateError;
use crate::error::FromScriptError;
use crate::error::LoadWithPersistError;
use crate::error::PersistenceError;
Expand Down Expand Up @@ -67,6 +65,7 @@ use crate::wallet::TxBuilder;
use crate::wallet::Wallet;

use bitcoin_ffi::Amount;
use bitcoin_ffi::FeeRate;
use bitcoin_ffi::Network;
use bitcoin_ffi::Script;

Expand Down
5 changes: 3 additions & 2 deletions bdk-ffi/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
use crate::bitcoin::{Address, OutPoint, Transaction, TxOut};
use crate::error::RequestBuilderError;

use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use bdk_core::spk_client::SyncItem;
use bdk_wallet::bitcoin::Transaction as BdkTransaction;
use bdk_wallet::chain::spk_client::FullScanRequest as BdkFullScanRequest;
Expand All @@ -16,8 +19,6 @@ use bdk_wallet::Balance as BdkBalance;
use bdk_wallet::KeychainKind;
use bdk_wallet::LocalOutput as BdkLocalOutput;
use bdk_wallet::Update as BdkUpdate;
use bitcoin_ffi::Amount;
use bitcoin_ffi::Script;

use std::sync::{Arc, Mutex};

Expand Down
3 changes: 2 additions & 1 deletion bdk-ffi/src/wallet.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::bitcoin::{FeeRate, OutPoint, Psbt, Transaction};
use crate::bitcoin::{OutPoint, Psbt, Transaction};
use crate::descriptor::Descriptor;
use crate::error::{
CalculateFeeError, CannotConnectError, CreateTxError, CreateWithPersistError,
Expand All @@ -9,6 +9,7 @@ use crate::types::{AddressInfo, Balance, CanonicalTx, LocalOutput, ScriptAmount}
use crate::types::{FullScanRequestBuilder, SyncRequestBuilder, Update};

use bitcoin_ffi::Amount;
use bitcoin_ffi::FeeRate;
use bitcoin_ffi::Script;

use bdk_wallet::bitcoin::amount::Amount as BdkAmount;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.Amount

import org.rustbitcoin.bitcoin.FeeRate

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import kotlin.test.assertTrue
import java.io.File
import org.rustbitcoin.bitcoin.Amount
import org.rustbitcoin.bitcoin.Network
import org.rustbitcoin.bitcoin.FeeRate

private const val SIGNET_ESPLORA_URL = "http://signet.bitcoindevkit.net"
private const val TESTNET_ESPLORA_URL = "https://esplora.testnet.kuutamo.cloud"
Expand Down
1 change: 1 addition & 0 deletions bdk-python/tests/test_live_tx_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from bdkpython import Connection
from bdkpython.bitcoin import Network
from bdkpython.bitcoin import Amount
from bdkpython.bitcoin import FeeRate

import unittest
import os
Expand Down
2 changes: 1 addition & 1 deletion bdk-python/tests/test_live_wallet.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
from bdkpython import Psbt
from bdkpython import TxBuilder
from bdkpython import Connection
from bdkpython.bitcoin import Script
from bdkpython.bitcoin import Network
from bdkpython.bitcoin import Amount
from bdkpython.bitcoin import FeeRate

import unittest
import os
Expand Down

0 comments on commit 1fa179d

Please sign in to comment.