-
Notifications
You must be signed in to change notification settings - Fork 10
/
api_transactions.go
651 lines (555 loc) · 17.3 KB
/
api_transactions.go
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
package blockfrost
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/http"
"net/url"
)
const (
resourceTxs = "txs"
resourceTx = "tx"
resourceTxStakes = "stakes"
resourceTxUTXOs = "utxos"
resourceTxWithdrawals = "withdrawals"
resourceTxMetadata = "metadata"
resourceTxRedeemers = "redeemers"
resourceCbor = "cbor"
resourceTxDelegations = "delegations"
resourceTxPoolUpdates = "pool_updates"
resourceTxPoolRetires = "pool_retires"
resourceTxSubmit = "submit"
resourceTxEvaluate = "utils/txs/evaluate"
resourceTxEvaluateUtxos = "utils/txs/evaluate/utxos"
)
type TransactionContent struct {
// Count of asset mints and burns within the transaction
AssetMintOrBurnCount int `json:"asset_mint_or_burn_count"`
// Block hash
Block string `json:"block"`
// Block number
BlockHeight int `json:"block_height"`
// Block creation time
BlockTime int `json:"block_time"`
// Count of the delegations within the transaction
DelegationCount int `json:"delegation_count"`
// Deposit within the transaction in Lovelaces
Deposit string `json:"deposit"`
// Fees of the transaction in Lovelaces
Fees string `json:"fees"`
// Transaction hash
Hash string `json:"hash"`
// Transaction index within the block
Index int `json:"index"`
// Left (included) endpoint of the timelock validity intervals
InvalidBefore *string `json:"invalid_before"`
// Right (excluded) endpoint of the timelock validity intervals
InvalidHereafter *string `json:"invalid_hereafter"`
// Count of the MIR certificates within the transaction
MirCertCount int `json:"mir_cert_count"`
OutputAmount []struct {
// The quantity of the unit
Quantity string `json:"quantity"`
// The unit of the value
Unit string `json:"unit"`
} `json:"output_amount"`
// Count of the stake pool retirement certificates within the transaction
PoolRetireCount int `json:"pool_retire_count"`
// Count of the stake pool registration and update certificates within the transaction
PoolUpdateCount int `json:"pool_update_count"`
// Count of redeemers within the transaction
RedeemerCount int `json:"redeemer_count"`
// Size of the transaction in Bytes
Size int `json:"size"`
// Slot number
Slot int `json:"slot"`
// Count of the stake keys (de)registration and delegation certificates within the transaction
StakeCertCount int `json:"stake_cert_count"`
// Count of UTXOs within the transaction
UtxoCount int `json:"utxo_count"`
// Script passed validation
ValidContract bool `json:"valid_contract"`
// Count of the withdrawals within the transaction
WithdrawalCount int `json:"withdrawal_count"`
}
type TxAmount struct {
// The quantity of the unit
Quantity string `json:"quantity"`
// The unit of the value
Unit string `json:"unit"`
}
type TransactionInput struct {
Address string `json:"address"`
Amount []TxAmount `json:"amount"`
OutputIndex float32 `json:"output_index"`
TxHash string `json:"tx_hash"`
DataHash *string `json:"data_hash"`
Collateral bool `json:"collateral"`
InlineDatum *string `json:"inline_datum"`
ReferenceScriptHash *string `json:"reference_script_hash"`
Reference *bool `json:"reference"`
}
type TransactionOutput struct {
Address string `json:"address"`
Amount []TxAmount `json:"amount"`
OutputIndex int `json:"output_index"`
DataHash *string `json:"data_hash"`
InlineDatum *string `json:"inline_datum"`
ReferenceScriptHash *string `json:"reference_script_hash"`
}
type TransactionUTXOs struct {
// Transaction hash
Hash string `json:"hash"`
Inputs []TransactionInput `json:"inputs"`
Outputs []TransactionOutput `json:"outputs"`
}
type TransactionStakeAddressCert struct {
// Delegation stake address
Address string `json:"address"`
// Index of the certificate within the transaction
CertIndex int `json:"cert_index"`
// Registration boolean, false if deregistration
Registration bool `json:"registration"`
}
type TransactionDelegation struct {
// Epoch in which the delegation becomes active
ActiveEpoch int `json:"active_epoch"`
// Bech32 delegation stake address
Address string `json:"address"`
// Index of the certificate within the transaction
CertIndex int `json:"cert_index"`
// Index of the certificate within the transaction
Index int `json:"index"`
// Bech32 ID of delegated stake pool
PoolId string `json:"pool_id"`
}
type TransactionWidthrawal struct {
// Bech32 withdrawal address
Address string `json:"address"`
// Withdrawal amount in Lovelaces
Amount string `json:"amount"`
}
type TransactionMIR struct {
// Bech32 stake address
Address string `json:"address"`
// MIR amount in Lovelaces
Amount string `json:"amount"`
// Index of the certificate within the transaction
CertIndex int `json:"cert_index"`
// Source of MIR funds
Pot string `json:"pot"`
}
type TransactionPoolCert struct {
// Epoch that the delegation becomes active
ActiveEpoch int `json:"active_epoch"`
// Index of the certificate within the transaction
CertIndex int `json:"cert_index"`
// Fixed tax cost of the stake pool in Lovelaces
FixedCost string `json:"fixed_cost"`
// Margin tax cost of the stake pool
MarginCost float32 `json:"margin_cost"`
Metadata *struct {
// Description of the stake pool
Description string `json:"description"`
// Hash of the metadata file
Hash string `json:"hash"`
// Home page of the stake pool
Homepage string `json:"homepage"`
// Name of the stake pool
Name string `json:"name"`
// Ticker of the stake pool
Ticker string `json:"ticker"`
// URL to the stake pool metadata
Url string `json:"url"`
} `json:"metadata"`
Owners []string `json:"owners"`
// Stake pool certificate pledge in Lovelaces
Pledge string `json:"pledge"`
// Bech32 encoded pool ID
PoolId string `json:"pool_id"`
Relays []struct {
// DNS name of the relay
Dns string `json:"dns"`
// DNS SRV entry of the relay
DnsSrv string `json:"dns_srv"`
// IPv4 address of the relay
Ipv4 string `json:"ipv4"`
// IPv6 address of the relay
Ipv6 string `json:"ipv6"`
// Network port of the relay
Port int `json:"port"`
} `json:"relays"`
// Bech32 reward account of the stake pool
RewardAccount string `json:"reward_account"`
// VRF key hash
VrfKey string `json:"vrf_key"`
}
type TransactionPoolRetires struct {
// Index of the certificate within the transaction
CertIndex int `json:"cert_index"`
// Bech32 stake pool ID
PoolId string `json:"pool_id"`
// Retiring epoch
RetiringEpoch int `json:"retiring_epoch"`
}
type TransactionMetadata struct {
// Content of the metadata
JsonMetadata interface{} `json:"json_metadata"`
// Metadata label
Label string `json:"label"`
}
type TransactionMetadataCbor struct {
// Deprecated: Use Metadata instead.
CborMetadata *string `json:"cbor_metadata"`
// Content of the CBOR metadata
Metadata string `json:"metadata"`
// Metadata label
Label string `json:"label"`
}
type TransactionRedeemer struct {
TxIndex int `json:"tx_index"`
Purpose string `json:"purpose"`
ScriptHash string `json:"script_hash"`
RedeemerDataHash string `json:"redeemer_data_hash"`
UnitMem string `json:"unit_mem"`
UnitSteps string `json:"unit_steps"`
Fee string `json:"fee"`
}
type Quantity string
type Value struct {
Coins Quantity `json:"coins"`
Assets map[string]Quantity `json:"assets,omitempty"`
}
type TxOutScript interface{}
type AdditionalUtxoSetTxIn struct {
TxID string `json:"txId"`
Index int `json:"index"`
}
type AdditionalUtxoSetTxOut struct {
Address string `json:"address"`
Value Value `json:"value"`
DatumHash *string `json:"datumHash"`
Datum interface{} `json:"datum"` // Could be various types
Script *TxOutScript `json:"script"`
}
// AdditionalUtxoSet represents a slice of tuples (TxIn, TxOut)
type AdditionalUtxoSet []struct {
TxIn AdditionalUtxoSetTxIn `json:"txIn"`
TxOut AdditionalUtxoSetTxOut `json:"txOut"`
}
type OgmiosResponse struct {
Type string `json:"type"`
Version string `json:"version"`
ServiceName string `json:"servicename"`
MethodName string `json:"methodname"`
Reflection struct {
Id string `json:"id"`
} `json:"reflection"`
Result json.RawMessage `json:"result"`
}
func (c *apiClient) Transaction(ctx context.Context, hash string) (tc TransactionContent, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourceTxs, hash))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tc); err != nil {
return
}
return tc, nil
}
func (c *apiClient) TransactionUTXOs(ctx context.Context, hash string) (tu TransactionUTXOs, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxUTXOs))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tu); err != nil {
return
}
return tu, nil
}
func (c *apiClient) TransactionStakeAddressCerts(ctx context.Context, hash string) (tc []TransactionStakeAddressCert, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxStakes))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tc); err != nil {
return
}
return tc, nil
}
func (c *apiClient) TransactionWithdrawals(ctx context.Context, hash string) (tw []TransactionWidthrawal, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxWithdrawals))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tw); err != nil {
return
}
return tw, nil
}
func (c *apiClient) TransactionMIRs(ctx context.Context, hash string) (tw []TransactionMIR, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxWithdrawals))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tw); err != nil {
return
}
return tw, nil
}
func (c *apiClient) TransactionMetadata(ctx context.Context, hash string) (tm []TransactionMetadata, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxMetadata))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tm); err != nil {
return
}
return tm, nil
}
func (c *apiClient) TransactionMetadataInCBORs(ctx context.Context, hash string) (tmc []TransactionMetadataCbor, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxMetadata, resourceCbor))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tmc); err != nil {
return
}
return tmc, nil
}
func (c *apiClient) TransactionRedeemers(ctx context.Context, hash string) (tm []TransactionRedeemer, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxRedeemers))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tm); err != nil {
return
}
return tm, nil
}
func (c *apiClient) TransactionDelegationCerts(ctx context.Context, hash string) (td []TransactionDelegation, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxDelegations))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&td); err != nil {
return
}
return td, nil
}
func (c *apiClient) TransactionPoolUpdates(ctx context.Context, hash string) (td []TransactionPoolCert, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxDelegations))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&td); err != nil {
return
}
return td, nil
}
func (c *apiClient) TransactionPoolUpdateCerts(ctx context.Context, hash string) (tcs []TransactionPoolCert, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxPoolUpdates))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tcs); err != nil {
return
}
return tcs, nil
}
func (c *apiClient) TransactionPoolRetirementCerts(ctx context.Context, hash string) (tcs []TransactionPoolCert, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceTxs, hash, resourceTxPoolRetires))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&tcs); err != nil {
return
}
return tcs, nil
}
func (c *apiClient) TransactionSubmit(ctx context.Context, cbor []byte) (hash string, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourceTx, resourceTxSubmit))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestUrl.String(), bytes.NewReader(cbor))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/cbor")
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&hash); err != nil {
return
}
return hash, nil
}
func (c *apiClient) TransactionEvaluate(ctx context.Context, cbor []byte) (jsonResponse OgmiosResponse, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceTxEvaluate))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestUrl.String(), bytes.NewReader(cbor))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/cbor")
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&jsonResponse); err != nil {
return
}
return jsonResponse, nil
}
func (c *apiClient) TransactionEvaluateUTXOs(ctx context.Context, cbor []byte, additionalUtxoSet AdditionalUtxoSet) (jsonResponse OgmiosResponse, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceTxEvaluateUtxos))
if err != nil {
return
}
// Convert addition utxo set from custom go data type (array of struct with TxIn, TxOut properties) to
// format required by the API endpoint ([[TxIn, TxOut], ...])
convertedAdditionalUtxoSet := make([][]interface{}, len(additionalUtxoSet))
for i, utxo := range additionalUtxoSet {
convertedAdditionalUtxoSet[i] = []interface{}{utxo.TxIn, utxo.TxOut}
}
payload := struct {
Cbor string `json:"cbor"`
AdditionalUtxoSet [][]interface{} `json:"additionalUtxoSet"`
}{
Cbor: string(cbor),
AdditionalUtxoSet: convertedAdditionalUtxoSet,
}
jsonData, err := json.Marshal(payload)
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodPost, requestUrl.String(), bytes.NewBuffer(jsonData))
if err != nil {
return
}
req.Header.Add("Content-Type", "application/json")
res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()
if err = json.NewDecoder(res.Body).Decode(&jsonResponse); err != nil {
return
}
return jsonResponse, nil
}