-
Notifications
You must be signed in to change notification settings - Fork 179
/
transactions.go
81 lines (69 loc) · 2.08 KB
/
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
package routes
import (
entitiesproto "github.com/onflow/flow/protobuf/go/flow/entities"
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/models"
"github.com/onflow/flow-go/engine/access/rest/request"
)
// GetTransactionByID gets a transaction by requested ID.
func GetTransactionByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
req, err := r.GetTransactionRequest()
if err != nil {
return nil, models.NewBadRequestError(err)
}
tx, err := backend.GetTransaction(r.Context(), req.ID)
if err != nil {
return nil, err
}
var txr *access.TransactionResult
// only lookup result if transaction result is to be expanded
if req.ExpandsResult {
txr, err = backend.GetTransactionResult(
r.Context(),
req.ID,
req.BlockID,
req.CollectionID,
entitiesproto.EventEncodingVersion_JSON_CDC_V0,
)
if err != nil {
return nil, err
}
}
var response models.Transaction
response.Build(tx, txr, link)
return response, nil
}
// GetTransactionResultByID retrieves transaction result by the transaction ID.
func GetTransactionResultByID(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
req, err := r.GetTransactionResultRequest()
if err != nil {
return nil, models.NewBadRequestError(err)
}
txr, err := backend.GetTransactionResult(
r.Context(),
req.ID,
req.BlockID,
req.CollectionID,
entitiesproto.EventEncodingVersion_JSON_CDC_V0,
)
if err != nil {
return nil, err
}
var response models.TransactionResult
response.Build(txr, req.ID, link)
return response, nil
}
// CreateTransaction creates a new transaction from provided payload.
func CreateTransaction(r *request.Request, backend access.API, link models.LinkGenerator) (interface{}, error) {
req, err := r.CreateTransactionRequest()
if err != nil {
return nil, models.NewBadRequestError(err)
}
err = backend.SendTransaction(r.Context(), &req.Transaction)
if err != nil {
return nil, err
}
var response models.Transaction
response.Build(&req.Transaction, nil, link)
return response, nil
}