Skip to content

Commit ad03d62

Browse files
committed
add new method GetTx to explorer
1 parent a05fa1a commit ad03d62

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

pkg/client-sdk/explorer/explorer.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ func newUtxo(explorerUtxo ExplorerUtxo, delay uint) Utxo {
5757
}
5858
}
5959

60+
type ExplorerTx struct {
61+
Txid string `json:"txid"`
62+
Vout []struct {
63+
Address string `json:"scriptpubkey_address"`
64+
Amount uint64 `json:"value"`
65+
} `json:"vout"`
66+
Status struct {
67+
Confirmed bool `json:"confirmed"`
68+
Blocktime int64 `json:"block_time"`
69+
} `json:"status"`
70+
}
71+
6072
type ExplorerUtxo struct {
6173
Txid string `json:"txid"`
6274
Vout uint32 `json:"vout"`
@@ -75,6 +87,7 @@ func (e ExplorerUtxo) ToUtxo(delay uint) Utxo {
7587
type Explorer interface {
7688
GetTxHex(txid string) (string, error)
7789
Broadcast(txHex string) (string, error)
90+
GetTxs(addr string) ([]ExplorerTx, error)
7891
GetUtxos(addr string) ([]ExplorerUtxo, error)
7992
GetBalance(addr string) (uint64, error)
8093
GetRedeemedVtxosBalance(
@@ -180,6 +193,28 @@ func (e *explorerSvc) Broadcast(txStr string) (string, error) {
180193
return txid, nil
181194
}
182195

196+
func (e *explorerSvc) GetTxs(addr string) ([]ExplorerTx, error) {
197+
resp, err := http.Get(fmt.Sprintf("%s/address/%s/txs", e.baseUrl, addr))
198+
if err != nil {
199+
return nil, err
200+
}
201+
202+
defer resp.Body.Close()
203+
body, err := io.ReadAll(resp.Body)
204+
if err != nil {
205+
return nil, err
206+
}
207+
if resp.StatusCode != http.StatusOK {
208+
return nil, fmt.Errorf("failed to get txs: %s", string(body))
209+
}
210+
payload := []ExplorerTx{}
211+
if err := json.Unmarshal(body, &payload); err != nil {
212+
return nil, err
213+
}
214+
215+
return payload, nil
216+
}
217+
183218
func (e *explorerSvc) GetUtxos(addr string) ([]ExplorerUtxo, error) {
184219
resp, err := http.Get(fmt.Sprintf("%s/address/%s/utxo", e.baseUrl, addr))
185220
if err != nil {

0 commit comments

Comments
 (0)