-
Notifications
You must be signed in to change notification settings - Fork 20
/
transaction.go
213 lines (189 loc) · 6.35 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
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
package overflow
import (
"context"
"fmt"
"math"
"strings"
"github.com/bjartek/underflow"
"github.com/grpc-ecosystem/go-grpc-middleware/v2/interceptors/logging"
"github.com/onflow/cadence/common"
"github.com/onflow/cadence/parser"
"github.com/onflow/flow-go-sdk"
)
type FilterFunction func(OverflowTransaction) bool
type Argument struct {
Value interface{}
Key string
}
type OverflowTransaction struct {
Error error
AuthorizerTypes map[string][]string
Stakeholders map[string][]string
Payer string
Id string
Status string
BlockId string
Authorizers []string
Arguments []Argument
Events []OverflowEvent
Imports []Import
Script []byte
ProposalKey flow.ProposalKey
Fee float64
TransactionIndex int
GasLimit uint64
GasUsed uint64
ExecutionEffort float64
Balances FeeBalance
PayloadSignatures []flow.TransactionSignature
EnvelopeSignatures []flow.TransactionSignature
}
func (o *OverflowState) CreateOverflowTransaction(blockId string, transactionResult flow.TransactionResult, transaction flow.Transaction, txIndex int) (*OverflowTransaction, error) {
feeAmount := 0.0
events, fee := o.ParseEvents(transactionResult.Events)
feeRaw, ok := fee.Fields["amount"]
if ok {
feeAmount, ok = feeRaw.(float64)
if !ok {
return nil, fmt.Errorf("failed casting fee amount to float64")
}
}
executionEffort, ok := fee.Fields["executionEffort"].(float64)
gas := 0
if ok {
factor := 100000000
gas = int(math.Round(executionEffort * float64(factor)))
}
status := transactionResult.Status.String()
args := []Argument{}
argInfo := declarationInfo(transaction.Script)
for i := range transaction.Arguments {
arg, err := transaction.Argument(i)
if err != nil {
status = fmt.Sprintf("%s failed getting argument at index %d", status, i)
}
var key string
if len(argInfo.ParameterOrder) <= i {
key = "invalid"
} else {
key = argInfo.ParameterOrder[i]
}
argStruct := Argument{
Key: key,
Value: underflow.CadenceValueToInterfaceWithOption(arg, o.UnderflowOptions),
}
args = append(args, argStruct)
}
standardStakeholders := map[string][]string{}
imports, err := GetAddressImports(transaction.Script)
if err != nil {
status = fmt.Sprintf("%s failed getting imports", status)
}
authorizerTypes := map[string][]string{}
authorizers := []string{}
for i, authorizer := range transaction.Authorizers {
auth := fmt.Sprintf("0x%s", authorizer.Hex())
authorizers = append(authorizers, auth)
standardStakeholders[auth] = []string{"authorizer"}
if len(argInfo.Authorizers) > i {
authorizerTypes[auth] = argInfo.Authorizers[i]
}
}
payerRoles, ok := standardStakeholders[fmt.Sprintf("0x%s", transaction.Payer.Hex())]
if !ok {
standardStakeholders[fmt.Sprintf("0x%s", transaction.Payer.Hex())] = []string{"payer"}
} else {
payerRoles = append(payerRoles, "payer")
standardStakeholders[fmt.Sprintf("0x%s", transaction.Payer.Hex())] = payerRoles
}
proposer, ok := standardStakeholders[fmt.Sprintf("0x%s", transaction.ProposalKey.Address.Hex())]
if !ok {
standardStakeholders[fmt.Sprintf("0x%s", transaction.ProposalKey.Address.Hex())] = []string{"proposer"}
} else {
proposer = append(proposer, "proposer")
standardStakeholders[fmt.Sprintf("0x%s", transaction.ProposalKey.Address.Hex())] = proposer
}
// TODO: here we need to get out the balance of the payer and the fee recipient
eventsWithoutFees, balanceFees := events.FilterFees(feeAmount, fmt.Sprintf("0x%s", transaction.Payer.Hex()))
eventList := []OverflowEvent{}
for _, evList := range eventsWithoutFees {
eventList = append(eventList, evList...)
}
return &OverflowTransaction{
Id: transactionResult.TransactionID.String(),
TransactionIndex: txIndex,
BlockId: blockId,
Status: status,
Events: eventList,
Stakeholders: eventsWithoutFees.GetStakeholders(standardStakeholders),
Imports: imports,
Error: transactionResult.Error,
Arguments: args,
Fee: feeAmount,
Script: transaction.Script,
Payer: fmt.Sprintf("0x%s", transaction.Payer.String()),
ProposalKey: transaction.ProposalKey,
GasLimit: transaction.GasLimit,
GasUsed: uint64(gas),
ExecutionEffort: executionEffort,
Authorizers: authorizers,
AuthorizerTypes: authorizerTypes,
Balances: balanceFees,
PayloadSignatures: transaction.PayloadSignatures,
EnvelopeSignatures: transaction.EnvelopeSignatures,
}, nil
}
func (o *OverflowState) GetOverflowTransactionById(ctx context.Context, id flow.Identifier) (*OverflowTransaction, error) {
ctx = logging.InjectLogField(ctx, "transaction_id", id)
tx, txr, err := o.Flowkit.GetTransactionByID(ctx, id, false)
if err != nil {
return nil, err
}
txIndex := 0
if len(txr.Events) > 0 {
txIndex = txr.Events[0].TransactionIndex
}
return o.CreateOverflowTransaction(txr.BlockID.String(), *txr, *tx, txIndex)
}
func (o *OverflowState) GetTransactionById(ctx context.Context, id flow.Identifier) (*flow.Transaction, error) {
ctx = logging.InjectLogField(ctx, "transaction_id", id)
tx, _, err := o.Flowkit.GetTransactionByID(ctx, id, false)
if err != nil {
return nil, err
}
return tx, nil
}
func (o *OverflowState) GetTransactionsByBlockId(ctx context.Context, id flow.Identifier) ([]*flow.Transaction, []*flow.TransactionResult, error) {
ctx = logging.InjectLogField(ctx, "block_id", id)
tx, txr, err := o.Flowkit.GetTransactionsByBlockID(ctx, id)
if err != nil {
return nil, nil, err
}
return tx, txr, nil
}
func GetAddressImports(code []byte) ([]Import, error) {
deps := []Import{}
program, err := parser.ParseProgram(nil, code, parser.Config{})
if err != nil {
return deps, err
}
for _, imp := range program.ImportDeclarations() {
address, isAddressImport := imp.Location.(common.AddressLocation)
if isAddressImport {
for _, id := range imp.Identifiers {
deps = append(deps, Import{
Address: fmt.Sprintf("0x%s", address.Address.Hex()),
Name: id.Identifier,
})
}
}
}
return deps, nil
}
type Import struct {
Address string
Name string
}
func (i Import) Identifier() string {
return fmt.Sprintf("A.%s.%s", strings.TrimPrefix(i.Address, "0x"), i.Name)
}