-
Notifications
You must be signed in to change notification settings - Fork 1
/
blockfrost.go
151 lines (134 loc) · 3.48 KB
/
blockfrost.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
package rum
import (
"context"
"encoding/hex"
"strconv"
"github.com/blockfrost/blockfrost-go"
)
var NetworkMap = map[string]string{
"testnet": blockfrost.CardanoTestNet,
"preview": blockfrost.CardanoPreview,
"preprod": blockfrost.CardanoPreProd,
"mainnet": blockfrost.CardanoMainNet,
}
func GetServerURL(projectID string) string {
if len(projectID) >= 7 {
prefix := projectID[:7]
if url, exists := NetworkMap[prefix]; exists {
return url
}
}
return NetworkMap["mainnet"]
}
type BlockfrostProvider struct {
blockfrostClient blockfrost.APIClient
}
func NewBlockfrostProvider(projectID string) *BlockfrostProvider {
blockfrostClient := blockfrost.NewAPIClient(
blockfrost.APIClientOptions{
ProjectID: projectID,
Server: GetServerURL(projectID),
},
)
return &BlockfrostProvider{
blockfrostClient: blockfrostClient,
}
}
func (bf *BlockfrostProvider) SubmitTx(txCbor string) (string, error) {
txBuffer, err := hex.DecodeString(txCbor)
if err != nil {
return "", err
}
txHash, err := bf.blockfrostClient.TransactionSubmit(context.TODO(), txBuffer)
return txHash, err
}
func (bf *BlockfrostProvider) FetchTxInfo(hash string) (TransactionInfo, error) {
bfTxInfo, err := bf.blockfrostClient.Transaction(context.TODO(), hash)
if err != nil {
return TransactionInfo{}, err
}
invalidHereafter := ""
if bfTxInfo.InvalidHereafter != nil {
invalidHereafter = *bfTxInfo.InvalidHereafter
}
invalidBefore := ""
if bfTxInfo.InvalidBefore != nil {
invalidBefore = *bfTxInfo.InvalidBefore
}
txInfo := TransactionInfo{
Block: bfTxInfo.Block,
Deposit: bfTxInfo.Deposit,
Fees: bfTxInfo.Fees,
Hash: bfTxInfo.Hash,
Index: int(bfTxInfo.Index),
InvalidAfter: invalidHereafter,
InvalidBefore: invalidBefore,
Slot: strconv.FormatInt(int64(bfTxInfo.Slot), 10),
Size: int(bfTxInfo.Size),
}
return txInfo, nil
}
func (bf *BlockfrostProvider) FetchUTxOs(hash string, index *int) ([]UTxO, error) {
res, err := bf.blockfrostClient.TransactionUTXOs(context.TODO(), hash)
if err != nil {
return nil, err
}
bfOutputs := res.Outputs
utxos := BfToUtxos(bfOutputs, hash)
if index != nil {
utxo := FindUtxoByIndex(utxos, *index)
if utxo != nil {
return []UTxO{*utxo}, nil
}
return []UTxO{}, nil
}
return utxos, nil
}
func BfToUtxos(bfUtxos []blockfrost.TransactionOutput, hash string) []UTxO {
utxos := make([]UTxO, len(bfUtxos))
for i, bfUtxo := range bfUtxos {
utxos[i] = BfToUtxo(bfUtxo, hash)
}
return utxos
}
func BfToUtxo(bfUtxo blockfrost.TransactionOutput, hash string) UTxO {
dataHash := ""
if bfUtxo.DataHash != nil {
dataHash = *bfUtxo.DataHash
}
inlineDatum := ""
if bfUtxo.InlineDatum != nil {
inlineDatum = *bfUtxo.InlineDatum
}
referenceScriptHash := ""
if bfUtxo.ReferenceScriptHash != nil {
referenceScriptHash = *bfUtxo.ReferenceScriptHash
}
return UTxO{
Input: Input{
TxHash: hash,
OutputIndex: int(bfUtxo.OutputIndex),
},
Output: Output{
Amount: BfToAssets(bfUtxo.Amount),
Address: bfUtxo.Address,
DataHash: dataHash,
PlutusData: inlineDatum,
ScriptRef: "", // TODO: add script ref
ScriptHash: referenceScriptHash,
},
}
}
func BfToAssets(bfAssets []blockfrost.TxAmount) []Asset {
assets := make([]Asset, len(bfAssets))
for i, bfAsset := range bfAssets {
assets[i] = BfToAsset(bfAsset)
}
return assets
}
func BfToAsset(bfAsset blockfrost.TxAmount) Asset {
return Asset{
Quantity: bfAsset.Quantity,
Unit: bfAsset.Unit,
}
}