diff --git a/btc/explorer_api.go b/btc/explorer_api.go index c36b902..34f5b90 100644 --- a/btc/explorer_api.go +++ b/btc/explorer_api.go @@ -156,10 +156,27 @@ func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) { for _, tx := range txs { for voutIdx, vout := range tx.Vout { if vout.ScriptPubkeyAddr == addr { - vout.Outspend = &Outspend{ - Txid: tx.TXID, - Vin: voutIdx, + // We need to also make sure that the tx is not + // already spent before including it as unspent. + // + // NOTE: Somehow LND sometimes contructs + // channels with the same keyfamily base hence + // the same pubkey. Needs to be investigated on + // the LND side. + url := fmt.Sprintf( + "%s/tx/%s/outspend/%d", a.BaseURL, + tx.TXID, voutIdx, + ) + outspend := Outspend{} + err := fetchJSON(url, &outspend) + if err != nil { + return nil, err } + + if outspend.Spent { + continue + } + outputs = append(outputs, vout) } }