-
Notifications
You must be signed in to change notification settings - Fork 178
/
transaction.go
147 lines (121 loc) · 3.72 KB
/
transaction.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
package models
import (
"github.com/onflow/flow-go/access"
"github.com/onflow/flow-go/engine/access/rest/util"
"github.com/onflow/flow-go/model/flow"
)
func (t *Transaction) Build(tx *flow.TransactionBody, txr *access.TransactionResult, link LinkGenerator) {
args := make([]string, len(tx.Arguments))
for i, arg := range tx.Arguments {
args[i] = util.ToBase64(arg)
}
auths := make([]string, len(tx.Authorizers))
for i, auth := range tx.Authorizers {
auths[i] = auth.String()
}
// if transaction result is provided then add that to the response, else add the result link to the expandable
t.Expandable = &TransactionExpandable{}
if txr != nil {
var txResult TransactionResult
txResult.Build(txr, tx.ID(), link)
t.Result = &txResult
} else {
resultLink, _ := link.TransactionResultLink(tx.ID())
t.Expandable.Result = resultLink
}
var payloadSigs TransactionSignatures
payloadSigs.Build(tx.PayloadSignatures)
var envelopeSigs TransactionSignatures
envelopeSigs.Build(tx.EnvelopeSignatures)
var proposalKey ProposalKey
proposalKey.Build(tx.ProposalKey)
t.Id = tx.ID().String()
t.Script = util.ToBase64(tx.Script)
t.Arguments = args
t.ReferenceBlockId = tx.ReferenceBlockID.String()
t.GasLimit = util.FromUint64(tx.GasLimit)
t.Payer = tx.Payer.String()
t.ProposalKey = &proposalKey
t.Authorizers = auths
t.PayloadSignatures = payloadSigs
t.EnvelopeSignatures = envelopeSigs
self, _ := SelfLink(tx.ID(), link.TransactionLink)
t.Links = self
}
type Transactions []Transaction
func (t *Transactions) Build(transactions []*flow.TransactionBody, link LinkGenerator) {
txs := make([]Transaction, len(transactions))
for i, tr := range transactions {
var tx Transaction
tx.Build(tr, nil, link)
txs[i] = tx
}
*t = txs
}
type TransactionSignatures []TransactionSignature
func (t *TransactionSignatures) Build(signatures []flow.TransactionSignature) {
sigs := make([]TransactionSignature, len(signatures))
for i, s := range signatures {
var sig TransactionSignature
sig.Build(s)
sigs[i] = sig
}
*t = sigs
}
func (t *TransactionSignature) Build(sig flow.TransactionSignature) {
t.Address = sig.Address.String()
t.KeyIndex = util.FromUint64(sig.KeyIndex)
t.Signature = util.ToBase64(sig.Signature)
}
func (t *TransactionResult) Build(txr *access.TransactionResult, txID flow.Identifier, link LinkGenerator) {
var status TransactionStatus
status.Build(txr.Status)
var execution TransactionExecution
execution.Build(txr)
var events Events
events.Build(txr.Events)
if txr.BlockID != flow.ZeroID { // don't send back 0 ID
t.BlockId = txr.BlockID.String()
}
t.Status = &status
t.Execution = &execution
t.StatusCode = int32(txr.StatusCode)
t.ErrorMessage = txr.ErrorMessage
t.ComputationUsed = util.FromUint64(0) // todo: define this
t.Events = events
self, _ := SelfLink(txID, link.TransactionResultLink)
t.Links = self
}
func (t *TransactionStatus) Build(status flow.TransactionStatus) {
switch status {
case flow.TransactionStatusExpired:
*t = EXPIRED
case flow.TransactionStatusExecuted:
*t = EXECUTED
case flow.TransactionStatusFinalized:
*t = FINALIZED
case flow.TransactionStatusSealed:
*t = SEALED
case flow.TransactionStatusPending:
*t = PENDING
default:
*t = ""
}
}
func (t *TransactionExecution) Build(result *access.TransactionResult) {
*t = PENDING_RESULT
if result.Status == flow.TransactionStatusSealed && result.ErrorMessage == "" {
*t = SUCCESS_RESULT
}
if result.ErrorMessage != "" {
*t = FAILURE_RESULT
}
if result.Status == flow.TransactionStatusExpired {
*t = FAILURE_RESULT
}
}
func (p *ProposalKey) Build(key flow.ProposalKey) {
p.Address = key.Address.String()
p.KeyIndex = util.FromUint64(key.KeyIndex)
p.SequenceNumber = util.FromUint64(key.SequenceNumber)
}