-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.go
More file actions
221 lines (190 loc) · 5.51 KB
/
helpers.go
File metadata and controls
221 lines (190 loc) · 5.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package ordinals
import (
"bytes"
"encoding/json"
"fmt"
"io"
"net/http"
"strconv"
"github.com/bsv-blockchain/go-sdk/transaction"
)
const (
// OneSatApiBase is the base URL for the 1Sat API
OneSatApiBase = "https://ordinals.gorillapool.io/api/v1"
)
// UTXOResponse represents a UTXO response from the 1Sat API
type UTXOResponse struct {
Txid string `json:"txid"`
Vout int `json:"vout"`
Value int `json:"value"`
Height int `json:"height"`
Script string `json:"script"`
}
// FetchPayUtxos fetches UTXOs for payment from the 1Sat API
func FetchPayUtxos(address string) ([]*Utxo, error) {
url := fmt.Sprintf("%s/address/%s/utxo", OneSatApiBase, address)
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch pay UTXOs: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("Error closing response body: %v\n", err)
}
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var utxoResp []UTXOResponse
if err := json.Unmarshal(body, &utxoResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
utxos := make([]*Utxo, 0, len(utxoResp))
for _, u := range utxoResp {
utxos = append(utxos, &Utxo{
TxID: u.Txid,
Vout: uint32(u.Vout),
ScriptPubKey: u.Script,
Satoshis: uint64(u.Value),
})
}
return utxos, nil
}
// NftUtxoResponse represents an NFT UTXO response from the 1Sat API
type NftUtxoResponse struct {
UTXOResponse
Origin string `json:"origin"`
ContentType string `json:"contentType"`
}
// FetchNftUtxos fetches NFT UTXOs from the 1Sat API
func FetchNftUtxos(address string, collectionID string) ([]*NftUtxo, error) {
url := fmt.Sprintf("%s/address/%s/ordinals", OneSatApiBase, address)
if collectionID != "" {
url += "?collection=" + collectionID
}
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch NFT UTXOs: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("Error closing response body: %v\n", err)
}
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var utxoResp []NftUtxoResponse
if err := json.Unmarshal(body, &utxoResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
utxos := make([]*NftUtxo, 0, len(utxoResp))
for _, u := range utxoResp {
utxos = append(utxos, &NftUtxo{
Utxo: Utxo{
TxID: u.Txid,
Vout: uint32(u.Vout),
ScriptPubKey: u.Script,
Satoshis: uint64(u.Value),
},
ContentType: u.ContentType,
CollectionID: u.Origin,
})
}
return utxos, nil
}
// TokenUtxoResponse represents a token UTXO response from the 1Sat API
type TokenUtxoResponse struct {
UTXOResponse
TokenID string `json:"id"`
Protocol string `json:"protocol"`
Amount string `json:"amount"`
Decimals int `json:"decimals"`
}
// FetchTokenUtxos fetches token UTXOs from the 1Sat API
func FetchTokenUtxos(protocol TokenType, tokenID string, address string) ([]*TokenUtxo, error) {
url := fmt.Sprintf("%s/address/%s/tokens?protocol=%s", OneSatApiBase, address, protocol)
if tokenID != "" {
url += "&id=" + tokenID
}
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("failed to fetch token UTXOs: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("Error closing response body: %v\n", err)
}
}()
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
var utxoResp []TokenUtxoResponse
if err := json.Unmarshal(body, &utxoResp); err != nil {
return nil, fmt.Errorf("failed to unmarshal response: %w", err)
}
utxos := make([]*TokenUtxo, 0, len(utxoResp))
for _, u := range utxoResp {
amount, err := strconv.ParseUint(u.Amount, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed to parse amount: %w", err)
}
utxos = append(utxos, &TokenUtxo{
Utxo: Utxo{
TxID: u.Txid,
Vout: uint32(u.Vout),
ScriptPubKey: u.Script,
Satoshis: uint64(u.Value),
},
TokenID: u.TokenID,
Protocol: TokenType(u.Protocol),
Amount: amount,
Decimals: uint8(u.Decimals),
})
}
return utxos, nil
}
// OneSatBroadcaster returns a function for broadcasting transactions using the 1Sat API
func OneSatBroadcaster() BroadcastFunc {
return func(tx *transaction.Transaction) (*BroadcastResult, error) {
url := fmt.Sprintf("%s/tx", OneSatApiBase)
// Get the transaction hex
txHex := tx.String()
// Create the request body
reqBody := map[string]string{"rawtx": txHex}
reqJSON, err := json.Marshal(reqBody)
if err != nil {
return nil, fmt.Errorf("failed to marshal request: %w", err)
}
// Make the request
resp, err := http.Post(url, "application/json", bytes.NewBuffer(reqJSON))
if err != nil {
return nil, fmt.Errorf("failed to broadcast transaction: %w", err)
}
defer func() {
if err := resp.Body.Close(); err != nil {
fmt.Printf("Error closing response body: %v\n", err)
}
}()
// Parse the response
body, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
// Check for success
if resp.StatusCode != http.StatusOK {
return &BroadcastResult{
Status: "error",
Message: string(body),
}, nil
}
// Return the txid as success
return &BroadcastResult{
Status: "success",
TxID: tx.TxID().String(),
}, nil
}
}