Skip to content

Commit

Permalink
test bitcoin-ffi #7
Browse files Browse the repository at this point in the history
  • Loading branch information
thunderbiscuit committed Aug 29, 2024
1 parent 3c42cba commit 228e0e0
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 76 deletions.
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/reez/bitcoin-ffi.git", branch = "feerate" }

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 @@ -694,20 +689,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 @@ -728,5 +709,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;
64 changes: 31 additions & 33 deletions bdk-ffi/src/bitcoin.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,23 @@
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;
Expand Down Expand Up @@ -318,34 +316,34 @@ 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()
}
}
// #[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 {
Expand Down
26 changes: 7 additions & 19 deletions bdk-ffi/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -379,11 +379,11 @@ pub enum ExtractTxError {
OtherExtractTxErr,
}

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

#[derive(Debug, thiserror::Error)]
pub enum FromScriptError {
Expand Down Expand Up @@ -1225,8 +1225,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 +1599,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
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
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

0 comments on commit 228e0e0

Please sign in to comment.