-
Notifications
You must be signed in to change notification settings - Fork 4
/
script.go
326 lines (282 loc) · 8.1 KB
/
script.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
// SPDX-License-Identifier: Apache-2.0
//
// Copyright © 2022 The Cardano Community Authors
package koios
import (
"context"
"encoding/json"
"fmt"
"io"
"github.com/shopspring/decimal"
)
type (
// ScriptHash defines type for _script_hash.
ScriptHash string
DatumHash string
// ScriptRedeemers defines model for script_redeemers.
ScriptRedeemers struct {
// Hash of Transaction for which details are being shown
ScriptHash ScriptHash `json:"script_hash"`
// Redeemers list
Redeemers []ScriptRedeemer `json:"redeemers"`
}
ScriptInfo struct {
// The Hash of the Plutus Data
ScriptHash string `json:"script_hash"`
// CreationTxHash is the hash of the transaction that created the script
CreationTxHash TxHash `json:"creation_tx_hash"`
// type
Type string `json:"type"`
//
Value *json.RawMessage `json:"value"`
Bytes string `json:"bytes"`
Size uint `json:"size"`
}
// ScriptRedeemer model.
ScriptRedeemer struct {
// TxHash of Transaction containing the redeemer.
TxHash TxHash `json:"tx_hash"`
// TxIndex The index of the redeemer pointer in the transaction.
TxIndex uint32 `json:"tx_index"`
// The budget in Memory to run a script.
UnitMem int `json:"unit_mem"`
// The budget in Cpu steps to run a script.
UnitSteps int `json:"unit_steps"`
// The budget in fees to run a script - the fees depend on the
// ExUnits and the current prices.
Fee decimal.Decimal `json:"fee,omitempty"`
// What kind pf validation this redeemer is used for,
// it can be one of 'spend', 'mint', 'cert', 'reward'.
Purpose string `json:"purpose"`
// The Hash of the Plutus Data
DatumHash string `json:"datum_hash"`
// The actual data in json format
DatumValue map[string]any `json:"datum_value"`
}
// DatumInfo datum information for given datum hash
DatumInfo struct {
DatumHash DatumHash `json:"datum_hash"`
CreationTxHash TxHash `json:"creation_tx_hash"`
Bytes string `json:"bytes"`
Value *json.RawMessage `json:"value"`
}
PlutusContract struct {
Address Address `json:"address"`
ScriptHash ScriptHash `json:"script_hash"`
ByteCode string `json:"bytecode"`
Size uint `json:"size"`
ValidContract bool `json:"valid_contract"`
}
// NativeScript item of native script list.
NativeScript struct {
// Hash of the script creation transaction
CreationTxHash TxHash `json:"creation_tx_hash"`
// Hash of a script
ScriptHash string `json:"script_hash"`
Type string `json:"type"`
Size uint `json:"size"`
}
// NativeScriptListResponse represents response from `/native_script_list` endpoint.
NativeScriptListResponse struct {
Response
Data []NativeScript `json:"data"`
}
// PlutusScriptListItem item of plutus script list.
PlutusScriptListItem struct {
// Hash of the script creation transaction
CreationTxHash TxHash `json:"creation_tx_hash"`
// Hash of a script
ScriptHash string `json:"script_hash"`
// Type of the script
Type string `json:"type"`
// Size of the script
Size uint `json:"size"`
}
// PlutusScriptListResponse represents response from `/plutus_script_list` endpoint.
PlutusScriptListResponse struct {
Response
Data []PlutusScriptListItem `json:"data"`
}
// ScriptRedeemersResponse represents response from `/script_redeemers` endpoint.
ScriptRedeemersResponse struct {
Response
Data *ScriptRedeemers `json:"data"`
}
// DatumInfosResponse represents response from `/datum_info` endpoint.
DatumInfosResponse struct {
Response
Data []DatumInfo `json:"data"`
}
DatumInfoResponse struct {
Response
Data *DatumInfo `json:"data"`
}
ScriptInfosResponse struct {
Response
Data []ScriptInfo `json:"data"`
}
ScriptUTxOsResponse struct {
Response
Data []UTxO `json:"data"`
}
)
// String returns ScriptHash as string.
func (v ScriptHash) String() string {
return string(v)
}
// String returns DatumHash as string.
func (v DatumHash) String() string {
return string(v)
}
func (v *DatumHash) MarshalJSON() ([]byte, error) {
if len(v.String()) == 0 {
return []byte("null"), nil
}
return []byte(fmt.Sprintf(`"%s"`, v.String())), nil
}
// GetNativeScriptList returns list of all existing native script hashes
// along with their creation transaction hashes.
func (c *Client) GetNativeScripts(
ctx context.Context,
opts *RequestOptions,
) (res *NativeScriptListResponse, err error) {
res = &NativeScriptListResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/native_script_list", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetPlutusScriptList returns all existing Plutus script
// hashes along with their creation transaction hashes.
func (c *Client) GetPlutusScripts(
ctx context.Context,
opts *RequestOptions,
) (res *PlutusScriptListResponse, err error) {
res = &PlutusScriptListResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", "/plutus_script_list", nil, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}
// GetScriptRedeemers returns a list of all redeemers for a given script hash.
func (c *Client) GetScriptRedeemers(
ctx context.Context,
sh ScriptHash,
opts *RequestOptions,
) (res *ScriptRedeemersResponse, err error) {
res = &ScriptRedeemersResponse{}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_script_hash", sh.String())
rsp, err := c.request(ctx, &res.Response, "GET", "/script_redeemers", nil, opts)
if err != nil {
return
}
r := []ScriptRedeemers{}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &r)
if len(r) == 1 {
res.Data = &r[0]
}
return
}
// GetTxStatus returns status of transaction.
func (c *Client) GetDatumInfo(
ctx context.Context,
hash DatumHash,
opts *RequestOptions,
) (res *DatumInfoResponse, err error) {
res = &DatumInfoResponse{}
rsp, err := c.GetDatumInfos(ctx, []DatumHash{hash}, opts)
res.Response = rsp.Response
if len(rsp.Data) == 1 {
res.Data = &rsp.Data[0]
} else {
err = fmt.Errorf("%w: %s", ErrNoData, hash)
}
return
}
// GetTxsStatuses returns status of transaction(s).
func (c *Client) GetDatumInfos(
ctx context.Context,
hashes []DatumHash,
opts *RequestOptions,
) (*DatumInfosResponse, error) {
res := &DatumInfosResponse{}
if len(hashes) == 0 {
err := ErrNoDatumHash
res.applyError(nil, err)
return res, err
}
rsp, err := c.request(ctx, &res.Response, "POST", "/datum_info", datumHashesPL(hashes), opts)
if err != nil {
return res, err
}
return res, ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
}
func (c *Client) GetScriptInfo(
ctx context.Context,
hashes []ScriptHash,
opts *RequestOptions,
) (*ScriptInfosResponse, error) {
res := &ScriptInfosResponse{}
if len(hashes) == 0 {
err := ErrNoScriptHash
res.applyError(nil, err)
return res, err
}
rsp, err := c.request(ctx, &res.Response, "POST", "/script_info", scriptHashesPL(hashes), opts)
if err != nil {
return res, err
}
return res, ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
}
func (c *Client) GetScriptUtxos(
ctx context.Context,
hash ScriptHash,
Extended bool,
opts *RequestOptions,
) (*ScriptUTxOsResponse, error) {
res := &ScriptUTxOsResponse{}
if len(hash) == 0 {
err := ErrNoScriptHash
res.applyError(nil, err)
return res, err
}
if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_extended", fmt.Sprintf("%t", Extended))
opts.QuerySet("_script_hash", hash.String())
rsp, err := c.request(ctx, &res.Response, "GET", "/script_utxos", nil, opts)
if err != nil {
return res, err
}
return res, ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
}
func datumHashesPL(hashes []DatumHash) io.Reader {
var payload = struct {
DatumHashes []DatumHash `json:"_datum_hashes"`
}{hashes}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}
func scriptHashesPL(hashes []ScriptHash) io.Reader {
var payload = struct {
DatumHashes []ScriptHash `json:"_script_hashes"`
}{hashes}
rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()
return rpipe
}