Skip to content

Commit 3494520

Browse files
authored
Merge pull request #143 from lightninglabs/unspent-only
btc: filter address outputs, only return unspent
2 parents d173f8c + 83aa752 commit 3494520

File tree

1 file changed

+20
-1
lines changed

1 file changed

+20
-1
lines changed

btc/explorer_api.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,26 @@ func (a *ExplorerAPI) Unspent(addr string) ([]*Vout, error) {
165165
}
166166
}
167167

168-
return outputs, nil
168+
// Now filter those that are really unspent, because above we get all
169+
// outputs that are sent to the address.
170+
var unspent []*Vout
171+
for idx, vout := range outputs {
172+
url := fmt.Sprintf(
173+
"%s/tx/%s/outspend/%d", a.BaseURL, vout.Outspend.Txid,
174+
idx,
175+
)
176+
outspend := Outspend{}
177+
err := fetchJSON(url, &outspend)
178+
if err != nil {
179+
return nil, err
180+
}
181+
182+
if !outspend.Spent {
183+
unspent = append(unspent, vout)
184+
}
185+
}
186+
187+
return unspent, nil
169188
}
170189

171190
func (a *ExplorerAPI) Address(outpoint string) (string, error) {

0 commit comments

Comments
 (0)