Skip to content

Commit

Permalink
esplora: fix accounting change addresses
Browse files Browse the repository at this point in the history
  • Loading branch information
dr-orlovsky committed Oct 10, 2023
1 parent 38d1b63 commit 5e135e8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 8 deletions.
9 changes: 9 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ pub enum Party {
impl Party {
pub fn is_ourself(&self) -> bool { matches!(self, Party::Wallet(_)) }
pub fn is_external(&self) -> bool { !self.is_ourself() }
pub fn is_unknown(&self) -> bool { matches!(self, Party::Unknown(_)) }
pub fn derived_addr(&self) -> Option<DerivedAddr> {
match self {
Party::Wallet(addr) => Some(*addr),
Expand All @@ -257,6 +258,14 @@ impl Party {
terminal: wallet_addr.terminal,
})
}
pub fn script_pubkey(&self) -> Option<ScriptPubkey> {
match self {
Party::Subsidy => None,
Party::Counterparty(addr) => Some(addr.script_pubkey()),
Party::Unknown(script) => Some(script.clone()),
Party::Wallet(_) => None,
}
}
}

impl Display for Party {
Expand Down
16 changes: 8 additions & 8 deletions src/indexers/esplora.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,19 +143,19 @@ impl Indexer for BlockingClient {
for txid in txids {
let mut tx = cache.tx.remove(txid).expect("broken logic");
for debit in &mut tx.outputs {
let Party::Unknown(ref s) = debit.beneficiary else {
let Some(s) = debit.beneficiary.script_pubkey() else {
continue;
};
if s == script {
if &s == script {
cache.utxo.insert(debit.outpoint);
debit.beneficiary = Party::from_wallet_addr(wallet_addr);
wallet_addr.used = wallet_addr.used.saturating_add(1);
wallet_addr.volume.saturating_add_assign(debit.value);
wallet_addr.balance = wallet_addr
.balance
.saturating_add(debit.value.sats().try_into().expect("sats overflow"));
} else {
Address::with(s, descriptor.chain())
} else if debit.beneficiary.is_unknown() {
Address::with(&s, descriptor.chain())
.map(|addr| {
debit.beneficiary = Party::Counterparty(addr);
})
Expand All @@ -170,16 +170,16 @@ impl Indexer for BlockingClient {
for txid in txids {
let mut tx = cache.tx.remove(txid).expect("broken logic");
for credit in &mut tx.inputs {
let Party::Unknown(ref s) = credit.payer else {
let Some(s) = credit.payer.script_pubkey() else {
continue;
};
if s == script {
if &s == script {
credit.payer = Party::from_wallet_addr(wallet_addr);
wallet_addr.balance = wallet_addr
.balance
.saturating_sub(credit.value.sats().try_into().expect("sats overflow"));
} else {
Address::with(s, descriptor.chain())
} else if credit.payer.is_unknown() {
Address::with(&s, descriptor.chain())
.map(|addr| {
credit.payer = Party::Counterparty(addr);
})
Expand Down

0 comments on commit 5e135e8

Please sign in to comment.